Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Path / PathValidatorTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Path;
4
5 use Drupal\Core\Routing\RequestContext;
6 use Drupal\Core\Url;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\KernelTests\KernelTestBase;
9
10 /**
11  * Tests the path validator.
12  *
13  * @group Path
14  *
15  * @see \Drupal\Core\Path\PathValidator
16  */
17 class PathValidatorTest extends KernelTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = ['path', 'entity_test', 'user'];
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29     $this->installEntitySchema('entity_test');
30   }
31
32   public function testGetUrlIfValidWithoutAccessCheck() {
33     $requestContext = \Drupal::service('router.request_context');
34     $pathValidator = \Drupal::service('path.validator');
35
36     $entity = EntityTest::create([
37       'name' => 'test',
38     ]);
39     $entity->save();
40
41     $methods = [
42       'POST',
43       'GET',
44       'PUT',
45       'PATCH',
46       'DELETE',
47       NULL, // Used in CLI context.
48       FALSE, // If no request was even pushed onto the request stack, and hence
49     ];
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();
55         }
56         $this->container->set('router.request_context', new RequestContext());
57       }
58
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()]);
65     }
66   }
67
68 }