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