You are here

public function MenuTreeStorageTest::testLoadTree in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Menu/MenuTreeStorageTest.php \Drupal\KernelTests\Core\Menu\MenuTreeStorageTest::testLoadTree()

Tests the loadTreeData method.

File

core/tests/Drupal/KernelTests/Core/Menu/MenuTreeStorageTest.php, line 228

Class

MenuTreeStorageTest
Tests the menu tree storage.

Namespace

Drupal\KernelTests\Core\Menu

Code

public function testLoadTree() {
  $this
    ->addMenuLink('test1', '', 'test1');
  $this
    ->addMenuLink('test2', 'test1', 'test2');
  $this
    ->addMenuLink('test3', 'test2', 'test3');
  $this
    ->addMenuLink('test4', '', 'test4');
  $this
    ->addMenuLink('test5', 'test4', NULL);
  $data = $this->treeStorage
    ->loadTreeData('tools', new MenuTreeParameters());
  $tree = $data['tree'];
  $this
    ->assertCount(1, $tree['test1']['subtree']);
  $this
    ->assertCount(1, $tree['test1']['subtree']['test2']['subtree']);
  $this
    ->assertCount(0, $tree['test1']['subtree']['test2']['subtree']['test3']['subtree']);
  $this
    ->assertCount(1, $tree['test4']['subtree']);
  $this
    ->assertCount(0, $tree['test4']['subtree']['test5']['subtree']);

  // Ensure that route names element exists.
  $this
    ->assertNotEmpty($data['route_names']);

  // Ensure that the actual route names are set.
  $this
    ->assertContains('test1', $data['route_names']);
  $this
    ->assertNotContains('test5', $data['route_names']);
  $parameters = new MenuTreeParameters();
  $parameters
    ->setActiveTrail([
    'test4',
    'test5',
  ]);
  $data = $this->treeStorage
    ->loadTreeData('tools', $parameters);
  $tree = $data['tree'];
  $this
    ->assertCount(1, $tree['test1']['subtree']);
  $this
    ->assertFalse($tree['test1']['in_active_trail']);
  $this
    ->assertCount(1, $tree['test1']['subtree']['test2']['subtree']);
  $this
    ->assertFalse($tree['test1']['subtree']['test2']['in_active_trail']);
  $this
    ->assertCount(0, $tree['test1']['subtree']['test2']['subtree']['test3']['subtree']);
  $this
    ->assertFalse($tree['test1']['subtree']['test2']['subtree']['test3']['in_active_trail']);
  $this
    ->assertCount(1, $tree['test4']['subtree']);
  $this
    ->assertTrue($tree['test4']['in_active_trail']);
  $this
    ->assertCount(0, $tree['test4']['subtree']['test5']['subtree']);
  $this
    ->assertTrue($tree['test4']['subtree']['test5']['in_active_trail']);

  // Add some conditions to ensure that conditions work as expected.
  $parameters = new MenuTreeParameters();
  $parameters
    ->addCondition('parent', 'test1');
  $data = $this->treeStorage
    ->loadTreeData('tools', $parameters);
  $this
    ->assertCount(1, $data['tree']);
  $this
    ->assertEquals('test2', $data['tree']['test2']['definition']['id']);
  $this
    ->assertEquals([], $data['tree']['test2']['subtree']);

  // Test for only enabled links.
  $link = $this->treeStorage
    ->load('test3');
  $link['enabled'] = FALSE;
  $this->treeStorage
    ->save($link);
  $link = $this->treeStorage
    ->load('test4');
  $link['enabled'] = FALSE;
  $this->treeStorage
    ->save($link);
  $link = $this->treeStorage
    ->load('test5');
  $link['enabled'] = FALSE;
  $this->treeStorage
    ->save($link);
  $parameters = new MenuTreeParameters();
  $parameters
    ->onlyEnabledLinks();
  $data = $this->treeStorage
    ->loadTreeData('tools', $parameters);
  $this
    ->assertCount(1, $data['tree']);
  $this
    ->assertEquals('test1', $data['tree']['test1']['definition']['id']);
  $this
    ->assertCount(1, $data['tree']['test1']['subtree']);
  $this
    ->assertEquals('test2', $data['tree']['test1']['subtree']['test2']['definition']['id']);
  $this
    ->assertEquals([], $data['tree']['test1']['subtree']['test2']['subtree']);
}