class RoutePathGenerationTraitTest
@covers \Drupal\Core\Access\RoutePathGenerationTrait @group Access
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Access\RoutePathGenerationTraitTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of RoutePathGenerationTraitTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Access/ RoutePathGenerationTraitTest.php, line 23
Namespace
Drupal\Tests\Core\AccessView source
class RoutePathGenerationTraitTest extends UnitTestCase {
/**
* The mock CSRF token generator.
*/
protected CsrfTokenGenerator $csrfToken;
/**
* The request stack.
*/
protected RequestStack $requestStack;
/**
* The route processor.
*/
protected RouteProcessorCsrf $processor;
/**
* The CSRF access checker.
*/
protected CsrfAccessCheck $accessCheck;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->csrfToken = $this->getMockBuilder(CsrfTokenGenerator::class)
->disableOriginalConstructor()
->getMock();
// Make CsrfTokenGenerator mock use a simple hash of the value passed as
// parameter, as it is enough for the sake of our tests.
$this->csrfToken
->method('get')
->willReturnCallback(function ($value) {
return hash('sha256', $value);
});
$this->csrfToken
->method('validate')
->willReturnCallback(function ($token, $value) {
return $token === hash('sha256', $value);
});
$this->requestStack = $this->createMock(RequestStack::class);
$this->processor = new RouteProcessorCsrf($this->csrfToken, $this->requestStack);
$this->accessCheck = new CsrfAccessCheck($this->csrfToken);
}
/**
* Tests that CSRF token creation and validation is consistent.
*
* This checks that CsrfAccessCheck() and RouteProcessorCsrf() produce the
* same results.
*
* Multiple cases are provided for an optional parameter (non-empty, empty,
* null, undefined).
*
* @dataProvider providerTestCsrfTokenCompleteLifeCycle
*/
public function testCsrfTokenCompleteLifeCycle($params) : void {
// Mock a route.
$route = $this->createMock(Route::class);
$route->method('getPath')
->willReturn('test/example/{param}');
$route->method('hasRequirement')
->with('_csrf_token')
->willReturn(TRUE);
// Process the route so the "token" param is generated.
$routeParams = $params;
$this->processor
->processOutbound('test.example', $route, $routeParams);
$requestParams = $params + [
'token' => $routeParams['token'],
];
// Mock Parameter bag.
$parameterBag = $this->createMock(ParameterBagInterface::class);
$parameterBag->method('get')
->willReturnCallback(function ($key, $default = NULL) use ($requestParams) {
return $requestParams[$key] ?? $default;
});
$parameterBag->method('all')
->willReturn($requestParams);
// Get a real InputBag because it is a final class.
$inputBag = new InputBag($requestParams);
// Mock Request.
$request = $this->createMock(Request::class);
$request->query = $inputBag;
// Mock RouteMatch.
$routeMatch = $this->createMock(RouteMatchInterface::class);
$routeMatch->method('getRawParameters')
->willReturn($parameterBag);
// Check for allowed access.
$this->assertInstanceOf(AccessResultAllowed::class, $this->accessCheck
->access($route, $request, $routeMatch));
}
/**
* Data provider for testCsrfTokenCompleteLifeCycle().
*
* @return array
* An array of route parameters.
*/
public static function providerTestCsrfTokenCompleteLifeCycle() : array {
return [
[
[
'param' => 'value',
],
],
[
[
'param' => '',
],
],
[
[
'param' => NULL,
],
],
[
[],
],
];
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | |
ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
RoutePathGenerationTraitTest::$accessCheck | protected | property | The CSRF access checker. | |
RoutePathGenerationTraitTest::$csrfToken | protected | property | The mock CSRF token generator. | |
RoutePathGenerationTraitTest::$processor | protected | property | The route processor. | |
RoutePathGenerationTraitTest::$requestStack | protected | property | The request stack. | |
RoutePathGenerationTraitTest::providerTestCsrfTokenCompleteLifeCycle | public static | function | Data provider for testCsrfTokenCompleteLifeCycle(). | |
RoutePathGenerationTraitTest::setUp | protected | function | Overrides UnitTestCase::setUp | |
RoutePathGenerationTraitTest::testCsrfTokenCompleteLifeCycle | public | function | Tests that CSRF token creation and validation is consistent. | |
UnitTestCase::$root | protected | property | The app root. | |
UnitTestCase::getClassResolverStub | protected | function | Returns a stub class resolver. | |
UnitTestCase::getConfigFactoryStub | public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.