You are here

public function SectionTokenTest::testNodeSectionTokens in Workbench Access 8

Tests the node section tokens.

File

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

Class

SectionTokenTest
Tests workbench_access integration with tokens.

Namespace

Drupal\Tests\workbench_access\Kernel

Code

public function testNodeSectionTokens() {

  // Test a node that is not assigned to a section.
  $node1 = $this
    ->createNode([
    'type' => 'page',
    'title' => 'foo',
  ]);
  $tokens = [];
  $bubbleable_metadata = new BubbleableMetadata();
  $this
    ->assertTokens('node', [
    'node' => $node1,
  ], $tokens, $bubbleable_metadata);
  $term = Term::create([
    'name' => 'Test term',
    'vid' => $this->vocabulary
      ->id(),
  ]);
  $term
    ->save();

  // Create a node that is assigned to a term section.
  $node2 = $this
    ->createNode([
    'type' => 'page',
    'title' => 'bar',
    WorkbenchAccessManagerInterface::FIELD_NAME => $term
      ->id(),
  ]);
  $tokens = [
    'workbench-access-sections' => 'Test term',
  ];
  $bubbleable_metadata = new BubbleableMetadata();
  $this
    ->assertTokens('node', [
    'node' => $node2,
  ], $tokens, $bubbleable_metadata);
  $this
    ->assertContains($this->taxonomyScheme
    ->getCacheTags()[0], $bubbleable_metadata
    ->getCacheTags());

  // Assign to multiple terms.
  $term2 = Term::create([
    'name' => 'Test term two',
    'vid' => $this->vocabulary
      ->id(),
  ]);
  $term2
    ->save();

  // Create a node that is assigned to a term section.
  $node3 = $this
    ->createNode([
    'type' => 'page',
    'title' => 'bar',
    WorkbenchAccessManagerInterface::FIELD_NAME => [
      $term
        ->id(),
      $term2
        ->id(),
    ],
  ]);
  $tokens = [
    'workbench-access-sections' => 'Test term, Test term two',
  ];
  $bubbleable_metadata = new BubbleableMetadata();
  $this
    ->assertTokens('node', [
    'node' => $node3,
  ], $tokens, $bubbleable_metadata);
  $this
    ->assertContains($this->taxonomyScheme
    ->getCacheTags()[0], $bubbleable_metadata
    ->getCacheTags());

  // Create a node that is assigned to a menu section.
  $link = MenuLinkContent::create([
    'title' => 'Test menu link',
    'link' => [
      [
        'uri' => 'route:<front>',
      ],
    ],
    'menu_name' => $this->menu
      ->id(),
  ]);
  $link
    ->save();

  // Create a node that is in a menu section.
  $node4 = $this
    ->createNode([
    'type' => 'page',
    'title' => 'bar',
  ]);
  _menu_ui_node_save($node4, [
    'title' => 'Menu test',
    'menu_name' => 'main',
    'description' => 'view bar',
    'parent' => $link
      ->getPluginId(),
  ]);
  $tokens = [
    'workbench-access-sections' => 'Menu test',
  ];
  $bubbleable_metadata = new BubbleableMetadata();
  $this
    ->assertTokens('node', [
    'node' => $node4,
  ], $tokens, $bubbleable_metadata);
  $this
    ->assertContains($this->menuScheme
    ->getCacheTags()[0], $bubbleable_metadata
    ->getCacheTags());

  // Create a node that is assigned to both sections.
  $node5 = $this
    ->createNode([
    'type' => 'page',
    'title' => 'bar',
    'field_workbench_access' => $term
      ->id(),
  ]);
  _menu_ui_node_save($node5, [
    'title' => 'Test another menu link',
    'menu_name' => 'main',
    'description' => 'view bar',
    'parent' => $link
      ->getPluginId(),
  ]);
  $tokens = [
    'workbench-access-sections' => 'Test term, Test another menu link',
  ];
  $this
    ->assertTokens('node', [
    'node' => $node5,
  ], $tokens, $bubbleable_metadata);
  $this
    ->assertContains($this->taxonomyScheme
    ->getCacheTags()[0], $bubbleable_metadata
    ->getCacheTags());
  $this
    ->assertContains($this->menuScheme
    ->getCacheTags()[0], $bubbleable_metadata
    ->getCacheTags());
}