You are here

public function MenuTreeStorageTest::testLoadTree in Drupal 8

Same name and namespace in other branches
  1. 9 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', '');
  $this
    ->addMenuLink('test2', 'test1');
  $this
    ->addMenuLink('test3', 'test2');
  $this
    ->addMenuLink('test4');
  $this
    ->addMenuLink('test5', 'test4');
  $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']);
  $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
    ->assertEqual($data['tree']['test2']['definition']['id'], 'test2');
  $this
    ->assertEqual($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
    ->assertEqual($data['tree']['test1']['definition']['id'], 'test1');
  $this
    ->assertCount(1, $data['tree']['test1']['subtree']);
  $this
    ->assertEqual($data['tree']['test1']['subtree']['test2']['definition']['id'], 'test2');
  $this
    ->assertEqual($data['tree']['test1']['subtree']['test2']['subtree'], []);
}