35f82332323dd7266858cf45e8eaf58cad0f3f63
[yaffs-website] / web / modules / contrib / redirect / tests / src / Unit / RedirectCheckerTest.php
1 <?php
2
3 namespace Drupal\Tests\redirect\Unit;
4
5 use Drupal\redirect\RedirectChecker;
6 use Drupal\Tests\UnitTestCase;
7 use PHPUnit_Framework_MockObject_MockObject;
8 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
9 use Symfony\Component\HttpFoundation\ParameterBag;
10 use Symfony\Component\Routing\Route;
11
12 /**
13  * Tests the redirect logic.
14  *
15  * @group redirect
16  */
17 class RedirectCheckerTest extends UnitTestCase {
18
19   /**
20    * Tests the can redirect check.
21    */
22   public function testCanRedirect() {
23
24     $config = array('redirect.settings' => array('ignore_admin_path' => FALSE, 'access_check' => TRUE));
25
26     $state = $this->getMockBuilder('Drupal\Core\State\StateInterface')
27       ->getMock();
28     $state->expects($this->any())
29       ->method('get')
30       ->with('system.maintenance_mode')
31       ->will($this->returnValue(FALSE));
32     $access = $this->getMockBuilder('Drupal\Core\Access\AccessManager')
33       ->disableOriginalConstructor()
34       ->getMock();
35     $account = $this->getMockBuilder('Drupal\Core\Session\AccountInterface')
36       ->getMock();
37     $route_provider = $this->getMockBuilder('Drupal\Core\Routing\RouteProviderInterface')
38       ->getMock();
39
40     $route = new Route('/example');
41     $route_provider->expects($this->any())
42       ->method('getRouteByName')
43       ->willReturn($route);
44
45     $access->expects($this->any())
46       ->method('checkNamedRoute')
47       ->willReturnMap([
48         ['denied_route', [], $account, FALSE, FALSE],
49         ['allowed_route', [], $account, FALSE, TRUE],
50       ]);
51
52     $checker = new RedirectChecker($this->getConfigFactoryStub($config), $state, $access, $account, $route_provider);
53
54     // All fine - we can redirect.
55     $request = $this->getRequestStub('index.php', 'GET');
56     $this->assertTrue($checker->canRedirect($request), 'Can redirect');
57
58     // The script name is not index.php.
59     $request = $this->getRequestStub('statistics.php', 'GET');
60     $this->assertFalse($checker->canRedirect($request), 'Cannot redirect script name not index.php');
61
62     // The request method is not GET.
63     $request = $this->getRequestStub('index.php', 'POST');
64     $this->assertFalse($checker->canRedirect($request), 'Cannot redirect other than GET method');
65
66
67     // Route access check, deny access.
68     $request = $this->getRequestStub('index.php', 'GET');
69     $this->assertFalse($checker->canRedirect($request, 'denied_route'), 'Can not redirect');
70
71     // Route access check, allow access.
72     $request = $this->getRequestStub('index.php', 'GET');
73     $this->assertTrue($checker->canRedirect($request, 'allowed_route'), 'Can redirect');
74
75     // Check destination parameter.
76     $request = $this->getRequestStub('index.php', 'GET', [], ['destination' => 'paradise']);
77     $this->assertFalse($checker->canRedirect($request), 'Cannot redirect');
78
79     // Maintenance mode is on.
80     $state = $this->getMockBuilder('Drupal\Core\State\StateInterface')
81       ->getMock();
82     $state->expects($this->any())
83       ->method('get')
84       ->with('system.maintenance_mode')
85       ->will($this->returnValue(TRUE));
86
87     $checker = new RedirectChecker($this->getConfigFactoryStub($config), $state, $access, $account, $route_provider);
88
89     $request = $this->getRequestStub('index.php', 'GET');
90     $this->assertFalse($checker->canRedirect($request), 'Cannot redirect if maintenance mode is on');
91
92     // We are at a admin path.
93     $state = $this->getMockBuilder('Drupal\Core\State\StateInterface')
94       ->getMock();
95     $state->expects($this->any())
96       ->method('get')
97       ->with('system.maintenance_mode')
98       ->will($this->returnValue(FALSE));
99
100 //    $checker = new RedirectChecker($this->getConfigFactoryStub($config), $state);
101 //
102 //    $route = $this->getMockBuilder('Symfony\Component\Routing\Route')
103 //      ->disableOriginalConstructor()
104 //      ->getMock();
105 //    $route->expects($this->any())
106 //      ->method('getOption')
107 //      ->with('_admin_route')
108 //      ->will($this->returnValue('system.admin_config_search'));
109 //
110 //    $request = $this->getRequestStub('index.php', 'GET',
111 //      array(RouteObjectInterface::ROUTE_OBJECT => $route));
112 //    $this->assertFalse($checker->canRedirect($request), 'Cannot redirect if we are requesting a admin path');
113 //
114 //    // We are at admin path with ignore_admin_path set to TRUE.
115 //    $config['redirect.settings']['ignore_admin_path'] = TRUE;
116 //    $checker = new RedirectChecker($this->getConfigFactoryStub($config), $state);
117 //
118 //    $request = $this->getRequestStub('index.php', 'GET',
119 //      array(RouteObjectInterface::ROUTE_OBJECT => $route));
120 //    $this->assertTrue($checker->canRedirect($request), 'Can redirect a admin with ignore_admin_path set to TRUE');
121   }
122
123   /**
124    * Gets request mock object.
125    *
126    * @param string $script_name
127    *   The result of the getScriptName() method.
128    * @param string $method
129    *   The request method.
130    * @param array $attributes
131    *   Attributes to be passed into request->attributes.
132    * @param array $query
133    *   Query paramter to be passed into request->query.
134    *
135    * @return PHPUnit_Framework_MockObject_MockObject
136    *   Mocked request object.
137    */
138   protected function getRequestStub($script_name, $method, array $attributes = [], array $query = []) {
139     $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
140       ->disableOriginalConstructor()
141       ->getMock();
142     $request->expects($this->any())
143       ->method('getScriptName')
144       ->will($this->returnValue($script_name));
145     $request->expects($this->any())
146       ->method('isMethod')
147       ->with($this->anything())
148       ->will($this->returnValue($method == 'GET'));
149     $request->query = new ParameterBag($query);
150     $request->attributes = new ParameterBag($attributes);
151
152     return $request;
153   }
154
155 }