Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\rest\Unit;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Tests\UnitTestCase;
|
Chris@0
|
6 use Drupal\Core\DependencyInjection\ContainerBuilder;
|
Chris@0
|
7 use Drupal\rest\Plugin\views\display\RestExport;
|
Chris@0
|
8 use Symfony\Component\Routing\Route;
|
Chris@0
|
9 use Symfony\Component\Routing\RouteCollection;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Tests the REST export view plugin.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @group rest
|
Chris@0
|
15 */
|
Chris@0
|
16 class CollectRoutesTest extends UnitTestCase {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * The REST export instance.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var \Drupal\rest\Plugin\views\display\RestExport
|
Chris@0
|
22 */
|
Chris@0
|
23 protected $restExport;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * The RouteCollection.
|
Chris@0
|
27 */
|
Chris@0
|
28 protected $routes;
|
Chris@0
|
29
|
Chris@0
|
30 /**
|
Chris@0
|
31 * {@inheritdoc}
|
Chris@0
|
32 */
|
Chris@0
|
33 protected function setUp() {
|
Chris@0
|
34 parent::setUp();
|
Chris@0
|
35
|
Chris@0
|
36 $container = new ContainerBuilder();
|
Chris@0
|
37
|
Chris@0
|
38 $request = $this->getMockBuilder('\Symfony\Component\HttpFoundation\Request')
|
Chris@0
|
39 ->disableOriginalConstructor()
|
Chris@0
|
40 ->getMock();
|
Chris@0
|
41
|
Chris@0
|
42 $this->view = $this->getMock('\Drupal\views\Entity\View', ['initHandlers'], [
|
Chris@0
|
43 ['id' => 'test_view'],
|
Chris@0
|
44 'view',
|
Chris@0
|
45 ]);
|
Chris@0
|
46
|
Chris@0
|
47 $view_executable = $this->getMock('\Drupal\views\ViewExecutable', ['initHandlers', 'getTitle'], [], '', FALSE);
|
Chris@0
|
48 $view_executable->expects($this->any())
|
Chris@0
|
49 ->method('getTitle')
|
Chris@0
|
50 ->willReturn('View title');
|
Chris@0
|
51
|
Chris@0
|
52 $view_executable->storage = $this->view;
|
Chris@0
|
53 $view_executable->argument = [];
|
Chris@0
|
54
|
Chris@0
|
55 $display_manager = $this->getMockBuilder('\Drupal\views\Plugin\ViewsPluginManager')
|
Chris@0
|
56 ->disableOriginalConstructor()
|
Chris@0
|
57 ->getMock();
|
Chris@0
|
58 $container->set('plugin.manager.views.display', $display_manager);
|
Chris@0
|
59
|
Chris@0
|
60 $access_manager = $this->getMockBuilder('\Drupal\views\Plugin\ViewsPluginManager')
|
Chris@0
|
61 ->disableOriginalConstructor()
|
Chris@0
|
62 ->getMock();
|
Chris@0
|
63 $container->set('plugin.manager.views.access', $access_manager);
|
Chris@0
|
64
|
Chris@0
|
65 $route_provider = $this->getMockBuilder('\Drupal\Core\Routing\RouteProviderInterface')
|
Chris@0
|
66 ->disableOriginalConstructor()
|
Chris@0
|
67 ->getMock();
|
Chris@0
|
68 $container->set('router.route_provider', $route_provider);
|
Chris@0
|
69
|
Chris@0
|
70 $container->setParameter('authentication_providers', ['basic_auth' => 'basic_auth']);
|
Chris@0
|
71
|
Chris@0
|
72 $state = $this->getMock('\Drupal\Core\State\StateInterface');
|
Chris@0
|
73 $container->set('state', $state);
|
Chris@0
|
74
|
Chris@0
|
75 $style_manager = $this->getMockBuilder('\Drupal\views\Plugin\ViewsPluginManager')
|
Chris@0
|
76 ->disableOriginalConstructor()
|
Chris@0
|
77 ->getMock();
|
Chris@0
|
78 $container->set('plugin.manager.views.style', $style_manager);
|
Chris@0
|
79 $container->set('renderer', $this->getMock('Drupal\Core\Render\RendererInterface'));
|
Chris@0
|
80
|
Chris@0
|
81 $authentication_collector = $this->getMock('\Drupal\Core\Authentication\AuthenticationCollectorInterface');
|
Chris@0
|
82 $container->set('authentication_collector', $authentication_collector);
|
Chris@0
|
83 $authentication_collector->expects($this->any())
|
Chris@0
|
84 ->method('getSortedProviders')
|
Chris@0
|
85 ->will($this->returnValue(['basic_auth' => 'data', 'cookie' => 'data']));
|
Chris@0
|
86
|
Chris@14
|
87 $container->setParameter('serializer.format_providers', ['json']);
|
Chris@14
|
88
|
Chris@0
|
89 \Drupal::setContainer($container);
|
Chris@0
|
90
|
Chris@0
|
91 $this->restExport = RestExport::create($container, [], "test_routes", []);
|
Chris@0
|
92 $this->restExport->view = $view_executable;
|
Chris@0
|
93
|
Chris@0
|
94 // Initialize a display.
|
Chris@0
|
95 $this->restExport->display = ['id' => 'page_1'];
|
Chris@0
|
96
|
Chris@0
|
97 // Set the style option.
|
Chris@0
|
98 $this->restExport->setOption('style', ['type' => 'serializer']);
|
Chris@0
|
99
|
Chris@0
|
100 // Set the auth option.
|
Chris@0
|
101 $this->restExport->setOption('auth', ['basic_auth']);
|
Chris@0
|
102
|
Chris@0
|
103 $display_manager->expects($this->once())
|
Chris@0
|
104 ->method('getDefinition')
|
Chris@0
|
105 ->will($this->returnValue(['id' => 'test', 'provider' => 'test']));
|
Chris@0
|
106
|
Chris@0
|
107 $none = $this->getMockBuilder('\Drupal\views\Plugin\views\access\None')
|
Chris@0
|
108 ->disableOriginalConstructor()
|
Chris@0
|
109 ->getMock();
|
Chris@0
|
110
|
Chris@0
|
111 $access_manager->expects($this->once())
|
Chris@0
|
112 ->method('createInstance')
|
Chris@0
|
113 ->will($this->returnValue($none));
|
Chris@0
|
114
|
Chris@0
|
115 $style_plugin = $this->getMock('\Drupal\rest\Plugin\views\style\Serializer', ['getFormats', 'init'], [], '', FALSE);
|
Chris@0
|
116
|
Chris@0
|
117 $style_plugin->expects($this->once())
|
Chris@0
|
118 ->method('getFormats')
|
Chris@0
|
119 ->will($this->returnValue(['json']));
|
Chris@0
|
120
|
Chris@0
|
121 $style_plugin->expects($this->once())
|
Chris@0
|
122 ->method('init')
|
Chris@0
|
123 ->with($view_executable)
|
Chris@0
|
124 ->will($this->returnValue(TRUE));
|
Chris@0
|
125
|
Chris@0
|
126 $style_manager->expects($this->once())
|
Chris@0
|
127 ->method('createInstance')
|
Chris@0
|
128 ->will($this->returnValue($style_plugin));
|
Chris@0
|
129
|
Chris@0
|
130 $this->routes = new RouteCollection();
|
Chris@0
|
131 $this->routes->add('test_1', new Route('/test/1'));
|
Chris@0
|
132 $this->routes->add('view.test_view.page_1', new Route('/test/2'));
|
Chris@0
|
133
|
Chris@0
|
134 $this->view->addDisplay('page', NULL, 'page_1');
|
Chris@0
|
135 }
|
Chris@0
|
136
|
Chris@0
|
137 /**
|
Chris@0
|
138 * Tests if adding a requirement to a route only modify one route.
|
Chris@0
|
139 */
|
Chris@0
|
140 public function testRoutesRequirements() {
|
Chris@0
|
141 $this->restExport->collectRoutes($this->routes);
|
Chris@0
|
142
|
Chris@0
|
143 $requirements_1 = $this->routes->get('test_1')->getRequirements();
|
Chris@0
|
144 $requirements_2 = $this->routes->get('view.test_view.page_1')->getRequirements();
|
Chris@0
|
145
|
Chris@0
|
146 $this->assertEquals(0, count($requirements_1), 'First route has no requirement.');
|
Chris@0
|
147 $this->assertEquals(1, count($requirements_2), 'Views route with rest export had the format requirement added.');
|
Chris@0
|
148
|
Chris@0
|
149 // Check auth options.
|
Chris@0
|
150 $auth = $this->routes->get('view.test_view.page_1')->getOption('_auth');
|
Chris@0
|
151 $this->assertEquals(count($auth), 1, 'View route with rest export has an auth option added');
|
Chris@0
|
152 $this->assertEquals($auth[0], 'basic_auth', 'View route with rest export has the correct auth option added');
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 }
|