function PathValidatorTest::testGetUrlIfValidWithoutAccessCheck

Same name in this branch
  1. 10 core/tests/Drupal/Tests/Core/Path/PathValidatorTest.php \Drupal\Tests\Core\Path\PathValidatorTest::testGetUrlIfValidWithoutAccessCheck()
Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php \Drupal\KernelTests\Core\Path\PathValidatorTest::testGetUrlIfValidWithoutAccessCheck()
  2. 9 core/tests/Drupal/Tests/Core/Path/PathValidatorTest.php \Drupal\Tests\Core\Path\PathValidatorTest::testGetUrlIfValidWithoutAccessCheck()
  3. 8.9.x core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php \Drupal\KernelTests\Core\Path\PathValidatorTest::testGetUrlIfValidWithoutAccessCheck()
  4. 8.9.x core/tests/Drupal/Tests/Core/Path/PathValidatorTest.php \Drupal\Tests\Core\Path\PathValidatorTest::testGetUrlIfValidWithoutAccessCheck()
  5. 11.x core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php \Drupal\KernelTests\Core\Path\PathValidatorTest::testGetUrlIfValidWithoutAccessCheck()
  6. 11.x core/tests/Drupal/Tests/Core/Path/PathValidatorTest.php \Drupal\Tests\Core\Path\PathValidatorTest::testGetUrlIfValidWithoutAccessCheck()

File

core/tests/Drupal/KernelTests/Core/Path/PathValidatorTest.php, line 38

Class

PathValidatorTest
Tests the path validator.

Namespace

Drupal\KernelTests\Core\Path

Code

public function testGetUrlIfValidWithoutAccessCheck() : void {
  $requestContext = \Drupal::service('router.request_context');
  $pathValidator = \Drupal::service('path.validator');
  $entity = EntityTest::create([
    'name' => 'test',
  ]);
  $entity->save();
  $methods = [
    'POST',
    'GET',
    'PUT',
    'PATCH',
    'DELETE',
    // NULL is used in CLI context which results in a request method of an
    // empty string.
'',
    // If no request was even pushed onto the request stack, and hence.
FALSE,
  ];
  foreach ($methods as $method) {
    /** @var \Symfony\Component\HttpFoundation\Request|null $request */
    $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);
    }
    /** @var \Drupal\Core\Url $url */
    $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) {
      // Restore main request.
      $request_stack = $this->container
        ->get('request_stack');
      $request_stack->push($request);
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.