Fix404RedirectUILanguageTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace Drupal\redirect_404\Tests;
  3. use Drupal\Component\Utility\UrlHelper;
  4. use Drupal\Core\Language\LanguageInterface;
  5. use Drupal\Core\Url;
  6. use Drupal\language\Entity\ConfigurableLanguage;
  7. use Drupal\redirect\Tests\AssertRedirectTrait;
  8. /**
  9. * UI tests for redirect_404 module with language and content translation.
  10. *
  11. * This runs the exact same tests as Fix404RedirectUITest, but with both
  12. * language and content translation modules enabled.
  13. *
  14. * @group redirect_404
  15. */
  16. class Fix404RedirectUILanguageTest extends Redirect404TestBase {
  17. use AssertRedirectTrait;
  18. /**
  19. * Additional modules to enable.
  20. *
  21. * @var array
  22. */
  23. public static $modules = ['language'];
  24. /**
  25. * Admin user's permissions for this test.
  26. *
  27. * @var array
  28. */
  29. protected $adminPermissions = [
  30. 'administer redirects',
  31. 'administer redirect settings',
  32. 'access content',
  33. 'bypass node access',
  34. 'create url aliases',
  35. 'administer url aliases',
  36. 'administer languages',
  37. ];
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function setUp() {
  42. parent::setUp();
  43. // Enable some languages for this test.
  44. $language = ConfigurableLanguage::createFromLangcode('de');
  45. $language->save();
  46. $language = ConfigurableLanguage::createFromLangcode('es');
  47. $language->save();
  48. $language = ConfigurableLanguage::createFromLangcode('fr');
  49. $language->save();
  50. }
  51. /**
  52. * Tests the fix 404 pages workflow with language and content translation.
  53. */
  54. public function testFix404RedirectList() {
  55. // Visit a non existing page to have the 404 redirect_error entry.
  56. $this->drupalGet('fr/testing');
  57. $redirect = \Drupal::database()->select('redirect_404')
  58. ->fields('redirect_404')
  59. ->condition('path', '/testing')
  60. ->execute()
  61. ->fetchAll();
  62. if (count($redirect) == 0) {
  63. $this->fail('No record was added');
  64. }
  65. // Go to the "fix 404" page and check the listing.
  66. $this->drupalGet('admin/config/search/redirect/404');
  67. $this->assertText('testing');
  68. $this->assertLanguageInTableBody('French');
  69. // Check the Language view filter uses the default language filter.
  70. $this->assertOption('edit-langcode', 'All');
  71. $this->assertOption('edit-langcode', 'en');
  72. $this->assertOption('edit-langcode', 'de');
  73. $this->assertOption('edit-langcode', 'es');
  74. $this->assertOption('edit-langcode', 'fr');
  75. $this->assertOption('edit-langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED);
  76. $this->clickLink(t('Add redirect'));
  77. // Check if we generate correct Add redirect url and if the form is
  78. // pre-filled.
  79. $destination = Url::fromRoute('redirect_404.fix_404')->getInternalPath();
  80. $expected_query = [
  81. 'destination' => $destination,
  82. 'language' => 'fr',
  83. 'source' => 'testing',
  84. ];
  85. $parsed_url = UrlHelper::parse($this->getUrl());
  86. $this->assertEqual(Url::fromRoute('redirect.add')->setAbsolute()->toString(), $parsed_url['path']);
  87. $this->assertEqual($expected_query, $parsed_url['query']);
  88. $this->assertFieldByName('redirect_source[0][path]', 'testing');
  89. $this->assertOptionSelected('edit-language-0-value', 'fr');
  90. // Save the redirect.
  91. $edit = ['redirect_redirect[0][uri]' => '/node'];
  92. $this->drupalPostForm(NULL, $edit, t('Save'));
  93. $this->assertUrl('admin/config/search/redirect/404');
  94. $this->assertText('There are no 404 errors to fix.');
  95. // Check if the redirect works as expected.
  96. $this->assertRedirect('fr/testing', 'fr/node', 'HTTP/1.1 301 Moved Permanently');
  97. // Test removing a redirect assignment, visit again the non existing page.
  98. $this->drupalGet('admin/config/search/redirect');
  99. $this->assertText('testing');
  100. $this->assertLanguageInTableBody('French');
  101. $this->clickLink('Delete', 0);
  102. $this->drupalPostForm(NULL, [], 'Delete');
  103. $this->assertUrl('admin/config/search/redirect');
  104. $this->assertText('There is no redirect yet.');
  105. $this->drupalGet('admin/config/search/redirect/404');
  106. $this->assertText('There are no 404 errors to fix.');
  107. // Should be listed again in the 404 overview.
  108. $this->drupalGet('fr/testing');
  109. $this->drupalGet('admin/config/search/redirect/404');
  110. $this->assertLanguageInTableBody('French');
  111. // Check the error path visit count.
  112. $this->assertFieldByXPath('//table/tbody/tr/td[2]', 2);
  113. $this->clickLink('Add redirect');
  114. // Save the redirect with a different langcode.
  115. $this->assertFieldByName('redirect_source[0][path]', 'testing');
  116. $this->assertOptionSelected('edit-language-0-value', 'fr');
  117. $edit['language[0][value]'] = 'es';
  118. $this->drupalPostForm(NULL, $edit, 'Save');
  119. $this->assertUrl('admin/config/search/redirect/404');
  120. // Should still be listed, redirecting to another language does not resolve
  121. // the path.
  122. $this->assertLanguageInTableBody('French');
  123. $this->drupalGet('admin/config/search/redirect');
  124. $this->assertLanguageInTableBody('Spanish');
  125. // Check if the redirect works as expected.
  126. $this->assertRedirect('es/testing', 'es/node', 'HTTP/1.1 301 Moved Permanently');
  127. // Visit multiple non existing pages to test the Redirect 404 View.
  128. $this->drupalGet('testing1');
  129. $this->drupalGet('de/testing2');
  130. $this->drupalGet('de/testing2?test=1');
  131. $this->drupalGet('de/testing2?test=2');
  132. $this->drupalGet('admin/config/search/redirect/404');
  133. $this->assertLanguageInTableBody('French');
  134. $this->assertLanguageInTableBody('English');
  135. $this->assertLanguageInTableBody('German');
  136. $this->assertText('testing1');
  137. $this->assertText('testing2');
  138. $this->assertText('testing2?test=1');
  139. $this->assertText('testing2?test=2');
  140. // Test the Language view filter.
  141. $this->drupalGet('admin/config/search/redirect/404', ['query' => ['langcode' => 'de']]);
  142. $this->assertText('English');
  143. $this->assertNoLanguageInTableBody('English');
  144. $this->assertLanguageInTableBody('German');
  145. $this->assertNoText('testing1');
  146. $this->assertText('testing2');
  147. $this->assertText('testing2?test=1');
  148. $this->assertText('testing2?test=2');
  149. $this->drupalGet('admin/config/search/redirect/404');
  150. $this->assertLanguageInTableBody('English');
  151. $this->assertLanguageInTableBody('German');
  152. $this->assertText('testing1');
  153. $this->assertText('testing2');
  154. $this->assertText('testing2?test=1');
  155. $this->assertText('testing2?test=2');
  156. $this->drupalGet('admin/config/search/redirect/404', ['query' => ['langcode' => 'en']]);
  157. $this->assertLanguageInTableBody('English');
  158. $this->assertNoLanguageInTableBody('German');
  159. $this->assertText('testing1');
  160. $this->assertNoText('testing2');
  161. $this->assertNoText('testing2?test=1');
  162. $this->assertNoText('testing2?test=2');
  163. // Assign a redirect to 'testing1'.
  164. $this->clickLink('Add redirect');
  165. $expected_query = [
  166. 'destination' => $destination,
  167. 'language' => 'en',
  168. 'source' => 'testing1',
  169. ];
  170. $parsed_url = UrlHelper::parse($this->getUrl());
  171. $this->assertEqual(Url::fromRoute('redirect.add')->setAbsolute()->toString(), $parsed_url['path']);
  172. $this->assertEqual($expected_query, $parsed_url['query']);
  173. $this->assertFieldByName('redirect_source[0][path]', 'testing1');
  174. $this->assertOptionSelected('edit-language-0-value', 'en');
  175. $edit = ['redirect_redirect[0][uri]' => '/node'];
  176. $this->drupalPostForm(NULL, $edit, t('Save'));
  177. $this->assertUrl('admin/config/search/redirect/404');
  178. $this->assertNoLanguageInTableBody('English');
  179. $this->assertLanguageInTableBody('German');
  180. $this->drupalGet('admin/config/search/redirect');
  181. $this->assertLanguageInTableBody('Spanish');
  182. $this->assertLanguageInTableBody('English');
  183. // Check if the redirect works as expected.
  184. $this->assertRedirect('/testing1', '/node', 'HTTP/1.1 301 Moved Permanently');
  185. }
  186. }