View source
<?php
namespace Drupal\Tests\Core\Menu;
use Drupal\Core\Menu\MenuActiveTrail;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Tests\UnitTestCase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Route;
class MenuActiveTrailTest extends UnitTestCase {
protected $menuActiveTrail;
protected $requestStack;
protected $currentRouteMatch;
protected $menuLinkManager;
protected $cache;
protected $lock;
protected function setUp() {
parent::setUp();
$this->requestStack = new RequestStack();
$this->currentRouteMatch = new CurrentRouteMatch($this->requestStack);
$this->menuLinkManager = $this
->createMock('Drupal\\Core\\Menu\\MenuLinkManagerInterface');
$this->cache = $this
->createMock('\\Drupal\\Core\\Cache\\CacheBackendInterface');
$this->lock = $this
->createMock('\\Drupal\\Core\\Lock\\LockBackendInterface');
$this->menuActiveTrail = new MenuActiveTrail($this->menuLinkManager, $this->currentRouteMatch, $this->cache, $this->lock);
$container = new Container();
$container
->set('cache_tags.invalidator', $this
->createMock('\\Drupal\\Core\\Cache\\CacheTagsInvalidatorInterface'));
\Drupal::setContainer($container);
}
public function provider() {
$data = [];
$mock_route = new Route('');
$request = new Request();
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, 'baby_llama');
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, $mock_route);
$request->attributes
->set('_raw_variables', new ParameterBag([]));
$link_1 = MenuLinkMock::create([
'id' => 'baby_llama_link_1',
'route_name' => 'baby_llama',
'title' => 'Baby llama',
'parent' => 'mama_llama_link',
]);
$link_2 = MenuLinkMock::create([
'id' => 'baby_llama_link_2',
'route_name' => 'baby_llama',
'title' => 'Baby llama',
'parent' => 'papa_llama_link',
]);
$link_1_parent_ids = [
'baby_llama_link_1',
'mama_llama_link',
'',
];
$empty_active_trail = [
'',
];
$data[] = [
$request,
[],
$this
->randomMachineName(),
NULL,
$empty_active_trail,
];
$data[] = [
$request,
[
'baby_llama_link_1' => $link_1,
],
$this
->randomMachineName(),
$link_1,
$link_1_parent_ids,
];
$data[] = [
$request,
[
'baby_llama_link_1' => $link_1,
'baby_llama_link_2' => $link_2,
],
$this
->randomMachineName(),
$link_1,
$link_1_parent_ids,
];
$request = new Request();
$request->attributes
->set('_exception_statuscode', 403);
$data[] = [
$request,
FALSE,
$this
->randomMachineName(),
NULL,
$empty_active_trail,
];
$request = new Request();
$data[] = [
$request,
FALSE,
$this
->randomMachineName(),
NULL,
$empty_active_trail,
];
return $data;
}
public function testGetActiveLink(Request $request, $links, $menu_name, $expected_link) {
$this->requestStack
->push($request);
if ($links !== FALSE) {
$this->menuLinkManager
->expects($this
->exactly(2))
->method('loadLinksbyRoute')
->with('baby_llama')
->will($this
->returnValue($links));
}
$this
->assertSame($expected_link, $this->menuActiveTrail
->getActiveLink($menu_name));
$this
->assertSame($expected_link, $this->menuActiveTrail
->getActiveLink());
}
public function testGetActiveTrailIds(Request $request, $links, $menu_name, $expected_link, $expected_trail) {
$expected_trail_ids = array_combine($expected_trail, $expected_trail);
$this->requestStack
->push($request);
if ($links !== FALSE) {
$this->menuLinkManager
->expects($this
->exactly(2))
->method('loadLinksbyRoute')
->with('baby_llama')
->will($this
->returnValue($links));
if ($expected_link !== NULL) {
$this->menuLinkManager
->expects($this
->exactly(2))
->method('getParentIds')
->will($this
->returnValueMap([
[
$expected_link
->getPluginId(),
$expected_trail_ids,
],
]));
}
}
$this
->assertSame($expected_trail_ids, $this->menuActiveTrail
->getActiveTrailIds($menu_name));
$this
->assertSame($expected_trail_ids, $this->menuActiveTrail
->getActiveTrailIds($menu_name));
$this->menuActiveTrail
->clear();
$this
->assertSame($expected_trail_ids, $this->menuActiveTrail
->getActiveTrailIds($menu_name));
}
public function testGetCid() {
$data = $this
->provider()[1];
$request = $data[0];
$route = $request->attributes
->get(RouteObjectInterface::ROUTE_OBJECT);
$route
->setPath('/test/{b}/{a}');
$request->attributes
->get('_raw_variables')
->add([
'b' => 1,
'a' => 0,
]);
$this->requestStack
->push($request);
$this->menuLinkManager
->expects($this
->any())
->method('loadLinksbyRoute')
->with('baby_llama')
->will($this
->returnValue($data[1]));
$expected_link = $data[3];
$expected_trail = $data[4];
$expected_trail_ids = array_combine($expected_trail, $expected_trail);
$this->menuLinkManager
->expects($this
->any())
->method('getParentIds')
->will($this
->returnValueMap([
[
$expected_link
->getPluginId(),
$expected_trail_ids,
],
]));
$this
->assertSame($expected_trail_ids, $this->menuActiveTrail
->getActiveTrailIds($data[2]));
$this->cache
->expects($this
->once())
->method('set')
->with('active-trail:route:baby_llama:route_parameters:' . serialize([
'a' => 0,
'b' => 1,
]));
$this->lock
->expects($this
->any())
->method('acquire')
->willReturn(TRUE);
$this->menuActiveTrail
->destruct();
}
}