5 * Contains \Drupal\Tests\views\Unit\EventSubscriber\RouteSubscriberTest.
8 namespace Drupal\Tests\views\Unit\EventSubscriber;
10 use Drupal\Core\Routing\RouteBuildEvent;
11 use Drupal\Tests\UnitTestCase;
12 use Drupal\views\EventSubscriber\RouteSubscriber;
13 use Symfony\Component\Routing\Route;
14 use Symfony\Component\Routing\RouteCollection;
17 * @coversDefaultClass \Drupal\views\EventSubscriber\RouteSubscriber
20 class RouteSubscriberTest extends UnitTestCase {
23 * The mocked entity manager.
25 * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
27 protected $entityManager;
30 * The mocked view storage.
32 * @var \Drupal\views\ViewStorage|\PHPUnit_Framework_MockObject_MockObject
34 protected $viewStorage;
37 * The tested views route subscriber.
39 * @var \Drupal\views\EventSubscriber\RouteSubscriber|\Drupal\Tests\views\Unit\EventSubscriber\TestRouteSubscriber
41 protected $routeSubscriber;
44 * The mocked key value storage.
46 * @var \Drupal\Core\State\StateInterface|\PHPUnit_Framework_MockObject_MockObject
50 protected function setUp() {
51 $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
52 $this->viewStorage = $this->getMockBuilder('Drupal\Core\Config\Entity\ConfigEntityStorage')
53 ->disableOriginalConstructor()
55 $this->entityManager->expects($this->any())
56 ->method('getStorage')
58 ->will($this->returnValue($this->viewStorage));
59 $this->state = $this->getMock('\Drupal\Core\State\StateInterface');
60 $this->routeSubscriber = new TestRouteSubscriber($this->entityManager, $this->state);
64 * @covers ::routeRebuildFinished
66 public function testRouteRebuildFinished() {
67 list($display_1, $display_2) = $this->setupMocks();
69 $display_1->expects($this->once())
70 ->method('collectRoutes')
71 ->will($this->returnValue(['test_id.page_1' => 'views.test_id.page_1']));
72 $display_2->expects($this->once())
73 ->method('collectRoutes')
74 ->will($this->returnValue(['test_id.page_2' => 'views.test_id.page_2']));
76 $this->routeSubscriber->routes();
78 $this->state->expects($this->once())
80 ->with('views.view_route_names', ['test_id.page_1' => 'views.test_id.page_1', 'test_id.page_2' => 'views.test_id.page_2']);
81 $this->routeSubscriber->routeRebuildFinished();
85 * Tests the onAlterRoutes method.
87 * @see \Drupal\views\EventSubscriber\RouteSubscriber::onAlterRoutes()
89 public function testOnAlterRoutes() {
90 $collection = new RouteCollection();
91 // The first route will be overridden later.
92 $collection->add('test_route', new Route('test_route', ['_controller' => 'Drupal\Tests\Core\Controller\TestController']));
93 $route_2 = new Route('test_route/example', ['_controller' => 'Drupal\Tests\Core\Controller\TestController']);
94 $collection->add('test_route_2', $route_2);
96 $route_event = new RouteBuildEvent($collection, 'views');
98 list($display_1, $display_2) = $this->setupMocks();
100 // The page_1 display overrides an existing route, so the dynamicRoutes
101 // should only call the second display.
102 $display_1->expects($this->once())
103 ->method('collectRoutes')
104 ->willReturnCallback(function () use ($collection) {
105 $collection->add('views.test_id.page_1', new Route('test_route', ['_controller' => 'Drupal\views\Routing\ViewPageController']));
106 return ['test_id.page_1' => 'views.test_id.page_1'];
108 $display_1->expects($this->once())
109 ->method('alterRoutes')
110 ->willReturn(['test_id.page_1' => 'test_route']);
112 $display_2->expects($this->once())
113 ->method('collectRoutes')
114 ->willReturnCallback(function () use ($collection) {
115 $collection->add('views.test_id.page_2', new Route('test_route', ['_controller' => 'Drupal\views\Routing\ViewPageController']));
116 return ['test_id.page_2' => 'views.test_id.page_2'];
118 $display_2->expects($this->once())
119 ->method('alterRoutes')
122 // Ensure that even both the collectRoutes() and alterRoutes() methods
123 // are called on the displays, we ensure that the route first defined by
126 $this->routeSubscriber->routes();
127 $this->assertNull($this->routeSubscriber->onAlterRoutes($route_event));
129 $this->state->expects($this->once())
131 ->with('views.view_route_names', ['test_id.page_1' => 'test_route', 'test_id.page_2' => 'views.test_id.page_2']);
133 $collection = $route_event->getRouteCollection();
134 $this->assertEquals(['test_route', 'test_route_2', 'views.test_id.page_2'], array_keys($collection->all()));
136 $this->routeSubscriber->routeRebuildFinished();
140 * Sets up mocks of Views objects needed for testing.
142 * @return \Drupal\views\Plugin\views\display\DisplayRouterInterface[]|\PHPUnit_Framework_MockObject_MockObject[]
143 * An array of two mocked view displays.
145 protected function setupMocks() {
146 $executable = $this->getMockBuilder('Drupal\views\ViewExecutable')
147 ->disableOriginalConstructor()
149 $view = $this->getMockBuilder('Drupal\views\Entity\View')
150 ->disableOriginalConstructor()
152 $this->viewStorage->expects($this->any())
154 ->will($this->returnValue($view));
156 $view->expects($this->any())
157 ->method('getExecutable')
158 ->will($this->returnValue($executable));
159 $view->expects($this->any())
161 ->will($this->returnValue('test_id'));
162 $executable->storage = $view;
164 $executable->expects($this->any())
165 ->method('setDisplay')
166 ->will($this->returnValueMap([
172 // Ensure that only the first two displays are actually called.
173 $display_1 = $this->getMock('Drupal\views\Plugin\views\display\DisplayRouterInterface');
174 $display_2 = $this->getMock('Drupal\views\Plugin\views\display\DisplayRouterInterface');
176 $display_collection = $this->getMockBuilder('Drupal\views\DisplayPluginCollection')
177 ->disableOriginalConstructor()
179 $display_collection->expects($this->any())
181 ->will($this->returnValueMap([
182 ['page_1', $display_1],
183 ['page_2', $display_2],
185 $executable->displayHandlers = $display_collection;
187 $this->routeSubscriber->applicableViews = [];
188 $this->routeSubscriber->applicableViews[] = ['test_id', 'page_1'];
189 $this->routeSubscriber->applicableViews[] = ['test_id', 'page_2'];
190 $this->routeSubscriber->applicableViews[] = ['test_id', 'page_3'];
192 return [$display_1, $display_2];
198 * Provides a test route subscriber.
200 class TestRouteSubscriber extends RouteSubscriber {
203 * The applicable views.
207 public $applicableViews;
212 protected function getApplicableViews() {
213 return $this->applicableViews;