class RedirectDestinationTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Routing/RedirectDestinationTest.php \Drupal\Tests\Core\Routing\RedirectDestinationTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Routing/RedirectDestinationTest.php \Drupal\Tests\Core\Routing\RedirectDestinationTest
  3. 10 core/tests/Drupal/Tests/Core/Routing/RedirectDestinationTest.php \Drupal\Tests\Core\Routing\RedirectDestinationTest

@coversDefaultClass \Drupal\Core\Routing\RedirectDestination @group Routing

Hierarchy

Expanded class hierarchy of RedirectDestinationTest

File

core/tests/Drupal/Tests/Core/Routing/RedirectDestinationTest.php, line 17

Namespace

Drupal\Tests\Core\Routing
View source
class RedirectDestinationTest extends UnitTestCase {
    
    /**
     * The request stack.
     *
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    protected $requestStack;
    
    /**
     * The mocked URL generator.
     *
     * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit\Framework\MockObject\MockObject
     */
    protected $urlGenerator;
    
    /**
     * The tested redirect destination.
     *
     * @var \Drupal\Core\Routing\RedirectDestination
     */
    protected $redirectDestination;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->requestStack = new RequestStack();
        $this->urlGenerator = $this->createMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
        $this->redirectDestination = new RedirectDestination($this->requestStack, $this->urlGenerator);
    }
    protected function setupUrlGenerator() {
        $this->urlGenerator
            ->expects($this->any())
            ->method('generateFromRoute')
            ->willReturnCallback(function ($route, $parameters, $options) {
            $query_string = '';
            if (!empty($options['query'])) {
                $query_string = '?' . UrlHelper::buildQuery($options['query']);
            }
            return '/current-path' . $query_string;
        });
    }
    
    /**
     * Tests destination passed via $_GET.
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   The request to test.
     * @param string $expected_destination
     *   The expected destination.
     *
     * @dataProvider providerGet
     *
     * @covers ::get
     */
    public function testGet(Request $request, $expected_destination) : void {
        $this->requestStack
            ->push($request);
        $this->setupUrlGenerator();
        // Call in twice in order to ensure it returns the same the next time.
        $this->assertEquals($expected_destination, $this->redirectDestination
            ->get());
        $this->assertEquals($expected_destination, $this->redirectDestination
            ->get());
    }
    
    /**
     * @dataProvider providerGet
     *
     * @covers ::getAsArray
     */
    public function testGetAsArray(Request $request, $expected_destination) : void {
        $this->requestStack
            ->push($request);
        $this->setupUrlGenerator();
        // Call in twice in order to ensure it returns the same the next time.
        $this->assertEquals([
            'destination' => $expected_destination,
        ], $this->redirectDestination
            ->getAsArray());
        $this->assertEquals([
            'destination' => $expected_destination,
        ], $this->redirectDestination
            ->getAsArray());
    }
    public static function providerGet() {
        $data = [];
        $request = Request::create('/');
        $request->query
            ->set('destination', '/example');
        // A request with a destination query.
        $data[] = [
            $request,
            '/example',
        ];
        // A request without a destination query,
        $request = Request::create('/');
        $data[] = [
            $request,
            '/current-path',
        ];
        // A request without destination query, but other query attributes.
        $request = Request::create('/');
        $request->query
            ->set('other', 'value');
        $data[] = [
            $request,
            '/current-path?other=value',
        ];
        // A request with a dedicated specified external destination.
        $request = Request::create('/');
        $request->query
            ->set('destination', 'https://www.drupal.org');
        $data[] = [
            $request,
            '/',
        ];
        return $data;
    }
    
    /**
     * @covers ::set
     * @covers ::get
     */
    public function testSetBeforeGetCall() : void {
        $this->redirectDestination
            ->set('/example');
        $this->assertEquals('/example', $this->redirectDestination
            ->get());
    }
    
    /**
     * @covers ::set
     * @covers ::get
     */
    public function testSetAfterGetCall() : void {
        $request = Request::create('/');
        $request->query
            ->set('destination', '/other-example');
        $this->requestStack
            ->push($request);
        $this->setupUrlGenerator();
        $this->redirectDestination
            ->set('/example');
        $this->assertEquals('/example', $this->redirectDestination
            ->get());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
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.
RedirectDestinationTest::$redirectDestination protected property The tested redirect destination.
RedirectDestinationTest::$requestStack protected property The request stack.
RedirectDestinationTest::$urlGenerator protected property The mocked URL generator.
RedirectDestinationTest::providerGet public static function
RedirectDestinationTest::setUp protected function Overrides UnitTestCase::setUp
RedirectDestinationTest::setupUrlGenerator protected function
RedirectDestinationTest::testGet public function Tests destination passed via $_GET.
RedirectDestinationTest::testGetAsArray public function @dataProvider providerGet
RedirectDestinationTest::testSetAfterGetCall public function @covers ::set
@covers ::get
RedirectDestinationTest::testSetBeforeGetCall public function @covers ::set
@covers ::get
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::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

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