More updates to stop using dev or alpha or beta versions.
[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    * @group legacy
75    */
76   public function testGetArguments() {
77     $controller = function (EntityInterface $entity, $user, RouteMatchInterface $route_match, ServerRequestInterface $psr_7) {
78     };
79     $mock_entity = $this->getMockBuilder('Drupal\Core\Entity\Entity')
80       ->disableOriginalConstructor()
81       ->getMock();
82     $mock_account = $this->getMock('Drupal\Core\Session\AccountInterface');
83     $request = new Request([], [], [
84       'entity' => $mock_entity,
85       'user' => $mock_account,
86       '_raw_variables' => new ParameterBag(['entity' => 1, 'user' => 1]),
87     ], [], [], ['HTTP_HOST' => 'drupal.org']);
88     $arguments = $this->controllerResolver->getArguments($request, $controller);
89
90     $this->assertEquals($mock_entity, $arguments[0]);
91     $this->assertEquals($mock_account, $arguments[1]);
92     $this->assertEquals(RouteMatch::createFromRequest($request), $arguments[2], 'Ensure that the route match object is passed along as well');
93     $this->assertInstanceOf(ServerRequestInterface::class, $arguments[3], 'Ensure that the PSR-7 object is passed along as well');
94   }
95
96   /**
97    * Tests createController().
98    *
99    * @dataProvider providerTestCreateController
100    */
101   public function testCreateController($controller, $class, $output) {
102     $this->container->set('some_service', new MockController());
103     $result = $this->controllerResolver->getControllerFromDefinition($controller);
104     $this->assertCallableController($result, $class, $output);
105   }
106
107   /**
108    * Provides test data for testCreateController().
109    */
110   public function providerTestCreateController() {
111     return [
112       // Tests class::method.
113       ['Drupal\Tests\Core\Controller\MockController::getResult', 'Drupal\Tests\Core\Controller\MockController', 'This is a regular controller.'],
114       // Tests service:method.
115       ['some_service:getResult', 'Drupal\Tests\Core\Controller\MockController', 'This is a regular controller.'],
116       // Tests a class with injection.
117       ['Drupal\Tests\Core\Controller\MockContainerInjection::getResult', 'Drupal\Tests\Core\Controller\MockContainerInjection', 'This used injection.'],
118       // Tests a ContainerAware class.
119       ['Drupal\Tests\Core\Controller\MockContainerAware::getResult', 'Drupal\Tests\Core\Controller\MockContainerAware', 'This is container aware.'],
120     ];
121   }
122
123   /**
124    * Tests createController() with a non-existent class.
125    */
126   public function testCreateControllerNonExistentClass() {
127     $this->setExpectedException(\InvalidArgumentException::class);
128     $this->controllerResolver->getControllerFromDefinition('Class::method');
129   }
130
131   /**
132    * Tests createController() with an invalid name.
133    */
134   public function testCreateControllerInvalidName() {
135     $this->setExpectedException(\LogicException::class);
136     $this->controllerResolver->getControllerFromDefinition('ClassWithoutMethod');
137   }
138
139   /**
140    * Tests getController().
141    *
142    * @dataProvider providerTestGetController
143    */
144   public function testGetController($attributes, $class, $output = NULL) {
145     $request = new Request([], [], $attributes);
146     $result = $this->controllerResolver->getController($request);
147     if ($class) {
148       $this->assertCallableController($result, $class, $output);
149     }
150     else {
151       $this->assertSame(FALSE, $result);
152     }
153   }
154
155   /**
156    * Provides test data for testGetController().
157    */
158   public function providerTestGetController() {
159     return [
160       // Tests passing a controller via the request.
161       [['_controller' => 'Drupal\Tests\Core\Controller\MockContainerAware::getResult'], 'Drupal\Tests\Core\Controller\MockContainerAware', 'This is container aware.'],
162       // Tests a request with no controller specified.
163       [[], FALSE]
164     ];
165   }
166
167   /**
168    * Tests getControllerFromDefinition().
169    *
170    * @dataProvider providerTestGetControllerFromDefinition
171    */
172   public function testGetControllerFromDefinition($definition, $output) {
173     $controller = $this->controllerResolver->getControllerFromDefinition($definition);
174     $this->assertCallableController($controller, NULL, $output);
175   }
176
177   /**
178    * Provides test data for testGetControllerFromDefinition().
179    */
180   public function providerTestGetControllerFromDefinition() {
181     return [
182       // Tests a method on an object.
183       [[new MockController(), 'getResult'], 'This is a regular controller.'],
184       // Tests a function.
185       ['phpversion', phpversion()],
186       // Tests an object using __invoke().
187       [new MockInvokeController(), 'This used __invoke().'],
188       // Tests a class using __invoke().
189       ['Drupal\Tests\Core\Controller\MockInvokeController', 'This used __invoke().'],
190     ];
191   }
192   /**
193    * Tests getControllerFromDefinition() without a callable.
194    */
195   public function testGetControllerFromDefinitionNotCallable() {
196     $this->setExpectedException(\InvalidArgumentException::class);
197     $this->controllerResolver->getControllerFromDefinition('Drupal\Tests\Core\Controller\MockController::bananas');
198   }
199
200   /**
201    * Asserts that the controller is callable and produces the correct output.
202    *
203    * @param callable $controller
204    *   A callable controller.
205    * @param string|null $class
206    *   Either the name of the class the controller represents, or NULL if it is
207    *   not an object.
208    * @param mixed $output
209    *   The output expected for this controller.
210    */
211   protected function assertCallableController($controller, $class, $output) {
212     if ($class) {
213       $this->assertTrue(is_object($controller[0]));
214       $this->assertInstanceOf($class, $controller[0]);
215     }
216     $this->assertTrue(is_callable($controller));
217     $this->assertSame($output, call_user_func($controller));
218   }
219
220   /**
221    * Tests getArguments with a route match and a request.
222    *
223    * @covers ::getArguments
224    * @covers ::doGetArguments
225    *
226    * @group legacy
227    */
228   public function testGetArgumentsWithRouteMatchAndRequest() {
229     $request = Request::create('/test');
230     $mock_controller = new MockController();
231     $arguments = $this->controllerResolver->getArguments($request, [$mock_controller, 'getControllerWithRequestAndRouteMatch']);
232     $this->assertEquals([RouteMatch::createFromRequest($request), $request], $arguments);
233   }
234
235   /**
236    * Tests getArguments with a route match and a PSR-7 request.
237    *
238    * @covers ::getArguments
239    * @covers ::doGetArguments
240    *
241    * @group legacy
242    */
243   public function testGetArgumentsWithRouteMatchAndPsr7Request() {
244     $request = Request::create('/test');
245     $mock_controller = new MockControllerPsr7();
246     $arguments = $this->controllerResolver->getArguments($request, [$mock_controller, 'getControllerWithRequestAndRouteMatch']);
247     $this->assertEquals(RouteMatch::createFromRequest($request), $arguments[0], 'Ensure that the route match object is passed along as well');
248     $this->assertInstanceOf('Psr\Http\Message\ServerRequestInterface', $arguments[1], 'Ensure that the PSR-7 object is passed along as well');
249   }
250
251 }
252
253 class MockController {
254   public function getResult() {
255     return 'This is a regular controller.';
256   }
257
258   public function getControllerWithRequestAndRouteMatch(RouteMatchInterface $route_match, Request $request) {
259     return 'this is another example controller';
260   }
261
262 }
263 class MockControllerPsr7 {
264   public function getResult() {
265     return ['#markup' => 'This is a regular controller'];
266   }
267
268   public function getControllerWithRequestAndRouteMatch(RouteMatchInterface $route_match, ServerRequestInterface $request) {
269     return ['#markup' => 'this is another example controller'];
270   }
271
272 }
273
274 class MockContainerInjection implements ContainerInjectionInterface {
275   protected $result;
276   public function __construct($result) {
277     $this->result = $result;
278   }
279   public static function create(ContainerInterface $container) {
280     return new static('This used injection.');
281   }
282   public function getResult() {
283     return $this->result;
284   }
285
286 }
287 class MockContainerAware implements ContainerAwareInterface {
288   use ContainerAwareTrait;
289   public function getResult() {
290     return 'This is container aware.';
291   }
292
293 }
294 class MockInvokeController {
295   public function __invoke() {
296     return 'This used __invoke().';
297   }
298
299 }