class CsrfAccessCheckTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Core/Access/CsrfAccessCheckTest.php \Drupal\Tests\Core\Access\CsrfAccessCheckTest
@coversDefaultClass \Drupal\Core\Access\CsrfAccessCheck @group Access
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Core\Access\CsrfAccessCheckTest
Expanded class hierarchy of CsrfAccessCheckTest
File
- core/
tests/ Drupal/ Tests/ Core/ Access/ CsrfAccessCheckTest.php, line 20 - Contains \Drupal\Tests\Core\Access\CsrfAccessCheckTest.
Namespace
Drupal\Tests\Core\AccessView source
class CsrfAccessCheckTest extends UnitTestCase {
/**
* The mock CSRF token generator.
*
* @var \Drupal\Core\Access\CsrfTokenGenerator|\PHPUnit_Framework_MockObject_MockObject
*/
protected $csrfToken;
/**
* The access checker.
*
* @var \Drupal\Core\Access\CsrfAccessCheck
*/
protected $accessCheck;
/**
* The mock route match.
*
* @var \Drupal\Core\RouteMatch\RouteMatchInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $routeMatch;
protected function setUp() {
$this->csrfToken = $this
->getMockBuilder('Drupal\\Core\\Access\\CsrfTokenGenerator')
->disableOriginalConstructor()
->getMock();
$this->routeMatch = $this
->getMock('Drupal\\Core\\Routing\\RouteMatchInterface');
$this->accessCheck = new CsrfAccessCheck($this->csrfToken);
}
/**
* Tests the access() method with a valid token.
*/
public function testAccessTokenPass() {
$this->csrfToken
->expects($this
->once())
->method('validate')
->with('test_query', 'test-path/42')
->will($this
->returnValue(TRUE));
$this->routeMatch
->expects($this
->once())
->method('getRawParameters')
->will($this
->returnValue(array(
'node' => 42,
)));
$route = new Route('/test-path/{node}', array(), array(
'_csrf_token' => 'TRUE',
));
$request = Request::create('/test-path/42?token=test_query');
$this
->assertEquals(AccessResult::allowed()
->setCacheMaxAge(0), $this->accessCheck
->access($route, $request, $this->routeMatch));
}
/**
* Tests the access() method with an invalid token.
*/
public function testAccessTokenFail() {
$this->csrfToken
->expects($this
->once())
->method('validate')
->with('test_query', 'test-path')
->will($this
->returnValue(FALSE));
$this->routeMatch
->expects($this
->once())
->method('getRawParameters')
->will($this
->returnValue(array()));
$route = new Route('/test-path', array(), array(
'_csrf_token' => 'TRUE',
));
$request = Request::create('/test-path?token=test_query');
$this
->assertEquals(AccessResult::forbidden()
->setCacheMaxAge(0), $this->accessCheck
->access($route, $request, $this->routeMatch));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CsrfAccessCheckTest:: |
protected | property | The access checker. | |
CsrfAccessCheckTest:: |
protected | property | The mock CSRF token generator. | |
CsrfAccessCheckTest:: |
protected | property | The mock route match. | |
CsrfAccessCheckTest:: |
protected | function |
Overrides UnitTestCase:: |
|
CsrfAccessCheckTest:: |
public | function | Tests the access() method with an invalid token. | |
CsrfAccessCheckTest:: |
public | function | Tests the access() method with a valid token. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed in array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |