Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / redirect / modules / redirect_404 / src / Tests / Fix404RedirectUITest.php
1 <?php
2
3 namespace Drupal\redirect_404\Tests;
4
5 use Drupal\Component\Utility\UrlHelper;
6 use Drupal\Core\Url;
7
8 /**
9  * UI tests for redirect_404 module.
10  *
11  * @group redirect_404
12  */
13 class Fix404RedirectUITest extends Redirect404TestBase {
14
15   /**
16    * Tests the fix 404 pages workflow.
17    */
18   public function testFix404Pages() {
19     // Visit a non existing page to have the 404 redirect_error entry.
20     $this->drupalGet('non-existing0');
21
22     // Go to the "fix 404" page and check the listing.
23     $this->drupalGet('admin/config/search/redirect/404');
24     $this->assertText('non-existing0');
25     $this->clickLink(t('Add redirect'));
26
27     // Check if we generate correct Add redirect url and if the form is
28     // pre-filled.
29     $destination = Url::fromRoute('redirect_404.fix_404')->getInternalPath();
30     $expected_query = [
31       'destination' => $destination,
32       'language' => 'en',
33       'source' => 'non-existing0',
34     ];
35     $parsed_url = UrlHelper::parse($this->getUrl());
36     $this->assertEqual(Url::fromRoute('redirect.add')->setAbsolute()->toString(), $parsed_url['path']);
37     $this->assertEqual($expected_query, $parsed_url['query']);
38     $this->assertFieldByName('redirect_source[0][path]', 'non-existing0');
39     // Save the redirect.
40     $edit = ['redirect_redirect[0][uri]' => '/node'];
41     $this->drupalPostForm(NULL, $edit, t('Save'));
42     $this->assertUrl('admin/config/search/redirect/404');
43     $this->assertText('There are no 404 errors to fix.');
44     // Check if the redirect works as expected.
45     $this->drupalGet('non-existing0');
46     $this->assertUrl('node');
47
48     // Test removing a redirect assignment, visit again the non existing page.
49     $this->drupalGet('admin/config/search/redirect');
50     $this->assertText('non-existing0');
51     $this->clickLink('Delete', 0);
52     $this->drupalPostForm(NULL, [], 'Delete');
53     $this->assertUrl('admin/config/search/redirect');
54     $this->assertText('There is no redirect yet.');
55     $this->drupalGet('admin/config/search/redirect/404');
56     $this->assertText('There are no 404 errors to fix.');
57     // Should be listed again in the 404 overview.
58     $this->drupalGet('non-existing0');
59     $this->drupalGet('admin/config/search/redirect/404');
60     $this->assertText('non-existing0');
61
62     // Visit multiple non existing pages to test the Redirect 404 View.
63     $this->drupalGet('non-existing0?test=1');
64     $this->drupalGet('non-existing0?test=2');
65     $this->drupalGet('non-existing1');
66     $this->drupalGet('non-existing2');
67     $this->drupalGet('admin/config/search/redirect/404');
68     $this->assertText('non-existing0?test=1');
69     $this->assertText('non-existing0?test=2');
70     $this->assertText('non-existing0');
71     $this->assertText('non-existing1');
72     $this->assertText('non-existing2');
73
74     // Test the Path view filter.
75     $this->drupalGet('admin/config/search/redirect/404', ['query' => ['path' => 'test=']]);
76     $this->assertText('non-existing0?test=1');
77     $this->assertText('non-existing0?test=2');
78     $this->assertNoText('non-existing1');
79     $this->assertNoText('non-existing2');
80     $this->drupalGet('admin/config/search/redirect/404', ['query' => ['path' => 'existing1']]);
81     $this->assertNoText('non-existing0?test=1');
82     $this->assertNoText('non-existing0?test=2');
83     $this->assertNoText('non-existing0');
84     $this->assertText('non-existing1');
85     $this->assertNoText('non-existing2');
86     $this->drupalGet('admin/config/search/redirect/404');
87     $this->assertText('non-existing0?test=1');
88     $this->assertText('non-existing0?test=2');
89     $this->assertText('non-existing0');
90     $this->assertText('non-existing1');
91     $this->assertText('non-existing2');
92     $this->drupalGet('admin/config/search/redirect/404', ['query' => ['path' => 'g2']]);
93     $this->assertNoText('non-existing0?test=1');
94     $this->assertNoText('non-existing0?test=2');
95     $this->assertNoText('non-existing0');
96     $this->assertNoText('non-existing1');
97     $this->assertText('non-existing2');
98
99     // Assign a redirect to 'non-existing2'.
100     $this->clickLink('Add redirect');
101     $expected_query = [
102       'source' => 'non-existing2',
103       'language' => 'en',
104       'destination' => $destination,
105     ];
106     $parsed_url = UrlHelper::parse($this->getUrl());
107     $this->assertEqual(Url::fromRoute('redirect.add')->setAbsolute()->toString(), $parsed_url['path']);
108     $this->assertEqual($expected_query, $parsed_url['query']);
109     $this->assertFieldByName('redirect_source[0][path]', 'non-existing2');
110     $this->drupalPostForm(NULL, $edit, t('Save'));
111     $this->assertUrl('admin/config/search/redirect/404');
112     $this->assertText('non-existing0?test=1');
113     $this->assertText('non-existing0?test=2');
114     $this->assertText('non-existing0');
115     $this->assertText('non-existing1');
116     $this->assertNoText('non-existing2');
117     // Check if the redirect works as expected.
118     $this->drupalGet('admin/config/search/redirect');
119     $this->assertText('non-existing2');
120   }
121
122   /**
123    * Tests the redirect ignore pages.
124    */
125   public function testIgnorePages() {
126     // Create two nodes.
127     $node1 = $this->drupalCreateNode(['type' => 'page']);
128     $node2 = $this->drupalCreateNode(['type' => 'page']);
129
130     // Set some pages to be ignored just for the test.
131     $node_to_ignore = '/node/' . $node1->id() . '/test';
132     $terms_to_ignore = '/term/*';
133     $pages = $node_to_ignore . "\r\n" . $terms_to_ignore;
134     \Drupal::configFactory()
135       ->getEditable('redirect_404.settings')
136       ->set('pages', $pages)
137       ->save();
138
139     // Visit ignored or non existing pages.
140     $this->drupalGet('node/' . $node1->id() . '/test');
141     $this->drupalGet('term/foo');
142     $this->drupalGet('term/1');
143     // Go to the "fix 404" page and check there are no 404 entries.
144     $this->drupalGet('admin/config/search/redirect/404');
145     $this->assertNoText('node/' . $node1->id() . '/test');
146     $this->assertNoText('term/foo');
147     $this->assertNoText('term/1');
148
149     // Visit non existing but 'unignored' page.
150     $this->drupalGet('node/' . $node2->id() . '/test');
151     // Go to the "fix 404" page and check there is a 404 entry.
152     $this->drupalGet('admin/config/search/redirect/404');
153     $this->assertText('node/' . $node2->id() . '/test');
154
155     // Add this 404 entry to the 'ignore path' list, assert it works properly.
156     $path_to_ignore = '/node/' . $node2->id() . '/test';
157     $destination = '&destination=admin/config/search/redirect/404';
158     $this->clickLink('Ignore');
159     $this->assertUrl('admin/config/search/redirect/settings?ignore=' . $path_to_ignore . $destination);
160     $this->assertText('Resolved the path ' . $path_to_ignore . ' in the database. Please check the ignored list and save the settings.');
161     $xpath = $this->xpath('//*[@id="edit-ignore-pages"]')[0]->asXML();
162     $this->assertTrue(strpos($xpath, $node_to_ignore), $node_to_ignore . " in 'Path to ignore' found");
163     $this->assertTrue(strpos($xpath, $terms_to_ignore), $terms_to_ignore . " in 'Path to ignore' found");
164     $this->assertTrue(strpos($xpath, $path_to_ignore), $path_to_ignore . " in 'Path to ignore' found");
165
166     // Save the path with wildcard, but omitting the leading slash.
167     $nodes_to_ignore = 'node/*';
168     $edit = ['ignore_pages' => $nodes_to_ignore . "\r\n" . $terms_to_ignore];
169     $this->drupalPostForm(NULL, $edit, 'Save configuration');
170     // Should redirect to 'Fix 404'. Check the 404 entry is not shown anymore.
171     $this->assertUrl('admin/config/search/redirect/404');
172     $this->assertText('Configuration was saved.');
173     $this->assertNoText('node/' . $node2->id() . '/test');
174     $this->assertText('There are no 404 errors to fix.');
175
176     // Go back to the settings to check the 'Path to ignore' configurations.
177     $this->drupalGet('admin/config/search/redirect/settings');
178     $xpath = $this->xpath('//*[@id="edit-ignore-pages"]')[0]->asXML();
179     // Check that the new page to ignore has been saved with leading slash.
180     $this->assertTrue(strpos($xpath, '/' . $nodes_to_ignore), '/' . $nodes_to_ignore . " in 'Path to ignore' found");
181     $this->assertTrue(strpos($xpath, $terms_to_ignore), $terms_to_ignore . " in 'Path to ignore' found");
182     $this->assertFalse(strpos($xpath, $node_to_ignore), $node_to_ignore . " in 'Path to ignore' found");
183     $this->assertFalse(strpos($xpath, $path_to_ignore), $path_to_ignore . " in 'Path to ignore' found");
184   }
185
186 }