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