function MenuTreeStorageTest::testMenuDisabledChildLinks

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Menu/MenuTreeStorageTest.php \Drupal\KernelTests\Core\Menu\MenuTreeStorageTest::testMenuDisabledChildLinks()
  2. 10 core/tests/Drupal/KernelTests/Core/Menu/MenuTreeStorageTest.php \Drupal\KernelTests\Core\Menu\MenuTreeStorageTest::testMenuDisabledChildLinks()
  3. 11.x core/tests/Drupal/KernelTests/Core/Menu/MenuTreeStorageTest.php \Drupal\KernelTests\Core\Menu\MenuTreeStorageTest::testMenuDisabledChildLinks()

Tests with disabled child links.

File

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

Class

MenuTreeStorageTest
Tests the menu tree storage.

Namespace

Drupal\KernelTests\Core\Menu

Code

public function testMenuDisabledChildLinks() {
    // Add some links with parent on the previous one and test some values.
    // <tools>
    // - test1
    // -- test2 (disabled)
    $this->addMenuLink('test1', '');
    $this->assertMenuLink('test1', [
        'has_children' => 0,
        'depth' => 1,
    ]);
    $this->addMenuLink('test2', 'test1', '<front>', [], 'tools', [
        'enabled' => 0,
    ]);
    // The 1st link does not have any visible children, so has_children is 0.
    $this->assertMenuLink('test1', [
        'has_children' => 0,
        'depth' => 1,
    ]);
    $this->assertMenuLink('test2', [
        'has_children' => 0,
        'depth' => 2,
        'enabled' => 0,
    ], [
        'test1',
    ]);
    // Add more links with parent on the previous one.
    // <footer>
    // - footerA
    // ===============
    // <tools>
    // - test1
    // -- test2 (disabled)
    // --- test3
    // ---- test4
    // ----- test5
    // ------ test6
    // ------- test7
    // -------- test8
    // --------- test9
    $this->addMenuLink('footerA', '', '<front>', [], 'footer');
    $visible_children = [];
    for ($i = 3; $i <= $this->treeStorage
        ->maxDepth(); $i++) {
        $parent = $i - 1;
        $this->addMenuLink("test{$i}", "test{$parent}");
        $visible_children[] = "test{$i}";
    }
    // The 1st link does not have any visible children, so has_children is still
    // 0. However, it has visible links below it that will be found.
    $this->assertMenuLink('test1', [
        'has_children' => 0,
        'depth' => 1,
    ], [], $visible_children);
    // This should fail since test9 would end up at greater than max depth.
    try {
        $this->moveMenuLink('test1', 'footerA');
        $this->fail('Exception was not thrown');
    } catch (PluginException $e) {
        // Expected exception; just continue testing.
    }
    // The opposite move should work, and change the has_children flag.
    $this->moveMenuLink('footerA', 'test1');
    $visible_children[] = 'footerA';
    $this->assertMenuLink('test1', [
        'has_children' => 1,
        'depth' => 1,
    ], [], $visible_children);
}

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