function MenuTreeStorageTest::testLoadTree

Same name in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Menu/MenuTreeStorageTest.php \Drupal\KernelTests\Core\Menu\MenuTreeStorageTest::testLoadTree()
  2. 10 core/tests/Drupal/KernelTests/Core/Menu/MenuTreeStorageTest.php \Drupal\KernelTests\Core\Menu\MenuTreeStorageTest::testLoadTree()
  3. 11.x 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']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.