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',
40 'administer url aliases',
41 'administer languages',
47 public function setUp() {
50 // Enable some languages for this test.
51 $language = ConfigurableLanguage::createFromLangcode('de');
53 $language = ConfigurableLanguage::createFromLangcode('es');
55 $language = ConfigurableLanguage::createFromLangcode('fr');
60 * Tests the fix 404 pages workflow with language and content translation.
62 public function testFix404RedirectList() {
63 // Visit a non existing page to have the 404 redirect_error entry.
64 $this->drupalGet('fr/testing');
66 $redirect = \Drupal::database()->select('redirect_404')
67 ->fields('redirect_404')
68 ->condition('path', '/testing')
71 if (count($redirect) == 0) {
72 $this->fail('No record was added');
75 // Go to the "fix 404" page and check the listing.
76 $this->drupalGet('admin/config/search/redirect/404');
77 $this->assertText('testing');
78 $this->assertLanguageInTableBody('French');
79 // Check the Language view filter uses the default language filter.
80 $this->assertOption('edit-langcode', 'All');
81 $this->assertOption('edit-langcode', 'en');
82 $this->assertOption('edit-langcode', 'de');
83 $this->assertOption('edit-langcode', 'es');
84 $this->assertOption('edit-langcode', 'fr');
85 $this->assertOption('edit-langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED);
86 $this->clickLink(t('Add redirect'));
88 // Check if we generate correct Add redirect url and if the form is
90 $destination = Url::fromRoute('redirect_404.fix_404')->getInternalPath();
93 'source' => 'testing',
95 'destination' => $destination,
98 $this->assertUrl('admin/config/search/redirect/add', $options);
99 $this->assertFieldByName('redirect_source[0][path]', 'testing');
100 $this->assertOptionSelected('edit-language-0-value', 'fr');
101 // Save the redirect.
102 $edit = ['redirect_redirect[0][uri]' => '/node'];
103 $this->drupalPostForm(NULL, $edit, t('Save'));
104 $this->assertUrl('admin/config/search/redirect/404');
105 $this->assertText('There are no 404 errors to fix.');
106 // Check if the redirect works as expected.
107 $this->assertRedirect('fr/testing', 'fr/node', 'HTTP/1.1 301 Moved Permanently');
109 // Test removing a redirect assignment, visit again the non existing page.
110 $this->drupalGet('admin/config/search/redirect');
111 $this->assertText('testing');
112 $this->assertLanguageInTableBody('French');
113 $this->clickLink('Delete', 0);
114 $this->drupalPostForm(NULL, [], 'Delete');
115 $this->assertUrl('admin/config/search/redirect');
116 $this->assertText('There is no redirect yet.');
117 $this->drupalGet('admin/config/search/redirect/404');
118 $this->assertText('There are no 404 errors to fix.');
119 // Should be listed again in the 404 overview.
120 $this->drupalGet('fr/testing');
121 $this->drupalGet('admin/config/search/redirect/404');
122 $this->assertLanguageInTableBody('French');
123 // Check the error path visit count.
124 $this->assertFieldByXPath('//table/tbody/tr/td[2]', 2);
125 $this->clickLink('Add redirect');
126 // Save the redirect with a different langcode.
127 $this->assertFieldByName('redirect_source[0][path]', 'testing');
128 $this->assertOptionSelected('edit-language-0-value', 'fr');
129 $edit['language[0][value]'] = 'es';
130 $this->drupalPostForm(NULL, $edit, 'Save');
131 $this->assertUrl('admin/config/search/redirect/404');
132 // Should still be listed, redirecting to another language does not resolve
134 $this->assertLanguageInTableBody('French');
135 $this->drupalGet('admin/config/search/redirect');
136 $this->assertLanguageInTableBody('Spanish');
137 // Check if the redirect works as expected.
138 $this->assertRedirect('es/testing', 'es/node', 'HTTP/1.1 301 Moved Permanently');
140 // Visit multiple non existing pages to test the Redirect 404 View.
141 $this->drupalGet('testing1');
142 $this->drupalGet('de/testing2');
143 $this->drupalGet('de/testing2?test=1');
144 $this->drupalGet('de/testing2?test=2');
145 $this->drupalGet('admin/config/search/redirect/404');
146 $this->assertLanguageInTableBody('French');
147 $this->assertLanguageInTableBody('English');
148 $this->assertLanguageInTableBody('German');
149 $this->assertText('testing1');
150 $this->assertText('testing2');
151 $this->assertText('testing2?test=1');
152 $this->assertText('testing2?test=2');
154 // Test the Language view filter.
155 $this->drupalGet('admin/config/search/redirect/404', ['query' => ['langcode' => 'de']]);
156 $this->assertText('English');
157 $this->assertNoLanguageInTableBody('English');
158 $this->assertLanguageInTableBody('German');
159 $this->assertNoText('testing1');
160 $this->assertText('testing2');
161 $this->assertText('testing2?test=1');
162 $this->assertText('testing2?test=2');
163 $this->drupalGet('admin/config/search/redirect/404');
164 $this->assertLanguageInTableBody('English');
165 $this->assertLanguageInTableBody('German');
166 $this->assertText('testing1');
167 $this->assertText('testing2');
168 $this->assertText('testing2?test=1');
169 $this->assertText('testing2?test=2');
170 $this->drupalGet('admin/config/search/redirect/404', ['query' => ['langcode' => 'en']]);
171 $this->assertLanguageInTableBody('English');
172 $this->assertNoLanguageInTableBody('German');
173 $this->assertText('testing1');
174 $this->assertNoText('testing2');
175 $this->assertNoText('testing2?test=1');
176 $this->assertNoText('testing2?test=2');
178 // Assign a redirect to 'testing1'.
179 $this->clickLink('Add redirect');
182 'source' => 'testing1',
184 'destination' => $destination,
187 $this->assertUrl('admin/config/search/redirect/add', $options);
188 $this->assertFieldByName('redirect_source[0][path]', 'testing1');
189 $this->assertOptionSelected('edit-language-0-value', 'en');
190 $edit = ['redirect_redirect[0][uri]' => '/node'];
191 $this->drupalPostForm(NULL, $edit, t('Save'));
192 $this->assertUrl('admin/config/search/redirect/404');
193 $this->assertNoLanguageInTableBody('English');
194 $this->assertLanguageInTableBody('German');
195 $this->drupalGet('admin/config/search/redirect');
196 $this->assertLanguageInTableBody('Spanish');
197 $this->assertLanguageInTableBody('English');
198 // Check if the redirect works as expected.
199 $this->assertRedirect('/testing1', '/node', 'HTTP/1.1 301 Moved Permanently');