You are here

public function MenuTreeStorageTest::testLoadTree in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php \Drupal\system\Tests\Menu\MenuTreeStorageTest::testLoadTree()

Tests the loadTreeData method.

File

core/modules/system/src/Tests/Menu/MenuTreeStorageTest.php, line 240
Contains \Drupal\system\Tests\Menu\MenuTreeStorageTest.

Class

MenuTreeStorageTest
Tests the menu tree storage.

Namespace

Drupal\system\Tests\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
    ->assertEqual(count($tree['test1']['subtree']), 1);
  $this
    ->assertEqual(count($tree['test1']['subtree']['test2']['subtree']), 1);
  $this
    ->assertEqual(count($tree['test1']['subtree']['test2']['subtree']['test3']['subtree']), 0);
  $this
    ->assertEqual(count($tree['test4']['subtree']), 1);
  $this
    ->assertEqual(count($tree['test4']['subtree']['test5']['subtree']), 0);
  $parameters = new MenuTreeParameters();
  $parameters
    ->setActiveTrail(array(
    'test4',
    'test5',
  ));
  $data = $this->treeStorage
    ->loadTreeData('tools', $parameters);
  $tree = $data['tree'];
  $this
    ->assertEqual(count($tree['test1']['subtree']), 1);
  $this
    ->assertFalse($tree['test1']['in_active_trail']);
  $this
    ->assertEqual(count($tree['test1']['subtree']['test2']['subtree']), 1);
  $this
    ->assertFalse($tree['test1']['subtree']['test2']['in_active_trail']);
  $this
    ->assertEqual(count($tree['test1']['subtree']['test2']['subtree']['test3']['subtree']), 0);
  $this
    ->assertFalse($tree['test1']['subtree']['test2']['subtree']['test3']['in_active_trail']);
  $this
    ->assertEqual(count($tree['test4']['subtree']), 1);
  $this
    ->assertTrue($tree['test4']['in_active_trail']);
  $this
    ->assertEqual(count($tree['test4']['subtree']['test5']['subtree']), 0);
  $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
    ->assertEqual(count($data['tree']), 1);
  $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
    ->assertEqual(count($data['tree']), 1);
  $this
    ->assertEqual($data['tree']['test1']['definition']['id'], 'test1');
  $this
    ->assertEqual(count($data['tree']['test1']['subtree']), 1);
  $this
    ->assertEqual($data['tree']['test1']['subtree']['test2']['definition']['id'], 'test2');
  $this
    ->assertEqual($data['tree']['test1']['subtree']['test2']['subtree'], []);
}