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