View source
<?php
namespace Drupal\Tests\Core\Routing;
use Drupal\Core\Routing\RedirectDestination;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class RedirectDestinationTest extends UnitTestCase {
protected $requestStack;
protected $urlGenerator;
protected $redirectDestination;
protected function setUp() {
parent::setUp();
$this->requestStack = new RequestStack();
$this->urlGenerator = $this
->getMock('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 = '?' . $options['query'];
}
return '/current-path' . $query_string;
});
}
public function testGet(Request $request, $expected_destination) {
$this->requestStack
->push($request);
$this
->setupUrlGenerator();
$this
->assertEquals($expected_destination, $this->redirectDestination
->get());
$this
->assertEquals($expected_destination, $this->redirectDestination
->get());
}
public function testGetAsArray(Request $request, $expected_destination) {
$this->requestStack
->push($request);
$this
->setupUrlGenerator();
$this
->assertEquals([
'destination' => $expected_destination,
], $this->redirectDestination
->getAsArray());
$this
->assertEquals([
'destination' => $expected_destination,
], $this->redirectDestination
->getAsArray());
}
public function providerGet() {
$data = [];
$request = Request::create('/');
$request->query
->set('destination', '/example');
$data[] = [
$request,
'/example',
];
$request = Request::create('/');
$data[] = [
$request,
'/current-path',
];
$request = Request::create('/');
$request->query
->set('other', 'value');
$data[] = [
$request,
'/current-path?other=value',
];
$request = Request::create('/');
$request->query
->set('destination', 'https://www.drupal.org');
$data[] = [
$request,
'/',
];
return $data;
}
public function testSetBeforeGetCall() {
$this->redirectDestination
->set('/example');
$this
->assertEquals('/example', $this->redirectDestination
->get());
}
public function testSetAfterGetCall() {
$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());
}
}