You are here

public function TokenMenuTest::testMenuLinkParentsToken in Token 8

Tests menu link parents token.

File

tests/src/Functional/TokenMenuTest.php, line 409

Class

TokenMenuTest
Tests menu tokens.

Namespace

Drupal\Tests\token\Functional

Code

public function testMenuLinkParentsToken() {

  // Create a menu with a simple link hierarchy :
  // - parent
  //   - child-1
  //      - child-1-1
  Menu::create([
    'id' => 'menu_test',
    'label' => 'Test menu',
  ])
    ->save();
  $base_options = [
    'provider' => 'menu_test',
    'menu_name' => 'menu_test',
  ];
  $parent = $base_options + [
    'title' => 'parent title',
    'link' => [
      'uri' => 'internal:/menu-test/hierarchy/parent',
    ],
  ];
  $parent = MenuLinkContent::create($parent);
  $parent
    ->save();
  $child_1 = $base_options + [
    'title' => 'child_1 title',
    'link' => [
      'uri' => 'internal:/menu-test/hierarchy/parent/child_1',
    ],
    'parent' => $parent
      ->getPluginId(),
  ];
  $child_1 = MenuLinkContent::create($child_1);
  $child_1
    ->save();
  $child_1_1 = $base_options + [
    'title' => 'child_1_1 title',
    'link' => [
      'uri' => 'internal:/menu-test/hierarchy/parent/child_1/child_1_1',
    ],
    'parent' => $child_1
      ->getPluginId(),
  ];
  $child_1_1 = MenuLinkContent::create($child_1_1);
  $child_1_1
    ->save();
  $this
    ->assertTokens('menu-link', [
    'menu-link' => $child_1_1,
  ], [
    'parents' => 'parent title, child_1 title',
  ]);

  // Change the parent of child_1_1 to 'parent' at the entity level.
  $child_1_1->parent->value = $parent
    ->getPluginId();
  $child_1_1
    ->save();
  $this
    ->assertTokens('menu-link', [
    'menu-link' => $child_1_1,
  ], [
    'parents' => 'parent title',
  ]);

  // Change the parent of child_1_1 to 'main', at the entity level.
  $child_1_1->parent->value = '';
  $child_1_1
    ->save();

  // The token shouldn't have been generated; the menu link has no parent.
  $this
    ->assertNoTokens('menu-link', [
    'menu-link' => $child_1_1,
  ], [
    'parents',
  ]);
}