e171e047c8eff7ec82e172b90795ede17866bc5b
[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     $container->setParameter('serializer.format_providers', ['json']);
88
89     \Drupal::setContainer($container);
90
91     $this->restExport = RestExport::create($container, [], "test_routes", []);
92     $this->restExport->view = $view_executable;
93
94     // Initialize a display.
95     $this->restExport->display = ['id' => 'page_1'];
96
97     // Set the style option.
98     $this->restExport->setOption('style', ['type' => 'serializer']);
99
100     // Set the auth option.
101     $this->restExport->setOption('auth', ['basic_auth']);
102
103     $display_manager->expects($this->once())
104       ->method('getDefinition')
105       ->will($this->returnValue(['id' => 'test', 'provider' => 'test']));
106
107     $none = $this->getMockBuilder('\Drupal\views\Plugin\views\access\None')
108       ->disableOriginalConstructor()
109       ->getMock();
110
111     $access_manager->expects($this->once())
112       ->method('createInstance')
113       ->will($this->returnValue($none));
114
115     $style_plugin = $this->getMock('\Drupal\rest\Plugin\views\style\Serializer', ['getFormats', 'init'], [], '', FALSE);
116
117     $style_plugin->expects($this->once())
118       ->method('getFormats')
119       ->will($this->returnValue(['json']));
120
121     $style_plugin->expects($this->once())
122       ->method('init')
123       ->with($view_executable)
124       ->will($this->returnValue(TRUE));
125
126     $style_manager->expects($this->once())
127       ->method('createInstance')
128       ->will($this->returnValue($style_plugin));
129
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'));
133
134     $this->view->addDisplay('page', NULL, 'page_1');
135   }
136
137   /**
138    * Tests if adding a requirement to a route only modify one route.
139    */
140   public function testRoutesRequirements() {
141     $this->restExport->collectRoutes($this->routes);
142
143     $requirements_1 = $this->routes->get('test_1')->getRequirements();
144     $requirements_2 = $this->routes->get('view.test_view.page_1')->getRequirements();
145
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.');
148
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');
153   }
154
155 }