Chris@0: drupalGet('router_test/test1'); Chris@0: $this->assertRaw('test1', 'The correct string was returned because the route was successful.'); Chris@0: // Check expected headers from FinishResponseSubscriber. Chris@0: $headers = $this->drupalGetHeaders(); Chris@0: $this->assertEqual($headers['x-ua-compatible'], 'IE=edge'); Chris@0: $this->assertEqual($headers['content-language'], 'en'); Chris@0: $this->assertEqual($headers['x-content-type-options'], 'nosniff'); Chris@0: $this->assertEqual($headers['x-frame-options'], 'SAMEORIGIN'); Chris@0: Chris@0: $this->drupalGet('router_test/test2'); Chris@0: $this->assertRaw('test2', 'The correct string was returned because the route was successful.'); Chris@0: // Check expected headers from FinishResponseSubscriber. Chris@0: $headers = $this->drupalGetHeaders(); Chris@0: $this->assertEqual($headers['x-drupal-cache-contexts'], implode(' ', $expected_cache_contexts)); Chris@0: $this->assertEqual($headers['x-drupal-cache-tags'], 'config:user.role.anonymous http_response rendered'); Chris@0: // Confirm that the page wrapping is being added, so we're not getting a Chris@0: // raw body returned. Chris@0: $this->assertRaw('', 'Page markup was found.'); Chris@0: // In some instances, the subrequest handling may get confused and render Chris@0: // a page inception style. This test verifies that is not happening. Chris@0: $this->assertNoPattern('#.*#s', 'There was no double-page effect from a misrendered subrequest.'); Chris@0: Chris@0: // Confirm that route-level access check's cacheability is applied to the Chris@0: // X-Drupal-Cache-Contexts and X-Drupal-Cache-Tags headers. Chris@0: // 1. controller result: render array, globally cacheable route access. Chris@0: $this->drupalGet('router_test/test18'); Chris@0: $headers = $this->drupalGetHeaders(); Chris@0: $this->assertEqual($headers['x-drupal-cache-contexts'], implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, ['url']))); Chris@0: $this->assertEqual($headers['x-drupal-cache-tags'], 'config:user.role.anonymous foo http_response rendered'); Chris@0: // 2. controller result: render array, per-role cacheable route access. Chris@0: $this->drupalGet('router_test/test19'); Chris@0: $headers = $this->drupalGetHeaders(); Chris@0: $this->assertEqual($headers['x-drupal-cache-contexts'], implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, ['url', 'user.roles']))); Chris@0: $this->assertEqual($headers['x-drupal-cache-tags'], 'config:user.role.anonymous foo http_response rendered'); Chris@0: // 3. controller result: Response object, globally cacheable route access. Chris@0: $this->drupalGet('router_test/test1'); Chris@0: $headers = $this->drupalGetHeaders(); Chris@0: $this->assertFalse(isset($headers['x-drupal-cache-contexts'])); Chris@0: $this->assertFalse(isset($headers['x-drupal-cache-tags'])); Chris@0: // 4. controller result: Response object, per-role cacheable route access. Chris@0: $this->drupalGet('router_test/test20'); Chris@0: $headers = $this->drupalGetHeaders(); Chris@0: $this->assertFalse(isset($headers['x-drupal-cache-contexts'])); Chris@0: $this->assertFalse(isset($headers['x-drupal-cache-tags'])); Chris@0: // 5. controller result: CacheableResponse object, globally cacheable route access. Chris@0: $this->drupalGet('router_test/test21'); Chris@0: $headers = $this->drupalGetHeaders(); Chris@0: $this->assertEqual($headers['x-drupal-cache-contexts'], ''); Chris@0: $this->assertEqual($headers['x-drupal-cache-tags'], 'http_response'); Chris@0: // 6. controller result: CacheableResponse object, per-role cacheable route access. Chris@0: $this->drupalGet('router_test/test22'); Chris@0: $headers = $this->drupalGetHeaders(); Chris@0: $this->assertEqual($headers['x-drupal-cache-contexts'], 'user.roles'); Chris@0: $this->assertEqual($headers['x-drupal-cache-tags'], 'http_response'); Chris@0: Chris@0: // Finally, verify that the X-Drupal-Cache-Contexts and X-Drupal-Cache-Tags Chris@0: // headers are not sent when their container parameter is set to FALSE. Chris@0: $this->drupalGet('router_test/test18'); Chris@0: $headers = $this->drupalGetHeaders(); Chris@0: $this->assertTrue(isset($headers['x-drupal-cache-contexts'])); Chris@0: $this->assertTrue(isset($headers['x-drupal-cache-tags'])); Chris@0: $this->setHttpResponseDebugCacheabilityHeaders(FALSE); Chris@0: $this->drupalGet('router_test/test18'); Chris@0: $headers = $this->drupalGetHeaders(); Chris@0: $this->assertFalse(isset($headers['x-drupal-cache-contexts'])); Chris@0: $this->assertFalse(isset($headers['x-drupal-cache-tags'])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Confirms that multiple routes with the same path do not cause an error. Chris@0: */ Chris@0: public function testDuplicateRoutePaths() { Chris@0: // Tests two routes with exactly the same path. The route with the maximum Chris@0: // fit and lowest sorting route name will match, regardless of the order the Chris@0: // routes are declared. Chris@0: // @see \Drupal\Core\Routing\RouteProvider::getRoutesByPath() Chris@0: $this->drupalGet('router-test/duplicate-path2'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertRaw('router_test.two_duplicate1'); Chris@0: Chris@0: // Tests three routes with same the path. One of the routes the path has a Chris@0: // different case. Chris@0: $this->drupalGet('router-test/case-sensitive-duplicate-path3'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertRaw('router_test.case_sensitive_duplicate1'); Chris@0: // While case-insensitive matching works, exact matches are preferred. Chris@0: $this->drupalGet('router-test/case-sensitive-Duplicate-PATH3'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertRaw('router_test.case_sensitive_duplicate2'); Chris@0: // Test that case-insensitive matching works, falling back to the first Chris@0: // route defined. Chris@0: $this->drupalGet('router-test/case-sensitive-Duplicate-Path3'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertRaw('router_test.case_sensitive_duplicate1'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Confirms that placeholders in paths work correctly. Chris@0: */ Chris@0: public function testControllerPlaceholders() { Chris@0: // Test with 0 and a random value. Chris@0: $values = ["0", $this->randomMachineName()]; Chris@0: foreach ($values as $value) { Chris@0: $this->drupalGet('router_test/test3/' . $value); Chris@0: $this->assertResponse(200); Chris@0: $this->assertRaw($value, 'The correct string was returned because the route was successful.'); Chris@0: } Chris@0: Chris@0: // Confirm that the page wrapping is being added, so we're not getting a Chris@0: // raw body returned. Chris@0: $this->assertRaw('', 'Page markup was found.'); Chris@0: Chris@0: // In some instances, the subrequest handling may get confused and render Chris@0: // a page inception style. This test verifies that is not happening. Chris@0: $this->assertNoPattern('#.*#s', 'There was no double-page effect from a misrendered subrequest.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Confirms that default placeholders in paths work correctly. Chris@0: */ Chris@0: public function testControllerPlaceholdersDefaultValues() { Chris@0: $this->drupalGet('router_test/test4'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertRaw('narf', 'The correct string was returned because the route was successful.'); Chris@0: Chris@0: // Confirm that the page wrapping is being added, so we're not getting a Chris@0: // raw body returned. Chris@0: $this->assertRaw('', 'Page markup was found.'); Chris@0: Chris@0: // In some instances, the subrequest handling may get confused and render Chris@0: // a page inception style. This test verifies that is not happening. Chris@0: $this->assertNoPattern('#.*#s', 'There was no double-page effect from a misrendered subrequest.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Confirms that default placeholders in paths work correctly. Chris@0: */ Chris@0: public function testControllerPlaceholdersDefaultValuesProvided() { Chris@0: $this->drupalGet('router_test/test4/barf'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertRaw('barf', 'The correct string was returned because the route was successful.'); Chris@0: Chris@0: // Confirm that the page wrapping is being added, so we're not getting a Chris@0: // raw body returned. Chris@0: $this->assertRaw('', 'Page markup was found.'); Chris@0: Chris@0: // In some instances, the subrequest handling may get confused and render Chris@0: // a page inception style. This test verifies that is not happening. Chris@0: $this->assertNoPattern('#.*#s', 'There was no double-page effect from a misrendered subrequest.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that dynamically defined and altered routes work correctly. Chris@0: * Chris@0: * @see \Drupal\router_test\RouteSubscriber Chris@0: */ Chris@0: public function testDynamicRoutes() { Chris@0: // Test the altered route. Chris@0: $this->drupalGet('router_test/test6'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertRaw('test5', 'The correct string was returned because the route was successful.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that a request with text/html response gets rendered as a page. Chris@0: */ Chris@0: public function testControllerResolutionPage() { Chris@0: $this->drupalGet('/router_test/test10'); Chris@0: Chris@0: $this->assertRaw('abcde', 'Correct body was found.'); Chris@0: Chris@0: // Confirm that the page wrapping is being added, so we're not getting a Chris@0: // raw body returned. Chris@0: $this->assertRaw('', 'Page markup was found.'); Chris@0: Chris@0: // In some instances, the subrequest handling may get confused and render Chris@0: // a page inception style. This test verifies that is not happening. Chris@0: $this->assertNoPattern('#.*#s', 'There was no double-page effect from a misrendered subrequest.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks the generate method on the url generator using the front router. Chris@0: */ Chris@0: public function testUrlGeneratorFront() { Chris@0: $front_url = Url::fromRoute('', [], ['absolute' => TRUE]); Chris@0: // Compare to the site base URL. Chris@0: $base_url = Url::fromUri('base:/', ['absolute' => TRUE]); Chris@0: $this->assertIdentical($base_url->toString(), $front_url->toString()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that a page trying to match a path will succeed. Chris@0: */ Chris@0: public function testRouterMatching() { Chris@0: $this->drupalGet('router_test/test14/1'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText('User route "entity.user.canonical" was matched.'); Chris@0: Chris@0: // Try to match a route for a non-existent user. Chris@0: $this->drupalGet('router_test/test14/2'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText('Route not matched.'); Chris@0: Chris@0: // Check that very long paths don't cause an error. Chris@0: $path = 'router_test/test1'; Chris@0: $suffix = '/d/r/u/p/a/l'; Chris@0: for ($i = 0; $i < 10; $i++) { Chris@0: $path .= $suffix; Chris@0: $this->drupalGet($path); Chris@0: $this->assertResponse(404); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that a PSR-7 response works. Chris@0: */ Chris@0: public function testRouterResponsePsr7() { Chris@0: $this->drupalGet('/router_test/test23'); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText('test23'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the user account on the DIC. Chris@0: */ Chris@0: public function testUserAccount() { Chris@0: $account = $this->drupalCreateUser(); Chris@0: $this->drupalLogin($account); Chris@0: Chris@0: $second_account = $this->drupalCreateUser(); Chris@0: Chris@0: $this->drupalGet('router_test/test12/' . $second_account->id()); Chris@0: $this->assertText($account->getUsername() . ':' . $second_account->getUsername()); Chris@0: $this->assertEqual($account->id(), $this->loggedInUser->id(), 'Ensure that the user was not changed.'); Chris@0: Chris@0: $this->drupalGet('router_test/test13/' . $second_account->id()); Chris@0: $this->assertText($account->getUsername() . ':' . $second_account->getUsername()); Chris@0: $this->assertEqual($account->id(), $this->loggedInUser->id(), 'Ensure that the user was not changed.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks that an ajax request gets rendered as an Ajax response, by mime. Chris@0: */ Chris@0: public function testControllerResolutionAjax() { Chris@0: // This will fail with a JSON parse error if the request is not routed to Chris@0: // The correct controller. Chris@0: $this->drupalGetAjax('/router_test/test10'); Chris@0: Chris@0: $this->assertEqual($this->drupalGetHeader('Content-Type'), 'application/json', 'Correct mime content type was returned'); Chris@0: Chris@0: $this->assertRaw('abcde', 'Correct body was found.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that routes no longer exist for a module that has been uninstalled. Chris@0: */ Chris@0: public function testRouterUninstallInstall() { Chris@0: \Drupal::service('module_installer')->uninstall(['router_test']); Chris@0: \Drupal::service('router.builder')->rebuild(); Chris@0: try { Chris@0: \Drupal::service('router.route_provider')->getRouteByName('router_test.1'); Chris@0: $this->fail('Route was delete on uninstall.'); Chris@0: } Chris@0: catch (RouteNotFoundException $e) { Chris@0: $this->pass('Route was delete on uninstall.'); Chris@0: } Chris@0: // Install the module again. Chris@0: \Drupal::service('module_installer')->install(['router_test']); Chris@0: \Drupal::service('router.builder')->rebuild(); Chris@0: $route = \Drupal::service('router.route_provider')->getRouteByName('router_test.1'); Chris@0: $this->assertNotNull($route, 'Route exists after module installation'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensure that multiple leading slashes are redirected. Chris@0: */ Chris@0: public function testLeadingSlashes() { Chris@0: $request = $this->container->get('request_stack')->getCurrentRequest(); Chris@0: $url = $request->getUriForPath('//router_test/test1'); Chris@0: $this->drupalGet($url); Chris@0: $this->assertEqual(1, $this->redirectCount, $url . " redirected to " . $this->url); Chris@0: $this->assertUrl($request->getUriForPath('/router_test/test1')); Chris@0: Chris@0: // It should not matter how many leading slashes are used and query strings Chris@0: // should be preserved. Chris@0: $url = $request->getUriForPath('/////////////////////////////////////////////////router_test/test1') . '?qs=test'; Chris@0: $this->drupalGet($url); Chris@0: $this->assertEqual(1, $this->redirectCount, $url . " redirected to " . $this->url); Chris@0: $this->assertUrl($request->getUriForPath('/router_test/test1') . '?qs=test'); Chris@0: } Chris@0: Chris@0: }