PathEncodedTest.php 1.8 KB

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