PathValidatorTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Drupal\KernelTests\Core\Path;
  3. use Drupal\Core\Routing\RequestContext;
  4. use Drupal\Core\Url;
  5. use Drupal\entity_test\Entity\EntityTest;
  6. use Drupal\KernelTests\KernelTestBase;
  7. /**
  8. * Tests the path validator.
  9. *
  10. * @group Path
  11. *
  12. * @see \Drupal\Core\Path\PathValidator
  13. */
  14. class PathValidatorTest extends KernelTestBase {
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public static $modules = ['path', 'entity_test', 'user'];
  19. /**
  20. * {@inheritdoc}
  21. */
  22. protected function setUp() {
  23. parent::setUp();
  24. $this->installEntitySchema('entity_test');
  25. }
  26. public function testGetUrlIfValidWithoutAccessCheck() {
  27. $requestContext = \Drupal::service('router.request_context');
  28. $pathValidator = \Drupal::service('path.validator');
  29. $entity = EntityTest::create([
  30. 'name' => 'test',
  31. ]);
  32. $entity->save();
  33. $methods = [
  34. 'POST',
  35. 'GET',
  36. 'PUT',
  37. 'PATCH',
  38. 'DELETE',
  39. // Used in CLI context.
  40. NULL,
  41. // If no request was even pushed onto the request stack, and hence.
  42. FALSE,
  43. ];
  44. foreach ($methods as $method) {
  45. if ($method === FALSE) {
  46. $request_stack = $this->container->get('request_stack');
  47. while ($request_stack->getCurrentRequest()) {
  48. $request_stack->pop();
  49. }
  50. $this->container->set('router.request_context', new RequestContext());
  51. }
  52. $requestContext->setMethod($method);
  53. /** @var \Drupal\Core\Url $url */
  54. $url = $pathValidator->getUrlIfValidWithoutAccessCheck($entity->toUrl()->toString(TRUE)->getGeneratedUrl());
  55. $this->assertEquals($method, $requestContext->getMethod());
  56. $this->assertInstanceOf(Url::class, $url);
  57. $this->assertSame($url->getRouteParameters(), ['entity_test' => $entity->id()]);
  58. }
  59. }
  60. }