class RouteProcessorCsrfTest in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/tests/Drupal/Tests/Core/Access/RouteProcessorCsrfTest.php \Drupal\Tests\Core\Access\RouteProcessorCsrfTest
@coversDefaultClass \Drupal\Core\Access\RouteProcessorCsrf @group Access
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
- class \Drupal\Tests\Core\Access\RouteProcessorCsrfTest
Expanded class hierarchy of RouteProcessorCsrfTest
File
- core/
tests/ Drupal/ Tests/ Core/ Access/ RouteProcessorCsrfTest.php, line 19 - Contains \Drupal\Tests\Core\Access\RouteProcessorCsrfTest.
Namespace
Drupal\Tests\Core\AccessView source
class RouteProcessorCsrfTest extends UnitTestCase {
/**
* The mock CSRF token generator.
*
* @var \Drupal\Core\Access\CsrfTokenGenerator|\PHPUnit_Framework_MockObject_MockObject
*/
protected $csrfToken;
/**
* The route processor.
*
* @var \Drupal\Core\Access\RouteProcessorCsrf
*/
protected $processor;
protected function setUp() {
$this->csrfToken = $this
->getMockBuilder('Drupal\\Core\\Access\\CsrfTokenGenerator')
->disableOriginalConstructor()
->getMock();
$this->processor = new RouteProcessorCsrf($this->csrfToken);
}
/**
* Tests the processOutbound() method with no _csrf_token route requirement.
*/
public function testProcessOutboundNoRequirement() {
$this->csrfToken
->expects($this
->never())
->method('get');
$route = new Route('/test-path');
$parameters = array();
$bubbleable_metadata = new BubbleableMetadata();
$this->processor
->processOutbound('test', $route, $parameters, $bubbleable_metadata);
// No parameters should be added to the parameters array.
$this
->assertEmpty($parameters);
// Cacheability of routes without a _csrf_token route requirement is
// unaffected.
$this
->assertEquals(new BubbleableMetadata(), $bubbleable_metadata);
}
/**
* Tests the processOutbound() method with a _csrf_token route requirement.
*/
public function testProcessOutbound() {
$route = new Route('/test-path', array(), array(
'_csrf_token' => 'TRUE',
));
$parameters = array();
$bubbleable_metadata = new BubbleableMetadata();
$this->processor
->processOutbound('test', $route, $parameters, $bubbleable_metadata);
// 'token' should be added to the parameters array.
$this
->assertArrayHasKey('token', $parameters);
// Bubbleable metadata of routes with a _csrf_token route requirement is a
// placeholder.
$path = 'test-path';
$placeholder = hash('sha1', $path);
$placeholder_render_array = [
'#lazy_builder' => [
'route_processor_csrf:renderPlaceholderCsrfToken',
[
$path,
],
],
];
$this
->assertSame($parameters['token'], $placeholder);
$this
->assertEquals((new BubbleableMetadata())
->setAttachments([
'placeholders' => [
$placeholder => $placeholder_render_array,
],
]), $bubbleable_metadata);
}
/**
* Tests the processOutbound() method with a dynamic path and one replacement.
*/
public function testProcessOutboundDynamicOne() {
$route = new Route('/test-path/{slug}', array(), array(
'_csrf_token' => 'TRUE',
));
$parameters = array(
'slug' => 100,
);
$bubbleable_metadata = new BubbleableMetadata();
$this->processor
->processOutbound('test', $route, $parameters, $bubbleable_metadata);
// Bubbleable metadata of routes with a _csrf_token route requirement is a
// placeholder.
$path = 'test-path/100';
$placeholder = hash('sha1', $path);
$placeholder_render_array = [
'#lazy_builder' => [
'route_processor_csrf:renderPlaceholderCsrfToken',
[
$path,
],
],
];
$this
->assertEquals((new BubbleableMetadata())
->setAttachments([
'placeholders' => [
$placeholder => $placeholder_render_array,
],
]), $bubbleable_metadata);
}
/**
* Tests the processOutbound() method with two parameter replacements.
*/
public function testProcessOutboundDynamicTwo() {
$route = new Route('{slug_1}/test-path/{slug_2}', array(), array(
'_csrf_token' => 'TRUE',
));
$parameters = array(
'slug_1' => 100,
'slug_2' => 'test',
);
$bubbleable_metadata = new BubbleableMetadata();
$this->processor
->processOutbound('test', $route, $parameters, $bubbleable_metadata);
// Bubbleable metadata of routes with a _csrf_token route requirement is a
// placeholder.
$path = '100/test-path/test';
$placeholder = hash('sha1', $path);
$placeholder_render_array = [
'#lazy_builder' => [
'route_processor_csrf:renderPlaceholderCsrfToken',
[
$path,
],
],
];
$this
->assertEquals((new BubbleableMetadata())
->setAttachments([
'placeholders' => [
$placeholder => $placeholder_render_array,
],
]), $bubbleable_metadata);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RouteProcessorCsrfTest:: |
protected | property | The mock CSRF token generator. | |
RouteProcessorCsrfTest:: |
protected | property | The route processor. | |
RouteProcessorCsrfTest:: |
protected | function |
Overrides UnitTestCase:: |
|
RouteProcessorCsrfTest:: |
public | function | Tests the processOutbound() method with a _csrf_token route requirement. | |
RouteProcessorCsrfTest:: |
public | function | Tests the processOutbound() method with a dynamic path and one replacement. | |
RouteProcessorCsrfTest:: |
public | function | Tests the processOutbound() method with two parameter replacements. | |
RouteProcessorCsrfTest:: |
public | function | Tests the processOutbound() method with no _csrf_token route requirement. | |
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. |