View source
<?php
namespace Drupal\Tests\views\Unit\EventSubscriber;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteBuildEvent;
use Drupal\Tests\UnitTestCase;
use Drupal\views\EventSubscriber\RouteSubscriber;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class RouteSubscriberTest extends UnitTestCase {
protected $entityTypeManager;
protected $viewStorage;
protected $routeSubscriber;
protected $state;
protected function setUp() : void {
$this->entityTypeManager = $this
->createMock(EntityTypeManagerInterface::class);
$this->viewStorage = $this
->getMockBuilder('Drupal\\Core\\Config\\Entity\\ConfigEntityStorage')
->disableOriginalConstructor()
->getMock();
$this->entityTypeManager
->expects($this
->any())
->method('getStorage')
->with('view')
->will($this
->returnValue($this->viewStorage));
$this->state = $this
->createMock('\\Drupal\\Core\\State\\StateInterface');
$this->routeSubscriber = new TestRouteSubscriber($this->entityTypeManager, $this->state);
}
public function testRouteRebuildFinished() {
[
$display_1,
$display_2,
] = $this
->setupMocks();
$display_1
->expects($this
->once())
->method('collectRoutes')
->will($this
->returnValue([
'test_id.page_1' => 'views.test_id.page_1',
]));
$display_2
->expects($this
->once())
->method('collectRoutes')
->will($this
->returnValue([
'test_id.page_2' => 'views.test_id.page_2',
]));
$this->routeSubscriber
->routes();
$this->state
->expects($this
->once())
->method('set')
->with('views.view_route_names', [
'test_id.page_1' => 'views.test_id.page_1',
'test_id.page_2' => 'views.test_id.page_2',
]);
$this->routeSubscriber
->routeRebuildFinished();
}
public function testOnAlterRoutes() {
$collection = new RouteCollection();
$collection
->add('test_route', new Route('test_route', [
'_controller' => 'Drupal\\Tests\\Core\\Controller\\TestController',
]));
$route_2 = new Route('test_route/example', [
'_controller' => 'Drupal\\Tests\\Core\\Controller\\TestController',
]);
$collection
->add('test_route_2', $route_2);
$route_event = new RouteBuildEvent($collection, 'views');
[
$display_1,
$display_2,
] = $this
->setupMocks();
$display_1
->expects($this
->once())
->method('collectRoutes')
->willReturnCallback(function () use ($collection) {
$collection
->add('views.test_id.page_1', new Route('test_route', [
'_controller' => 'Drupal\\views\\Routing\\ViewPageController',
]));
return [
'test_id.page_1' => 'views.test_id.page_1',
];
});
$display_1
->expects($this
->once())
->method('alterRoutes')
->willReturn([
'test_id.page_1' => 'test_route',
]);
$display_2
->expects($this
->once())
->method('collectRoutes')
->willReturnCallback(function () use ($collection) {
$collection
->add('views.test_id.page_2', new Route('test_route', [
'_controller' => 'Drupal\\views\\Routing\\ViewPageController',
]));
return [
'test_id.page_2' => 'views.test_id.page_2',
];
});
$display_2
->expects($this
->once())
->method('alterRoutes')
->willReturn([]);
$this->routeSubscriber
->routes();
$this
->assertNull($this->routeSubscriber
->onAlterRoutes($route_event));
$this->state
->expects($this
->once())
->method('set')
->with('views.view_route_names', [
'test_id.page_1' => 'test_route',
'test_id.page_2' => 'views.test_id.page_2',
]);
$collection = $route_event
->getRouteCollection();
$this
->assertEquals([
'test_route',
'test_route_2',
'views.test_id.page_2',
], array_keys($collection
->all()));
$this->routeSubscriber
->routeRebuildFinished();
}
protected function setupMocks() {
$executable = $this
->getMockBuilder('Drupal\\views\\ViewExecutable')
->disableOriginalConstructor()
->getMock();
$view = $this
->getMockBuilder('Drupal\\views\\Entity\\View')
->disableOriginalConstructor()
->getMock();
$this->viewStorage
->expects($this
->any())
->method('load')
->will($this
->returnValue($view));
$view
->expects($this
->any())
->method('getExecutable')
->will($this
->returnValue($executable));
$view
->expects($this
->any())
->method('id')
->will($this
->returnValue('test_id'));
$executable->storage = $view;
$executable
->expects($this
->any())
->method('setDisplay')
->willReturnMap([
[
'page_1',
TRUE,
],
[
'page_2',
TRUE,
],
[
'page_3',
FALSE,
],
]);
$display_1 = $this
->createMock('Drupal\\views\\Plugin\\views\\display\\DisplayRouterInterface');
$display_2 = $this
->createMock('Drupal\\views\\Plugin\\views\\display\\DisplayRouterInterface');
$display_collection = $this
->getMockBuilder('Drupal\\views\\DisplayPluginCollection')
->disableOriginalConstructor()
->getMock();
$display_collection
->expects($this
->any())
->method('get')
->willReturnMap([
[
'page_1',
$display_1,
],
[
'page_2',
$display_2,
],
]);
$executable->displayHandlers = $display_collection;
$this->routeSubscriber->applicableViews = [];
$this->routeSubscriber->applicableViews[] = [
'test_id',
'page_1',
];
$this->routeSubscriber->applicableViews[] = [
'test_id',
'page_2',
];
$this->routeSubscriber->applicableViews[] = [
'test_id',
'page_3',
];
return [
$display_1,
$display_2,
];
}
}
class TestRouteSubscriber extends RouteSubscriber {
public $applicableViews;
protected function getApplicableViews() {
return $this->applicableViews;
}
}