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

Comments

alayham created an issue. See original summary.

cilefen’s picture

The best way forward would be for you to add a test to, or enhance a test in, Drupal core, here, in a patch or in a merge request.

shailja179’s picture

@alayham,
it will be better if you could give the steps too.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.