Newer
Older
Angie Byron
committed
<?php
/**
* @file
Dries Buytaert
committed
* Contains \Drupal\Tests\views\Unit\Plugin\display\PathPluginBaseTest.
Angie Byron
committed
*/
Dries Buytaert
committed
namespace Drupal\Tests\views\Unit\Plugin\display;
Angie Byron
committed
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
/**
* @coversDefaultClass \Drupal\views\Plugin\views\display\PathPluginBase
* @group views
Angie Byron
committed
*/
class PathPluginBaseTest extends UnitTestCase {
/**
* The route provider that should be used.
*
* @var \Drupal\Core\Routing\RouteProviderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $routeProvider;
/**
* The tested path plugin base.
*
* @var \Drupal\views\Plugin\views\display\PathPluginBase|\PHPUnit_Framework_MockObject_MockObject
Angie Byron
committed
*/
protected $pathPlugin;
/**
* The mocked views access plugin manager.
*
* @var \Drupal\views\Plugin\ViewsPluginManager|\PHPUnit_Framework_MockObject_MockObject
*/
protected $accessPluginManager;
/**
* The mocked key value storage.
*
Alex Pott
committed
* @var \Drupal\Core\State\StateInterface|\PHPUnit_Framework_MockObject_MockObject
Angie Byron
committed
*/
protected $state;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->routeProvider = $this->getMock('Drupal\Core\Routing\RouteProviderInterface');
Alex Pott
committed
$this->state = $this->getMock('\Drupal\Core\State\StateInterface');
Angie Byron
committed
$this->pathPlugin = $this->getMockBuilder('Drupal\views\Plugin\views\display\PathPluginBase')
->setConstructorArgs(array(array(), 'path_base', array(), $this->routeProvider, $this->state))
Angie Byron
committed
->setMethods(NULL)
->getMock();
$this->setupContainer();
Angie Byron
committed
}
/**
* Setup access plugin manager and config factory in the Drupal class.
Angie Byron
committed
*/
public function setupContainer() {
Angie Byron
committed
$this->accessPluginManager = $this->getMockBuilder('\Drupal\views\Plugin\ViewsPluginManager')
->disableOriginalConstructor()
->getMock();
$container = new ContainerBuilder();
$container->set('plugin.manager.views.access', $this->accessPluginManager);
$config = array(
'views.settings' => array(
'skip_cache' => TRUE,
'display_extenders' => array(),
),
);
$container->set('config.factory', $this->getConfigFactoryStub($config));
Angie Byron
committed
\Drupal::setContainer($container);
}
/**
* Tests the collectRoutes method.
*
* @see \Drupal\views\Plugin\views\display\PathPluginBase::collectRoutes()
*/
public function testCollectRoutes() {
list($view) = $this->setupViewExecutableAccessPlugin();
$display = array();
$display['display_plugin'] = 'page';
$display['id'] = 'page_1';
$display['display_options'] = array(
'path' => 'test_route',
);
$this->pathPlugin->initDisplay($view, $display);
$collection = new RouteCollection();
$result = $this->pathPlugin->collectRoutes($collection);
$this->assertEquals(array('test_id.page_1' => 'view.test_id.page_1'), $result);
$route = $collection->get('view.test_id.page_1');
$this->assertTrue($route instanceof Route);
$this->assertEquals('test_id', $route->getDefault('view_id'));
$this->assertEquals('page_1', $route->getDefault('display_id'));
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
$this->assertSame(FALSE, $route->getOption('returns_response'));
}
/**
* Tests the collectRoutes method with a display returning a response.
*
* @see \Drupal\views\Plugin\views\display\PathPluginBase::collectRoutes()
*/
public function testCollectRoutesWithDisplayReturnResponse() {
list($view) = $this->setupViewExecutableAccessPlugin();
$display = array();
$display['display_plugin'] = 'page';
$display['id'] = 'page_1';
$display['display_options'] = array(
'path' => 'test_route',
);
$this->pathPlugin = $this->getMockBuilder('Drupal\views\Plugin\views\display\PathPluginBase')
->setConstructorArgs(array(array(), 'path_base', array('returns_response' => TRUE), $this->routeProvider, $this->state))
->setMethods(NULL)
->getMock();
$this->pathPlugin->initDisplay($view, $display);
$collection = new RouteCollection();
$this->pathPlugin->collectRoutes($collection);
$route = $collection->get('view.test_id.page_1');
$this->assertSame(TRUE, $route->getOption('returns_response'));
}
/**
* Tests the collectRoutes method with arguments.
*
* @see \Drupal\views\Plugin\views\display\PathPluginBase::collectRoutes()
*/
public function testCollectRoutesWithArguments() {
list($view) = $this->setupViewExecutableAccessPlugin();
$display = array();
$display['display_plugin'] = 'page';
$display['id'] = 'page_1';
$display['display_options'] = array(
'path' => 'test_route/%/example',
);
$this->pathPlugin->initDisplay($view, $display);
$collection = new RouteCollection();
$result = $this->pathPlugin->collectRoutes($collection);
$this->assertEquals(array('test_id.page_1' => 'view.test_id.page_1'), $result);
$route = $collection->get('view.test_id.page_1');
$this->assertTrue($route instanceof Route);
$this->assertEquals('test_id', $route->getDefault('view_id'));
$this->assertEquals('page_1', $route->getDefault('display_id'));
$this->assertEquals(array('arg_0' => 'arg_0'), $route->getOption('_view_argument_map'));
}
/**
* Tests the collectRoutes method with arguments not specified in the path.
*
* @see \Drupal\views\Plugin\views\display\PathPluginBase::collectRoutes()
*/
public function testCollectRoutesWithArgumentsNotSpecifiedInPath() {
list($view) = $this->setupViewExecutableAccessPlugin();
$display = array();
$display['display_plugin'] = 'page';
$display['id'] = 'page_1';
$display['display_options'] = array(
'path' => 'test_with_arguments',
);
$display['display_options']['arguments'] = array(
'test_id' => array(),
);
$this->pathPlugin->initDisplay($view, $display);
$collection = new RouteCollection();
$result = $this->pathPlugin->collectRoutes($collection);
$this->assertEquals(array('test_id.page_1' => 'view.test_id.page_1'), $result);
$route = $collection->get('view.test_id.page_1');
$this->assertTrue($route instanceof Route);
$this->assertEquals('test_id', $route->getDefault('view_id'));
$this->assertEquals('page_1', $route->getDefault('display_id'));
$this->assertEquals(array('arg_0' => 'arg_0'), $route->getOption('_view_argument_map'));
Dries Buytaert
committed
}
/**
* Tests the collect routes method with an alternative route name in the UI.
*/
public function testCollectRoutesWithSpecialRouteName() {
list($view) = $this->setupViewExecutableAccessPlugin();
Angie Byron
committed
Dries Buytaert
committed
$display = array();
$display['display_plugin'] = 'page';
$display['id'] = 'page_1';
$display['display_options'] = array(
'path' => 'test_route',
'route_name' => 'test_route',
);
$this->pathPlugin->initDisplay($view, $display);
$collection = new RouteCollection();
$result = $this->pathPlugin->collectRoutes($collection);
$this->assertEquals(array('test_id.page_1' => 'test_route'), $result);
$route = $collection->get('test_route');
$this->assertTrue($route instanceof Route);
$this->assertEquals('test_id', $route->getDefault('view_id'));
$this->assertEquals('page_1', $route->getDefault('display_id'));
Angie Byron
committed
}
/**
* Tests the alter route method.
*/
public function testAlterRoute() {
$collection = new RouteCollection();
Dries Buytaert
committed
$collection->add('test_route', new Route('test_route', array('_controller' => 'Drupal\Tests\Core\Controller\TestController::content')));
$route_2 = new Route('test_route/example', array('_controller' => 'Drupal\Tests\Core\Controller\TestController::content'));
Angie Byron
committed
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
$collection->add('test_route_2', $route_2);
list($view) = $this->setupViewExecutableAccessPlugin();
$display = array();
$display['display_plugin'] = 'page';
$display['id'] = 'page_1';
$display['display_options'] = array(
'path' => 'test_route',
);
$this->pathPlugin->initDisplay($view, $display);
$view_route_names = $this->pathPlugin->alterRoutes($collection);
$this->assertEquals(array('test_id.page_1' => 'test_route'), $view_route_names);
// Ensure that the test_route is overridden.
$route = $collection->get('test_route');
$this->assertTrue($route instanceof Route);
$this->assertEquals('test_id', $route->getDefault('view_id'));
$this->assertEquals('page_1', $route->getDefault('display_id'));
// Ensure that the test_route_2 is not overridden.
$route = $collection->get('test_route_2');
$this->assertTrue($route instanceof Route);
$this->assertFalse($route->hasDefault('view_id'));
$this->assertFalse($route->hasDefault('display_id'));
$this->assertSame($collection->get('test_route_2'), $route_2);
}
catch
committed
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* Tests the collectRoutes method with a path containing named parameters.
*
* @see \Drupal\views\Plugin\views\display\PathPluginBase::collectRoutes()
*/
public function testCollectRoutesWithNamedParameters() {
/** @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject $view */
list($view) = $this->setupViewExecutableAccessPlugin();
$view->argument = array();
$view->argument['nid'] = $this->getMockBuilder('Drupal\views\Plugin\views\argument\ArgumentPluginBase')
->disableOriginalConstructor()
->getMock();
$display = array();
$display['display_plugin'] = 'page';
$display['id'] = 'page_1';
$display['display_options'] = array(
'path' => 'test_route/%node/example',
);
$this->pathPlugin->initDisplay($view, $display);
$collection = new RouteCollection();
$result = $this->pathPlugin->collectRoutes($collection);
$this->assertEquals(array('test_id.page_1' => 'view.test_id.page_1'), $result);
$route = $collection->get('view.test_id.page_1');
$this->assertTrue($route instanceof Route);
$this->assertEquals('/test_route/{node}/example', $route->getPath());
$this->assertEquals('test_id', $route->getDefault('view_id'));
$this->assertEquals('page_1', $route->getDefault('display_id'));
Alex Pott
committed
$this->assertEquals(array('arg_0' => 'node'), $route->getOption('_view_argument_map'));
catch
committed
}
Dries Buytaert
committed
/**
* Tests altering routes with parameters in the overridden route.
Dries Buytaert
committed
*/
public function testAlterRoutesWithParameters() {
$collection = new RouteCollection();
$collection->add('test_route', new Route('test_route/{parameter}', array('_controller' => 'Drupal\Tests\Core\Controller\TestController::content')));
list($view) = $this->setupViewExecutableAccessPlugin();
// Manually set up an argument handler.
Dries Buytaert
committed
$argument = $this->getMockBuilder('Drupal\views\Plugin\views\argument\ArgumentPluginBase')
->disableOriginalConstructor()
->getMock();
$view->argument['test_id'] = $argument;
$display = array();
$display['display_plugin'] = 'page';
$display['id'] = 'page_1';
$display['display_options'] = array(
'path' => 'test_route/%',
);
$this->pathPlugin->initDisplay($view, $display);
$view_route_names = $this->pathPlugin->alterRoutes($collection);
$this->assertEquals(array('test_id.page_1' => 'test_route'), $view_route_names);
// Ensure that the test_route is overridden.
$route = $collection->get('test_route');
$this->assertInstanceOf('\Symfony\Component\Routing\Route', $route);
$this->assertEquals('test_id', $route->getDefault('view_id'));
$this->assertEquals('page_1', $route->getDefault('display_id'));
// Ensure that the path did not changed and placeholders are respected.
$this->assertEquals('/test_route/{parameter}', $route->getPath());
Alex Pott
committed
$this->assertEquals(array('arg_0' => 'parameter'), $route->getOption('_view_argument_map'));
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
}
/**
* Tests altering routes with parameters and upcasting information
*/
public function testAlterRoutesWithParametersAndUpcasting() {
$collection = new RouteCollection();
$collection->add('test_route', new Route('test_route/{parameter}', ['_controller' => 'Drupal\Tests\Core\Controller\TestController::content'], [], ['parameters' => ['taxonomy_term' => 'entity:entity_test']]));
list($view) = $this->setupViewExecutableAccessPlugin();
// Manually set up an argument handler.
$argument = $this->getMockBuilder('Drupal\views\Plugin\views\argument\ArgumentPluginBase')
->disableOriginalConstructor()
->getMock();
$view->argument['test_id'] = $argument;
$display = array();
$display['display_plugin'] = 'page';
$display['id'] = 'page_1';
$display['display_options'] = [
'path' => 'test_route/%',
];
$this->pathPlugin->initDisplay($view, $display);
$view_route_names = $this->pathPlugin->alterRoutes($collection);
$this->assertEquals(['test_id.page_1' => 'test_route'], $view_route_names);
// Ensure that the test_route is overridden.
$route = $collection->get('test_route');
$this->assertInstanceOf('\Symfony\Component\Routing\Route', $route);
$this->assertEquals('test_id', $route->getDefault('view_id'));
$this->assertEquals('page_1', $route->getDefault('display_id'));
$this->assertEquals(['taxonomy_term' => 'entity:entity_test'], $route->getOption('parameters'));
// Ensure that the path did not changed and placeholders are respected kk.
$this->assertEquals('/test_route/{parameter}', $route->getPath());
$this->assertEquals(['arg_0' => 'parameter'], $route->getOption('_view_argument_map'));
}
/**
* Tests altering routes with optional parameters in the overridden route.
*/
public function testAlterRoutesWithOptionalParameters() {
$collection = new RouteCollection();
$collection->add('test_route', new Route('test_route/{parameter}', array('_controller' => 'Drupal\Tests\Core\Controller\TestController::content')));
list($view) = $this->setupViewExecutableAccessPlugin();
$display = array();
$display['display_plugin'] = 'page';
$display['id'] = 'page_1';
$display['display_options'] = array(
'path' => 'test_route/%',
);
$display['display_options']['arguments'] = array(
'test_id' => array(),
'test_id2' => array(),
);
$this->pathPlugin->initDisplay($view, $display);
$view_route_names = $this->pathPlugin->alterRoutes($collection);
$this->assertEquals(array('test_id.page_1' => 'test_route'), $view_route_names);
// Ensure that the test_route is overridden.
$route = $collection->get('test_route');
$this->assertInstanceOf('\Symfony\Component\Routing\Route', $route);
$this->assertEquals('test_id', $route->getDefault('view_id'));
$this->assertEquals('page_1', $route->getDefault('display_id'));
// Ensure that the path did not changed and placeholders are respected.
$this->assertEquals('/test_route/{parameter}/{arg_1}', $route->getPath());
$this->assertEquals(array('arg_0' => 'parameter'), $route->getOption('_view_argument_map'));
Dries Buytaert
committed
}
/**
* Tests the getRouteName method.
*/
public function testGetRouteName() {
list($view) = $this->setupViewExecutableAccessPlugin();
$display = array();
$display['display_plugin'] = 'page';
$display['id'] = 'page_1';
$display['display_options'] = array(
'path' => 'test_route',
);
$this->pathPlugin->initDisplay($view, $display);
$route_name = $this->pathPlugin->getRouteName();
// Ensure that the expected routename is returned.
$this->assertEquals('view.test_id.page_1', $route_name);
}
Angie Byron
committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**
* Returns some mocked view entity, view executable, and access plugin.
*/
protected function setupViewExecutableAccessPlugin() {
$view_entity = $this->getMockBuilder('Drupal\views\Entity\View')
->disableOriginalConstructor()
->getMock();
$view_entity->expects($this->any())
->method('id')
->will($this->returnValue('test_id'));
$view = $this->getMockBuilder('Drupal\views\ViewExecutable')
->disableOriginalConstructor()
->getMock();
$view->storage = $view_entity;
// Skip views options caching.
$view->editing = TRUE;
$access_plugin = $this->getMockBuilder('Drupal\views\Plugin\views\access\AccessPluginBase')
->disableOriginalConstructor()
->getMockForAbstractClass();
$this->accessPluginManager->expects($this->any())
->method('createInstance')
->will($this->returnValue($access_plugin));
return array($view, $view_entity, $access_plugin);
}
}