RouterTest.php 10.4 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\Routing\Exception\RouteNotFoundException;
14 15 16

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

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

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

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

45 46
    $this->drupalGet('router_test/test2');
    $this->assertRaw('test2', 'The correct string was returned because the route was successful.');
47 48 49 50
    // Check expected headers from FinishResponseSubscriber.
    $headers = $this->drupalGetHeaders();
    $this->assertEqual($headers['x-drupal-cache-contexts'], implode(' ', $renderer_required_cache_contexts));
    $this->assertEqual($headers['x-drupal-cache-tags'], 'rendered');
51 52
    // Confirm that the page wrapping is being added, so we're not getting a
    // raw body returned.
53
    $this->assertRaw('</html>', 'Page markup was found.');
54 55 56
    // 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.');
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73


    // 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'])));
    $this->assertEqual($headers['x-drupal-cache-tags'], 'foo rendered');
    // 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'])));
    $this->assertEqual($headers['x-drupal-cache-tags'], 'foo rendered');
    // 3. controller result: Response object, globally cacheable route access.
    $this->drupalGet('router_test/test1');
    $headers = $this->drupalGetHeaders();
74 75
    $this->assertFalse(isset($headers['x-drupal-cache-contexts']));
    $this->assertFalse(isset($headers['x-drupal-cache-tags']));
76 77 78
    // 4. controller result: Response object, per-role cacheable route access.
    $this->drupalGet('router_test/test20');
    $headers = $this->drupalGetHeaders();
79 80 81 82 83 84 85 86 87 88
    $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();
89 90
    $this->assertEqual($headers['x-drupal-cache-contexts'], 'user.roles');
    $this->assertEqual($headers['x-drupal-cache-tags'], '');
91 92
  }

93 94 95
  /**
   * Confirms that placeholders in paths work correctly.
   */
96
  public function testControllerPlaceholders() {
97
    // Test with 0 and a random value.
98
    $values = array("0", $this->randomMachineName());
99 100 101 102 103
    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.');
    }
104 105 106

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

    // 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.');
112 113
  }

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

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

    // 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.');
146 147
  }

148 149 150 151 152 153 154 155 156 157 158
  /**
   * 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.');
  }
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

  /**
   * 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.');
  }

177 178 179 180 181 182 183 184 185 186
  /**
   * 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);
  }

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

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

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  /**
   * 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.');
  }

219 220 221 222 223 224
  /**
   * 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.
225
    $this->drupalGetAjax('/router_test/test10');
226 227 228 229 230

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

    $this->assertRaw('abcde', 'Correct body was found.');
  }
231 232 233 234

  /**
   * Tests that routes no longer exist for a module that has been uninstalled.
   */
235
  public function testRouterUninstallInstall() {
236
    \Drupal::service('module_installer')->uninstall(array('router_test'));
237
    \Drupal::service('router.builder')->rebuild();
238 239 240 241 242 243 244 245
    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.
246
    \Drupal::service('module_installer')->install(array('router_test'));
247
    \Drupal::service('router.builder')->rebuild();
248 249
    $route = \Drupal::service('router.route_provider')->getRouteByName('router_test.1');
    $this->assertNotNull($route, 'Route exists after module installation');
250
  }
251
}