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