3 namespace Drupal\redirect_404\Tests;
5 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\redirect\Tests\AssertRedirectTrait;
11 * UI tests for redirect_404 module with language and content translation.
13 * This runs the exact same tests as Fix404RedirectUITest, but with both
14 * language and content translation modules enabled.
18 class Fix404RedirectUILanguageTest extends Redirect404TestBase {
20 use AssertRedirectTrait;
23 * Additional modules to enable.
27 public static $modules = ['language'];
30 * Admin user's permissions for this test.
34 protected $adminPermissions = [
35 'administer redirects',
36 'administer redirect settings',
37 'access site reports',
41 'administer url aliases',
42 'administer languages',
48 public function setUp() {
51 // Enable some languages for this test.
52 $language = ConfigurableLanguage::createFromLangcode('de');
54 $language = ConfigurableLanguage::createFromLangcode('es');
56 $language = ConfigurableLanguage::createFromLangcode('fr');
61 * Tests the fix 404 pages workflow with language and content translation.
63 public function testFix404RedirectList() {
64 // Visit a non existing page to have the 404 redirect_error entry.
65 $this->drupalGet('fr/testing');
67 $redirect = \Drupal::database()->select('redirect_404')
68 ->fields('redirect_404')
69 ->condition('path', '/testing')
72 if (count($redirect) == 0) {
73 $this->fail('No record was added');
76 // Go to the "fix 404" page and check the listing.
77 $this->drupalGet('admin/config/search/redirect/404');
78 $this->assertText('testing');
79 $this->assertLanguageInTableBody('French');
80 // Check the Language view filter uses the default language filter.
81 $this->assertOption('edit-langcode', 'All');
82 $this->assertOption('edit-langcode', 'en');
83 $this->assertOption('edit-langcode', 'de');
84 $this->assertOption('edit-langcode', 'es');
85 $this->assertOption('edit-langcode', 'fr');
86 $this->assertOption('edit-langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED);
87 $this->clickLink(t('Add redirect'));
89 // Check if we generate correct Add redirect url and if the form is
91 $destination = Url::fromRoute('redirect_404.fix_404')->getInternalPath();
94 'source' => 'testing',
96 'destination' => $destination,
99 $this->assertUrl('admin/config/search/redirect/add', $options);
100 $this->assertFieldByName('redirect_source[0][path]', 'testing');
101 $this->assertOptionSelected('edit-language-0-value', 'fr');
102 // Save the redirect.
103 $edit = ['redirect_redirect[0][uri]' => '/node'];
104 $this->drupalPostForm(NULL, $edit, t('Save'));
105 $this->assertUrl('admin/config/search/redirect/404');
106 $this->assertText('There are no 404 errors to fix.');
107 // Check if the redirect works as expected.
108 $this->assertRedirect('fr/testing', 'fr/node', 'HTTP/1.1 301 Moved Permanently');
110 // Test removing a redirect assignment, visit again the non existing page.
111 $this->drupalGet('admin/config/search/redirect');
112 $this->assertText('testing');
113 $this->assertLanguageInTableBody('French');
114 $this->clickLink('Delete', 0);
115 $this->drupalPostForm(NULL, [], 'Delete');
116 $this->assertUrl('admin/config/search/redirect');
117 $this->assertText('There is no redirect yet.');
118 $this->drupalGet('admin/config/search/redirect/404');
119 $this->assertText('There are no 404 errors to fix.');
120 // Should be listed again in the 404 overview.
121 $this->drupalGet('fr/testing');
122 $this->drupalGet('admin/config/search/redirect/404');
123 $this->assertLanguageInTableBody('French');
124 // Check the error path visit count.
125 $this->assertFieldByXPath('//table/tbody/tr/td[2]', 2);
126 $this->clickLink('Add redirect');
127 // Save the redirect with a different langcode.
128 $this->assertFieldByName('redirect_source[0][path]', 'testing');
129 $this->assertOptionSelected('edit-language-0-value', 'fr');
130 $edit['language[0][value]'] = 'es';
131 $this->drupalPostForm(NULL, $edit, 'Save');
132 $this->assertUrl('admin/config/search/redirect/404');
133 // Should still be listed, redirecting to another language does not resolve
135 $this->assertLanguageInTableBody('French');
136 $this->drupalGet('admin/config/search/redirect');
137 $this->assertLanguageInTableBody('Spanish');
138 // Check if the redirect works as expected.
139 $this->assertRedirect('es/testing', 'es/node', 'HTTP/1.1 301 Moved Permanently');
141 // Visit multiple non existing pages to test the Redirect 404 View.
142 $this->drupalGet('testing1');
143 $this->drupalGet('de/testing2');
144 $this->drupalGet('de/testing2?test=1');
145 $this->drupalGet('de/testing2?test=2');
146 $this->drupalGet('admin/config/search/redirect/404');
147 $this->assertLanguageInTableBody('French');
148 $this->assertLanguageInTableBody('English');
149 $this->assertLanguageInTableBody('German');
150 $this->assertText('testing1');
151 $this->assertText('testing2');
152 $this->assertText('testing2?test=1');
153 $this->assertText('testing2?test=2');
155 // Test the Language view filter.
156 $this->drupalGet('admin/config/search/redirect/404', ['query' => ['langcode' => 'de']]);
157 $this->assertText('English');
158 $this->assertNoLanguageInTableBody('English');
159 $this->assertLanguageInTableBody('German');
160 $this->assertNoText('testing1');
161 $this->assertText('testing2');
162 $this->assertText('testing2?test=1');
163 $this->assertText('testing2?test=2');
164 $this->drupalGet('admin/config/search/redirect/404');
165 $this->assertLanguageInTableBody('English');
166 $this->assertLanguageInTableBody('German');
167 $this->assertText('testing1');
168 $this->assertText('testing2');
169 $this->assertText('testing2?test=1');
170 $this->assertText('testing2?test=2');
171 $this->drupalGet('admin/config/search/redirect/404', ['query' => ['langcode' => 'en']]);
172 $this->assertLanguageInTableBody('English');
173 $this->assertNoLanguageInTableBody('German');
174 $this->assertText('testing1');
175 $this->assertNoText('testing2');
176 $this->assertNoText('testing2?test=1');
177 $this->assertNoText('testing2?test=2');
179 // Assign a redirect to 'testing1'.
180 $this->clickLink('Add redirect');
183 'source' => 'testing1',
185 'destination' => $destination,
188 $this->assertUrl('admin/config/search/redirect/add', $options);
189 $this->assertFieldByName('redirect_source[0][path]', 'testing1');
190 $this->assertOptionSelected('edit-language-0-value', 'en');
191 $edit = ['redirect_redirect[0][uri]' => '/node'];
192 $this->drupalPostForm(NULL, $edit, t('Save'));
193 $this->assertUrl('admin/config/search/redirect/404');
194 $this->assertNoLanguageInTableBody('English');
195 $this->assertLanguageInTableBody('German');
196 $this->drupalGet('admin/config/search/redirect');
197 $this->assertLanguageInTableBody('Spanish');
198 $this->assertLanguageInTableBody('English');
199 // Check if the redirect works as expected.
200 $this->assertRedirect('/testing1', '/node', 'HTTP/1.1 301 Moved Permanently');