View source
<?php
namespace Drupal\Tests\system\Functional\Routing;
use Drupal\Core\Cache\Cache;
use Drupal\Core\EventSubscriber\MainContentViewSubscriber;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Drupal\Core\Url;
class RouterTest extends BrowserTestBase {
protected static $modules = [
'router_test',
];
protected $defaultTheme = 'stark';
public function testFinishResponseSubscriber() {
$renderer_required_cache_contexts = [
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
];
$expected_cache_contexts = Cache::mergeContexts($renderer_required_cache_contexts, [
'url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT,
]);
sort($expected_cache_contexts);
$this
->drupalGet('router_test/test1');
$this
->assertSession()
->pageTextContains('test1');
$session = $this
->getSession();
$this
->assertSession()
->responseHeaderEquals('X-UA-Compatible', 'IE=edge');
$this
->assertSession()
->responseHeaderEquals('Content-language', 'en');
$this
->assertSession()
->responseHeaderEquals('X-Content-Type-Options', 'nosniff');
$this
->assertSession()
->responseHeaderEquals('X-Frame-Options', 'SAMEORIGIN');
$this
->assertSession()
->responseHeaderDoesNotExist('Vary');
$this
->drupalGet('router_test/test2');
$this
->assertSession()
->pageTextContains('test2');
$headers = $session
->getResponseHeaders();
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Contexts', implode(' ', $expected_cache_contexts));
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Tags', 'config:user.role.anonymous http_response rendered');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Max-Age', '-1 (Permanent)');
$this
->assertSession()
->responseContains('</html>');
$this
->assertSession()
->responseNotMatches('#</body>.*</body>#s');
$this
->drupalGet('router_test/test18');
$expected_cache_contexts = Cache::mergeContexts($renderer_required_cache_contexts, [
'url',
]);
sort($expected_cache_contexts);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Contexts', implode(' ', $expected_cache_contexts));
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Tags', 'config:user.role.anonymous foo http_response rendered');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Max-Age', '60');
$this
->drupalGet('router_test/test19');
$expected_cache_contexts = Cache::mergeContexts($renderer_required_cache_contexts, [
'url',
'user.roles',
]);
sort($expected_cache_contexts);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Contexts', implode(' ', $expected_cache_contexts));
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Tags', 'config:user.role.anonymous foo http_response rendered');
$this
->drupalGet('router_test/test1');
$headers = $session
->getResponseHeaders();
$this
->assertFalse(isset($headers['X-Drupal-Cache-Contexts']));
$this
->assertFalse(isset($headers['X-Drupal-Cache-Tags']));
$this
->assertFalse(isset($headers['X-Drupal-Cache-Max-Age']));
$this
->drupalGet('router_test/test20');
$headers = $session
->getResponseHeaders();
$this
->assertFalse(isset($headers['X-Drupal-Cache-Contexts']));
$this
->assertFalse(isset($headers['X-Drupal-Cache-Tags']));
$this
->assertFalse(isset($headers['X-Drupal-Cache-Max-Age']));
$this
->drupalGet('router_test/test21');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Contexts', '');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response');
$this
->drupalGet('router_test/test22');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Contexts', 'user.roles');
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache-Tags', 'http_response');
$this
->drupalGet('router_test/test18');
$headers = $session
->getResponseHeaders();
$this
->assertTrue(isset($headers['X-Drupal-Cache-Contexts']));
$this
->assertTrue(isset($headers['X-Drupal-Cache-Tags']));
$this
->assertTrue(isset($headers['X-Drupal-Cache-Max-Age']));
$this
->setContainerParameter('http.response.debug_cacheability_headers', FALSE);
$this
->rebuildContainer();
$this
->resetAll();
$this
->drupalGet('router_test/test18');
$headers = $session
->getResponseHeaders();
$this
->assertFalse(isset($headers['X-Drupal-Cache-Contexts']));
$this
->assertFalse(isset($headers['X-Drupal-Cache-Tags']));
$this
->assertFalse(isset($headers['X-Drupal-Cache-Max-Age']));
}
public function testDuplicateRoutePaths() {
$this
->drupalGet('router-test/duplicate-path2');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains('router_test.two_duplicate1');
$this
->drupalGet('router-test/case-sensitive-duplicate-path3');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains('router_test.case_sensitive_duplicate1');
$this
->drupalGet('router-test/case-sensitive-Duplicate-PATH3');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains('router_test.case_sensitive_duplicate2');
$this
->drupalGet('router-test/case-sensitive-Duplicate-Path3');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseContains('router_test.case_sensitive_duplicate1');
}
public function testControllerPlaceholders() {
$values = [
"0",
$this
->randomMachineName(),
];
foreach ($values as $value) {
$this
->drupalGet('router_test/test3/' . $value);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains($value);
}
$this
->assertSession()
->responseContains('</html>');
$this
->assertSession()
->responseNotMatches('#</body>.*</body>#s');
}
public function testControllerPlaceholdersDefaultValues() {
$this
->drupalGet('router_test/test4');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('narf');
$this
->assertSession()
->responseContains('</html>');
$this
->assertSession()
->responseNotMatches('#</body>.*</body>#s');
}
public function testControllerPlaceholdersDefaultValuesProvided() {
$this
->drupalGet('router_test/test4/barf');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('barf');
$this
->assertSession()
->responseContains('</html>');
$this
->assertSession()
->responseNotMatches('#</body>.*</body>#s');
}
public function testDynamicRoutes() {
$this
->drupalGet('router_test/test6');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('test5');
}
public function testControllerResolutionPage() {
$this
->drupalGet('/router_test/test10');
$this
->assertSession()
->pageTextContains('abcde');
$this
->assertSession()
->responseContains('</html>');
$this
->assertSession()
->responseNotMatches('#</body>.*</body>#s');
}
public function testUrlGeneratorFront() {
$front_url = Url::fromRoute('<front>', [], [
'absolute' => TRUE,
]);
$base_url = Url::fromUri('base:/', [
'absolute' => TRUE,
]);
$this
->assertSame($base_url
->toString(), $front_url
->toString());
}
public function testRouterMatching() {
$this
->drupalGet('router_test/test14/1');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('User route "entity.user.canonical" was matched.');
$this
->drupalGet('router_test/test14/2');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Route not matched.');
$path = 'router_test/test1';
$suffix = '/d/r/u/p/a/l';
for ($i = 0; $i < 10; $i++) {
$path .= $suffix;
$this
->drupalGet($path);
$this
->assertSession()
->statusCodeEquals(404);
}
}
public function testRouterResponsePsr7() {
$this
->drupalGet('/router_test/test23');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('test23');
}
public function testUserAccount() {
$account = $this
->drupalCreateUser();
$this
->drupalLogin($account);
$second_account = $this
->drupalCreateUser();
$this
->drupalGet('router_test/test12/' . $second_account
->id());
$this
->assertSession()
->pageTextContains($account
->getAccountName() . ':' . $second_account
->getAccountName());
$this
->assertEquals($this->loggedInUser
->id(), $account
->id(), 'Ensure that the user was not changed.');
$this
->drupalGet('router_test/test13/' . $second_account
->id());
$this
->assertSession()
->pageTextContains($account
->getAccountName() . ':' . $second_account
->getAccountName());
$this
->assertEquals($this->loggedInUser
->id(), $account
->id(), 'Ensure that the user was not changed.');
}
public function testControllerResolutionAjax() {
$options['query'][MainContentViewSubscriber::WRAPPER_FORMAT] = 'drupal_ajax';
$headers[] = 'X-Requested-With: XMLHttpRequest';
$this
->drupalGet('/router_test/test10', $options, $headers);
$this
->assertSession()
->responseHeaderEquals('Content-Type', 'application/json');
$this
->assertSession()
->pageTextContains('abcde');
}
public function testRouterUninstallInstall() {
\Drupal::service('module_installer')
->uninstall([
'router_test',
]);
try {
\Drupal::service('router.route_provider')
->getRouteByName('router_test.1');
$this
->fail('Route was delete on uninstall.');
} catch (RouteNotFoundException $e) {
}
\Drupal::service('module_installer')
->install([
'router_test',
]);
$route = \Drupal::service('router.route_provider')
->getRouteByName('router_test.1');
$this
->assertNotNull($route, 'Route exists after module installation');
}
public function testLeadingSlashes() {
$request = $this->container
->get('request_stack')
->getCurrentRequest();
$url = $request
->getUriForPath('//router_test/test1');
$this
->drupalGet($url);
$this
->assertSession()
->addressEquals($request
->getUriForPath('/router_test/test1'));
$url = $request
->getUriForPath('/////////////////////////////////////////////////router_test/test1') . '?qs=test';
$this
->drupalGet($url);
$this
->assertSession()
->addressEquals($request
->getUriForPath('/router_test/test1') . '?qs=test');
$url = $request
->getUriForPath('/////////////////////////////////////////////////router_test/test1') . '?qs=test&destination=http://www.example.com%5c@drupal8alt.test';
$this
->drupalGet($url);
$this
->assertSession()
->addressEquals($request
->getUriForPath('/router_test/test1') . '?qs=test');
}
}