Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / http-kernel / Tests / EventListener / FragmentListenerTest.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\EventListener;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17 use Symfony\Component\HttpKernel\EventListener\FragmentListener;
18 use Symfony\Component\HttpKernel\HttpKernelInterface;
19 use Symfony\Component\HttpKernel\UriSigner;
20
21 class FragmentListenerTest extends TestCase
22 {
23     public function testOnlyTriggeredOnFragmentRoute()
24     {
25         $request = Request::create('http://example.com/foo?_path=foo%3Dbar%26_controller%3Dfoo');
26
27         $listener = new FragmentListener(new UriSigner('foo'));
28         $event = $this->createGetResponseEvent($request);
29
30         $expected = $request->attributes->all();
31
32         $listener->onKernelRequest($event);
33
34         $this->assertEquals($expected, $request->attributes->all());
35         $this->assertTrue($request->query->has('_path'));
36     }
37
38     public function testOnlyTriggeredIfControllerWasNotDefinedYet()
39     {
40         $request = Request::create('http://example.com/_fragment?_path=foo%3Dbar%26_controller%3Dfoo');
41         $request->attributes->set('_controller', 'bar');
42
43         $listener = new FragmentListener(new UriSigner('foo'));
44         $event = $this->createGetResponseEvent($request, HttpKernelInterface::SUB_REQUEST);
45
46         $expected = $request->attributes->all();
47
48         $listener->onKernelRequest($event);
49
50         $this->assertEquals($expected, $request->attributes->all());
51     }
52
53     /**
54      * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
55      */
56     public function testAccessDeniedWithNonSafeMethods()
57     {
58         $request = Request::create('http://example.com/_fragment', 'POST');
59
60         $listener = new FragmentListener(new UriSigner('foo'));
61         $event = $this->createGetResponseEvent($request);
62
63         $listener->onKernelRequest($event);
64     }
65
66     /**
67      * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
68      */
69     public function testAccessDeniedWithWrongSignature()
70     {
71         $request = Request::create('http://example.com/_fragment', 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));
72
73         $listener = new FragmentListener(new UriSigner('foo'));
74         $event = $this->createGetResponseEvent($request);
75
76         $listener->onKernelRequest($event);
77     }
78
79     public function testWithSignature()
80     {
81         $signer = new UriSigner('foo');
82         $request = Request::create($signer->sign('http://example.com/_fragment?_path=foo%3Dbar%26_controller%3Dfoo'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));
83
84         $listener = new FragmentListener($signer);
85         $event = $this->createGetResponseEvent($request);
86
87         $listener->onKernelRequest($event);
88
89         $this->assertEquals(array('foo' => 'bar', '_controller' => 'foo'), $request->attributes->get('_route_params'));
90         $this->assertFalse($request->query->has('_path'));
91     }
92
93     public function testRemovesPathWithControllerDefined()
94     {
95         $request = Request::create('http://example.com/_fragment?_path=foo%3Dbar%26_controller%3Dfoo');
96
97         $listener = new FragmentListener(new UriSigner('foo'));
98         $event = $this->createGetResponseEvent($request, HttpKernelInterface::SUB_REQUEST);
99
100         $listener->onKernelRequest($event);
101
102         $this->assertFalse($request->query->has('_path'));
103     }
104
105     public function testRemovesPathWithControllerNotDefined()
106     {
107         $signer = new UriSigner('foo');
108         $request = Request::create($signer->sign('http://example.com/_fragment?_path=foo%3Dbar'), 'GET', array(), array(), array(), array('REMOTE_ADDR' => '10.0.0.1'));
109
110         $listener = new FragmentListener($signer);
111         $event = $this->createGetResponseEvent($request);
112
113         $listener->onKernelRequest($event);
114
115         $this->assertFalse($request->query->has('_path'));
116     }
117
118     private function createGetResponseEvent(Request $request, $requestType = HttpKernelInterface::MASTER_REQUEST)
119     {
120         return new GetResponseEvent($this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock(), $request, $requestType);
121     }
122 }