View source
<?php
namespace Drupal\system\Tests\Menu;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Menu\MenuTreeStorage;
use Drupal\simpletest\KernelTestBase;
class MenuTreeStorageTest extends KernelTestBase {
protected $treeStorage;
protected $connection;
public static $modules = array(
'system',
);
protected function setUp() {
parent::setUp();
$this->treeStorage = new MenuTreeStorage($this->container
->get('database'), $this->container
->get('cache.menu'), $this->container
->get('cache_tags.invalidator'), 'menu_tree');
$this->connection = $this->container
->get('database');
}
public function testBasicMethods() {
$this
->doTestEmptyStorage();
$this
->doTestTable();
}
protected function doTestEmptyStorage() {
$this
->assertEqual(0, $this->treeStorage
->countMenuLinks());
}
protected function doTestTable() {
$tree_storage = new MenuTreeStorage($this->container
->get('database'), $this->container
->get('cache.menu'), $this->container
->get('cache_tags.invalidator'), 'test_menu_tree');
$this
->assertFalse($this->connection
->schema()
->tableExists('test_menu_tree'), 'Test table is not yet created');
$tree_storage
->countMenuLinks();
$this
->assertTrue($this->connection
->schema()
->tableExists('test_menu_tree'), 'Test table was created');
}
public function testSimpleHierarchy() {
$this
->addMenuLink('test1', '');
$this
->assertMenuLink('test1', array(
'has_children' => 0,
'depth' => 1,
));
$this
->addMenuLink('test2', 'test1');
$this
->assertMenuLink('test1', array(
'has_children' => 1,
'depth' => 1,
), array(), array(
'test2',
));
$this
->assertMenuLink('test2', array(
'has_children' => 0,
'depth' => 2,
), array(
'test1',
));
$this
->addMenuLink('test3', 'test2');
$this
->assertMenuLink('test1', array(
'has_children' => 1,
'depth' => 1,
), array(), array(
'test2',
'test3',
));
$this
->assertMenuLink('test2', array(
'has_children' => 1,
'depth' => 2,
), array(
'test1',
), array(
'test3',
));
$this
->assertMenuLink('test3', array(
'has_children' => 0,
'depth' => 3,
), array(
'test2',
'test1',
));
}
public function testMenuLinkMoving() {
$this
->addMenuLink('test1', '');
$this
->addMenuLink('test2', 'test1');
$this
->addMenuLink('test3', 'test2');
$this
->addMenuLink('test4', '');
$this
->addMenuLink('test5', 'test4');
$this
->addMenuLink('test6', 'test5');
$this
->assertMenuLink('test1', array(
'has_children' => 1,
'depth' => 1,
), array(), array(
'test2',
'test3',
));
$this
->assertMenuLink('test2', array(
'has_children' => 1,
'depth' => 2,
), array(
'test1',
), array(
'test3',
));
$this
->assertMenuLink('test4', array(
'has_children' => 1,
'depth' => 1,
), array(), array(
'test5',
'test6',
));
$this
->assertMenuLink('test5', array(
'has_children' => 1,
'depth' => 2,
), array(
'test4',
), array(
'test6',
));
$this
->assertMenuLink('test6', array(
'has_children' => 0,
'depth' => 3,
), array(
'test5',
'test4',
));
$this
->moveMenuLink('test2', 'test5');
$this
->assertMenuLink('test1', array(
'has_children' => 0,
'depth' => 1,
));
$this
->assertMenuLink('test2', array(
'has_children' => 1,
'depth' => 3,
), array(
'test5',
'test4',
), array(
'test3',
));
$this
->assertMenuLink('test3', array(
'has_children' => 0,
'depth' => 4,
), array(
'test2',
'test5',
'test4',
));
$this
->assertMenuLink('test4', array(
'has_children' => 1,
'depth' => 1,
), array(), array(
'test5',
'test2',
'test3',
'test6',
));
$this
->assertMenuLink('test5', array(
'has_children' => 1,
'depth' => 2,
), array(
'test4',
), array(
'test2',
'test3',
'test6',
));
$this
->assertMenuLink('test6', array(
'has_children' => 0,
'depth' => 3,
), array(
'test5',
'test4',
));
$this
->moveMenuLink('test4', 'test1');
$this
->moveMenuLink('test3', 'test1');
$this
->assertMenuLink('test1', array(
'has_children' => 1,
'depth' => 1,
), array(), array(
'test4',
'test5',
'test2',
'test3',
'test6',
));
$this
->assertMenuLink('test2', array(
'has_children' => 0,
'depth' => 4,
), array(
'test5',
'test4',
'test1',
));
$this
->assertMenuLink('test3', array(
'has_children' => 0,
'depth' => 2,
), array(
'test1',
));
$this
->assertMenuLink('test4', array(
'has_children' => 1,
'depth' => 2,
), array(
'test1',
), array(
'test2',
'test5',
'test6',
));
$this
->assertMenuLink('test5', array(
'has_children' => 1,
'depth' => 3,
), array(
'test4',
'test1',
), array(
'test2',
'test6',
));
$this
->assertMenuLink('test6', array(
'has_children' => 0,
'depth' => 4,
), array(
'test5',
'test4',
'test1',
));
$this->treeStorage
->delete('test4');
$this
->assertMenuLink('test1', array(
'has_children' => 1,
'depth' => 1,
), array(), array(
'test5',
'test2',
'test3',
'test6',
));
$this
->assertMenuLink('test2', array(
'has_children' => 0,
'depth' => 3,
), array(
'test5',
'test1',
));
$this
->assertMenuLink('test3', array(
'has_children' => 0,
'depth' => 2,
), array(
'test1',
));
$this
->assertFalse($this->treeStorage
->load('test4'));
$this
->assertMenuLink('test5', array(
'has_children' => 1,
'depth' => 2,
), array(
'test1',
), array(
'test2',
'test6',
));
$this
->assertMenuLink('test6', array(
'has_children' => 0,
'depth' => 3,
), array(
'test5',
'test1',
));
}
public function testMenuDisabledChildLinks() {
$this
->addMenuLink('test1', '');
$this
->assertMenuLink('test1', array(
'has_children' => 0,
'depth' => 1,
));
$this
->addMenuLink('test2', 'test1', '<front>', array(), 'tools', array(
'enabled' => 0,
));
$this
->assertMenuLink('test1', array(
'has_children' => 0,
'depth' => 1,
));
$this
->assertMenuLink('test2', array(
'has_children' => 0,
'depth' => 2,
'enabled' => 0,
), array(
'test1',
));
$this
->addMenuLink('footerA', '', '<front>', array(), 'footer');
$visible_children = array();
for ($i = 3; $i <= $this->treeStorage
->maxDepth(); $i++) {
$parent = $i - 1;
$this
->addMenuLink("test{$i}", "test{$parent}");
$visible_children[] = "test{$i}";
}
$this
->assertMenuLink('test1', array(
'has_children' => 0,
'depth' => 1,
), array(), $visible_children);
try {
$this
->moveMenuLink('test1', 'footerA');
$this
->fail('Exception was not thrown');
} catch (PluginException $e) {
$this
->pass($e
->getMessage());
}
$this
->moveMenuLink('footerA', 'test1');
$visible_children[] = 'footerA';
$this
->assertMenuLink('test1', array(
'has_children' => 1,
'depth' => 1,
), array(), $visible_children);
}
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']);
$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'], []);
$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'], []);
}
public function testSubtreeHeight() {
$this
->addMenuLink('root');
$this
->addMenuLink('child1', 'root');
$this
->addMenuLink('child2', 'child1');
$this
->addMenuLink('child3', 'child2');
$this
->addMenuLink('child4', 'child3');
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('root'), 5);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('child1'), 4);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('child2'), 3);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('child3'), 2);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('child4'), 1);
}
public function testMenuRebuild() {
$this
->addMenuLink('root');
$this
->addMenuLink('child1', 'root');
$this
->addMenuLink('child2', 'child1');
$this
->addMenuLink('child3', 'child2');
$this
->addMenuLink('child4', 'child3');
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('root'), 5);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('child1'), 4);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('child2'), 3);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('child3'), 2);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('child4'), 1);
$definitions = $this->treeStorage
->loadMultiple([
'root',
'child1',
'child2',
'child4',
]);
$this->treeStorage
->rebuild($definitions);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('root'), 5);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('child1'), 4);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('child2'), 3);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('child3'), 2);
$this
->assertEqual($this->treeStorage
->getSubtreeHeight('child4'), 1);
}
public function testLoadByProperties() {
$tests = array(
array(
'foo' => 'bar',
),
array(
0 => 'wrong',
),
);
$message = 'An invalid property name throws an exception.';
foreach ($tests as $properties) {
try {
$this->treeStorage
->loadByProperties($properties);
$this
->fail($message);
} catch (\InvalidArgumentException $e) {
$this
->assertTrue(preg_match('/^An invalid property name, .+ was specified. Allowed property names are:/', $e
->getMessage()), 'Found expected exception message.');
$this
->pass($message);
}
}
$this
->addMenuLink('test_link.1', '', 'test', array(), 'menu1');
$properties = array(
'menu_name' => 'menu1',
);
$links = $this->treeStorage
->loadByProperties($properties);
$this
->assertEqual('menu1', $links['test_link.1']['menu_name']);
$this
->assertEqual('test', $links['test_link.1']['route_name']);
}
protected function addMenuLink($id, $parent = '', $route_name = 'test', $route_parameters = array(), $menu_name = 'tools', $extra = array()) {
$link = array(
'id' => $id,
'menu_name' => $menu_name,
'route_name' => $route_name,
'route_parameters' => $route_parameters,
'title' => 'test',
'parent' => $parent,
'options' => array(),
'metadata' => array(),
) + $extra;
$this->treeStorage
->save($link);
}
protected function moveMenuLink($id, $new_parent) {
$menu_link = $this->treeStorage
->load($id);
$menu_link['parent'] = $new_parent;
$this->treeStorage
->save($menu_link);
}
protected function assertMenuLink($id, array $expected_properties, array $parents = array(), array $children = array()) {
$query = $this->connection
->select('menu_tree');
$query
->fields('menu_tree');
$query
->condition('id', $id);
foreach ($expected_properties as $field => $value) {
$query
->condition($field, $value);
}
$all = $query
->execute()
->fetchAll(\PDO::FETCH_ASSOC);
$this
->assertEqual(count($all), 1, "Found link {$id} matching all the expected properties");
$raw = reset($all);
array_unshift($parents, $raw['id']);
$query = $this->connection
->select('menu_tree');
$query
->fields('menu_tree', array(
'id',
'mlid',
));
$query
->condition('id', $parents, 'IN');
$found_parents = $query
->execute()
->fetchAllKeyed(0, 1);
$this
->assertEqual(count($parents), count($found_parents), 'Found expected number of parents');
$this
->assertEqual($raw['depth'], count($found_parents), 'Number of parents is the same as the depth');
$materialized_path = $this->treeStorage
->getRootPathIds($id);
$this
->assertEqual(array_values($materialized_path), array_values($parents), 'Parents match the materialized path');
for ($i = $raw['depth']; $i >= 1; $i--) {
$parent_id = array_shift($parents);
$this
->assertEqual($raw["p{$i}"], $found_parents[$parent_id], "mlid of parent matches at column p{$i}");
}
for ($i = $raw['depth'] + 1; $i <= $this->treeStorage
->maxDepth(); $i++) {
$this
->assertEqual($raw["p{$i}"], 0, "parent is 0 at column p{$i} greater than depth");
}
if ($parents) {
$this
->assertEqual($raw['parent'], end($parents), 'Ensure that the parent field is set properly');
}
$found_children = array_keys($this->treeStorage
->loadAllChildren($id));
$this
->assertEqual(count($children), count($found_children), "Found expected number of children for {$id}");
$this
->assertEqual(array_intersect($children, $found_children), $children, 'Child IDs match');
}
}