[TASK] Introduce base class for PHPUnit tests
[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 \TYPO3\CMS\Core\Tests\UnitTestCase {
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 $this->assertEquals($expected, $this->fixture->calculateCacheHash($params));
71 }
72
73 /**
74 * @return array
75 */
76 public function cacheHashCalculationDataprovider() {
77 return array(
78 'Empty parameters should not return a hash' => array(
79 array(),
80 ''
81 ),
82 'Trivial key value combination should generate hash' => array(
83 array(
84 'encryptionKey' => 't3lib_cacheHashTest',
85 'key' => 'value'
86 ),
87 '5cfdcf826275558b3613dd51714a0a17'
88 ),
89 'Multiple parameters should generate hash' => array(
90 array(
91 'a' => 'v',
92 'b' => 'v',
93 'encryptionKey' => 't3lib_cacheHashTest'
94 ),
95 '0f40b089cdad149aea99e9bf4badaa93'
96 )
97 );
98 }
99
100 /**
101 * @dataProvider getRelevantParametersDataprovider
102 * @test
103 */
104 public function getRelevantParametersWorks($params, $expected) {
105 $actual = $this->fixture->getRelevantParameters($params);
106 $this->assertEquals($expected, array_keys($actual));
107 }
108
109 /**
110 * @return array
111 */
112 public function getRelevantParametersDataprovider() {
113 return array(
114 'Empty list should be passed through' => array('', array()),
115 'Simple parameter should be passed through and the encryptionKey should be added' => array(
116 'key=v',
117 array('encryptionKey', 'key')
118 ),
119 'Simple parameter should be passed through' => array(
120 'key1=v&key2=v',
121 array('encryptionKey', 'key1', 'key2')
122 ),
123 'System and exclude paramters should be omitted' => array(
124 'id=1&type=3&exclude1=x&no_cache=1',
125 array()
126 ),
127 'System and exclude paramters should be omitted' => array(
128 'id=1&type=3&key=x&no_cache=1',
129 array('encryptionKey', 'key')
130 )
131 );
132 }
133
134 /**
135 * @dataProvider canGenerateForParametersDataprovider
136 * @test
137 */
138 public function canGenerateForParameters($params, $expected) {
139 $this->assertEquals($expected, $this->fixture->generateForParameters($params));
140 }
141
142 /**
143 * @return array
144 */
145 public function canGenerateForParametersDataprovider() {
146 $knowHash = '5cfdcf826275558b3613dd51714a0a17';
147 return array(
148 'Empty parameters should not return an hash' => array('', ''),
149 'Querystring has no relevant parameters so we should not have a cacheHash' => array('&exclude1=val', ''),
150 'Querystring has only system parameters so we should not have a cacheHash' => array('id=1&type=val', ''),
151 'Trivial key value combination should generate hash' => array('&key=value', $knowHash),
152 'Only the relevant parts should be taken into account' => array('&key=value&exclude1=val', $knowHash),
153 'Only the relevant parts should be taken into account' => array('&exclude2=val&key=value', $knowHash),
154 'System parameters should not be taken into account' => array('&id=1&key=value', $knowHash),
155 'Admin panel parameters should not be taken into account' => array('&TSFE_ADMIN_PANEL[display]=7&key=value', $knowHash),
156 'Trivial hash for sorted parameters should be right' => array('a=v&b=v', '0f40b089cdad149aea99e9bf4badaa93'),
157 'Parameters should be sorted before is created' => array('b=v&a=v', '0f40b089cdad149aea99e9bf4badaa93')
158 );
159 }
160
161 /**
162 * @dataProvider parametersRequireCacheHashDataprovider
163 * @test
164 */
165 public function parametersRequireCacheHashWorks($params, $expected) {
166 $this->assertEquals($expected, $this->fixture->doParametersRequireCacheHash($params));
167 }
168
169 /**
170 * @return array
171 */
172 public function parametersRequireCacheHashDataprovider() {
173 return array(
174 'Empty parameter strings should not require anything.' => array('', FALSE),
175 'Normal parameters aren\'t required.' => array('key=value', FALSE),
176 'Configured "req1" to be required.' => array('req1=value', TRUE),
177 'Configured "req1" to be requiredm, should also work in combined context' => array('&key=value&req1=value', TRUE),
178 'Configured "req1" to be requiredm, should also work in combined context' => array('req1=value&key=value', TRUE)
179 );
180 }
181
182 /**
183 * In case the cHashOnlyForParameters is set, other parameters should not
184 * incluence the cHash (except the encryption key of course)
185 *
186 * @dataProvider canWhitelistParametersDataprovider
187 * @test
188 */
189 public function canWhitelistParameters($params, $expected) {
190 $method = new \ReflectionMethod('TYPO3\\CMS\\Frontend\\Page\\CacheHashCalculator', 'setCachedParametersWhiteList');
191 $method->setAccessible(TRUE);
192 $method->invoke($this->fixture, array('whitep1', 'whitep2'));
193 $this->assertEquals($expected, $this->fixture->generateForParameters($params));
194 }
195
196 /**
197 * @return array
198 */
199 public function canWhitelistParametersDataprovider() {
200 $oneParamHash = 'e2c0f2edf08be18bcff2f4272e11f66b';
201 $twoParamHash = 'f6f08c2e10a97d91b6ec61a6e2ddd0e7';
202 return array(
203 'Even with the whitelist enabled, empty parameters should not return an hash.' => array('', ''),
204 'Whitelisted parameters should have a hash.' => array('whitep1=value', $oneParamHash),
205 'Blacklisted parameter should not influence hash.' => array('whitep1=value&black=value', $oneParamHash),
206 'Multiple whitelisted parameters should work' => array('&whitep1=value&whitep2=value', $twoParamHash),
207 'The order should not influce the hash.' => array('whitep2=value&black=value&whitep1=value', $twoParamHash)
208 );
209 }
210
211 /**
212 * @dataProvider canSkipParametersWithEmptyValuesDataprovider
213 * @test
214 */
215 public function canSkipParametersWithEmptyValues($params, $settings, $expected) {
216 $this->fixture->setConfiguration($settings);
217 $actual = $this->fixture->getRelevantParameters($params);
218 $this->assertEquals($expected, array_keys($actual));
219 }
220
221 /**
222 * @return array
223 */
224 public function canSkipParametersWithEmptyValuesDataprovider() {
225 return array(
226 'The default configuration does not allow to skip an empty key.' => array(
227 'key1=v&key2=&key3=',
228 array('excludedParametersIfEmpty' => array(), 'excludeAllEmptyParameters' => FALSE),
229 array('encryptionKey', 'key1', 'key2', 'key3')
230 ),
231 'Due to the empty value, "key2" should be skipped' => array(
232 'key1=v&key2=&key3=',
233 array('excludedParametersIfEmpty' => array('key2'), 'excludeAllEmptyParameters' => FALSE),
234 array('encryptionKey', 'key1', 'key3')
235 ),
236 'Due to the empty value, "key2" should be skipped' => array(
237 'key1=v&key2&key3',
238 array('excludedParametersIfEmpty' => array('key2'), 'excludeAllEmptyParameters' => FALSE),
239 array('encryptionKey', 'key1', 'key3')
240 ),
241 'Due to the empty value, "key2" and "key3" should be skipped' => array(
242 'key1=v&key2=&key3=',
243 array('excludedParametersIfEmpty' => array(), 'excludeAllEmptyParameters' => TRUE),
244 array('encryptionKey', 'key1')
245 )
246 );
247 }
248
249 }
250
251 ?>