3 namespace Drupal\Tests\redirect\Unit;
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;
13 * Tests the redirect logic.
17 class RedirectCheckerTest extends UnitTestCase {
20 * Tests the can redirect check.
22 public function testCanRedirect() {
24 $config = array('redirect.settings' => array('ignore_admin_path' => FALSE, 'access_check' => TRUE));
26 $state = $this->getMockBuilder('Drupal\Core\State\StateInterface')
28 $state->expects($this->any())
30 ->with('system.maintenance_mode')
31 ->will($this->returnValue(FALSE));
32 $access = $this->getMockBuilder('Drupal\Core\Access\AccessManager')
33 ->disableOriginalConstructor()
35 $account = $this->getMockBuilder('Drupal\Core\Session\AccountInterface')
37 $route_provider = $this->getMockBuilder('Drupal\Core\Routing\RouteProviderInterface')
40 $route = new Route('/example');
41 $route_provider->expects($this->any())
42 ->method('getRouteByName')
45 $access->expects($this->any())
46 ->method('checkNamedRoute')
48 ['denied_route', [], $account, FALSE, FALSE],
49 ['allowed_route', [], $account, FALSE, TRUE],
52 $checker = new RedirectChecker($this->getConfigFactoryStub($config), $state, $access, $account, $route_provider);
54 // All fine - we can redirect.
55 $request = $this->getRequestStub('index.php', 'GET');
56 $this->assertTrue($checker->canRedirect($request), 'Can redirect');
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');
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');
67 // Route access check, deny access.
68 $request = $this->getRequestStub('index.php', 'GET');
69 $this->assertFalse($checker->canRedirect($request, 'denied_route'), 'Can not redirect');
71 // Route access check, allow access.
72 $request = $this->getRequestStub('index.php', 'GET');
73 $this->assertTrue($checker->canRedirect($request, 'allowed_route'), 'Can redirect');
75 // Check destination parameter.
76 $request = $this->getRequestStub('index.php', 'GET', [], ['destination' => 'paradise']);
77 $this->assertFalse($checker->canRedirect($request), 'Cannot redirect');
79 // Maintenance mode is on.
80 $state = $this->getMockBuilder('Drupal\Core\State\StateInterface')
82 $state->expects($this->any())
84 ->with('system.maintenance_mode')
85 ->will($this->returnValue(TRUE));
87 $checker = new RedirectChecker($this->getConfigFactoryStub($config), $state, $access, $account, $route_provider);
89 $request = $this->getRequestStub('index.php', 'GET');
90 $this->assertFalse($checker->canRedirect($request), 'Cannot redirect if maintenance mode is on');
92 // We are at a admin path.
93 $state = $this->getMockBuilder('Drupal\Core\State\StateInterface')
95 $state->expects($this->any())
97 ->with('system.maintenance_mode')
98 ->will($this->returnValue(FALSE));
100 // $checker = new RedirectChecker($this->getConfigFactoryStub($config), $state);
102 // $route = $this->getMockBuilder('Symfony\Component\Routing\Route')
103 // ->disableOriginalConstructor()
105 // $route->expects($this->any())
106 // ->method('getOption')
107 // ->with('_admin_route')
108 // ->will($this->returnValue('system.admin_config_search'));
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');
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);
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');
124 * Gets request mock object.
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.
135 * @return PHPUnit_Framework_MockObject_MockObject
136 * Mocked request object.
138 protected function getRequestStub($script_name, $method, array $attributes = [], array $query = []) {
139 $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
140 ->disableOriginalConstructor()
142 $request->expects($this->any())
143 ->method('getScriptName')
144 ->will($this->returnValue($script_name));
145 $request->expects($this->any())
147 ->with($this->anything())
148 ->will($this->returnValue($method == 'GET'));
149 $request->query = new ParameterBag($query);
150 $request->attributes = new ParameterBag($attributes);