You are here

public function SectionTokenTest::testUserSectionTokens in Workbench Access 8

Tests the user section tokens.

File

tests/src/Kernel/SectionTokenTest.php, line 111

Class

SectionTokenTest
Tests workbench_access integration with tokens.

Namespace

Drupal\Tests\workbench_access\Kernel

Code

public function testUserSectionTokens() {
  $user = $this
    ->createUser();
  $link = MenuLinkContent::create([
    'title' => 'Test menu link',
    'link' => [
      [
        'uri' => 'route:<front>',
      ],
    ],
    'menu_name' => $this->menu
      ->id(),
  ]);
  $link
    ->save();
  $this->userStorage
    ->addUser($this->menuScheme, $user, [
    $link
      ->getPluginId(),
  ]);
  $tokens = [
    'workbench-access-sections' => 'Test menu link',
  ];
  $bubbleable_metadata = new BubbleableMetadata();
  $this
    ->assertTokens('user', [
    'user' => $user,
  ], $tokens, $bubbleable_metadata);
  $this
    ->assertContains($this->menuScheme
    ->getCacheTags()[0], $bubbleable_metadata
    ->getCacheTags());
  $term = Term::create([
    'name' => 'Test term',
    'vid' => $this->vocabulary
      ->id(),
  ]);
  $term
    ->save();
  $this->userStorage
    ->addUser($this->taxonomyScheme, $user, [
    $term
      ->id(),
  ]);
  $tokens = [
    'workbench-access-sections' => 'Test term, Test menu link',
  ];
  $this
    ->assertTokens('user', [
    'user' => $user,
  ], $tokens, $bubbleable_metadata);
  $this
    ->assertContains($this->taxonomyScheme
    ->getCacheTags()[0], $bubbleable_metadata
    ->getCacheTags());
  $term = Term::create([
    'name' => 'Test term 2',
    'vid' => $this->vocabulary
      ->id(),
  ]);
  $term
    ->save();
  $this->userStorage
    ->addUser($this->taxonomyScheme, $user, [
    $term
      ->id(),
  ]);
  $tokens = [
    'workbench-access-sections' => 'Test term, Test term 2, Test menu link',
  ];
  $this
    ->assertTokens('user', [
    'user' => $user,
  ], $tokens, $bubbleable_metadata);
  $this
    ->setCurrentUser($user);
  $this
    ->assertTokens('current-user', [], $tokens, $bubbleable_metadata);
}