3 namespace Drupal\Tests\rest\Unit;
5 use Drupal\Tests\UnitTestCase;
6 use Drupal\Core\DependencyInjection\ContainerBuilder;
7 use Drupal\rest\Plugin\views\display\RestExport;
8 use Symfony\Component\Routing\Route;
9 use Symfony\Component\Routing\RouteCollection;
12 * Tests the REST export view plugin.
16 class CollectRoutesTest extends UnitTestCase {
19 * The REST export instance.
21 * @var \Drupal\rest\Plugin\views\display\RestExport
23 protected $restExport;
26 * The RouteCollection.
33 protected function setUp() {
36 $container = new ContainerBuilder();
38 $request = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Request')
39 ->disableOriginalConstructor()
42 $this->view = $this->getMock('\Drupal\views\Entity\View', ['initHandlers'], [
43 ['id' => 'test_view'],
47 $view_executable = $this->getMock('\Drupal\views\ViewExecutable', ['initHandlers', 'getTitle'], [], '', FALSE);
48 $view_executable->expects($this->any())
50 ->willReturn('View title');
52 $view_executable->storage = $this->view;
53 $view_executable->argument = [];
55 $display_manager = $this->getMockBuilder('\Drupal\views\Plugin\ViewsPluginManager')
56 ->disableOriginalConstructor()
58 $container->set('plugin.manager.views.display', $display_manager);
60 $access_manager = $this->getMockBuilder('\Drupal\views\Plugin\ViewsPluginManager')
61 ->disableOriginalConstructor()
63 $container->set('plugin.manager.views.access', $access_manager);
65 $route_provider = $this->getMockBuilder('\Drupal\Core\Routing\RouteProviderInterface')
66 ->disableOriginalConstructor()
68 $container->set('router.route_provider', $route_provider);
70 $container->setParameter('authentication_providers', ['basic_auth' => 'basic_auth']);
72 $state = $this->getMock('\Drupal\Core\State\StateInterface');
73 $container->set('state', $state);
75 $style_manager = $this->getMockBuilder('\Drupal\views\Plugin\ViewsPluginManager')
76 ->disableOriginalConstructor()
78 $container->set('plugin.manager.views.style', $style_manager);
79 $container->set('renderer', $this->getMock('Drupal\Core\Render\RendererInterface'));
81 $authentication_collector = $this->getMock('\Drupal\Core\Authentication\AuthenticationCollectorInterface');
82 $container->set('authentication_collector', $authentication_collector);
83 $authentication_collector->expects($this->any())
84 ->method('getSortedProviders')
85 ->will($this->returnValue(['basic_auth' => 'data', 'cookie' => 'data']));
87 $container->setParameter('serializer.format_providers', ['json']);
89 \Drupal::setContainer($container);
91 $this->restExport = RestExport::create($container, [], "test_routes", []);
92 $this->restExport->view = $view_executable;
94 // Initialize a display.
95 $this->restExport->display = ['id' => 'page_1'];
97 // Set the style option.
98 $this->restExport->setOption('style', ['type' => 'serializer']);
100 // Set the auth option.
101 $this->restExport->setOption('auth', ['basic_auth']);
103 $display_manager->expects($this->once())
104 ->method('getDefinition')
105 ->will($this->returnValue(['id' => 'test', 'provider' => 'test']));
107 $none = $this->getMockBuilder('\Drupal\views\Plugin\views\access\None')
108 ->disableOriginalConstructor()
111 $access_manager->expects($this->once())
112 ->method('createInstance')
113 ->will($this->returnValue($none));
115 $style_plugin = $this->getMock('\Drupal\rest\Plugin\views\style\Serializer', ['getFormats', 'init'], [], '', FALSE);
117 $style_plugin->expects($this->once())
118 ->method('getFormats')
119 ->will($this->returnValue(['json']));
121 $style_plugin->expects($this->once())
123 ->with($view_executable)
124 ->will($this->returnValue(TRUE));
126 $style_manager->expects($this->once())
127 ->method('createInstance')
128 ->will($this->returnValue($style_plugin));
130 $this->routes = new RouteCollection();
131 $this->routes->add('test_1', new Route('/test/1'));
132 $this->routes->add('view.test_view.page_1', new Route('/test/2'));
134 $this->view->addDisplay('page', NULL, 'page_1');
138 * Tests if adding a requirement to a route only modify one route.
140 public function testRoutesRequirements() {
141 $this->restExport->collectRoutes($this->routes);
143 $requirements_1 = $this->routes->get('test_1')->getRequirements();
144 $requirements_2 = $this->routes->get('view.test_view.page_1')->getRequirements();
146 $this->assertEquals(0, count($requirements_1), 'First route has no requirement.');
147 $this->assertEquals(1, count($requirements_2), 'Views route with rest export had the format requirement added.');
149 // Check auth options.
150 $auth = $this->routes->get('view.test_view.page_1')->getOption('_auth');
151 $this->assertEquals(count($auth), 1, 'View route with rest export has an auth option added');
152 $this->assertEquals($auth[0], 'basic_auth', 'View route with rest export has the correct auth option added');