39fce70bd38ec2da8a4a3c1e08057be42c040c80
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / Unit / Page / CacheHashCalculatorTest.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Tests\Unit\Page;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Testcase
19 */
20 class CacheHashCalculatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * @var \TYPO3\CMS\Frontend\Page\CacheHashCalculator
24 */
25 protected $subject;
26
27 /**
28 * @var array
29 */
30 protected $confCache = [];
31
32 protected function setUp()
33 {
34 $this->confCache = [
35 'encryptionKey' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
36 ];
37 $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 't3lib_cacheHashTest';
38 $this->subject = $this->getMockBuilder(\TYPO3\CMS\Frontend\Page\CacheHashCalculator::class)
39 ->setMethods(['foo'])
40 ->getMock();
41 $this->subject->setConfiguration([
42 'excludedParameters' => ['exclude1', 'exclude2'],
43 'cachedParametersWhiteList' => [],
44 'requireCacheHashPresenceParameters' => ['req1', 'req2'],
45 'excludedParametersIfEmpty' => [],
46 'excludeAllEmptyParameters' => false
47 ]);
48 }
49
50 /**
51 * @dataProvider cacheHashCalculationDataprovider
52 * @test
53 */
54 public function cacheHashCalculationWorks($params, $expected)
55 {
56 $this->assertEquals($expected, $this->subject->calculateCacheHash($params));
57 }
58
59 /**
60 * @return array
61 */
62 public function cacheHashCalculationDataprovider()
63 {
64 return [
65 'Empty parameters should not return a hash' => [
66 [],
67 ''
68 ],
69 'Trivial key value combination should generate hash' => [
70 [
71 'encryptionKey' => 't3lib_cacheHashTest',
72 'key' => 'value'
73 ],
74 '5cfdcf826275558b3613dd51714a0a17'
75 ],
76 'Multiple parameters should generate hash' => [
77 [
78 'a' => 'v',
79 'b' => 'v',
80 'encryptionKey' => 't3lib_cacheHashTest'
81 ],
82 '0f40b089cdad149aea99e9bf4badaa93'
83 ]
84 ];
85 }
86
87 /**
88 * @dataProvider getRelevantParametersDataprovider
89 * @test
90 */
91 public function getRelevantParametersWorks($params, $expected)
92 {
93 $actual = $this->subject->getRelevantParameters($params);
94 $this->assertEquals($expected, array_keys($actual));
95 }
96
97 /**
98 * @return array
99 */
100 public function getRelevantParametersDataprovider()
101 {
102 return [
103 'Empty list should be passed through' => ['', []],
104 'Simple parameter should be passed through and the encryptionKey should be added' => [
105 'key=v',
106 ['encryptionKey', 'key']
107 ],
108 'Simple parameter should be passed through' => [
109 'key1=v&key2=v',
110 ['encryptionKey', 'key1', 'key2']
111 ],
112 'System and exclude parameters should be omitted' => [
113 'id=1&type=3&exclude1=x&no_cache=1',
114 []
115 ],
116 'System and exclude parameters should be omitted, others should stay' => [
117 'id=1&type=3&key=x&no_cache=1',
118 ['encryptionKey', 'key']
119 ]
120 ];
121 }
122
123 /**
124 * @dataProvider canGenerateForParametersDataprovider
125 * @test
126 */
127 public function canGenerateForParameters($params, $expected)
128 {
129 $this->assertEquals($expected, $this->subject->generateForParameters($params));
130 }
131
132 /**
133 * @return array
134 */
135 public function canGenerateForParametersDataprovider()
136 {
137 $knowHash = '5cfdcf826275558b3613dd51714a0a17';
138 return [
139 'Empty parameters should not return an hash' => ['', ''],
140 'Querystring has no relevant parameters so we should not have a cacheHash' => ['&exclude1=val', ''],
141 'Querystring has only system parameters so we should not have a cacheHash' => ['id=1&type=val', ''],
142 'Trivial key value combination should generate hash' => ['&key=value', $knowHash],
143 'Only the relevant parts should be taken into account' => ['&key=value&exclude1=val', $knowHash],
144 'Only the relevant parts should be taken into account(exclude2 before key)' => ['&exclude2=val&key=value', $knowHash],
145 'System parameters should not be taken into account' => ['&id=1&key=value', $knowHash],
146 'Admin panel parameters should not be taken into account' => ['&TSFE_ADMIN_PANEL[display]=7&key=value', $knowHash],
147 'Trivial hash for sorted parameters should be right' => ['a=v&b=v', '0f40b089cdad149aea99e9bf4badaa93'],
148 'Parameters should be sorted before is created' => ['b=v&a=v', '0f40b089cdad149aea99e9bf4badaa93']
149 ];
150 }
151
152 /**
153 * @dataProvider parametersRequireCacheHashDataprovider
154 * @test
155 */
156 public function parametersRequireCacheHashWorks($params, $expected)
157 {
158 $this->assertEquals($expected, $this->subject->doParametersRequireCacheHash($params));
159 }
160
161 /**
162 * @return array
163 */
164 public function parametersRequireCacheHashDataprovider()
165 {
166 return [
167 'Empty parameter strings should not require anything.' => ['', false],
168 'Normal parameters aren\'t required.' => ['key=value', false],
169 'Configured "req1" to be required.' => ['req1=value', true],
170 'Configured "req1" to be required, should also work in combined context' => ['&key=value&req1=value', true],
171 'Configured "req1" to be required, should also work in combined context (key at the end)' => ['req1=value&key=value', true]
172 ];
173 }
174
175 /**
176 * In case the cHashOnlyForParameters is set, other parameters should not
177 * incluence the cHash (except the encryption key of course)
178 *
179 * @dataProvider canWhitelistParametersDataprovider
180 * @test
181 */
182 public function canWhitelistParameters($params, $expected)
183 {
184 $method = new \ReflectionMethod(\TYPO3\CMS\Frontend\Page\CacheHashCalculator::class, 'setCachedParametersWhiteList');
185 $method->setAccessible(true);
186 $method->invoke($this->subject, ['whitep1', 'whitep2']);
187 $this->assertEquals($expected, $this->subject->generateForParameters($params));
188 }
189
190 /**
191 * @return array
192 */
193 public function canWhitelistParametersDataprovider()
194 {
195 $oneParamHash = 'e2c0f2edf08be18bcff2f4272e11f66b';
196 $twoParamHash = 'f6f08c2e10a97d91b6ec61a6e2ddd0e7';
197 return [
198 'Even with the whitelist enabled, empty parameters should not return an hash.' => ['', ''],
199 'Whitelisted parameters should have a hash.' => ['whitep1=value', $oneParamHash],
200 'Blacklisted parameter should not influence hash.' => ['whitep1=value&black=value', $oneParamHash],
201 'Multiple whitelisted parameters should work' => ['&whitep1=value&whitep2=value', $twoParamHash],
202 'The order should not influce the hash.' => ['whitep2=value&black=value&whitep1=value', $twoParamHash]
203 ];
204 }
205
206 /**
207 * @dataProvider canSkipParametersWithEmptyValuesDataprovider
208 * @test
209 */
210 public function canSkipParametersWithEmptyValues($params, $settings, $expected)
211 {
212 $this->subject->setConfiguration($settings);
213 $actual = $this->subject->getRelevantParameters($params);
214 $this->assertEquals($expected, array_keys($actual));
215 }
216
217 /**
218 * @return array
219 */
220 public function canSkipParametersWithEmptyValuesDataprovider()
221 {
222 return [
223 'The default configuration does not allow to skip an empty key.' => [
224 'key1=v&key2=&key3=',
225 ['excludedParametersIfEmpty' => [], 'excludeAllEmptyParameters' => false],
226 ['encryptionKey', 'key1', 'key2', 'key3']
227 ],
228 'Due to the empty value, "key2" should be skipped(with equals sign' => [
229 'key1=v&key2=&key3=',
230 ['excludedParametersIfEmpty' => ['key2'], 'excludeAllEmptyParameters' => false],
231 ['encryptionKey', 'key1', 'key3']
232 ],
233 'Due to the empty value, "key2" should be skipped(without equals sign)' => [
234 'key1=v&key2&key3',
235 ['excludedParametersIfEmpty' => ['key2'], 'excludeAllEmptyParameters' => false],
236 ['encryptionKey', 'key1', 'key3']
237 ],
238 'Due to the empty value, "key2" and "key3" should be skipped' => [
239 'key1=v&key2=&key3=',
240 ['excludedParametersIfEmpty' => [], 'excludeAllEmptyParameters' => true],
241 ['encryptionKey', 'key1']
242 ]
243 ];
244 }
245 }