PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/redirect/tests/src/Kernel/RedirectAPITest.php

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