View source
<?php
namespace Drupal\system\Tests\Block;
use Drupal\Core\Render\Element;
use Drupal\simpletest\KernelTestBase;
use Drupal\system\Tests\Routing\MockRouteProvider;
use Drupal\Tests\Core\Menu\MenuLinkMock;
use Drupal\user\Entity\User;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class SystemMenuBlockTest extends KernelTestBase {
public static $modules = array(
'system',
'block',
'menu_test',
'menu_link_content',
'field',
'user',
'link',
);
protected $block;
protected $menu;
protected $linkTree;
protected $menuLinkManager;
protected $blockManager;
protected function setUp() {
parent::setUp();
$this
->installSchema('system', 'sequences');
$this
->installEntitySchema('user');
$this
->installSchema('system', array(
'router',
));
$this
->installEntitySchema('menu_link_content');
$account = User::create([
'name' => $this
->randomMachineName(),
'status' => 1,
]);
$account
->save();
$this->container
->get('current_user')
->setAccount($account);
$this->menuLinkManager = $this->container
->get('plugin.manager.menu.link');
$this->linkTree = $this->container
->get('menu.link_tree');
$this->blockManager = $this->container
->get('plugin.manager.block');
$routes = new RouteCollection();
$requirements = array(
'_access' => 'TRUE',
);
$options = array(
'_access_checks' => array(
'access_check.default',
),
);
$routes
->add('example1', new Route('/example1', array(), $requirements, $options));
$routes
->add('example2', new Route('/example2', array(), $requirements, $options));
$routes
->add('example3', new Route('/example3', array(), $requirements, $options));
$routes
->add('example4', new Route('/example4', array(), $requirements, $options));
$routes
->add('example5', new Route('/example5', array(), $requirements, $options));
$routes
->add('example6', new Route('/example6', array(), $requirements, $options));
$routes
->add('example7', new Route('/example7', array(), $requirements, $options));
$routes
->add('example8', new Route('/example8', array(), $requirements, $options));
$mock_route_provider = new MockRouteProvider($routes);
$this->container
->set('router.route_provider', $mock_route_provider);
$menu_name = 'mock';
$label = $this
->randomMachineName(16);
$this->menu = entity_create('menu', array(
'id' => $menu_name,
'label' => $label,
'description' => 'Description text',
));
$this->menu
->save();
$links = array(
1 => MenuLinkMock::create(array(
'id' => 'test.example1',
'route_name' => 'example1',
'title' => 'foo',
'parent' => '',
'weight' => 0,
)),
2 => MenuLinkMock::create(array(
'id' => 'test.example2',
'route_name' => 'example2',
'title' => 'bar',
'parent' => '',
'route_parameters' => array(
'foo' => 'bar',
),
'weight' => 1,
)),
3 => MenuLinkMock::create(array(
'id' => 'test.example3',
'route_name' => 'example3',
'title' => 'baz',
'parent' => 'test.example2',
'weight' => 2,
)),
4 => MenuLinkMock::create(array(
'id' => 'test.example4',
'route_name' => 'example4',
'title' => 'qux',
'parent' => 'test.example3',
'weight' => 3,
)),
5 => MenuLinkMock::create(array(
'id' => 'test.example5',
'route_name' => 'example5',
'title' => 'foofoo',
'parent' => '',
'expanded' => TRUE,
'weight' => 4,
)),
6 => MenuLinkMock::create(array(
'id' => 'test.example6',
'route_name' => '',
'url' => 'https://www.drupal.org/',
'title' => 'barbar',
'parent' => '',
'weight' => 5,
)),
7 => MenuLinkMock::create(array(
'id' => 'test.example7',
'route_name' => 'example7',
'title' => 'bazbaz',
'parent' => 'test.example5',
'weight' => 6,
)),
8 => MenuLinkMock::create(array(
'id' => 'test.example8',
'route_name' => 'example8',
'title' => 'quxqux',
'parent' => '',
'weight' => 7,
)),
);
foreach ($links as $instance) {
$this->menuLinkManager
->addDefinition($instance
->getPluginId(), $instance
->getPluginDefinition());
}
}
public function testSystemMenuBlockConfigDependencies() {
$block = entity_create('block', array(
'plugin' => 'system_menu_block:' . $this->menu
->id(),
'region' => 'footer',
'id' => 'machinename',
'theme' => 'stark',
));
$dependencies = $block
->calculateDependencies()
->getDependencies();
$expected = array(
'config' => array(
'system.menu.' . $this->menu
->id(),
),
'module' => array(
'system',
),
'theme' => array(
'stark',
),
);
$this
->assertIdentical($expected, $dependencies);
}
public function testConfigLevelDepth() {
$place_block = function ($level, $depth) {
return $this->blockManager
->createInstance('system_menu_block:' . $this->menu
->id(), array(
'region' => 'footer',
'id' => 'machinename',
'theme' => 'stark',
'level' => $level,
'depth' => $depth,
));
};
$blocks = [
'all' => $place_block(1, 0),
'level_1_only' => $place_block(1, 1),
'level_2_only' => $place_block(2, 1),
'level_3_only' => $place_block(3, 1),
'level_1_and_beyond' => $place_block(1, 0),
'level_2_and_beyond' => $place_block(2, 0),
'level_3_and_beyond' => $place_block(3, 0),
];
$no_active_trail_expectations = [];
$no_active_trail_expectations['all'] = [
'test.example1' => [],
'test.example2' => [],
'test.example5' => [
'test.example7' => [],
],
'test.example6' => [],
'test.example8' => [],
];
$no_active_trail_expectations['level_1_only'] = [
'test.example1' => [],
'test.example2' => [],
'test.example5' => [],
'test.example6' => [],
'test.example8' => [],
];
$no_active_trail_expectations['level_2_only'] = [
'test.example7' => [],
];
$no_active_trail_expectations['level_3_only'] = [];
$no_active_trail_expectations['level_1_and_beyond'] = $no_active_trail_expectations['all'];
$no_active_trail_expectations['level_2_and_beyond'] = $no_active_trail_expectations['level_2_only'];
$no_active_trail_expectations['level_3_and_beyond'] = [];
foreach ($blocks as $id => $block) {
$block_build = $block
->build();
$items = isset($block_build['#items']) ? $block_build['#items'] : [];
$this
->assertIdentical($no_active_trail_expectations[$id], $this
->convertBuiltMenuToIdTree($items), format_string('Menu block %id with no active trail renders the expected tree.', [
'%id' => $id,
]));
}
$route = $this->container
->get('router.route_provider')
->getRouteByName('example3');
$request = new Request();
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, 'example3');
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, $route);
$this->container
->get('request_stack')
->push($request);
\Drupal::service('menu.active_trail')
->clear();
$active_trail_expectations = [];
$active_trail_expectations['all'] = [
'test.example1' => [],
'test.example2' => [
'test.example3' => [
'test.example4' => [],
],
],
'test.example5' => [
'test.example7' => [],
],
'test.example6' => [],
'test.example8' => [],
];
$active_trail_expectations['level_1_only'] = [
'test.example1' => [],
'test.example2' => [],
'test.example5' => [],
'test.example6' => [],
'test.example8' => [],
];
$active_trail_expectations['level_2_only'] = [
'test.example3' => [],
'test.example7' => [],
];
$active_trail_expectations['level_3_only'] = [
'test.example4' => [],
];
$active_trail_expectations['level_1_and_beyond'] = $active_trail_expectations['all'];
$active_trail_expectations['level_2_and_beyond'] = [
'test.example3' => [
'test.example4' => [],
],
'test.example7' => [],
];
$active_trail_expectations['level_3_and_beyond'] = $active_trail_expectations['level_3_only'];
foreach ($blocks as $id => $block) {
$block_build = $block
->build();
$items = isset($block_build['#items']) ? $block_build['#items'] : [];
$this
->assertIdentical($active_trail_expectations[$id], $this
->convertBuiltMenuToIdTree($items), format_string('Menu block %id with an active trail renders the expected tree.', [
'%id' => $id,
]));
}
}
protected function convertBuiltMenuToIdTree(array $build) {
$level = [];
foreach (Element::children($build) as $id) {
$level[$id] = [];
if (isset($build[$id]['below'])) {
$level[$id] = $this
->convertBuiltMenuToIdTree($build[$id]['below']);
}
}
return $level;
}
}