class UnroutedUrlTest

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/UnroutedUrlTest.php \Drupal\Tests\Core\UnroutedUrlTest
  2. 10 core/tests/Drupal/Tests/Core/UnroutedUrlTest.php \Drupal\Tests\Core\UnroutedUrlTest
  3. 9 core/tests/Drupal/Tests/Core/UnroutedUrlTest.php \Drupal\Tests\Core\UnroutedUrlTest
  4. 8.9.x core/tests/Drupal/Tests/Core/UnroutedUrlTest.php \Drupal\Tests\Core\UnroutedUrlTest

Tests Drupal\Core\Url.

Attributes

#[CoversClass(Url::class)] #[Group('UrlTest')]

Hierarchy

Expanded class hierarchy of UnroutedUrlTest

File

core/tests/Drupal/Tests/Core/UnroutedUrlTest.php, line 20

Namespace

Drupal\Tests\Core
View source
class UnroutedUrlTest extends UnitTestCase {
  
  /**
   * The URL assembler.
   *
   * @var \Drupal\Core\Utility\UnroutedUrlAssemblerInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $urlAssembler;
  
  /**
   * The router.
   *
   * @var \Drupal\Tests\Core\Routing\TestRouterInterface|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $router;
  
  /**
   * An unrouted, external URL to test.
   *
   * @var string
   */
  protected $unroutedExternal = 'https://www.drupal.org';
  
  /**
   * An unrouted, internal URL to test.
   *
   * @var string
   */
  protected $unroutedInternal = 'base:robots.txt';
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->urlAssembler = $this->createMock('Drupal\\Core\\Utility\\UnroutedUrlAssemblerInterface');
    $this->urlAssembler
      ->expects($this->any())
      ->method('assemble')
      ->willReturnArgument(0);
    $this->router = $this->createMock('Drupal\\Tests\\Core\\Routing\\TestRouterInterface');
    $container = new ContainerBuilder();
    $container->set('router.no_access_checks', $this->router);
    $container->set('unrouted_url_assembler', $this->urlAssembler);
    \Drupal::setContainer($container);
  }
  
  /**
   * Tests the fromUri() method.
   */
  public function testFromUri(string $uri, bool $is_external) : void {
    $url = Url::fromUri($uri);
    $this->assertInstanceOf('Drupal\\Core\\Url', $url);
  }
  
  /**
   * Data provider for testFromUri().
   */
  public static function providerFromUri() : array {
    return [
      // [$uri, $is_external]
      // An external URI.
[
        'https://www.drupal.org',
        TRUE,
      ],
      // A protocol-relative URL.
[
        '//www.drupal.org',
        TRUE,
      ],
      // An internal, unrouted, base-relative URI.
[
        'base:robots.txt',
        FALSE,
      ],
      // Base-relative URIs with special characters.
[
        'base:AKI@&hO@',
        FALSE,
      ],
      [
        'base:(:;2&+h^',
        FALSE,
      ],
      // Various token formats.
[
        'base:node/[token]',
        FALSE,
      ],
      [
        'base:node/%',
        FALSE,
      ],
      [
        'base:node/[token:token]',
        FALSE,
      ],
      [
        'base:node/{{ token }}',
        FALSE,
      ],
    ];
  }
  
  /**
   * Tests the fromUri() method.
   *
   * @legacy-covers ::fromUri
   */
  public function testFromInvalidUri(string $uri) : void {
    $this->expectException(\InvalidArgumentException::class);
    Url::fromUri($uri);
  }
  
  /**
   * Data provider for testFromInvalidUri().
   */
  public static function providerFromInvalidUri() : array {
    return [
      // Schemeless paths.
[
        'test',
      ],
      [
        '/test',
      ],
      // Schemeless path with a query string.
[
        'foo?bar',
      ],
      // Only a query string.
[
        '?bar',
      ],
      // Only a fragment.
[
        '#foo',
      ],
      // Disallowed characters in the authority (host name) that are valid
      // elsewhere in the path.
[
        'base://(:;2&+h^',
      ],
    ];
  }
  
  /**
   * Tests the createFromRequest method.
   */
  public function testCreateFromRequest() : void {
    $request = Request::create('/test-path');
    $this->router
      ->expects($this->once())
      ->method('matchRequest')
      ->with($request)
      ->will($this->throwException(new ResourceNotFoundException()));
    $this->expectException(ResourceNotFoundException::class);
    Url::createFromRequest($request);
  }
  
  /**
   * Tests the isExternal() method.
   */
  public function testIsExternal(string $uri, bool $is_external) : void {
    $url = Url::fromUri($uri);
    $this->assertSame($url->isExternal(), $is_external);
  }
  
  /**
   * Tests the toString() method.
   */
  public function testToString(string $uri, bool $is_external) : void {
    $url = Url::fromUri($uri);
    $this->assertSame($uri, $url->toString());
  }
  
  /**
   * Tests the getRouteName() method.
   */
  public function testGetRouteName(string $uri, bool $is_external) : void {
    $url = Url::fromUri($uri);
    $this->expectException(\UnexpectedValueException::class);
    $url->getRouteName();
  }
  
  /**
   * Tests the getRouteParameters() method.
   */
  public function testGetRouteParameters(string $uri, bool $is_external) : void {
    $url = Url::fromUri($uri);
    $this->expectException(\UnexpectedValueException::class);
    $url->getRouteParameters();
  }
  
  /**
   * Tests the getInternalPath() method.
   */
  public function testGetInternalPath(string $uri, bool $is_external) : void {
    $url = Url::fromUri($uri);
    $this->expectException(\Exception::class);
    $url->getInternalPath();
  }
  
  /**
   * Tests the getPath() method.
   */
  public function testGetUri(string $uri, bool $is_external) : void {
    $url = Url::fromUri($uri);
    $this->assertNotNull($url->getUri());
  }
  
  /**
   * Tests the getOptions() method.
   */
  public function testGetOptions(string $uri, bool $is_external) : void {
    $url = Url::fromUri($uri);
    $this->assertIsArray($url->getOptions());
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title
DrupalTestCaseTrait::checkErrorHandlerOnTearDown public function Checks the test error handler after test execution.
ExpectDeprecationTrait::expectDeprecation Deprecated public function Adds an expected deprecation.
ExpectDeprecationTrait::regularExpressionForFormatDescription private function
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.
UnroutedUrlTest::$router protected property The router.
UnroutedUrlTest::$unroutedExternal protected property An unrouted, external URL to test.
UnroutedUrlTest::$unroutedInternal protected property An unrouted, internal URL to test.
UnroutedUrlTest::$urlAssembler protected property The URL assembler.
UnroutedUrlTest::providerFromInvalidUri public static function Data provider for testFromInvalidUri().
UnroutedUrlTest::providerFromUri public static function Data provider for testFromUri().
UnroutedUrlTest::setUp protected function Overrides UnitTestCase::setUp
UnroutedUrlTest::testCreateFromRequest public function Tests the createFromRequest method.
UnroutedUrlTest::testFromInvalidUri public function Tests the fromUri() method.
UnroutedUrlTest::testFromUri public function Tests the fromUri() method.
UnroutedUrlTest::testGetInternalPath public function Tests the getInternalPath() method.
UnroutedUrlTest::testGetOptions public function Tests the getOptions() method.
UnroutedUrlTest::testGetRouteName public function Tests the getRouteName() method.
UnroutedUrlTest::testGetRouteParameters public function Tests the getRouteParameters() method.
UnroutedUrlTest::testGetUri public function Tests the getPath() method.
UnroutedUrlTest::testIsExternal public function Tests the isExternal() method.
UnroutedUrlTest::testToString public function Tests the toString() method.

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