annotate core/modules/system/src/Tests/Routing/RouterTest.php @ 14:1fec387a4317

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