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