PathEncodedTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Drupal\FunctionalTests\Routing;
  3. use Drupal\Core\Url;
  4. use Drupal\Tests\BrowserTestBase;
  5. /**
  6. * Tests url generation and routing for route paths with encoded characters.
  7. *
  8. * @group routing
  9. */
  10. class PathEncodedTest extends BrowserTestBase {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public static $modules = ['system', 'path_encoded_test'];
  15. public function testGetEncoded() {
  16. $route_paths = [
  17. 'path_encoded_test.colon' => '/hi/llamma:party',
  18. 'path_encoded_test.atsign' => '/bloggy/@Dries',
  19. 'path_encoded_test.parens' => '/cat(box)',
  20. ];
  21. foreach ($route_paths as $route_name => $path) {
  22. $this->drupalGet(Url::fromRoute($route_name));
  23. $this->assertSession()->pageTextContains('PathEncodedTestController works');
  24. }
  25. }
  26. public function testAliasToEncoded() {
  27. $route_paths = [
  28. 'path_encoded_test.colon' => '/hi/llamma:party',
  29. 'path_encoded_test.atsign' => '/bloggy/@Dries',
  30. 'path_encoded_test.parens' => '/cat(box)',
  31. ];
  32. /** @var \Drupal\Core\Path\AliasStorageInterface $alias_storage */
  33. $alias_storage = $this->container->get('path.alias_storage');
  34. $aliases = [];
  35. foreach ($route_paths as $route_name => $path) {
  36. $aliases[$route_name] = $this->randomMachineName();
  37. $alias_storage->save($path, '/' . $aliases[$route_name]);
  38. }
  39. foreach ($route_paths as $route_name => $path) {
  40. // The alias may be only a suffix of the generated path when the test is
  41. // run with Drupal installed in a subdirectory.
  42. $this->assertRegExp('@/' . rawurlencode($aliases[$route_name]) . '$@', Url::fromRoute($route_name)->toString());
  43. $this->drupalGet(Url::fromRoute($route_name));
  44. $this->assertSession()->pageTextContains('PathEncodedTestController works');
  45. }
  46. }
  47. }