Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / redirect / tests / src / Kernel / RedirectAPITest.php
1 <?php
2
3 namespace Drupal\Tests\redirect\Kernel;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\redirect\Entity\Redirect;
7 use Drupal\Core\Language\Language;
8 use Drupal\redirect\Exception\RedirectLoopException;
9 use Drupal\KernelTests\KernelTestBase;
10
11 /**
12  * Redirect entity and redirect API test coverage.
13  *
14  * @group redirect
15  */
16 class RedirectAPITest extends KernelTestBase {
17
18   /**
19    * @var \Drupal\Core\Entity\EntityStorageInterface
20    */
21   protected $controller;
22
23   /**
24    * Modules to enable.
25    *
26    * @var array
27    */
28   public static $modules = array('redirect', 'link', 'field', 'system', 'user', 'language', 'views');
29
30   /**
31    * {@inheritdoc}
32    */
33   public function setUp() {
34     parent::setUp();
35
36     $this->installEntitySchema('redirect');
37     $this->installEntitySchema('user');
38     $this->installSchema('system', ['router']);
39     $this->installConfig(array('redirect'));
40
41     $language = ConfigurableLanguage::createFromLangcode('de');
42     $language->save();
43
44     $this->controller = $this->container->get('entity.manager')->getStorage('redirect');
45   }
46
47   /**
48    * Test redirect entity logic.
49    */
50   public function testRedirectEntity() {
51     // Create a redirect and test if hash has been generated correctly.
52     /** @var \Drupal\redirect\Entity\Redirect $redirect */
53     $redirect = $this->controller->create();
54     $redirect->setSource('some-url', array('key' => 'val'));
55     $redirect->setRedirect('node');
56
57     $redirect->save();
58     $this->assertEquals(Redirect::generateHash('some-url', array('key' => 'val'), Language::LANGCODE_NOT_SPECIFIED), $redirect->getHash());
59     // Update the redirect source query and check if hash has been updated as
60     // expected.
61     $redirect->setSource('some-url', array('key1' => 'val1'));
62     $redirect->save();
63     $this->assertEqual(Redirect::generateHash('some-url', array('key1' => 'val1'), Language::LANGCODE_NOT_SPECIFIED), $redirect->getHash());
64     // Update the redirect source path and check if hash has been updated as
65     // expected.
66     $redirect->setSource('another-url', array('key1' => 'val1'));
67     $redirect->save();
68     $this->assertEqual(Redirect::generateHash('another-url', array('key1' => 'val1'), Language::LANGCODE_NOT_SPECIFIED), $redirect->getHash());
69     // Update the redirect language and check if hash has been updated as
70     // expected.
71     $redirect->setLanguage('de');
72     $redirect->save();
73     $this->assertEqual(Redirect::generateHash('another-url', array('key1' => 'val1'), 'de'), $redirect->getHash());
74     // Create a few more redirects to test the select.
75     for ($i = 0; $i < 5; $i++) {
76       $redirect = $this->controller->create();
77       $redirect->setSource($this->randomMachineName());
78       $redirect->save();
79     }
80     /** @var \Drupal\redirect\RedirectRepository $repository */
81     $repository = \Drupal::service('redirect.repository');
82     $redirect = $repository->findMatchingRedirect('another-url', array('key1' => 'val1'), 'de');
83     if (!empty($redirect)) {
84       $this->assertEqual($redirect->getSourceUrl(), '/another-url?key1=val1');
85     }
86     else {
87       $this->fail(t('Failed to find matching redirect.'));
88     }
89
90     // Load the redirect based on url.
91     $redirects = $repository->findBySourcePath('another-url');
92     $redirect = array_shift($redirects);
93     if (!empty($redirect)) {
94       $this->assertEqual($redirect->getSourceUrl(), '/another-url?key1=val1');
95     }
96     else {
97       $this->fail(t('Failed to find redirect by source path.'));
98     }
99
100     // Test passthrough_querystring.
101     $redirect = $this->controller->create();
102     $redirect->setSource('a-different-url');
103     $redirect->setRedirect('node');
104     $redirect->save();
105     $redirect = $repository->findMatchingRedirect('a-different-url', ['key1' => 'val1'], 'de');
106     if (!empty($redirect)) {
107       $this->assertEqual($redirect->getSourceUrl(), '/a-different-url');
108     }
109     else {
110       $this->fail('Failed to find redirect by source path with query string.');
111     }
112
113     // Add another redirect to the same path, with a query. This should always
114     // be found before the source without a query set.
115     /** @var \Drupal\redirect\Entity\Redirect $new_redirect */
116     $new_redirect = $this->controller->create();
117     $new_redirect->setSource('a-different-url', ['foo' => 'bar']);
118     $new_redirect->setRedirect('node');
119     $new_redirect->save();
120     $found = $repository->findMatchingRedirect('a-different-url', ['foo' => 'bar'], 'de');
121     if (!empty($found)) {
122       $this->assertEqual($found->getSourceUrl(), '/a-different-url?foo=bar');
123     }
124     else {
125       $this->fail('Failed to find a redirect by source path with query string.');
126     }
127
128     // Add a redirect to an external URL.
129     $external_redirect = $this->controller->create();
130     $external_redirect->setSource('google');
131     $external_redirect->setRedirect('https://google.com');
132     $external_redirect->save();
133     $found = $repository->findMatchingRedirect('google');
134     if (!empty($found)) {
135       $this->assertEqual($found->getRedirectUrl()->toString(), 'https://google.com');
136     }
137     else {
138       $this->fail('Failed to find a redirect for google.');
139     }
140
141     // Hashes should be case-insensitive since the source paths are.
142     /** @var \Drupal\redirect\Entity\Redirect $redirect */
143     $redirect = $this->controller->create();
144     $redirect->setSource('Case-Sensitive-Path');
145     $redirect->setRedirect('node');
146     $redirect->save();
147     $found = $repository->findBySourcePath('case-sensitive-path');
148     if (!empty($found)) {
149       $found = reset($found);
150       $this->assertEqual($found->getSourceUrl(), '/Case-Sensitive-Path');
151     }
152     else {
153       $this->fail('findBySourcePath is case sensitive');
154     }
155     $found = $repository->findMatchingRedirect('case-sensitive-path');
156     if (!empty($found)) {
157       $this->assertEqual($found->getSourceUrl(), '/Case-Sensitive-Path');
158     }
159     else {
160       $this->fail('findMatchingRedirect is case sensitive.');
161     }
162   }
163
164   /**
165    * Test slash is removed from source path in findMatchingRedirect.
166    */
167   public function testDuplicateRedirectEntry() {
168     $redirect = $this->controller->create();
169     $redirect->setSource('/foo/foo', []);
170     $redirect->setRedirect('foo');
171     $redirect->save();
172
173     $redirect_repository = \Drupal::service('redirect.repository');
174     $matched_redirect = $redirect_repository->findMatchingRedirect('/foo/foo', [], 'en-AU');
175     $this->assertNotNull($matched_redirect);
176
177     $null_redirect = $redirect_repository->findMatchingRedirect('/foo/foo-bar', [], 'en-AU');
178     $this->assertNull($null_redirect);
179   }
180
181   /**
182    * Test redirect_sort_recursive().
183    */
184   public function testSortRecursive() {
185     $test_cases = array(
186       array(
187         'input' => array('b' => 'aa', 'c' => array('c2' => 'aa', 'c1' => 'aa'), 'a' => 'aa'),
188         'expected' => array('a' => 'aa', 'b' => 'aa', 'c' => array('c1' => 'aa', 'c2' => 'aa')),
189         'callback' => 'ksort',
190       ),
191     );
192     foreach ($test_cases as $index => $test_case) {
193       $output = $test_case['input'];
194       redirect_sort_recursive($output, $test_case['callback']);
195       $this->assertIdentical($output, $test_case['expected']);
196     }
197   }
198
199   /**
200    * Test loop detection.
201    */
202   public function testLoopDetection() {
203     // Add a chained redirect that isn't a loop.
204     /** @var \Drupal\redirect\Entity\Redirect $one */
205     $one = $this->controller->create();
206     $one->setSource('my-path');
207     $one->setRedirect('node');
208     $one->save();
209     /** @var \Drupal\redirect\Entity\Redirect $two */
210     $two = $this->controller->create();
211     $two->setSource('second-path');
212     $two->setRedirect('my-path');
213     $two->save();
214     /** @var \Drupal\redirect\Entity\Redirect $three */
215     $three = $this->controller->create();
216     $three->setSource('third-path');
217     $three->setRedirect('second-path');
218     $three->save();
219
220     /** @var \Drupal\redirect\RedirectRepository $repository */
221     $repository = \Drupal::service('redirect.repository');
222     $found = $repository->findMatchingRedirect('third-path');
223     if (!empty($found)) {
224       $this->assertEqual($found->getRedirectUrl()->toString(), '/node', 'Chained redirects properly resolved in findMatchingRedirect.');
225     }
226     else {
227       $this->fail('Failed to resolve a chained redirect.');
228     }
229
230     // Create a loop.
231     $one->setRedirect('third-path');
232     $one->save();
233     try {
234       $repository->findMatchingRedirect('third-path');
235       $this->fail('Failed to detect a redirect loop.');
236     }
237     catch (RedirectLoopException $e) {
238       $this->pass('Properly detected a redirect loop.');
239     }
240   }
241
242   /**
243    * Test redirect_parse_url().
244    */
245   public function testParseURL() {
246     //$test_cases = array(
247     //  array(
248     //    'input' => array('b' => 'aa', 'c' => array('c2' => 'aa', 'c1' => 'aa'), 'a' => 'aa'),
249     //    'expected' => array('a' => 'aa', 'b' => 'aa', 'c' => array('c1' => 'aa', 'c2' => 'aa')),
250     //  ),
251     //);
252     //foreach ($test_cases as $index => $test_case) {
253     //  $output = redirect_parse_url($test_case['input']);
254     //  $this->assertIdentical($output, $test_case['expected']);
255     //}
256   }
257
258   /**
259    * Test multilingual redirects.
260    */
261   public function testMultilanguageCases() {
262     // Add a redirect for english.
263     /** @var \Drupal\redirect\Entity\Redirect $en_redirect */
264     $en_redirect = $this->controller->create();
265     $en_redirect->setSource('langpath');
266     $en_redirect->setRedirect('/about');
267     $en_redirect->setLanguage('en');
268     $en_redirect->save();
269
270     // Add a redirect for germany.
271     /** @var \Drupal\redirect\Entity\Redirect $en_redirect */
272     $en_redirect = $this->controller->create();
273     $en_redirect->setSource('langpath');
274     $en_redirect->setRedirect('node');
275     $en_redirect->setLanguage('de');
276     $en_redirect->save();
277
278     // Check redirect for english.
279     /** @var \Drupal\redirect\RedirectRepository $repository */
280     $repository = \Drupal::service('redirect.repository');
281
282     $found = $repository->findBySourcePath('langpath');
283     if (!empty($found)) {
284       $this->assertEqual($found[1]->getRedirectUrl()->toString(), '/about', 'Multilingual redirect resolved properly.');
285       $this->assertEqual($found[1]->get('language')[0]->value, 'en', 'Multilingual redirect resolved properly.');
286     }
287     else {
288       $this->fail('Failed to resolve the multilingual redirect.');
289     }
290
291     // Check redirect for germany.
292     \Drupal::configFactory()->getEditable('system.site')->set('default_langcode', 'de')->save();
293     /** @var \Drupal\redirect\RedirectRepository $repository */
294     $repository = \Drupal::service('redirect.repository');
295     $found = $repository->findBySourcePath('langpath');
296     if (!empty($found)) {
297       $this->assertEqual($found[2]->getRedirectUrl()->toString(), '/node', 'Multilingual redirect resolved properly.');
298       $this->assertEqual($found[2]->get('language')[0]->value, 'de', 'Multilingual redirect resolved properly.');
299     }
300     else {
301       $this->fail('Failed to resolve the multilingual redirect.');
302     }
303   }
304
305 }