Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Controller / ControllerResolverTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\Controller\ControllerResolverTest.
6  */
7
8 namespace Drupal\Tests\Core\Controller;
9
10 use Drupal\Core\Controller\ControllerResolver;
11 use Drupal\Core\DependencyInjection\ClassResolver;
12 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
13 use Drupal\Core\Entity\EntityInterface;
14 use Drupal\Core\Routing\RouteMatch;
15 use Drupal\Core\Routing\RouteMatchInterface;
16 use Drupal\Tests\UnitTestCase;
17 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
18 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
19 use Symfony\Component\DependencyInjection\ContainerBuilder;
20 use Symfony\Component\DependencyInjection\ContainerInterface;
21 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
22 use Symfony\Component\HttpFoundation\Request;
23 use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
24 use Psr\Http\Message\ServerRequestInterface;
25
26 /**
27  * @coversDefaultClass \Drupal\Core\Controller\ControllerResolver
28  * @group Controller
29  */
30 class ControllerResolverTest extends UnitTestCase {
31
32   /**
33    * The tested controller resolver.
34    *
35    * @var \Drupal\Core\Controller\ControllerResolver
36    */
37   public $controllerResolver;
38
39   /**
40    * The container.
41    *
42    * @var \Symfony\Component\DependencyInjection\ContainerBuilder
43    */
44   protected $container;
45
46   /**
47    * The PSR-7 converter.
48    *
49    * @var \Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface
50    */
51   protected $httpMessageFactory;
52
53   /**
54    * {@inheritdoc}
55    */
56   protected function setUp() {
57     parent::setUp();
58
59     $this->container = new ContainerBuilder();
60     $class_resolver = new ClassResolver();
61     $class_resolver->setContainer($this->container);
62     $this->httpMessageFactory = new DiactorosFactory();
63     $this->controllerResolver = new ControllerResolver($this->httpMessageFactory, $class_resolver);
64   }
65
66   /**
67    * Tests getArguments().
68    *
69    * Ensure that doGetArguments uses converted arguments if available.
70    *
71    * @see \Drupal\Core\Controller\ControllerResolver::getArguments()
72    * @see \Drupal\Core\Controller\ControllerResolver::doGetArguments()
73    */
74   public function testGetArguments() {
75     $controller = function(EntityInterface $entity, $user, RouteMatchInterface $route_match, ServerRequestInterface $psr_7) {
76     };
77     $mock_entity = $this->getMockBuilder('Drupal\Core\Entity\Entity')
78       ->disableOriginalConstructor()
79       ->getMock();
80     $mock_account = $this->getMock('Drupal\Core\Session\AccountInterface');
81     $request = new Request([], [], [
82       'entity' => $mock_entity,
83       'user' => $mock_account,
84       '_raw_variables' => new ParameterBag(['entity' => 1, 'user' => 1]),
85     ], [], [], ['HTTP_HOST' => 'drupal.org']);
86     $arguments = $this->controllerResolver->getArguments($request, $controller);
87
88     $this->assertEquals($mock_entity, $arguments[0]);
89     $this->assertEquals($mock_account, $arguments[1]);
90     $this->assertEquals(RouteMatch::createFromRequest($request), $arguments[2], 'Ensure that the route match object is passed along as well');
91     $this->assertInstanceOf(ServerRequestInterface::class, $arguments[3], 'Ensure that the PSR-7 object is passed along as well');
92   }
93
94   /**
95    * Tests createController().
96    *
97    * @dataProvider providerTestCreateController
98    */
99   public function testCreateController($controller, $class, $output) {
100     $this->container->set('some_service', new MockController());
101     $result = $this->controllerResolver->getControllerFromDefinition($controller);
102     $this->assertCallableController($result, $class, $output);
103   }
104
105   /**
106    * Provides test data for testCreateController().
107    */
108   public function providerTestCreateController() {
109     return [
110       // Tests class::method.
111       ['Drupal\Tests\Core\Controller\MockController::getResult', 'Drupal\Tests\Core\Controller\MockController', 'This is a regular controller.'],
112       // Tests service:method.
113       ['some_service:getResult', 'Drupal\Tests\Core\Controller\MockController', 'This is a regular controller.'],
114       // Tests a class with injection.
115       ['Drupal\Tests\Core\Controller\MockContainerInjection::getResult', 'Drupal\Tests\Core\Controller\MockContainerInjection', 'This used injection.'],
116       // Tests a ContainerAware class.
117       ['Drupal\Tests\Core\Controller\MockContainerAware::getResult', 'Drupal\Tests\Core\Controller\MockContainerAware', 'This is container aware.'],
118     ];
119   }
120
121   /**
122    * Tests createController() with a non-existent class.
123    */
124   public function testCreateControllerNonExistentClass() {
125     $this->setExpectedException(\InvalidArgumentException::class);
126     $this->controllerResolver->getControllerFromDefinition('Class::method');
127   }
128
129   /**
130    * Tests createController() with an invalid name.
131    */
132   public function testCreateControllerInvalidName() {
133     $this->setExpectedException(\LogicException::class);
134     $this->controllerResolver->getControllerFromDefinition('ClassWithoutMethod');
135   }
136
137   /**
138    * Tests getController().
139    *
140    * @dataProvider providerTestGetController
141    */
142   public function testGetController($attributes, $class, $output = NULL) {
143     $request = new Request([], [], $attributes);
144     $result = $this->controllerResolver->getController($request);
145     if ($class) {
146       $this->assertCallableController($result, $class, $output);
147     }
148     else {
149       $this->assertSame(FALSE, $result);
150     }
151   }
152
153   /**
154    * Provides test data for testGetController().
155    */
156   public function providerTestGetController() {
157     return [
158       // Tests passing a controller via the request.
159       [['_controller' => 'Drupal\Tests\Core\Controller\MockContainerAware::getResult'], 'Drupal\Tests\Core\Controller\MockContainerAware', 'This is container aware.'],
160       // Tests a request with no controller specified.
161       [[], FALSE]
162     ];
163   }
164
165   /**
166    * Tests getControllerFromDefinition().
167    *
168    * @dataProvider providerTestGetControllerFromDefinition
169    */
170   public function testGetControllerFromDefinition($definition, $output) {
171     $controller = $this->controllerResolver->getControllerFromDefinition($definition);
172     $this->assertCallableController($controller, NULL, $output);
173   }
174
175   /**
176    * Provides test data for testGetControllerFromDefinition().
177    */
178   public function providerTestGetControllerFromDefinition() {
179     return [
180       // Tests a method on an object.
181       [[new MockController(), 'getResult'], 'This is a regular controller.'],
182       // Tests a function.
183       ['phpversion', phpversion()],
184       // Tests an object using __invoke().
185       [new MockInvokeController(), 'This used __invoke().'],
186       // Tests a class using __invoke().
187       ['Drupal\Tests\Core\Controller\MockInvokeController', 'This used __invoke().'],
188     ];
189   }
190   /**
191    * Tests getControllerFromDefinition() without a callable.
192    */
193   public function testGetControllerFromDefinitionNotCallable() {
194     $this->setExpectedException(\InvalidArgumentException::class);
195     $this->controllerResolver->getControllerFromDefinition('Drupal\Tests\Core\Controller\MockController::bananas');
196   }
197
198   /**
199    * Asserts that the controller is callable and produces the correct output.
200    *
201    * @param callable $controller
202    *   A callable controller.
203    * @param string|null $class
204    *   Either the name of the class the controller represents, or NULL if it is
205    *   not an object.
206    * @param mixed $output
207    *   The output expected for this controller.
208    */
209   protected function assertCallableController($controller, $class, $output) {
210     if ($class) {
211       $this->assertTrue(is_object($controller[0]));
212       $this->assertInstanceOf($class, $controller[0]);
213     }
214     $this->assertTrue(is_callable($controller));
215     $this->assertSame($output, call_user_func($controller));
216   }
217
218   /**
219    * Tests getArguments with a route match and a request.
220    *
221    * @covers ::getArguments
222    * @covers ::doGetArguments
223    */
224   public function testGetArgumentsWithRouteMatchAndRequest() {
225     $request = Request::create('/test');
226     $mock_controller = new MockController();
227     $arguments = $this->controllerResolver->getArguments($request, [$mock_controller, 'getControllerWithRequestAndRouteMatch']);
228     $this->assertEquals([RouteMatch::createFromRequest($request), $request], $arguments);
229   }
230
231   /**
232    * Tests getArguments with a route match and a PSR-7 request.
233    *
234    * @covers ::getArguments
235    * @covers ::doGetArguments
236    */
237   public function testGetArgumentsWithRouteMatchAndPsr7Request() {
238     $request = Request::create('/test');
239     $mock_controller = new MockControllerPsr7();
240     $arguments = $this->controllerResolver->getArguments($request, [$mock_controller, 'getControllerWithRequestAndRouteMatch']);
241     $this->assertEquals(RouteMatch::createFromRequest($request), $arguments[0], 'Ensure that the route match object is passed along as well');
242     $this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $arguments[1], 'Ensure that the PSR-7 object is passed along as well');
243   }
244
245 }
246
247 class MockController {
248   public function getResult() {
249     return 'This is a regular controller.';
250   }
251
252   public function getControllerWithRequestAndRouteMatch(RouteMatchInterface $route_match, Request $request) {
253     return 'this is another example controller';
254   }
255
256 }
257 class MockControllerPsr7 {
258   public function getResult() {
259     return ['#markup' => 'This is a regular controller'];
260   }
261
262   public function getControllerWithRequestAndRouteMatch(RouteMatchInterface $route_match, ServerRequestInterface $request) {
263     return ['#markup' => 'this is another example controller'];
264   }
265
266 }
267
268 class MockContainerInjection implements ContainerInjectionInterface {
269   protected $result;
270   public function __construct($result) {
271     $this->result = $result;
272   }
273   public static function create(ContainerInterface $container) {
274     return new static('This used injection.');
275   }
276   public function getResult() {
277     return $this->result;
278   }
279
280 }
281 class MockContainerAware implements ContainerAwareInterface {
282   use ContainerAwareTrait;
283   public function getResult() {
284     return 'This is container aware.';
285   }
286
287 }
288 class MockInvokeController {
289   public function __invoke() {
290     return 'This used __invoke().';
291   }
292
293 }