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