Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Routing / RedirectDestinationTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Routing;
4
5 use Drupal\Component\Utility\UrlHelper;
6 use Drupal\Core\Routing\RedirectDestination;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpFoundation\RequestStack;
10
11 /**
12  * @coversDefaultClass \Drupal\Core\Routing\RedirectDestination
13  * @group Routing
14  */
15 class RedirectDestinationTest extends UnitTestCase {
16
17   /**
18    * The request stack.
19    *
20    * @var \Symfony\Component\HttpFoundation\RequestStack
21    */
22   protected $requestStack;
23
24   /**
25    * The mocked URL generator.
26    *
27    * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
28    */
29   protected $urlGenerator;
30
31   /**
32    * The tested redirect destination.
33    *
34    * @var \Drupal\Core\Routing\RedirectDestination
35    */
36   protected $redirectDestination;
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUp() {
42     parent::setUp();
43
44     $this->requestStack = new RequestStack();
45     $this->urlGenerator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
46     $this->redirectDestination = new RedirectDestination($this->requestStack, $this->urlGenerator);
47   }
48
49   protected function setupUrlGenerator() {
50     $this->urlGenerator->expects($this->any())
51       ->method('generateFromRoute')
52       ->willReturnCallback(function ($route, $parameters, $options) {
53         $query_string = '';
54         if (!empty($options['query'])) {
55           $query_string = '?' . UrlHelper::buildQuery($options['query']);
56         }
57
58         return '/current-path' . $query_string;
59       });
60   }
61
62   /**
63    * Tests destination passed via $_GET.
64    *
65    * @param \Symfony\Component\HttpFoundation\Request $request
66    *   The request to test.
67    * @param string $expected_destination
68    *   The expected destination.
69    *
70    * @dataProvider providerGet
71    *
72    * @covers ::get
73    */
74   public function testGet(Request $request, $expected_destination) {
75     $this->requestStack->push($request);
76     $this->setupUrlGenerator();
77
78     // Call in twice in order to ensure it returns the same the next time.
79     $this->assertEquals($expected_destination, $this->redirectDestination->get());
80     $this->assertEquals($expected_destination, $this->redirectDestination->get());
81   }
82
83   /**
84    * @dataProvider providerGet
85    *
86    * @covers ::getAsArray
87    */
88   public function testGetAsArray(Request $request, $expected_destination) {
89     $this->requestStack->push($request);
90     $this->setupUrlGenerator();
91
92     // Call in twice in order to ensure it returns the same the next time.
93     $this->assertEquals(['destination' => $expected_destination], $this->redirectDestination->getAsArray());
94     $this->assertEquals(['destination' => $expected_destination], $this->redirectDestination->getAsArray());
95   }
96
97   public function providerGet() {
98     $data = [];
99
100     $request = Request::create('/');
101     $request->query->set('destination', '/example');
102     // A request with a destination query.
103     $data[] = [$request, '/example'];
104
105     // A request without a destination query,
106     $request = Request::create('/');
107     $data[] = [$request, '/current-path'];
108
109     // A request without destination query, but other query attributes.
110     $request = Request::create('/');
111     $request->query->set('other', 'value');
112     $data[] = [$request, '/current-path?other=value'];
113
114     // A request with a dedicated specified external destination.
115     $request = Request::create('/');
116     $request->query->set('destination', 'https://www.drupal.org');
117     $data[] = [$request, '/'];
118
119     return $data;
120   }
121
122   /**
123    * @covers ::set
124    * @covers ::get
125    */
126   public function testSetBeforeGetCall() {
127     $this->redirectDestination->set('/example');
128     $this->assertEquals('/example', $this->redirectDestination->get());
129   }
130
131   /**
132    * @covers ::set
133    * @covers ::get
134    */
135   public function testSetAfterGetCall() {
136     $request = Request::create('/');
137     $request->query->set('destination', '/other-example');
138     $this->requestStack->push($request);
139     $this->setupUrlGenerator();
140
141     $this->redirectDestination->set('/example');
142     $this->assertEquals('/example', $this->redirectDestination->get());
143   }
144
145 }