More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Access / RouteProcessorCsrfTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Access;
4
5 use Drupal\Component\Utility\Crypt;
6 use Drupal\Core\Render\BubbleableMetadata;
7 use Drupal\Tests\UnitTestCase;
8 use Drupal\Core\Access\RouteProcessorCsrf;
9 use Symfony\Component\Routing\Route;
10
11 /**
12  * @coversDefaultClass \Drupal\Core\Access\RouteProcessorCsrf
13  * @group Access
14  */
15 class RouteProcessorCsrfTest extends UnitTestCase {
16
17   /**
18    * The mock CSRF token generator.
19    *
20    * @var \Drupal\Core\Access\CsrfTokenGenerator|\PHPUnit_Framework_MockObject_MockObject
21    */
22   protected $csrfToken;
23
24   /**
25    * The route processor.
26    *
27    * @var \Drupal\Core\Access\RouteProcessorCsrf
28    */
29   protected $processor;
30
31   protected function setUp() {
32     $this->csrfToken = $this->getMockBuilder('Drupal\Core\Access\CsrfTokenGenerator')
33       ->disableOriginalConstructor()
34       ->getMock();
35
36     $this->processor = new RouteProcessorCsrf($this->csrfToken);
37   }
38
39   /**
40  * Tests the processOutbound() method with no _csrf_token route requirement.
41  */
42   public function testProcessOutboundNoRequirement() {
43     $this->csrfToken->expects($this->never())
44       ->method('get');
45
46     $route = new Route('/test-path');
47     $parameters = [];
48
49     $bubbleable_metadata = new BubbleableMetadata();
50     $this->processor->processOutbound('test', $route, $parameters, $bubbleable_metadata);
51     // No parameters should be added to the parameters array.
52     $this->assertEmpty($parameters);
53     // Cacheability of routes without a _csrf_token route requirement is
54     // unaffected.
55     $this->assertEquals((new BubbleableMetadata()), $bubbleable_metadata);
56   }
57
58   /**
59    * Tests the processOutbound() method with a _csrf_token route requirement.
60    */
61   public function testProcessOutbound() {
62     $route = new Route('/test-path', [], ['_csrf_token' => 'TRUE']);
63     $parameters = [];
64
65     $bubbleable_metadata = new BubbleableMetadata();
66     $this->processor->processOutbound('test', $route, $parameters, $bubbleable_metadata);
67     // 'token' should be added to the parameters array.
68     $this->assertArrayHasKey('token', $parameters);
69     // Bubbleable metadata of routes with a _csrf_token route requirement is a
70     // placeholder.
71     $path = 'test-path';
72     $placeholder = Crypt::hashBase64($path);
73     $placeholder_render_array = [
74       '#lazy_builder' => ['route_processor_csrf:renderPlaceholderCsrfToken', [$path]],
75     ];
76     $this->assertSame($parameters['token'], $placeholder);
77     $this->assertEquals((new BubbleableMetadata())->setAttachments(['placeholders' => [$placeholder => $placeholder_render_array]]), $bubbleable_metadata);
78   }
79
80   /**
81    * Tests the processOutbound() method with a dynamic path and one replacement.
82    */
83   public function testProcessOutboundDynamicOne() {
84     $route = new Route('/test-path/{slug}', [], ['_csrf_token' => 'TRUE']);
85     $parameters = ['slug' => 100];
86
87     $bubbleable_metadata = new BubbleableMetadata();
88     $this->processor->processOutbound('test', $route, $parameters, $bubbleable_metadata);
89     // Bubbleable metadata of routes with a _csrf_token route requirement is a
90     // placeholder.
91     $path = 'test-path/100';
92     $placeholder = Crypt::hashBase64($path);
93     $placeholder_render_array = [
94       '#lazy_builder' => ['route_processor_csrf:renderPlaceholderCsrfToken', [$path]],
95     ];
96     $this->assertEquals((new BubbleableMetadata())->setAttachments(['placeholders' => [$placeholder => $placeholder_render_array]]), $bubbleable_metadata);
97   }
98
99   /**
100    * Tests the processOutbound() method with two parameter replacements.
101    */
102   public function testProcessOutboundDynamicTwo() {
103     $route = new Route('{slug_1}/test-path/{slug_2}', [], ['_csrf_token' => 'TRUE']);
104     $parameters = ['slug_1' => 100, 'slug_2' => 'test'];
105
106     $bubbleable_metadata = new BubbleableMetadata();
107     $this->processor->processOutbound('test', $route, $parameters, $bubbleable_metadata);
108     // Bubbleable metadata of routes with a _csrf_token route requirement is a
109     // placeholder.
110     $path = '100/test-path/test';
111     $placeholder = Crypt::hashBase64($path);
112     $placeholder_render_array = [
113       '#lazy_builder' => ['route_processor_csrf:renderPlaceholderCsrfToken', [$path]],
114     ];
115     $this->assertEquals((new BubbleableMetadata())->setAttachments(['placeholders' => [$placeholder => $placeholder_render_array]]), $bubbleable_metadata);
116   }
117
118 }