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