View source
<?php
namespace Drupal\Tests\views\Unit\Routing {
use Drupal\Core\Routing\RouteMatch;
use Drupal\Tests\UnitTestCase;
use Drupal\views\Routing\ViewPageController;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
class ViewPageControllerTest extends UnitTestCase {
public $pageController;
protected $defaultRenderArray = [
'#cache_properties' => [
'#view_id',
'#view_display_show_admin_links',
'#view_display_plugin_id',
],
'#view_id' => 'test_page_view',
'#view_display_plugin_id' => NULL,
'#view_display_show_admin_links' => NULL,
];
protected function setUp() {
$this->pageController = new ViewPageController();
}
public function testPageController() {
$build = [
'#type' => 'view',
'#name' => 'test_page_view',
'#display_id' => 'default',
'#embed' => FALSE,
'#arguments' => [],
'#cache' => [
'keys' => [
'view',
'test_page_view',
'display',
'default',
],
],
] + $this->defaultRenderArray;
$request = new Request();
$request->attributes
->set('view_id', 'test_page_view');
$request->attributes
->set('display_id', 'default');
$options = [
'_view_display_plugin_class' => '\\Drupal\\views\\Plugin\\views\\display\\Page',
];
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/test', [
'view_id' => 'test_page_view',
'display_id' => 'default',
], [], $options));
$route_match = RouteMatch::createFromRequest($request);
$output = $this->pageController
->handle($route_match
->getParameter('view_id'), $route_match
->getParameter('display_id'), $route_match);
$this
->assertInternalType('array', $output);
$this
->assertEquals($build, $output);
}
public function testHandleWithArgumentsWithoutOverridden() {
$request = new Request();
$request->attributes
->set('view_id', 'test_page_view');
$request->attributes
->set('display_id', 'page_1');
$request->attributes
->set('arg_0', 'test-argument');
$options = [
'_view_argument_map' => [
'arg_0' => 'arg_0',
],
'_view_display_plugin_class' => '\\Drupal\\views\\Plugin\\views\\display\\Page',
];
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/test/{arg_0}', [
'view_id' => 'test_page_view',
'display_id' => 'default',
], [], $options));
$route_match = RouteMatch::createFromRequest($request);
$result = $this->pageController
->handle($route_match
->getParameter('view_id'), $route_match
->getParameter('display_id'), $route_match);
$build = [
'#type' => 'view',
'#name' => 'test_page_view',
'#display_id' => 'page_1',
'#embed' => FALSE,
'#arguments' => [
'test-argument',
],
'#cache' => [
'keys' => [
'view',
'test_page_view',
'display',
'page_1',
'args',
'test-argument',
],
],
] + $this->defaultRenderArray;
$this
->assertEquals($build, $result);
}
public function testHandleWithArgumentsOnOverriddenRoute() {
$request = new Request();
$request->attributes
->set('view_id', 'test_page_view');
$request->attributes
->set('display_id', 'page_1');
$request->attributes
->set('parameter', 'test-argument');
$options = [
'_view_argument_map' => [
'arg_0' => 'parameter',
],
'_view_display_plugin_class' => '\\Drupal\\views\\Plugin\\views\\display\\Page',
];
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/test/{parameter}', [
'view_id' => 'test_page_view',
'display_id' => 'default',
], [], $options));
$route_match = RouteMatch::createFromRequest($request);
$result = $this->pageController
->handle($route_match
->getParameter('view_id'), $route_match
->getParameter('display_id'), $route_match);
$build = [
'#type' => 'view',
'#name' => 'test_page_view',
'#display_id' => 'page_1',
'#embed' => FALSE,
'#arguments' => [
'test-argument',
],
'#cache' => [
'keys' => [
'view',
'test_page_view',
'display',
'page_1',
'args',
'test-argument',
],
],
] + $this->defaultRenderArray;
$this
->assertEquals($build, $result);
}
public function testHandleWithArgumentsOnOverriddenRouteWithUpcasting() {
$request = new Request();
$request->attributes
->set('view_id', 'test_page_view');
$request->attributes
->set('display_id', 'page_1');
$request->attributes
->set('test_entity', $this
->getMock('Drupal\\Core\\Entity\\EntityInterface'));
$raw_variables = new ParameterBag(array(
'test_entity' => 'example_id',
));
$request->attributes
->set('_raw_variables', $raw_variables);
$options = [
'_view_argument_map' => [
'arg_0' => 'test_entity',
],
'_view_display_plugin_class' => '\\Drupal\\views\\Plugin\\views\\display\\Page',
];
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/test/{test_entity}', [
'view_id' => 'test_page_view',
'display_id' => 'default',
], [], $options));
$route_match = RouteMatch::createFromRequest($request);
$result = $this->pageController
->handle($route_match
->getParameter('view_id'), $route_match
->getParameter('display_id'), $route_match);
$build = [
'#type' => 'view',
'#name' => 'test_page_view',
'#display_id' => 'page_1',
'#embed' => FALSE,
'#arguments' => [
'example_id',
],
'#cache' => [
'keys' => [
'view',
'test_page_view',
'display',
'page_1',
'args',
'example_id',
],
],
] + $this->defaultRenderArray;
$this
->assertEquals($build, $result);
}
}
}
namespace {
if (!function_exists('views_add_contextual_links')) {
function views_add_contextual_links() {
}
}
}