3 namespace Drupal\Tests\Core\Routing;
5 use Drupal\Component\Utility\UrlHelper;
6 use Drupal\Core\Routing\RedirectDestination;
7 use Drupal\Tests\UnitTestCase;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpFoundation\RequestStack;
12 * @coversDefaultClass \Drupal\Core\Routing\RedirectDestination
15 class RedirectDestinationTest extends UnitTestCase {
20 * @var \Symfony\Component\HttpFoundation\RequestStack
22 protected $requestStack;
25 * The mocked URL generator.
27 * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject
29 protected $urlGenerator;
32 * The tested redirect destination.
34 * @var \Drupal\Core\Routing\RedirectDestination
36 protected $redirectDestination;
41 protected function setUp() {
44 $this->requestStack = new RequestStack();
45 $this->urlGenerator = $this->getMock('Drupal\Core\Routing\UrlGeneratorInterface');
46 $this->redirectDestination = new RedirectDestination($this->requestStack, $this->urlGenerator);
49 protected function setupUrlGenerator() {
50 $this->urlGenerator->expects($this->any())
51 ->method('generateFromRoute')
52 ->willReturnCallback(function ($route, $parameters, $options) {
54 if (!empty($options['query'])) {
55 $query_string = '?' . UrlHelper::buildQuery($options['query']);
58 return '/current-path' . $query_string;
63 * Tests destination passed via $_GET.
65 * @param \Symfony\Component\HttpFoundation\Request $request
66 * The request to test.
67 * @param string $expected_destination
68 * The expected destination.
70 * @dataProvider providerGet
74 public function testGet(Request $request, $expected_destination) {
75 $this->requestStack->push($request);
76 $this->setupUrlGenerator();
78 // Call in twice in order to ensure it returns the same the next time.
79 $this->assertEquals($expected_destination, $this->redirectDestination->get());
80 $this->assertEquals($expected_destination, $this->redirectDestination->get());
84 * @dataProvider providerGet
86 * @covers ::getAsArray
88 public function testGetAsArray(Request $request, $expected_destination) {
89 $this->requestStack->push($request);
90 $this->setupUrlGenerator();
92 // Call in twice in order to ensure it returns the same the next time.
93 $this->assertEquals(['destination' => $expected_destination], $this->redirectDestination->getAsArray());
94 $this->assertEquals(['destination' => $expected_destination], $this->redirectDestination->getAsArray());
97 public function providerGet() {
100 $request = Request::create('/');
101 $request->query->set('destination', '/example');
102 // A request with a destination query.
103 $data[] = [$request, '/example'];
105 // A request without a destination query,
106 $request = Request::create('/');
107 $data[] = [$request, '/current-path'];
109 // A request without destination query, but other query attributes.
110 $request = Request::create('/');
111 $request->query->set('other', 'value');
112 $data[] = [$request, '/current-path?other=value'];
114 // A request with a dedicated specified external destination.
115 $request = Request::create('/');
116 $request->query->set('destination', 'https://www.drupal.org');
117 $data[] = [$request, '/'];
126 public function testSetBeforeGetCall() {
127 $this->redirectDestination->set('/example');
128 $this->assertEquals('/example', $this->redirectDestination->get());
135 public function testSetAfterGetCall() {
136 $request = Request::create('/');
137 $request->query->set('destination', '/other-example');
138 $this->requestStack->push($request);
139 $this->setupUrlGenerator();
141 $this->redirectDestination->set('/example');
142 $this->assertEquals('/example', $this->redirectDestination->get());