class UnroutedUrlAssemblerTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Utility/UnroutedUrlAssemblerTest.php \Drupal\Tests\Core\Utility\UnroutedUrlAssemblerTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Utility/UnroutedUrlAssemblerTest.php \Drupal\Tests\Core\Utility\UnroutedUrlAssemblerTest
  3. 11.x core/tests/Drupal/Tests/Core/Utility/UnroutedUrlAssemblerTest.php \Drupal\Tests\Core\Utility\UnroutedUrlAssemblerTest

@coversDefaultClass \Drupal\Core\Utility\UnroutedUrlAssembler @group Utility

Hierarchy

Expanded class hierarchy of UnroutedUrlAssemblerTest

File

core/tests/Drupal/Tests/Core/Utility/UnroutedUrlAssemblerTest.php, line 18

Namespace

Drupal\Tests\Core\Utility
View source
class UnroutedUrlAssemblerTest extends UnitTestCase {
    
    /**
     * The request stack.
     *
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    protected $requestStack;
    
    /**
     * The mocked config factory.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $configFactory;
    
    /**
     * The tested unrouted URL assembler.
     *
     * @var \Drupal\Core\Utility\UnroutedUrlAssembler
     */
    protected $unroutedUrlAssembler;
    
    /**
     * The mocked outbound path processor.
     *
     * @var \Drupal\Core\PathProcessor\OutboundPathProcessorInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $pathProcessor;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->requestStack = new RequestStack();
        $this->pathProcessor = $this->createMock('Drupal\\Core\\PathProcessor\\OutboundPathProcessorInterface');
        $this->unroutedUrlAssembler = new UnroutedUrlAssembler($this->requestStack, $this->pathProcessor);
    }
    
    /**
     * @covers ::assemble
     */
    public function testAssembleWithNeitherExternalNorDomainLocalUri() : void {
        $this->expectException(\InvalidArgumentException::class);
        $this->unroutedUrlAssembler
            ->assemble('wrong-url');
    }
    
    /**
     * @covers ::assemble
     */
    public function testAssembleWithLeadingSlash() : void {
        $this->expectException(\InvalidArgumentException::class);
        $this->unroutedUrlAssembler
            ->assemble('/drupal.org');
    }
    
    /**
     * @covers ::assemble
     * @covers ::buildExternalUrl
     *
     * @dataProvider providerTestAssembleWithExternalUrl
     */
    public function testAssembleWithExternalUrl($uri, array $options, $expected) : void {
        $this->setupRequestStack(FALSE);
        $this->assertEquals($expected, $this->unroutedUrlAssembler
            ->assemble($uri, $options));
        $generated_url = $this->unroutedUrlAssembler
            ->assemble($uri, $options, TRUE);
        $this->assertEquals($expected, $generated_url->getGeneratedUrl());
        $this->assertInstanceOf('\\Drupal\\Core\\Render\\BubbleableMetadata', $generated_url);
    }
    
    /**
     * Provides test data for testAssembleWithExternalUrl.
     */
    public static function providerTestAssembleWithExternalUrl() {
        return [
            [
                'http://example.com/test',
                [],
                'http://example.com/test',
            ],
            [
                'http://example.com/test',
                [
                    'fragment' => 'example',
                ],
                'http://example.com/test#example',
            ],
            [
                'http://example.com/test',
                [
                    'fragment' => 'example',
                ],
                'http://example.com/test#example',
            ],
            [
                'http://example.com/test',
                [
                    'query' => [
                        'foo' => 'bar',
                    ],
                ],
                'http://example.com/test?foo=bar',
            ],
            [
                'http://example.com/test',
                [
                    'https' => TRUE,
                ],
                'https://example.com/test',
            ],
            [
                'https://example.com/test',
                [
                    'https' => FALSE,
                ],
                'http://example.com/test',
            ],
            [
                'https://example.com/test?foo=1#bar',
                [],
                'https://example.com/test?foo=1#bar',
            ],
            'override-query' => [
                'https://example.com/test?foo=1#bar',
                [
                    'query' => [
                        'foo' => 2,
                    ],
                ],
                'https://example.com/test?foo=2#bar',
            ],
            'override-query-merge' => [
                'https://example.com/test?foo=1#bar',
                [
                    'query' => [
                        'bar' => 2,
                    ],
                ],
                'https://example.com/test?foo=1&bar=2#bar',
            ],
            'override-deep-query-merge' => [
                'https://example.com/test?foo=1#bar',
                [
                    'query' => [
                        'bar' => [
                            'baz' => 'foo',
                        ],
                    ],
                ],
                'https://example.com/test?foo=1&bar%5Bbaz%5D=foo#bar',
            ],
            'override-deep-query-merge-int-ket' => [
                'https://example.com/test?120=1',
                [
                    'query' => [
                        'bar' => [
                            'baz' => 'foo',
                        ],
                    ],
                ],
                'https://example.com/test?120=1&bar%5Bbaz%5D=foo',
            ],
            'override-fragment' => [
                'https://example.com/test?foo=1#bar',
                [
                    'fragment' => 'baz',
                ],
                'https://example.com/test?foo=1#baz',
            ],
            [
                '//www.drupal.org',
                [],
                '//www.drupal.org',
            ],
        ];
    }
    
    /**
     * @covers ::assemble
     * @covers ::buildLocalUrl
     *
     * @dataProvider providerTestAssembleWithLocalUri
     */
    public function testAssembleWithLocalUri($uri, array $options, $subdir, $expected) : void {
        $this->setupRequestStack($subdir);
        $this->assertEquals($expected, $this->unroutedUrlAssembler
            ->assemble($uri, $options));
    }
    
    /**
     * @return array
     */
    public static function providerTestAssembleWithLocalUri() {
        return [
            [
                'base:example',
                [],
                FALSE,
                '/example',
            ],
            [
                'base:example',
                [
                    'query' => [
                        'foo' => 'bar',
                    ],
                ],
                FALSE,
                '/example?foo=bar',
            ],
            [
                'base:example',
                [
                    'query' => [
                        'foo' => '"bar"',
                    ],
                ],
                FALSE,
                '/example?foo=%22bar%22',
            ],
            [
                'base:example',
                [
                    'query' => [
                        'foo' => '"bar"',
                        'zoo' => 'baz',
                    ],
                ],
                FALSE,
                '/example?foo=%22bar%22&zoo=baz',
            ],
            [
                'base:example',
                [
                    'fragment' => 'example',
                ],
                FALSE,
                '/example#example',
            ],
            [
                'base:example',
                [],
                TRUE,
                '/subdir/example',
            ],
            [
                'base:example',
                [
                    'query' => [
                        'foo' => 'bar',
                    ],
                ],
                TRUE,
                '/subdir/example?foo=bar',
            ],
            [
                'base:example',
                [
                    'fragment' => 'example',
                ],
                TRUE,
                '/subdir/example#example',
            ],
            [
                'base:/drupal.org',
                [],
                FALSE,
                '/drupal.org',
            ],
        ];
    }
    
    /**
     * @covers ::assemble
     */
    public function testAssembleWithNotEnabledProcessing() : void {
        $this->setupRequestStack(FALSE);
        $this->pathProcessor
            ->expects($this->never())
            ->method('processOutbound');
        $result = $this->unroutedUrlAssembler
            ->assemble('base:test-uri', []);
        $this->assertEquals('/test-uri', $result);
    }
    
    /**
     * @covers ::assemble
     */
    public function testAssembleWithEnabledProcessing() : void {
        $this->setupRequestStack(FALSE);
        $this->pathProcessor
            ->expects($this->exactly(2))
            ->method('processOutbound')
            ->willReturnCallback(function ($path, &$options = [], ?Request $request = NULL, ?BubbleableMetadata $bubbleable_metadata = NULL) {
            if ($bubbleable_metadata) {
                $bubbleable_metadata->setCacheContexts([
                    'some-cache-context',
                ]);
            }
            return '/test-other-uri';
        });
        $result = $this->unroutedUrlAssembler
            ->assemble('base:test-uri', [
            'path_processing' => TRUE,
        ]);
        $this->assertEquals('/test-other-uri', $result);
        $result = $this->unroutedUrlAssembler
            ->assemble('base:test-uri', [
            'path_processing' => TRUE,
        ], TRUE);
        $expected_generated_url = new GeneratedUrl();
        $expected_generated_url->setGeneratedUrl('/test-other-uri')
            ->setCacheContexts([
            'some-cache-context',
        ]);
        $this->assertEquals($expected_generated_url, $result);
    }
    
    /**
     * @covers ::assemble
     */
    public function testAssembleWithStartingSlashEnabledProcessing() : void {
        $this->setupRequestStack(FALSE);
        $this->pathProcessor
            ->expects($this->exactly(2))
            ->method('processOutbound')
            ->with('/test-uri', $this->anything(), $this->anything(), $this->anything())
            ->willReturnCallback(function ($path, &$options = [], ?Request $request = NULL, ?BubbleableMetadata $bubbleable_metadata = NULL) {
            $bubbleable_metadata?->setCacheContexts([
                'some-cache-context',
            ]);
            return '/test-other-uri';
        });
        $result = $this->unroutedUrlAssembler
            ->assemble('base:/test-uri', [
            'path_processing' => TRUE,
        ]);
        $this->assertEquals('/test-other-uri', $result);
        $result = $this->unroutedUrlAssembler
            ->assemble('base:/test-uri', [
            'path_processing' => TRUE,
        ], TRUE);
        $expected_generated_url = new GeneratedUrl();
        $expected_generated_url->setGeneratedUrl('/test-other-uri')
            ->setCacheContexts([
            'some-cache-context',
        ]);
        $this->assertEquals($expected_generated_url, $result);
    }
    
    /**
     * Setups the request stack for a given subdir.
     *
     * @param bool $subdir
     *   TRUE to use a subdir.
     */
    protected function setupRequestStack($subdir) {
        $server = [];
        if ($subdir) {
            // Setup a fake request which looks like a Drupal installed under the
            // subdir "subdir" on the domain www.example.com.
            // To reproduce the values install Drupal like that and use a debugger.
            $server = [
                'SCRIPT_NAME' => '/subdir/index.php',
                'SCRIPT_FILENAME' => $this->root . '/index.php',
                'SERVER_NAME' => 'www.example.com',
            ];
            $request = Request::create('/subdir/');
        }
        else {
            $request = Request::create('/');
        }
        $request->server
            ->add($server);
        $this->requestStack
            ->push($request);
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
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.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 1
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
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::setUpBeforeClass public static function
UnitTestCase::__get public function
UnroutedUrlAssemblerTest::$configFactory protected property The mocked config factory.
UnroutedUrlAssemblerTest::$pathProcessor protected property The mocked outbound path processor.
UnroutedUrlAssemblerTest::$requestStack protected property The request stack.
UnroutedUrlAssemblerTest::$unroutedUrlAssembler protected property The tested unrouted URL assembler.
UnroutedUrlAssemblerTest::providerTestAssembleWithExternalUrl public static function Provides test data for testAssembleWithExternalUrl.
UnroutedUrlAssemblerTest::providerTestAssembleWithLocalUri public static function
UnroutedUrlAssemblerTest::setUp protected function Overrides UnitTestCase::setUp
UnroutedUrlAssemblerTest::setupRequestStack protected function Setups the request stack for a given subdir.
UnroutedUrlAssemblerTest::testAssembleWithEnabledProcessing public function @covers ::assemble
UnroutedUrlAssemblerTest::testAssembleWithExternalUrl public function @covers ::assemble
@covers ::buildExternalUrl
UnroutedUrlAssemblerTest::testAssembleWithLeadingSlash public function @covers ::assemble
UnroutedUrlAssemblerTest::testAssembleWithLocalUri public function @covers ::assemble
@covers ::buildLocalUrl
UnroutedUrlAssemblerTest::testAssembleWithNeitherExternalNorDomainLocalUri public function @covers ::assemble
UnroutedUrlAssemblerTest::testAssembleWithNotEnabledProcessing public function @covers ::assemble
UnroutedUrlAssemblerTest::testAssembleWithStartingSlashEnabledProcessing public function @covers ::assemble

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