Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\system\Tests\Routing;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\Cache;
|
Chris@0
|
6 use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
|
Chris@0
|
7 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
8 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
9 use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
Chris@0
|
10 use Drupal\Core\Url;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Functional class for the full integrated routing system.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @group Routing
|
Chris@0
|
16 */
|
Chris@0
|
17 class RouterTest extends WebTestBase {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Modules to enable.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var array
|
Chris@0
|
23 */
|
Chris@0
|
24 public static $modules = ['router_test'];
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Confirms that our FinishResponseSubscriber logic works properly.
|
Chris@0
|
28 */
|
Chris@0
|
29 public function testFinishResponseSubscriber() {
|
Chris@0
|
30 $renderer_required_cache_contexts = ['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'];
|
Chris@0
|
31 $expected_cache_contexts = Cache::mergeContexts($renderer_required_cache_contexts, ['url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT]);
|
Chris@0
|
32
|
Chris@0
|
33 // Confirm that the router can get to a controller.
|
Chris@0
|
34 $this->drupalGet('router_test/test1');
|
Chris@0
|
35 $this->assertRaw('test1', 'The correct string was returned because the route was successful.');
|
Chris@0
|
36 // Check expected headers from FinishResponseSubscriber.
|
Chris@0
|
37 $headers = $this->drupalGetHeaders();
|
Chris@0
|
38 $this->assertEqual($headers['x-ua-compatible'], 'IE=edge');
|
Chris@0
|
39 $this->assertEqual($headers['content-language'], 'en');
|
Chris@0
|
40 $this->assertEqual($headers['x-content-type-options'], 'nosniff');
|
Chris@0
|
41 $this->assertEqual($headers['x-frame-options'], 'SAMEORIGIN');
|
Chris@0
|
42
|
Chris@0
|
43 $this->drupalGet('router_test/test2');
|
Chris@0
|
44 $this->assertRaw('test2', 'The correct string was returned because the route was successful.');
|
Chris@0
|
45 // Check expected headers from FinishResponseSubscriber.
|
Chris@0
|
46 $headers = $this->drupalGetHeaders();
|
Chris@0
|
47 $this->assertEqual($headers['x-drupal-cache-contexts'], implode(' ', $expected_cache_contexts));
|
Chris@0
|
48 $this->assertEqual($headers['x-drupal-cache-tags'], 'config:user.role.anonymous http_response rendered');
|
Chris@0
|
49 // Confirm that the page wrapping is being added, so we're not getting a
|
Chris@0
|
50 // raw body returned.
|
Chris@0
|
51 $this->assertRaw('</html>', 'Page markup was found.');
|
Chris@0
|
52 // In some instances, the subrequest handling may get confused and render
|
Chris@0
|
53 // a page inception style. This test verifies that is not happening.
|
Chris@0
|
54 $this->assertNoPattern('#</body>.*</body>#s', 'There was no double-page effect from a misrendered subrequest.');
|
Chris@0
|
55
|
Chris@0
|
56
|
Chris@0
|
57 // Confirm that route-level access check's cacheability is applied to the
|
Chris@0
|
58 // X-Drupal-Cache-Contexts and X-Drupal-Cache-Tags headers.
|
Chris@0
|
59 // 1. controller result: render array, globally cacheable route access.
|
Chris@0
|
60 $this->drupalGet('router_test/test18');
|
Chris@0
|
61 $headers = $this->drupalGetHeaders();
|
Chris@0
|
62 $this->assertEqual($headers['x-drupal-cache-contexts'], implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, ['url'])));
|
Chris@0
|
63 $this->assertEqual($headers['x-drupal-cache-tags'], 'config:user.role.anonymous foo http_response rendered');
|
Chris@0
|
64 // 2. controller result: render array, per-role cacheable route access.
|
Chris@0
|
65 $this->drupalGet('router_test/test19');
|
Chris@0
|
66 $headers = $this->drupalGetHeaders();
|
Chris@0
|
67 $this->assertEqual($headers['x-drupal-cache-contexts'], implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, ['url', 'user.roles'])));
|
Chris@0
|
68 $this->assertEqual($headers['x-drupal-cache-tags'], 'config:user.role.anonymous foo http_response rendered');
|
Chris@0
|
69 // 3. controller result: Response object, globally cacheable route access.
|
Chris@0
|
70 $this->drupalGet('router_test/test1');
|
Chris@0
|
71 $headers = $this->drupalGetHeaders();
|
Chris@0
|
72 $this->assertFalse(isset($headers['x-drupal-cache-contexts']));
|
Chris@0
|
73 $this->assertFalse(isset($headers['x-drupal-cache-tags']));
|
Chris@0
|
74 // 4. controller result: Response object, per-role cacheable route access.
|
Chris@0
|
75 $this->drupalGet('router_test/test20');
|
Chris@0
|
76 $headers = $this->drupalGetHeaders();
|
Chris@0
|
77 $this->assertFalse(isset($headers['x-drupal-cache-contexts']));
|
Chris@0
|
78 $this->assertFalse(isset($headers['x-drupal-cache-tags']));
|
Chris@0
|
79 // 5. controller result: CacheableResponse object, globally cacheable route access.
|
Chris@0
|
80 $this->drupalGet('router_test/test21');
|
Chris@0
|
81 $headers = $this->drupalGetHeaders();
|
Chris@0
|
82 $this->assertEqual($headers['x-drupal-cache-contexts'], '');
|
Chris@0
|
83 $this->assertEqual($headers['x-drupal-cache-tags'], 'http_response');
|
Chris@0
|
84 // 6. controller result: CacheableResponse object, per-role cacheable route access.
|
Chris@0
|
85 $this->drupalGet('router_test/test22');
|
Chris@0
|
86 $headers = $this->drupalGetHeaders();
|
Chris@0
|
87 $this->assertEqual($headers['x-drupal-cache-contexts'], 'user.roles');
|
Chris@0
|
88 $this->assertEqual($headers['x-drupal-cache-tags'], 'http_response');
|
Chris@0
|
89
|
Chris@0
|
90 // Finally, verify that the X-Drupal-Cache-Contexts and X-Drupal-Cache-Tags
|
Chris@0
|
91 // headers are not sent when their container parameter is set to FALSE.
|
Chris@0
|
92 $this->drupalGet('router_test/test18');
|
Chris@0
|
93 $headers = $this->drupalGetHeaders();
|
Chris@0
|
94 $this->assertTrue(isset($headers['x-drupal-cache-contexts']));
|
Chris@0
|
95 $this->assertTrue(isset($headers['x-drupal-cache-tags']));
|
Chris@0
|
96 $this->setHttpResponseDebugCacheabilityHeaders(FALSE);
|
Chris@0
|
97 $this->drupalGet('router_test/test18');
|
Chris@0
|
98 $headers = $this->drupalGetHeaders();
|
Chris@0
|
99 $this->assertFalse(isset($headers['x-drupal-cache-contexts']));
|
Chris@0
|
100 $this->assertFalse(isset($headers['x-drupal-cache-tags']));
|
Chris@0
|
101 }
|
Chris@0
|
102
|
Chris@0
|
103 /**
|
Chris@0
|
104 * Confirms that multiple routes with the same path do not cause an error.
|
Chris@0
|
105 */
|
Chris@0
|
106 public function testDuplicateRoutePaths() {
|
Chris@0
|
107 // Tests two routes with exactly the same path. The route with the maximum
|
Chris@0
|
108 // fit and lowest sorting route name will match, regardless of the order the
|
Chris@0
|
109 // routes are declared.
|
Chris@0
|
110 // @see \Drupal\Core\Routing\RouteProvider::getRoutesByPath()
|
Chris@0
|
111 $this->drupalGet('router-test/duplicate-path2');
|
Chris@0
|
112 $this->assertResponse(200);
|
Chris@0
|
113 $this->assertRaw('router_test.two_duplicate1');
|
Chris@0
|
114
|
Chris@0
|
115 // Tests three routes with same the path. One of the routes the path has a
|
Chris@0
|
116 // different case.
|
Chris@0
|
117 $this->drupalGet('router-test/case-sensitive-duplicate-path3');
|
Chris@0
|
118 $this->assertResponse(200);
|
Chris@0
|
119 $this->assertRaw('router_test.case_sensitive_duplicate1');
|
Chris@0
|
120 // While case-insensitive matching works, exact matches are preferred.
|
Chris@0
|
121 $this->drupalGet('router-test/case-sensitive-Duplicate-PATH3');
|
Chris@0
|
122 $this->assertResponse(200);
|
Chris@0
|
123 $this->assertRaw('router_test.case_sensitive_duplicate2');
|
Chris@0
|
124 // Test that case-insensitive matching works, falling back to the first
|
Chris@0
|
125 // route defined.
|
Chris@0
|
126 $this->drupalGet('router-test/case-sensitive-Duplicate-Path3');
|
Chris@0
|
127 $this->assertResponse(200);
|
Chris@0
|
128 $this->assertRaw('router_test.case_sensitive_duplicate1');
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 /**
|
Chris@0
|
132 * Confirms that placeholders in paths work correctly.
|
Chris@0
|
133 */
|
Chris@0
|
134 public function testControllerPlaceholders() {
|
Chris@0
|
135 // Test with 0 and a random value.
|
Chris@0
|
136 $values = ["0", $this->randomMachineName()];
|
Chris@0
|
137 foreach ($values as $value) {
|
Chris@0
|
138 $this->drupalGet('router_test/test3/' . $value);
|
Chris@0
|
139 $this->assertResponse(200);
|
Chris@0
|
140 $this->assertRaw($value, 'The correct string was returned because the route was successful.');
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 // Confirm that the page wrapping is being added, so we're not getting a
|
Chris@0
|
144 // raw body returned.
|
Chris@0
|
145 $this->assertRaw('</html>', 'Page markup was found.');
|
Chris@0
|
146
|
Chris@0
|
147 // In some instances, the subrequest handling may get confused and render
|
Chris@0
|
148 // a page inception style. This test verifies that is not happening.
|
Chris@0
|
149 $this->assertNoPattern('#</body>.*</body>#s', 'There was no double-page effect from a misrendered subrequest.');
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 /**
|
Chris@0
|
153 * Confirms that default placeholders in paths work correctly.
|
Chris@0
|
154 */
|
Chris@0
|
155 public function testControllerPlaceholdersDefaultValues() {
|
Chris@0
|
156 $this->drupalGet('router_test/test4');
|
Chris@0
|
157 $this->assertResponse(200);
|
Chris@0
|
158 $this->assertRaw('narf', 'The correct string was returned because the route was successful.');
|
Chris@0
|
159
|
Chris@0
|
160 // Confirm that the page wrapping is being added, so we're not getting a
|
Chris@0
|
161 // raw body returned.
|
Chris@0
|
162 $this->assertRaw('</html>', 'Page markup was found.');
|
Chris@0
|
163
|
Chris@0
|
164 // In some instances, the subrequest handling may get confused and render
|
Chris@0
|
165 // a page inception style. This test verifies that is not happening.
|
Chris@0
|
166 $this->assertNoPattern('#</body>.*</body>#s', 'There was no double-page effect from a misrendered subrequest.');
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 /**
|
Chris@0
|
170 * Confirms that default placeholders in paths work correctly.
|
Chris@0
|
171 */
|
Chris@0
|
172 public function testControllerPlaceholdersDefaultValuesProvided() {
|
Chris@0
|
173 $this->drupalGet('router_test/test4/barf');
|
Chris@0
|
174 $this->assertResponse(200);
|
Chris@0
|
175 $this->assertRaw('barf', 'The correct string was returned because the route was successful.');
|
Chris@0
|
176
|
Chris@0
|
177 // Confirm that the page wrapping is being added, so we're not getting a
|
Chris@0
|
178 // raw body returned.
|
Chris@0
|
179 $this->assertRaw('</html>', 'Page markup was found.');
|
Chris@0
|
180
|
Chris@0
|
181 // In some instances, the subrequest handling may get confused and render
|
Chris@0
|
182 // a page inception style. This test verifies that is not happening.
|
Chris@0
|
183 $this->assertNoPattern('#</body>.*</body>#s', 'There was no double-page effect from a misrendered subrequest.');
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 /**
|
Chris@0
|
187 * Checks that dynamically defined and altered routes work correctly.
|
Chris@0
|
188 *
|
Chris@0
|
189 * @see \Drupal\router_test\RouteSubscriber
|
Chris@0
|
190 */
|
Chris@0
|
191 public function testDynamicRoutes() {
|
Chris@0
|
192 // Test the altered route.
|
Chris@0
|
193 $this->drupalGet('router_test/test6');
|
Chris@0
|
194 $this->assertResponse(200);
|
Chris@0
|
195 $this->assertRaw('test5', 'The correct string was returned because the route was successful.');
|
Chris@0
|
196 }
|
Chris@0
|
197
|
Chris@0
|
198 /**
|
Chris@0
|
199 * Checks that a request with text/html response gets rendered as a page.
|
Chris@0
|
200 */
|
Chris@0
|
201 public function testControllerResolutionPage() {
|
Chris@0
|
202 $this->drupalGet('/router_test/test10');
|
Chris@0
|
203
|
Chris@0
|
204 $this->assertRaw('abcde', 'Correct body was found.');
|
Chris@0
|
205
|
Chris@0
|
206 // Confirm that the page wrapping is being added, so we're not getting a
|
Chris@0
|
207 // raw body returned.
|
Chris@0
|
208 $this->assertRaw('</html>', 'Page markup was found.');
|
Chris@0
|
209
|
Chris@0
|
210 // In some instances, the subrequest handling may get confused and render
|
Chris@0
|
211 // a page inception style. This test verifies that is not happening.
|
Chris@0
|
212 $this->assertNoPattern('#</body>.*</body>#s', 'There was no double-page effect from a misrendered subrequest.');
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 /**
|
Chris@0
|
216 * Checks the generate method on the url generator using the front router.
|
Chris@0
|
217 */
|
Chris@0
|
218 public function testUrlGeneratorFront() {
|
Chris@0
|
219 $front_url = Url::fromRoute('<front>', [], ['absolute' => TRUE]);
|
Chris@0
|
220 // Compare to the site base URL.
|
Chris@0
|
221 $base_url = Url::fromUri('base:/', ['absolute' => TRUE]);
|
Chris@0
|
222 $this->assertIdentical($base_url->toString(), $front_url->toString());
|
Chris@0
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 /**
|
Chris@0
|
226 * Tests that a page trying to match a path will succeed.
|
Chris@0
|
227 */
|
Chris@0
|
228 public function testRouterMatching() {
|
Chris@0
|
229 $this->drupalGet('router_test/test14/1');
|
Chris@0
|
230 $this->assertResponse(200);
|
Chris@0
|
231 $this->assertText('User route "entity.user.canonical" was matched.');
|
Chris@0
|
232
|
Chris@0
|
233 // Try to match a route for a non-existent user.
|
Chris@0
|
234 $this->drupalGet('router_test/test14/2');
|
Chris@0
|
235 $this->assertResponse(200);
|
Chris@0
|
236 $this->assertText('Route not matched.');
|
Chris@0
|
237
|
Chris@0
|
238 // Check that very long paths don't cause an error.
|
Chris@0
|
239 $path = 'router_test/test1';
|
Chris@0
|
240 $suffix = '/d/r/u/p/a/l';
|
Chris@0
|
241 for ($i = 0; $i < 10; $i++) {
|
Chris@0
|
242 $path .= $suffix;
|
Chris@0
|
243 $this->drupalGet($path);
|
Chris@0
|
244 $this->assertResponse(404);
|
Chris@0
|
245 }
|
Chris@0
|
246 }
|
Chris@0
|
247
|
Chris@0
|
248 /**
|
Chris@0
|
249 * Tests that a PSR-7 response works.
|
Chris@0
|
250 */
|
Chris@0
|
251 public function testRouterResponsePsr7() {
|
Chris@0
|
252 $this->drupalGet('/router_test/test23');
|
Chris@0
|
253 $this->assertResponse(200);
|
Chris@0
|
254 $this->assertText('test23');
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * Tests the user account on the DIC.
|
Chris@0
|
259 */
|
Chris@0
|
260 public function testUserAccount() {
|
Chris@0
|
261 $account = $this->drupalCreateUser();
|
Chris@0
|
262 $this->drupalLogin($account);
|
Chris@0
|
263
|
Chris@0
|
264 $second_account = $this->drupalCreateUser();
|
Chris@0
|
265
|
Chris@0
|
266 $this->drupalGet('router_test/test12/' . $second_account->id());
|
Chris@0
|
267 $this->assertText($account->getUsername() . ':' . $second_account->getUsername());
|
Chris@0
|
268 $this->assertEqual($account->id(), $this->loggedInUser->id(), 'Ensure that the user was not changed.');
|
Chris@0
|
269
|
Chris@0
|
270 $this->drupalGet('router_test/test13/' . $second_account->id());
|
Chris@0
|
271 $this->assertText($account->getUsername() . ':' . $second_account->getUsername());
|
Chris@0
|
272 $this->assertEqual($account->id(), $this->loggedInUser->id(), 'Ensure that the user was not changed.');
|
Chris@0
|
273 }
|
Chris@0
|
274
|
Chris@0
|
275 /**
|
Chris@0
|
276 * Checks that an ajax request gets rendered as an Ajax response, by mime.
|
Chris@0
|
277 */
|
Chris@0
|
278 public function testControllerResolutionAjax() {
|
Chris@0
|
279 // This will fail with a JSON parse error if the request is not routed to
|
Chris@0
|
280 // The correct controller.
|
Chris@0
|
281 $this->drupalGetAjax('/router_test/test10');
|
Chris@0
|
282
|
Chris@0
|
283 $this->assertEqual($this->drupalGetHeader('Content-Type'), 'application/json', 'Correct mime content type was returned');
|
Chris@0
|
284
|
Chris@0
|
285 $this->assertRaw('abcde', 'Correct body was found.');
|
Chris@0
|
286 }
|
Chris@0
|
287
|
Chris@0
|
288 /**
|
Chris@0
|
289 * Tests that routes no longer exist for a module that has been uninstalled.
|
Chris@0
|
290 */
|
Chris@0
|
291 public function testRouterUninstallInstall() {
|
Chris@0
|
292 \Drupal::service('module_installer')->uninstall(['router_test']);
|
Chris@0
|
293 \Drupal::service('router.builder')->rebuild();
|
Chris@0
|
294 try {
|
Chris@0
|
295 \Drupal::service('router.route_provider')->getRouteByName('router_test.1');
|
Chris@0
|
296 $this->fail('Route was delete on uninstall.');
|
Chris@0
|
297 }
|
Chris@0
|
298 catch (RouteNotFoundException $e) {
|
Chris@0
|
299 $this->pass('Route was delete on uninstall.');
|
Chris@0
|
300 }
|
Chris@0
|
301 // Install the module again.
|
Chris@0
|
302 \Drupal::service('module_installer')->install(['router_test']);
|
Chris@0
|
303 \Drupal::service('router.builder')->rebuild();
|
Chris@0
|
304 $route = \Drupal::service('router.route_provider')->getRouteByName('router_test.1');
|
Chris@0
|
305 $this->assertNotNull($route, 'Route exists after module installation');
|
Chris@0
|
306 }
|
Chris@0
|
307
|
Chris@0
|
308 /**
|
Chris@0
|
309 * Ensure that multiple leading slashes are redirected.
|
Chris@0
|
310 */
|
Chris@0
|
311 public function testLeadingSlashes() {
|
Chris@0
|
312 $request = $this->container->get('request_stack')->getCurrentRequest();
|
Chris@0
|
313 $url = $request->getUriForPath('//router_test/test1');
|
Chris@0
|
314 $this->drupalGet($url);
|
Chris@0
|
315 $this->assertEqual(1, $this->redirectCount, $url . " redirected to " . $this->url);
|
Chris@0
|
316 $this->assertUrl($request->getUriForPath('/router_test/test1'));
|
Chris@0
|
317
|
Chris@0
|
318 // It should not matter how many leading slashes are used and query strings
|
Chris@0
|
319 // should be preserved.
|
Chris@0
|
320 $url = $request->getUriForPath('/////////////////////////////////////////////////router_test/test1') . '?qs=test';
|
Chris@0
|
321 $this->drupalGet($url);
|
Chris@0
|
322 $this->assertEqual(1, $this->redirectCount, $url . " redirected to " . $this->url);
|
Chris@0
|
323 $this->assertUrl($request->getUriForPath('/router_test/test1') . '?qs=test');
|
Chris@0
|
324 }
|
Chris@0
|
325
|
Chris@0
|
326 }
|