RouterTest.php 11.6 KB
Newer Older
1 2
<?php

Crell's avatar
Crell committed
3 4
/**
 * @file
5
 * Contains \Drupal\system\Tests\Routing\RouterTest.
Crell's avatar
Crell committed
6 7
 */

8 9
namespace Drupal\system\Tests\Routing;

10 11
use Drupal\Core\Cache\Cache;
use Drupal\Core\Language\LanguageInterface;
12
use Drupal\simpletest\WebTestBase;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\Routing\Exception\RouteNotFoundException;
15 16 17

/**
 * Functional class for the full integrated routing system.
18 19
 *
 * @group Routing
20 21 22 23 24 25 26 27
 */
class RouterTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
28
  public static $modules = array('router_test');
29

30
  /**
31
   * Confirms that our FinishResponseSubscriber logic works properly.
32
   */
33
  public function testFinishResponseSubscriber() {
34
    $renderer_required_cache_contexts = ['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'];
35

36
    // Confirm that the router can get to a controller.
37 38
    $this->drupalGet('router_test/test1');
    $this->assertRaw('test1', 'The correct string was returned because the route was successful.');
39
    // Check expected headers from FinishResponseSubscriber.
40
    $headers = $this->drupalGetHeaders();
41
    $this->assertEqual($headers['x-ua-compatible'], 'IE=edge');
42 43
    $this->assertEqual($headers['content-language'], 'en');
    $this->assertEqual($headers['x-content-type-options'], 'nosniff');
44
    $this->assertEqual($headers['x-frame-options'], 'SAMEORIGIN');
45

46 47
    $this->drupalGet('router_test/test2');
    $this->assertRaw('test2', 'The correct string was returned because the route was successful.');
48 49 50
    // Check expected headers from FinishResponseSubscriber.
    $headers = $this->drupalGetHeaders();
    $this->assertEqual($headers['x-drupal-cache-contexts'], implode(' ', $renderer_required_cache_contexts));
51
    $this->assertEqual($headers['x-drupal-cache-tags'], 'config:user.role.anonymous rendered');
52 53
    // Confirm that the page wrapping is being added, so we're not getting a
    // raw body returned.
54
    $this->assertRaw('</html>', 'Page markup was found.');
55 56 57
    // In some instances, the subrequest handling may get confused and render
    // a page inception style.  This test verifies that is not happening.
    $this->assertNoPattern('#</body>.*</body>#s', 'There was no double-page effect from a misrendered subrequest.');
58 59 60 61 62 63 64 65


    // Confirm that route-level access check's cacheability is applied to the
    // X-Drupal-Cache-Contexts and X-Drupal-Cache-Tags headers.
    // 1. controller result: render array, globally cacheable route access.
    $this->drupalGet('router_test/test18');
    $headers = $this->drupalGetHeaders();
    $this->assertEqual($headers['x-drupal-cache-contexts'], implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, ['url'])));
66
    $this->assertEqual($headers['x-drupal-cache-tags'], 'config:user.role.anonymous foo rendered');
67 68 69 70
    // 2. controller result: render array, per-role cacheable route access.
    $this->drupalGet('router_test/test19');
    $headers = $this->drupalGetHeaders();
    $this->assertEqual($headers['x-drupal-cache-contexts'], implode(' ', Cache::mergeContexts($renderer_required_cache_contexts, ['url', 'user.roles'])));
71
    $this->assertEqual($headers['x-drupal-cache-tags'], 'config:user.role.anonymous foo rendered');
72 73 74
    // 3. controller result: Response object, globally cacheable route access.
    $this->drupalGet('router_test/test1');
    $headers = $this->drupalGetHeaders();
75 76
    $this->assertFalse(isset($headers['x-drupal-cache-contexts']));
    $this->assertFalse(isset($headers['x-drupal-cache-tags']));
77 78 79
    // 4. controller result: Response object, per-role cacheable route access.
    $this->drupalGet('router_test/test20');
    $headers = $this->drupalGetHeaders();
80 81 82 83 84 85 86 87 88 89
    $this->assertFalse(isset($headers['x-drupal-cache-contexts']));
    $this->assertFalse(isset($headers['x-drupal-cache-tags']));
    // 5. controller result: CacheableResponse object, globally cacheable route access.
    $this->drupalGet('router_test/test21');
    $headers = $this->drupalGetHeaders();
    $this->assertEqual($headers['x-drupal-cache-contexts'], '');
    $this->assertEqual($headers['x-drupal-cache-tags'], '');
    // 6. controller result: CacheableResponse object, per-role cacheable route access.
    $this->drupalGet('router_test/test22');
    $headers = $this->drupalGetHeaders();
90 91
    $this->assertEqual($headers['x-drupal-cache-contexts'], 'user.roles');
    $this->assertEqual($headers['x-drupal-cache-tags'], '');
92 93
  }

94 95 96
  /**
   * Confirms that placeholders in paths work correctly.
   */
97
  public function testControllerPlaceholders() {
98
    // Test with 0 and a random value.
99
    $values = array("0", $this->randomMachineName());
100 101 102 103 104
    foreach ($values as $value) {
      $this->drupalGet('router_test/test3/' . $value);
      $this->assertResponse(200);
      $this->assertRaw($value, 'The correct string was returned because the route was successful.');
    }
105 106 107

    // Confirm that the page wrapping is being added, so we're not getting a
    // raw body returned.
108
    $this->assertRaw('</html>', 'Page markup was found.');
109 110 111 112

    // In some instances, the subrequest handling may get confused and render
    // a page inception style.  This test verifies that is not happening.
    $this->assertNoPattern('#</body>.*</body>#s', 'There was no double-page effect from a misrendered subrequest.');
113 114
  }

115 116 117 118 119
  /**
   * Confirms that default placeholders in paths work correctly.
   */
  public function testControllerPlaceholdersDefaultValues() {
    $this->drupalGet('router_test/test4');
120
    $this->assertResponse(200);
121
    $this->assertRaw('narf', 'The correct string was returned because the route was successful.');
122 123

    // Confirm that the page wrapping is being added, so we're not getting a
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    // raw body returned.
    $this->assertRaw('</html>', 'Page markup was found.');

    // In some instances, the subrequest handling may get confused and render
    // a page inception style.  This test verifies that is not happening.
    $this->assertNoPattern('#</body>.*</body>#s', 'There was no double-page effect from a misrendered subrequest.');
  }

  /**
   * Confirms that default placeholders in paths work correctly.
   */
  public function testControllerPlaceholdersDefaultValuesProvided() {
    $this->drupalGet('router_test/test4/barf');
    $this->assertResponse(200);
    $this->assertRaw('barf', 'The correct string was returned because the route was successful.');

    // Confirm that the page wrapping is being added, so we're not getting a
141
    // raw body returned.
142
    $this->assertRaw('</html>', 'Page markup was found.');
143 144 145 146

    // In some instances, the subrequest handling may get confused and render
    // a page inception style.  This test verifies that is not happening.
    $this->assertNoPattern('#</body>.*</body>#s', 'There was no double-page effect from a misrendered subrequest.');
147 148
  }

149 150 151 152 153 154 155 156 157 158 159
  /**
   * Checks that dynamically defined and altered routes work correctly.
   *
   * @see \Drupal\router_test\RouteSubscriber
   */
  public function testDynamicRoutes() {
    // Test the altered route.
    $this->drupalGet('router_test/test6');
    $this->assertResponse(200);
    $this->assertRaw('test5', 'The correct string was returned because the route was successful.');
  }
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

  /**
   * Checks that a request with text/html response gets rendered as a page.
   */
  public function testControllerResolutionPage() {
    $this->drupalGet('/router_test/test10');

    $this->assertRaw('abcde', 'Correct body was found.');

    // Confirm that the page wrapping is being added, so we're not getting a
    // raw body returned.
    $this->assertRaw('</html>', 'Page markup was found.');

    // In some instances, the subrequest handling may get confused and render
    // a page inception style. This test verifies that is not happening.
    $this->assertNoPattern('#</body>.*</body>#s', 'There was no double-page effect from a misrendered subrequest.');
  }

178 179 180 181 182 183 184 185 186 187
  /**
   * Checks the generate method on the url generator using the front router.
   */
  public function testUrlGeneratorFront() {
    global $base_path;

    $this->assertEqual($this->container->get('url_generator')->generate('<front>'), $base_path);
    $this->assertEqual($this->container->get('url_generator')->generateFromPath('<front>'), $base_path);
  }

188 189 190 191 192 193
  /**
   * Tests that a page trying to match a path will succeed.
   */
  public function testRouterMatching() {
    $this->drupalGet('router_test/test14/1');
    $this->assertResponse(200);
194
    $this->assertText('User route "entity.user.canonical" was matched.');
195 196 197 198 199 200 201

    // Try to match a route for a non-existent user.
    $this->drupalGet('router_test/test14/2');
    $this->assertResponse(200);
    $this->assertText('Route not matched.');
  }

202 203 204 205 206 207 208 209 210
  /**
   * Tests that a PSR-7 response works.
   */
  public function testRouterResponsePsr7() {
    $this->drupalGet('/router_test/test23');
    $this->assertResponse(200);
    $this->assertText('test23');
  }

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
  /**
   * Tests the user account on the DIC.
   */
  public function testUserAccount() {
    $account = $this->drupalCreateUser();
    $this->drupalLogin($account);

    $second_account = $this->drupalCreateUser();

    $this->drupalGet('router_test/test12/' . $second_account->id());
    $this->assertText($account->getUsername() . ':' . $second_account->getUsername());
    $this->assertEqual($account->id(), $this->loggedInUser->id(), 'Ensure that the user was not changed.');

    $this->drupalGet('router_test/test13/' . $second_account->id());
    $this->assertText($account->getUsername() . ':' . $second_account->getUsername());
    $this->assertEqual($account->id(), $this->loggedInUser->id(), 'Ensure that the user was not changed.');
  }

229 230 231 232 233 234
  /**
   * Checks that an ajax request gets rendered as an Ajax response, by mime.
   */
  public function testControllerResolutionAjax() {
    // This will fail with a JSON parse error if the request is not routed to
    // The correct controller.
235
    $this->drupalGetAjax('/router_test/test10');
236 237 238 239 240

    $this->assertEqual($this->drupalGetHeader('Content-Type'), 'application/json', 'Correct mime content type was returned');

    $this->assertRaw('abcde', 'Correct body was found.');
  }
241 242 243 244

  /**
   * Tests that routes no longer exist for a module that has been uninstalled.
   */
245
  public function testRouterUninstallInstall() {
246
    \Drupal::service('module_installer')->uninstall(array('router_test'));
247
    \Drupal::service('router.builder')->rebuild();
248 249 250 251 252 253 254 255
    try {
      \Drupal::service('router.route_provider')->getRouteByName('router_test.1');
      $this->fail('Route was delete on uninstall.');
    }
    catch (RouteNotFoundException $e) {
      $this->pass('Route was delete on uninstall.');
    }
    // Install the module again.
256
    \Drupal::service('module_installer')->install(array('router_test'));
257
    \Drupal::service('router.builder')->rebuild();
258 259
    $route = \Drupal::service('router.route_provider')->getRouteByName('router_test.1');
    $this->assertNotNull($route, 'Route exists after module installation');
260
  }
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

  /**
   * Ensure that multiple leading slashes are redirected.
   */
  public function testLeadingSlashes() {
    $request = $this->container->get('request_stack')->getCurrentRequest();
    $url = $request->getUriForPath('//router_test/test1');
    $this->drupalGet($url);
    $this->assertEqual(1, $this->redirectCount, $url . " redirected to " . $this->url);
    $this->assertUrl($request->getUriForPath('/router_test/test1'));

    // It should not matter how many leading slashes are used and query strings
    // should be preserved.
    $url = $request->getUriForPath('/////////////////////////////////////////////////router_test/test1') . '?qs=test';
    $this->drupalGet($url);
    $this->assertEqual(1, $this->redirectCount, $url . " redirected to " . $this->url);
    $this->assertUrl($request->getUriForPath('/router_test/test1') . '?qs=test');
  }

280
}