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