Fix404RedirectUITest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Drupal\redirect_404\Tests;
  3. use Drupal\Component\Utility\UrlHelper;
  4. use Drupal\Core\Url;
  5. /**
  6. * UI tests for redirect_404 module.
  7. *
  8. * @group redirect_404
  9. */
  10. class Fix404RedirectUITest extends Redirect404TestBase {
  11. /**
  12. * Tests the fix 404 pages workflow.
  13. */
  14. public function testFix404Pages() {
  15. // Visit a non existing page to have the 404 redirect_error entry.
  16. $this->drupalGet('non-existing0');
  17. // Go to the "fix 404" page and check the listing.
  18. $this->drupalGet('admin/config/search/redirect/404');
  19. $this->assertText('non-existing0');
  20. $this->clickLink(t('Add redirect'));
  21. // Check if we generate correct Add redirect url and if the form is
  22. // pre-filled.
  23. $destination = Url::fromRoute('redirect_404.fix_404')->getInternalPath();
  24. $expected_query = [
  25. 'destination' => $destination,
  26. 'language' => 'en',
  27. 'source' => 'non-existing0',
  28. ];
  29. $parsed_url = UrlHelper::parse($this->getUrl());
  30. $this->assertEqual(Url::fromRoute('redirect.add')->setAbsolute()->toString(), $parsed_url['path']);
  31. $this->assertEqual($expected_query, $parsed_url['query']);
  32. $this->assertFieldByName('redirect_source[0][path]', 'non-existing0');
  33. // Save the redirect.
  34. $edit = ['redirect_redirect[0][uri]' => '/node'];
  35. $this->drupalPostForm(NULL, $edit, t('Save'));
  36. $this->assertUrl('admin/config/search/redirect/404');
  37. $this->assertText('There are no 404 errors to fix.');
  38. // Check if the redirect works as expected.
  39. $this->drupalGet('non-existing0');
  40. $this->assertUrl('node');
  41. // Test removing a redirect assignment, visit again the non existing page.
  42. $this->drupalGet('admin/config/search/redirect');
  43. $this->assertText('non-existing0');
  44. $this->clickLink('Delete', 0);
  45. $this->drupalPostForm(NULL, [], 'Delete');
  46. $this->assertUrl('admin/config/search/redirect');
  47. $this->assertText('There is no redirect yet.');
  48. $this->drupalGet('admin/config/search/redirect/404');
  49. $this->assertText('There are no 404 errors to fix.');
  50. // Should be listed again in the 404 overview.
  51. $this->drupalGet('non-existing0');
  52. $this->drupalGet('admin/config/search/redirect/404');
  53. $this->assertText('non-existing0');
  54. // Visit multiple non existing pages to test the Redirect 404 View.
  55. $this->drupalGet('non-existing0?test=1');
  56. $this->drupalGet('non-existing0?test=2');
  57. $this->drupalGet('non-existing1');
  58. $this->drupalGet('non-existing2');
  59. $this->drupalGet('admin/config/search/redirect/404');
  60. $this->assertText('non-existing0?test=1');
  61. $this->assertText('non-existing0?test=2');
  62. $this->assertText('non-existing0');
  63. $this->assertText('non-existing1');
  64. $this->assertText('non-existing2');
  65. // Test the Path view filter.
  66. $this->drupalGet('admin/config/search/redirect/404', ['query' => ['path' => 'test=']]);
  67. $this->assertText('non-existing0?test=1');
  68. $this->assertText('non-existing0?test=2');
  69. $this->assertNoText('non-existing1');
  70. $this->assertNoText('non-existing2');
  71. $this->drupalGet('admin/config/search/redirect/404', ['query' => ['path' => 'existing1']]);
  72. $this->assertNoText('non-existing0?test=1');
  73. $this->assertNoText('non-existing0?test=2');
  74. $this->assertNoText('non-existing0');
  75. $this->assertText('non-existing1');
  76. $this->assertNoText('non-existing2');
  77. $this->drupalGet('admin/config/search/redirect/404');
  78. $this->assertText('non-existing0?test=1');
  79. $this->assertText('non-existing0?test=2');
  80. $this->assertText('non-existing0');
  81. $this->assertText('non-existing1');
  82. $this->assertText('non-existing2');
  83. $this->drupalGet('admin/config/search/redirect/404', ['query' => ['path' => 'g2']]);
  84. $this->assertNoText('non-existing0?test=1');
  85. $this->assertNoText('non-existing0?test=2');
  86. $this->assertNoText('non-existing0');
  87. $this->assertNoText('non-existing1');
  88. $this->assertText('non-existing2');
  89. // Assign a redirect to 'non-existing2'.
  90. $this->clickLink('Add redirect');
  91. $expected_query = [
  92. 'source' => 'non-existing2',
  93. 'language' => 'en',
  94. 'destination' => $destination,
  95. ];
  96. $parsed_url = UrlHelper::parse($this->getUrl());
  97. $this->assertEqual(Url::fromRoute('redirect.add')->setAbsolute()->toString(), $parsed_url['path']);
  98. $this->assertEqual($expected_query, $parsed_url['query']);
  99. $this->assertFieldByName('redirect_source[0][path]', 'non-existing2');
  100. $this->drupalPostForm(NULL, $edit, t('Save'));
  101. $this->assertUrl('admin/config/search/redirect/404');
  102. $this->assertText('non-existing0?test=1');
  103. $this->assertText('non-existing0?test=2');
  104. $this->assertText('non-existing0');
  105. $this->assertText('non-existing1');
  106. $this->assertNoText('non-existing2');
  107. // Check if the redirect works as expected.
  108. $this->drupalGet('admin/config/search/redirect');
  109. $this->assertText('non-existing2');
  110. }
  111. /**
  112. * Tests the redirect ignore pages.
  113. */
  114. public function testIgnorePages() {
  115. // Create two nodes.
  116. $node1 = $this->drupalCreateNode(['type' => 'page']);
  117. $node2 = $this->drupalCreateNode(['type' => 'page']);
  118. // Set some pages to be ignored just for the test.
  119. $node_to_ignore = '/node/' . $node1->id() . '/test';
  120. $terms_to_ignore = '/term/*';
  121. $pages = $node_to_ignore . "\r\n" . $terms_to_ignore;
  122. \Drupal::configFactory()
  123. ->getEditable('redirect_404.settings')
  124. ->set('pages', $pages)
  125. ->save();
  126. // Visit ignored or non existing pages.
  127. $this->drupalGet('node/' . $node1->id() . '/test');
  128. $this->drupalGet('term/foo');
  129. $this->drupalGet('term/1');
  130. // Go to the "fix 404" page and check there are no 404 entries.
  131. $this->drupalGet('admin/config/search/redirect/404');
  132. $this->assertNoText('node/' . $node1->id() . '/test');
  133. $this->assertNoText('term/foo');
  134. $this->assertNoText('term/1');
  135. // Visit non existing but 'unignored' page.
  136. $this->drupalGet('node/' . $node2->id() . '/test');
  137. // Go to the "fix 404" page and check there is a 404 entry.
  138. $this->drupalGet('admin/config/search/redirect/404');
  139. $this->assertText('node/' . $node2->id() . '/test');
  140. // Add this 404 entry to the 'ignore path' list, assert it works properly.
  141. $path_to_ignore = '/node/' . $node2->id() . '/test';
  142. $destination = '&destination=admin/config/search/redirect/404';
  143. $this->clickLink('Ignore');
  144. $this->assertUrl('admin/config/search/redirect/settings?ignore=' . $path_to_ignore . $destination);
  145. $this->assertText('Resolved the path ' . $path_to_ignore . ' in the database. Please check the ignored list and save the settings.');
  146. $xpath = $this->xpath('//*[@id="edit-ignore-pages"]')[0]->asXML();
  147. $this->assertTrue(strpos($xpath, $node_to_ignore), $node_to_ignore . " in 'Path to ignore' found");
  148. $this->assertTrue(strpos($xpath, $terms_to_ignore), $terms_to_ignore . " in 'Path to ignore' found");
  149. $this->assertTrue(strpos($xpath, $path_to_ignore), $path_to_ignore . " in 'Path to ignore' found");
  150. // Save the path with wildcard, but omitting the leading slash.
  151. $nodes_to_ignore = 'node/*';
  152. $edit = ['ignore_pages' => $nodes_to_ignore . "\r\n" . $terms_to_ignore];
  153. $this->drupalPostForm(NULL, $edit, 'Save configuration');
  154. // Should redirect to 'Fix 404'. Check the 404 entry is not shown anymore.
  155. $this->assertUrl('admin/config/search/redirect/404');
  156. $this->assertText('Configuration was saved.');
  157. $this->assertNoText('node/' . $node2->id() . '/test');
  158. $this->assertText('There are no 404 errors to fix.');
  159. // Go back to the settings to check the 'Path to ignore' configurations.
  160. $this->drupalGet('admin/config/search/redirect/settings');
  161. $xpath = $this->xpath('//*[@id="edit-ignore-pages"]')[0]->asXML();
  162. // Check that the new page to ignore has been saved with leading slash.
  163. $this->assertTrue(strpos($xpath, '/' . $nodes_to_ignore), '/' . $nodes_to_ignore . " in 'Path to ignore' found");
  164. $this->assertTrue(strpos($xpath, $terms_to_ignore), $terms_to_ignore . " in 'Path to ignore' found");
  165. $this->assertFalse(strpos($xpath, $node_to_ignore), $node_to_ignore . " in 'Path to ignore' found");
  166. $this->assertFalse(strpos($xpath, $path_to_ignore), $path_to_ignore . " in 'Path to ignore' found");
  167. }
  168. }