Same filename in this branch
- 10 core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php
- 10 core/tests/Drupal/Tests/Core/Path/PathValidatorTest.php
Same filename and directory in other branches
- 8.9.x core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php
- 9 core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php
Namespace
Drupal\KernelTests\Core\Path
File
core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\KernelTests\Core\Path;
use Drupal\Core\Routing\RequestContext;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
class PathValidatorTest extends KernelTestBase {
use UserCreationTrait;
protected static $modules = [
'path',
'entity_test',
'system',
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->setUpCurrentUser();
$this
->installEntitySchema('entity_test');
}
public function testGetUrlIfValidWithoutAccessCheck() {
$requestContext = \Drupal::service('router.request_context');
$pathValidator = \Drupal::service('path.validator');
$entity = EntityTest::create([
'name' => 'test',
]);
$entity
->save();
$methods = [
'POST',
'GET',
'PUT',
'PATCH',
'DELETE',
'',
FALSE,
];
foreach ($methods as $method) {
$request = NULL;
if ($method === FALSE) {
$request_stack = $this->container
->get('request_stack');
while ($request_stack
->getCurrentRequest()) {
$request = $request_stack
->pop();
}
$this->container
->set('router.request_context', new RequestContext());
}
else {
$requestContext
->setMethod($method);
}
$url = $pathValidator
->getUrlIfValidWithoutAccessCheck($entity
->toUrl()
->toString(TRUE)
->getGeneratedUrl());
$this
->assertEquals($method, $requestContext
->getMethod());
$this
->assertInstanceOf(Url::class, $url);
$this
->assertSame([
'entity_test' => $entity
->id(),
], $url
->getRouteParameters());
if ($method === FALSE) {
$request_stack = $this->container
->get('request_stack');
$request_stack
->push($request);
}
}
}
}
Classes