3 namespace Drupal\KernelTests\Core\Path;
5 use Drupal\Core\Routing\RequestContext;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\KernelTests\KernelTestBase;
11 * Tests the path validator.
15 * @see \Drupal\Core\Path\PathValidator
17 class PathValidatorTest extends KernelTestBase {
22 public static $modules = ['path', 'entity_test', 'user'];
27 protected function setUp() {
29 $this->installEntitySchema('entity_test');
32 public function testGetUrlIfValidWithoutAccessCheck() {
33 $requestContext = \Drupal::service('router.request_context');
34 $pathValidator = \Drupal::service('path.validator');
36 $entity = EntityTest::create([
47 NULL, // Used in CLI context.
48 FALSE, // If no request was even pushed onto the request stack, and hence
50 foreach ($methods as $method) {
51 if ($method === FALSE) {
52 $request_stack = $this->container->get('request_stack');
53 while ($request_stack->getCurrentRequest()) {
54 $request_stack->pop();
56 $this->container->set('router.request_context', new RequestContext());
59 $requestContext->setMethod($method);
60 /** @var \Drupal\Core\Url $url */
61 $url = $pathValidator->getUrlIfValidWithoutAccessCheck($entity->toUrl()->toString(TRUE)->getGeneratedUrl());
62 $this->assertEquals($method, $requestContext->getMethod());
63 $this->assertInstanceOf(Url::class, $url);
64 $this->assertSame($url->getRouteParameters(), ['entity_test' => $entity->id()]);