c7657447c00f798f1e3435571bfcba40d48a9230
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_404 / src / Tests / Fix404RedirectUILanguageTest.php
1 <?php
2
3 namespace Drupal\redirect_404\Tests;
4
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\Core\Url;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\redirect\Tests\AssertRedirectTrait;
9
10 /**
11  * UI tests for redirect_404 module with language and content translation.
12  *
13  * This runs the exact same tests as Fix404RedirectUITest, but with both
14  * language and content translation modules enabled.
15  *
16  * @group redirect_404
17  */
18 class Fix404RedirectUILanguageTest extends Redirect404TestBase {
19
20   use AssertRedirectTrait;
21
22   /**
23    * Additional modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = ['language'];
28
29   /**
30    * Admin user's permissions for this test.
31    *
32    * @var array
33    */
34   protected $adminPermissions = [
35     'administer redirects',
36     'administer redirect settings',
37     'access content',
38     'bypass node access',
39     'create url aliases',
40     'administer url aliases',
41     'administer languages',
42   ];
43
44   /**
45    * {@inheritdoc}
46    */
47   public function setUp() {
48     parent::setUp();
49
50     // Enable some languages for this test.
51     $language = ConfigurableLanguage::createFromLangcode('de');
52     $language->save();
53     $language = ConfigurableLanguage::createFromLangcode('es');
54     $language->save();
55     $language = ConfigurableLanguage::createFromLangcode('fr');
56     $language->save();
57   }
58
59   /**
60    * Tests the fix 404 pages workflow with language and content translation.
61    */
62   public function testFix404RedirectList() {
63     // Visit a non existing page to have the 404 redirect_error entry.
64     $this->drupalGet('fr/testing');
65
66     $redirect = \Drupal::database()->select('redirect_404')
67       ->fields('redirect_404')
68       ->condition('path', '/testing')
69       ->execute()
70       ->fetchAll();
71     if (count($redirect) == 0) {
72       $this->fail('No record was added');
73     }
74
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'));
87
88     // Check if we generate correct Add redirect url and if the form is
89     // pre-filled.
90     $destination = Url::fromRoute('redirect_404.fix_404')->getInternalPath();
91     $options = [
92       'query' => [
93         'source' => 'testing',
94         'language' => 'fr',
95         'destination' => $destination,
96       ]
97     ];
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');
108
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
133     // the path.
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');
139
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');
153
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');
177
178     // Assign a redirect to 'testing1'.
179     $this->clickLink('Add redirect');
180     $options = [
181       'query' => [
182         'source' => 'testing1',
183         'language' => 'en',
184         'destination' => $destination,
185       ]
186     ];
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');
200   }
201
202 }