Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / http-kernel / Tests / DataCollector / RequestDataCollectorTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Tests\DataCollector;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\ParameterBag;
16 use Symfony\Component\HttpFoundation\RedirectResponse;
17 use Symfony\Component\HttpFoundation\Session\Session;
18 use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
19 use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
20 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
21 use Symfony\Component\HttpKernel\HttpKernel;
22 use Symfony\Component\HttpKernel\HttpKernelInterface;
23 use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
24 use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
25 use Symfony\Component\HttpFoundation\Request;
26 use Symfony\Component\HttpFoundation\Response;
27 use Symfony\Component\HttpFoundation\Cookie;
28 use Symfony\Component\EventDispatcher\EventDispatcher;
29
30 class RequestDataCollectorTest extends TestCase
31 {
32     public function testCollect()
33     {
34         $c = new RequestDataCollector();
35
36         $c->collect($request = $this->createRequest(), $this->createResponse());
37         $c->lateCollect();
38
39         $attributes = $c->getRequestAttributes();
40
41         $this->assertSame('request', $c->getName());
42         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestHeaders());
43         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestServer());
44         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestCookies());
45         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $attributes);
46         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestRequest());
47         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getRequestQuery());
48         $this->assertInstanceOf(ParameterBag::class, $c->getResponseCookies());
49         $this->assertSame('html', $c->getFormat());
50         $this->assertEquals('foobar', $c->getRoute());
51         $this->assertEquals(array('name' => 'foo'), $c->getRouteParams());
52         $this->assertSame(array(), $c->getSessionAttributes());
53         $this->assertSame('en', $c->getLocale());
54         $this->assertContains(__FILE__, $attributes->get('resource'));
55         $this->assertSame('stdClass', $attributes->get('object')->getType());
56
57         $this->assertInstanceOf('Symfony\Component\HttpFoundation\ParameterBag', $c->getResponseHeaders());
58         $this->assertSame('OK', $c->getStatusText());
59         $this->assertSame(200, $c->getStatusCode());
60         $this->assertSame('application/json', $c->getContentType());
61     }
62
63     public function testCollectWithoutRouteParams()
64     {
65         $request = $this->createRequest(array());
66
67         $c = new RequestDataCollector();
68         $c->collect($request, $this->createResponse());
69         $c->lateCollect();
70
71         $this->assertEquals(array(), $c->getRouteParams());
72     }
73
74     /**
75      * @dataProvider provideControllerCallables
76      */
77     public function testControllerInspection($name, $callable, $expected)
78     {
79         $c = new RequestDataCollector();
80         $request = $this->createRequest();
81         $response = $this->createResponse();
82         $this->injectController($c, $callable, $request);
83         $c->collect($request, $response);
84         $c->lateCollect();
85
86         $this->assertSame($expected, $c->getController()->getValue(true), sprintf('Testing: %s', $name));
87     }
88
89     public function provideControllerCallables()
90     {
91         // make sure we always match the line number
92         $r1 = new \ReflectionMethod($this, 'testControllerInspection');
93         $r2 = new \ReflectionMethod($this, 'staticControllerMethod');
94         $r3 = new \ReflectionClass($this);
95
96         // test name, callable, expected
97         return array(
98             array(
99                 '"Regular" callable',
100                 array($this, 'testControllerInspection'),
101                 array(
102                     'class' => __NAMESPACE__.'\RequestDataCollectorTest',
103                     'method' => 'testControllerInspection',
104                     'file' => __FILE__,
105                     'line' => $r1->getStartLine(),
106                 ),
107             ),
108
109             array(
110                 'Closure',
111                 function () { return 'foo'; },
112                 array(
113                     'class' => __NAMESPACE__.'\{closure}',
114                     'method' => null,
115                     'file' => __FILE__,
116                     'line' => __LINE__ - 5,
117                 ),
118             ),
119
120             array(
121                 'Static callback as string',
122                 __NAMESPACE__.'\RequestDataCollectorTest::staticControllerMethod',
123                 array(
124                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
125                     'method' => 'staticControllerMethod',
126                     'file' => __FILE__,
127                     'line' => $r2->getStartLine(),
128                 ),
129             ),
130
131             array(
132                 'Static callable with instance',
133                 array($this, 'staticControllerMethod'),
134                 array(
135                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
136                     'method' => 'staticControllerMethod',
137                     'file' => __FILE__,
138                     'line' => $r2->getStartLine(),
139                 ),
140             ),
141
142             array(
143                 'Static callable with class name',
144                 array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'staticControllerMethod'),
145                 array(
146                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
147                     'method' => 'staticControllerMethod',
148                     'file' => __FILE__,
149                     'line' => $r2->getStartLine(),
150                 ),
151             ),
152
153             array(
154                 'Callable with instance depending on __call()',
155                 array($this, 'magicMethod'),
156                 array(
157                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
158                     'method' => 'magicMethod',
159                     'file' => 'n/a',
160                     'line' => 'n/a',
161                 ),
162             ),
163
164             array(
165                 'Callable with class name depending on __callStatic()',
166                 array('Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest', 'magicMethod'),
167                 array(
168                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
169                     'method' => 'magicMethod',
170                     'file' => 'n/a',
171                     'line' => 'n/a',
172                 ),
173             ),
174
175             array(
176                 'Invokable controller',
177                 $this,
178                 array(
179                     'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
180                     'method' => null,
181                     'file' => __FILE__,
182                     'line' => $r3->getStartLine(),
183                 ),
184             ),
185         );
186     }
187
188     public function testItIgnoresInvalidCallables()
189     {
190         $request = $this->createRequestWithSession();
191         $response = new RedirectResponse('/');
192
193         $c = new RequestDataCollector();
194         $c->collect($request, $response);
195
196         $this->assertSame('n/a', $c->getController());
197     }
198
199     public function testItAddsRedirectedAttributesWhenRequestContainsSpecificCookie()
200     {
201         $request = $this->createRequest();
202         $request->cookies->add(array(
203             'sf_redirect' => '{}',
204         ));
205
206         $kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
207
208         $c = new RequestDataCollector();
209         $c->onKernelResponse(new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $this->createResponse()));
210
211         $this->assertTrue($request->attributes->get('_redirected'));
212     }
213
214     public function testItSetsARedirectCookieIfTheResponseIsARedirection()
215     {
216         $c = new RequestDataCollector();
217
218         $response = $this->createResponse();
219         $response->setStatusCode(302);
220         $response->headers->set('Location', '/somewhere-else');
221
222         $c->collect($request = $this->createRequest(), $response);
223         $c->lateCollect();
224
225         $cookie = $this->getCookieByName($response, 'sf_redirect');
226
227         $this->assertNotEmpty($cookie->getValue());
228     }
229
230     public function testItCollectsTheRedirectionAndClearTheCookie()
231     {
232         $c = new RequestDataCollector();
233
234         $request = $this->createRequest();
235         $request->attributes->set('_redirected', true);
236         $request->cookies->add(array(
237             'sf_redirect' => '{"method": "POST"}',
238         ));
239
240         $c->collect($request, $response = $this->createResponse());
241         $c->lateCollect();
242
243         $this->assertEquals('POST', $c->getRedirect()['method']);
244
245         $cookie = $this->getCookieByName($response, 'sf_redirect');
246         $this->assertNull($cookie->getValue());
247     }
248
249     protected function createRequest($routeParams = array('name' => 'foo'))
250     {
251         $request = Request::create('http://test.com/foo?bar=baz');
252         $request->attributes->set('foo', 'bar');
253         $request->attributes->set('_route', 'foobar');
254         $request->attributes->set('_route_params', $routeParams);
255         $request->attributes->set('resource', fopen(__FILE__, 'r'));
256         $request->attributes->set('object', new \stdClass());
257
258         return $request;
259     }
260
261     private function createRequestWithSession()
262     {
263         $request = $this->createRequest();
264         $request->attributes->set('_controller', 'Foo::bar');
265         $request->setSession(new Session(new MockArraySessionStorage()));
266         $request->getSession()->start();
267
268         return $request;
269     }
270
271     protected function createResponse()
272     {
273         $response = new Response();
274         $response->setStatusCode(200);
275         $response->headers->set('Content-Type', 'application/json');
276         $response->headers->set('X-Foo-Bar', null);
277         $response->headers->setCookie(new Cookie('foo', 'bar', 1, '/foo', 'localhost', true, true));
278         $response->headers->setCookie(new Cookie('bar', 'foo', new \DateTime('@946684800')));
279         $response->headers->setCookie(new Cookie('bazz', 'foo', '2000-12-12'));
280
281         return $response;
282     }
283
284     /**
285      * Inject the given controller callable into the data collector.
286      */
287     protected function injectController($collector, $controller, $request)
288     {
289         $resolver = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface')->getMock();
290         $httpKernel = new HttpKernel(new EventDispatcher(), $resolver, null, $this->getMockBuilder(ArgumentResolverInterface::class)->getMock());
291         $event = new FilterControllerEvent($httpKernel, $controller, $request, HttpKernelInterface::MASTER_REQUEST);
292         $collector->onKernelController($event);
293     }
294
295     /**
296      * Dummy method used as controller callable.
297      */
298     public static function staticControllerMethod()
299     {
300         throw new \LogicException('Unexpected method call');
301     }
302
303     /**
304      * Magic method to allow non existing methods to be called and delegated.
305      */
306     public function __call($method, $args)
307     {
308         throw new \LogicException('Unexpected method call');
309     }
310
311     /**
312      * Magic method to allow non existing methods to be called and delegated.
313      */
314     public static function __callStatic($method, $args)
315     {
316         throw new \LogicException('Unexpected method call');
317     }
318
319     public function __invoke()
320     {
321         throw new \LogicException('Unexpected method call');
322     }
323
324     private function getCookieByName(Response $response, $name)
325     {
326         foreach ($response->headers->getCookies() as $cookie) {
327             if ($cookie->getName() == $name) {
328                 return $cookie;
329             }
330         }
331
332         throw new \InvalidArgumentException(sprintf('Cookie named "%s" is not in response', $name));
333     }
334 }