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