summaryrefslogtreecommitdiffstats
path: root/core/tests/Drupal/Tests/Core/Routing/RedirectDestinationTest.php
blob: 3f388597783fa0719749564fa13310323d3aa368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php

/**
 * @file
 * Contains \Drupal\Tests\Core\Routing\RedirectDestinationTest.
 */

namespace Drupal\Tests\Core\Routing;

use Drupal\Core\Routing\RedirectDestination;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * @coversDefaultClass \Drupal\Core\Routing\RedirectDestination
 * @group Routing
 */
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() {
    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;
      });
  }

  /**
   * @dataProvider providerGet
   *
   * @covers ::get
   */
  public function testGet(Request $request, $expected_destination) {
    $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) {
    $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 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'];

    return $data;
  }

  /**
   * @covers ::set
   * @covers ::get
   */
  public function testSetBeforeGetCall() {
    $this->redirectDestination->set('/example');
    $this->assertEquals('/example', $this->redirectDestination->get());
  }

  /**
   * @covers ::set
   * @covers ::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());
  }

}