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 \Drupal::setContainer($container);
89 $this->restExport = RestExport::create($container, [], "test_routes", []);
90 $this->restExport->view = $view_executable;
92 // Initialize a display.
93 $this->restExport->display = ['id' => 'page_1'];
95 // Set the style option.
96 $this->restExport->setOption('style', ['type' => 'serializer']);
98 // Set the auth option.
99 $this->restExport->setOption('auth', ['basic_auth']);
101 $display_manager->expects($this->once())
102 ->method('getDefinition')
103 ->will($this->returnValue(['id' => 'test', 'provider' => 'test']));
105 $none = $this->getMockBuilder('\Drupal\views\Plugin\views\access\None')
106 ->disableOriginalConstructor()
109 $access_manager->expects($this->once())
110 ->method('createInstance')
111 ->will($this->returnValue($none));
113 $style_plugin = $this->getMock('\Drupal\rest\Plugin\views\style\Serializer', ['getFormats', 'init'], [], '', FALSE);
115 $style_plugin->expects($this->once())
116 ->method('getFormats')
117 ->will($this->returnValue(['json']));
119 $style_plugin->expects($this->once())
121 ->with($view_executable)
122 ->will($this->returnValue(TRUE));
124 $style_manager->expects($this->once())
125 ->method('createInstance')
126 ->will($this->returnValue($style_plugin));
128 $this->routes = new RouteCollection();
129 $this->routes->add('test_1', new Route('/test/1'));
130 $this->routes->add('view.test_view.page_1', new Route('/test/2'));
132 $this->view->addDisplay('page', NULL, 'page_1');
136 * Tests if adding a requirement to a route only modify one route.
138 public function testRoutesRequirements() {
139 $this->restExport->collectRoutes($this->routes);
141 $requirements_1 = $this->routes->get('test_1')->getRequirements();
142 $requirements_2 = $this->routes->get('view.test_view.page_1')->getRequirements();
144 $this->assertEquals(count($requirements_1), 0, 'First route has no requirement.');
145 $this->assertEquals(count($requirements_2), 2, 'Views route with rest export had the format and method requirements added.');
147 // Check auth options.
148 $auth = $this->routes->get('view.test_view.page_1')->getOption('_auth');
149 $this->assertEquals(count($auth), 1, 'View route with rest export has an auth option added');
150 $this->assertEquals($auth[0], 'basic_auth', 'View route with rest export has the correct auth option added');