/src/Tests/GlobalRedirectTest.php

https://gitlab.com/Drulenium-bot/redirect · PHP · 218 lines · 111 code · 40 blank · 67 comment · 3 complexity · f512d9bd8a7aa6300cd60eba860f3ba9 MD5 · raw file

  1. <?php
  2. /**
  3. * @file
  4. * Global Redirect functionality tests
  5. */
  6. namespace Drupal\redirect\Tests;
  7. use Drupal\Component\Utility\SafeMarkup;
  8. use Drupal\Core\Language\Language;
  9. use Drupal\simpletest\WebTestBase;
  10. /**
  11. * Global redirect test cases.
  12. *
  13. * @group redirect
  14. */
  15. class GlobalRedirectTest extends WebTestBase {
  16. /**
  17. * Modules to enable.
  18. *
  19. * @var array
  20. */
  21. public static $modules = array('path', 'node', 'redirect', 'taxonomy', 'forum', 'views');
  22. /**
  23. * @var \Drupal\Core\Session\AccountInterface
  24. */
  25. protected $normalUser;
  26. /**
  27. * @var \Drupal\Core\Session\AccountInterface
  28. */
  29. protected $adminUser;
  30. /**
  31. * @var \Drupal\Core\Config\Config
  32. */
  33. protected $config;
  34. /**
  35. * @var \Drupal\Core\Entity\ContentEntityInterface
  36. */
  37. protected $forumTerm;
  38. /**
  39. * @var \Drupal\Core\Entity\ContentEntityInterface
  40. */
  41. protected $term;
  42. /**
  43. * @var \Drupal\Core\Entity\ContentEntityInterface
  44. */
  45. protected $node;
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function setUp() {
  50. parent::setUp();
  51. $this->config = $this->config('redirect.settings');
  52. $this->drupalCreateContentType(['type' => 'page', 'name' => 'Page']);
  53. $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
  54. // Create a users for testing the access.
  55. $this->normalUser = $this->drupalCreateUser([
  56. 'access content',
  57. 'create page content',
  58. 'create url aliases',
  59. 'access administration pages',
  60. ]);
  61. $this->adminUser = $this->drupalCreateUser([
  62. 'administer site configuration',
  63. 'access administration pages',
  64. ]);
  65. // Save the node.
  66. $this->node = $this->drupalCreateNode([
  67. 'type' => 'page',
  68. 'title' => 'Test Page Node',
  69. 'path' => ['alias' => '/test-node'],
  70. 'language' => Language::LANGCODE_NOT_SPECIFIED,
  71. ]);
  72. // Create an alias for the create story path - this is used in the
  73. // "redirect with permissions testing" test.
  74. \Drupal::service('path.alias_storage')->save('/admin/config/system/site-information', '/site-info');
  75. // Create a taxonomy term for the forum.
  76. $term = entity_create('taxonomy_term', [
  77. 'name' => 'Test Forum Term',
  78. 'vid' => 'forums',
  79. 'langcode' => Language::LANGCODE_NOT_SPECIFIED,
  80. ]);
  81. $term->save();
  82. $this->forumTerm = $term;
  83. // Create another taxonomy vocabulary with a term.
  84. $vocab = entity_create('taxonomy_vocabulary', [
  85. 'name' => 'test vocab',
  86. 'vid' => 'test-vocab',
  87. 'langcode' => Language::LANGCODE_NOT_SPECIFIED,
  88. ]);
  89. $vocab->save();
  90. $term = entity_create('taxonomy_term', [
  91. 'name' => 'Test Term',
  92. 'vid' => $vocab->id(),
  93. 'langcode' => Language::LANGCODE_NOT_SPECIFIED,
  94. 'path' => ['alias' => '/test-term'],
  95. ]);
  96. $term->save();
  97. $this->term = $term;
  98. }
  99. /**
  100. * Will test the redirects.
  101. */
  102. public function testRedirects() {
  103. // Test alias normalization.
  104. $this->config->set('normalize_aliases', TRUE)->save();
  105. $this->assertRedirect('node/' . $this->node->id(), 'test-node');
  106. $this->assertRedirect('Test-node', 'test-node');
  107. $this->config->set('normalize_aliases', FALSE)->save();
  108. $this->assertRedirect('node/' . $this->node->id(), NULL, 'HTTP/1.1 200 OK');
  109. $this->assertRedirect('Test-node', NULL, 'HTTP/1.1 200 OK');
  110. // Test deslashing.
  111. $this->config->set('deslash', TRUE)->save();
  112. $this->assertRedirect('test-node/', 'test-node');
  113. $this->config->set('deslash', FALSE)->save();
  114. $this->assertRedirect('test-node/', NULL, 'HTTP/1.1 200 OK');
  115. // Test front page redirects.
  116. $this->config->set('frontpage_redirect', TRUE)->save();
  117. $this->config('system.site')->set('page.front', '/node')->save();
  118. $this->assertRedirect('node', '<front>');
  119. // Test front page redirects with an alias.
  120. \Drupal::service('path.alias_storage')->save('/node', '/node-alias');
  121. $this->assertRedirect('node-alias', '<front>');
  122. $this->config->set('frontpage_redirect', FALSE)->save();
  123. $this->assertRedirect('node', NULL, 'HTTP/1.1 200 OK');
  124. $this->assertRedirect('node-alias', NULL, 'HTTP/1.1 200 OK');
  125. // Test post request.
  126. $this->config->set('normalize_aliases', TRUE)->save();
  127. $this->drupalPost('Test-node', 'application/json', array());
  128. // Does not do a redirect, stays in the same path.
  129. $this->assertEqual(basename($this->getUrl()), 'Test-node');
  130. // Test the access checking.
  131. $this->config->set('normalize_aliases', TRUE)->save();
  132. $this->config->set('access_check', TRUE)->save();
  133. $this->assertRedirect('admin/config/system/site-information', NULL, 'HTTP/1.1 403 Forbidden');
  134. $this->config->set('access_check', FALSE)->save();
  135. // @todo - here it seems that the access check runs prior to our redirecting
  136. // check why so and enable the test.
  137. //$this->assertRedirect('admin/config/system/site-information', 'site-info');
  138. // Login as user with admin privileges.
  139. $this->drupalLogin($this->adminUser);
  140. // Test ignoring admin paths.
  141. $this->config->set('ignore_admin_path', FALSE)->save();
  142. $this->assertRedirect('admin/config/system/site-information', 'site-info');
  143. $this->config->set('ignore_admin_path', TRUE)->save();
  144. $this->assertRedirect('admin/config/system/site-information', NULL, 'HTTP/1.1 200 OK');
  145. }
  146. /**
  147. * Asserts the redirect from $path to the $expected_ending_url.
  148. *
  149. * @param string $path
  150. * The request path.
  151. * @param $expected_ending_url
  152. * The path where we expect it to redirect. If NULL value provided, no
  153. * redirect is expected.
  154. * @param string $expected_ending_status
  155. * The status we expect to get with the first request.
  156. */
  157. public function assertRedirect($path, $expected_ending_url, $expected_ending_status = 'HTTP/1.1 301 Moved Permanently') {
  158. $this->drupalHead($GLOBALS['base_url'] . '/' . $path);
  159. $headers = $this->drupalGetHeaders(TRUE);
  160. $ending_url = isset($headers[0]['location']) ? $headers[0]['location'] : NULL;
  161. $message = SafeMarkup::format('Testing redirect from %from to %to. Ending url: %url', array(
  162. '%from' => $path,
  163. '%to' => $expected_ending_url,
  164. '%url' => $ending_url,
  165. ));
  166. if ($expected_ending_url == '<front>') {
  167. $expected_ending_url = $GLOBALS['base_url'] . '/';
  168. }
  169. elseif (!empty($expected_ending_url)) {
  170. $expected_ending_url = $GLOBALS['base_url'] . '/' . $expected_ending_url;
  171. }
  172. else {
  173. $expected_ending_url = NULL;
  174. }
  175. $this->assertEqual($expected_ending_url, $ending_url);
  176. $this->assertEqual($headers[0][':status'], $expected_ending_status);
  177. }
  178. }