Account switcher does not properly validate the list of available text formats

Created on 27 May 2023, over 1 year ago
Updated 29 May 2023, over 1 year ago

Problem/Motivation

I faced an issue with the account switcher when trying the to switch accounts in a drush command. It looks to me that the account switcher does not properly refresh the list of available text formats when a session is passed, however the issue does not happen when a user entity is passed.

Steps to reproduce

The following test proves the issue

<?php

namespace Drupal\Tests\gu_kop\Functional;

use Drupal\Core\Session\UserSession;
use Drupal\Tests\BrowserTestBase;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;

/**
 * Base class for GU KoP tests.
 */
class SessionSwitcherTest extends BrowserTestBase {

  protected static $modules = [
    'node',
    'filter',
    'user'
  ];

  protected $defaultTheme= 'classy';

  public function testSessionSwitcher() {
    $format = FilterFormat::create([
      'format' => $this->randomMachineName(),
      'name' => $this->randomString(),
      'weight' => 1,
      'filters' => [],
    ]);
    $format->save();
    $role = $this->drupalCreateRole([$format->getPermissionName()]);
    $user = $this->drupalCreateUser(['administer nodes'], $this->randomMachineName());
    $user->addRole($role);
    $node_type = NodeType::create([
      'type' => $this->randomMachineName(),
      'name' => $this->randomString(),
    ]);
    $node_type->save();

    $text_field = 'field_text';
    $field_storage = FieldStorageConfig::create([
      'field_name' => $text_field,
      'entity_type' => 'node',
      'type' => 'text_long',
    ]);
    $field_storage->save();

    $field = FieldConfig::create([
      'field_storage' => $field_storage,
      'bundle' => $node_type->id(),
    ]);
    $field->save();

    $node = Node::create([
      'type' => $node_type->id(),
      'title' => $this->randomString(),
      $text_field => [
        'value' => $this->randomString(),
        'format' => $format->id(),
      ],
    ]);
    $account_switcher = \Drupal::service('account_switcher');
    
    $violations = $node->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals($text_field . '.0.format', $violations[0]->getPropertyPath());
    $session = new UserSession(['uid' => $user->id()]);
    $account_switcher->switchTo($session);
    $violations = $node->validate();
    $this->assertCount(0, $violations);
    $account_switcher->switchBack();
    $violations = $node->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals($text_field . '.0.format', $violations[0]->getPropertyPath());

  }
}

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Active

Version

9.5

Component
User module 

Last updated 1 day ago

Created by

🇸🇪Sweden alayham

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024