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