View source
<?php
namespace Drupal\menu_link_content\Tests;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\simpletest\WebTestBase;
class LinksTest extends WebTestBase {
public static $modules = array(
'router_test',
'menu_link_content',
);
protected $menuLinkManager;
protected function setUp() {
parent::setUp();
$this->menuLinkManager = \Drupal::service('plugin.manager.menu.link');
entity_create('menu', array(
'id' => 'menu_test',
'label' => 'Test menu',
'description' => 'Description text',
))
->save();
}
function createLinkHierarchy($module = 'menu_test') {
$this->menuLinkManager
->deleteLinksInMenu('menu_test');
$base_options = array(
'title' => 'Menu link test',
'provider' => $module,
'menu_name' => 'menu_test',
);
$parent = $base_options + array(
'link' => [
'uri' => 'internal:/menu-test/hierarchy/parent',
],
);
$link = entity_create('menu_link_content', $parent);
$link
->save();
$links['parent'] = $link
->getPluginId();
$child_1 = $base_options + array(
'link' => [
'uri' => 'internal:/menu-test/hierarchy/parent/child',
],
'parent' => $links['parent'],
);
$link = entity_create('menu_link_content', $child_1);
$link
->save();
$links['child-1'] = $link
->getPluginId();
$child_1_1 = $base_options + array(
'link' => [
'uri' => 'internal:/menu-test/hierarchy/parent/child2/child',
],
'parent' => $links['child-1'],
);
$link = entity_create('menu_link_content', $child_1_1);
$link
->save();
$links['child-1-1'] = $link
->getPluginId();
$child_1_2 = $base_options + array(
'link' => [
'uri' => 'internal:/menu-test/hierarchy/parent/child2/child',
],
'parent' => $links['child-1'],
);
$link = entity_create('menu_link_content', $child_1_2);
$link
->save();
$links['child-1-2'] = $link
->getPluginId();
$child_2 = $base_options + array(
'link' => [
'uri' => 'internal:/menu-test/hierarchy/parent/child',
],
'parent' => $links['parent'],
);
$link = entity_create('menu_link_content', $child_2);
$link
->save();
$links['child-2'] = $link
->getPluginId();
return $links;
}
function assertMenuLinkParents($links, $expected_hierarchy) {
foreach ($expected_hierarchy as $id => $parent) {
$menu_link_plugin = $this->menuLinkManager
->createInstance($links[$id]);
$expected_parent = isset($links[$parent]) ? $links[$parent] : '';
$this
->assertEqual($menu_link_plugin
->getParent(), $expected_parent, SafeMarkup::format('Menu link %id has parent of %parent, expected %expected_parent.', array(
'%id' => $id,
'%parent' => $menu_link_plugin
->getParent(),
'%expected_parent' => $expected_parent,
)));
}
}
public function testCreateLink() {
$options = array(
'menu_name' => 'menu_test',
'bundle' => 'menu_link_content',
'link' => [
[
'uri' => 'internal:/',
],
],
);
$link = entity_create('menu_link_content', $options);
$link
->save();
$this
->assertEqual($link
->getChangedTime(), REQUEST_TIME, 'Creating a menu link sets the "changed" timestamp.');
$options = array(
'title' => 'Test Link',
);
$link->link->options = $options;
$link->changed->value = 0;
$link
->save();
$this
->assertEqual($link
->getChangedTime(), REQUEST_TIME, 'Changing a menu link sets "changed" timestamp.');
}
function testMenuLinkReparenting($module = 'menu_test') {
$links = $this
->createLinkHierarchy($module);
$expected_hierarchy = array(
'parent' => '',
'child-1' => 'parent',
'child-1-1' => 'child-1',
'child-1-2' => 'child-1',
'child-2' => 'parent',
);
$this
->assertMenuLinkParents($links, $expected_hierarchy);
$links = $this
->createLinkHierarchy($module);
$this->menuLinkManager
->updateDefinition($links['child-1'], array(
'parent' => $links['child-2'],
));
$menu_link_plugin = $this->menuLinkManager
->createInstance($links['child-1']);
$entity = \Drupal::entityManager()
->loadEntityByUuid('menu_link_content', $menu_link_plugin
->getDerivativeId());
$this
->assertEqual($entity
->getParentId(), $links['child-2']);
$expected_hierarchy = array(
'parent' => '',
'child-1' => 'child-2',
'child-1-1' => 'child-1',
'child-1-2' => 'child-1',
'child-2' => 'parent',
);
$this
->assertMenuLinkParents($links, $expected_hierarchy);
$links = $this
->createLinkHierarchy($module);
$this->menuLinkManager
->removeDefinition($links['child-1']);
$expected_hierarchy = array(
'parent' => FALSE,
'child-1-1' => 'parent',
'child-1-2' => 'parent',
'child-2' => 'parent',
);
$this
->assertMenuLinkParents($links, $expected_hierarchy);
}
public function testModuleUninstalledMenuLinks() {
\Drupal::service('module_installer')
->install(array(
'menu_test',
));
\Drupal::service('router.builder')
->rebuild();
\Drupal::service('plugin.manager.menu.link')
->rebuild();
$menu_links = $this->menuLinkManager
->loadLinksByRoute('menu_test.menu_test');
$this
->assertEqual(count($menu_links), 1);
$menu_link = reset($menu_links);
$this
->assertEqual($menu_link
->getPluginId(), 'menu_test');
\Drupal::service('module_installer')
->uninstall(array(
'menu_test',
));
\Drupal::service('plugin.manager.menu.link')
->rebuild();
$menu_links = $this->menuLinkManager
->loadLinksByRoute('menu_test.menu_test');
$this
->assertEqual(count($menu_links), 0);
}
}