TYPO3  7.6
CacheHashCalculatorTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Frontend\Tests\Unit\Page;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 class CacheHashCalculatorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
25  protected $subject;
26 
30  protected $confCache = array();
31 
32  protected function setUp()
33  {
34  $this->confCache = array(
35  'encryptionKey' => $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey']
36  );
37  $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 't3lib_cacheHashTest';
38  $this->subject = $this->getMock(\TYPO3\CMS\Frontend\Page\CacheHashCalculator::class, array('foo'));
39  $this->subject->setConfiguration(array(
40  'excludedParameters' => array('exclude1', 'exclude2'),
41  'cachedParametersWhiteList' => array(),
42  'requireCacheHashPresenceParameters' => array('req1', 'req2'),
43  'excludedParametersIfEmpty' => array(),
44  'excludeAllEmptyParameters' => false
45  ));
46  }
47 
52  public function cacheHashCalculationWorks($params, $expected)
53  {
54  $this->assertEquals($expected, $this->subject->calculateCacheHash($params));
55  }
56 
61  {
62  return array(
63  'Empty parameters should not return a hash' => array(
64  array(),
65  ''
66  ),
67  'Trivial key value combination should generate hash' => array(
68  array(
69  'encryptionKey' => 't3lib_cacheHashTest',
70  'key' => 'value'
71  ),
72  '5cfdcf826275558b3613dd51714a0a17'
73  ),
74  'Multiple parameters should generate hash' => array(
75  array(
76  'a' => 'v',
77  'b' => 'v',
78  'encryptionKey' => 't3lib_cacheHashTest'
79  ),
80  '0f40b089cdad149aea99e9bf4badaa93'
81  )
82  );
83  }
84 
89  public function getRelevantParametersWorks($params, $expected)
90  {
91  $actual = $this->subject->getRelevantParameters($params);
92  $this->assertEquals($expected, array_keys($actual));
93  }
94 
99  {
100  return array(
101  'Empty list should be passed through' => array('', array()),
102  'Simple parameter should be passed through and the encryptionKey should be added' => array(
103  'key=v',
104  array('encryptionKey', 'key')
105  ),
106  'Simple parameter should be passed through' => array(
107  'key1=v&key2=v',
108  array('encryptionKey', 'key1', 'key2')
109  ),
110  'System and exclude parameters should be omitted' => array(
111  'id=1&type=3&exclude1=x&no_cache=1',
112  array()
113  ),
114  'System and exclude parameters should be omitted, others should stay' => array(
115  'id=1&type=3&key=x&no_cache=1',
116  array('encryptionKey', 'key')
117  )
118  );
119  }
120 
125  public function canGenerateForParameters($params, $expected)
126  {
127  $this->assertEquals($expected, $this->subject->generateForParameters($params));
128  }
129 
134  {
135  $knowHash = '5cfdcf826275558b3613dd51714a0a17';
136  return array(
137  'Empty parameters should not return an hash' => array('', ''),
138  'Querystring has no relevant parameters so we should not have a cacheHash' => array('&exclude1=val', ''),
139  'Querystring has only system parameters so we should not have a cacheHash' => array('id=1&type=val', ''),
140  'Trivial key value combination should generate hash' => array('&key=value', $knowHash),
141  'Only the relevant parts should be taken into account' => array('&key=value&exclude1=val', $knowHash),
142  'Only the relevant parts should be taken into account(exclude2 before key)' => array('&exclude2=val&key=value', $knowHash),
143  'System parameters should not be taken into account' => array('&id=1&key=value', $knowHash),
144  'Admin panel parameters should not be taken into account' => array('&TSFE_ADMIN_PANEL[display]=7&key=value', $knowHash),
145  'Trivial hash for sorted parameters should be right' => array('a=v&b=v', '0f40b089cdad149aea99e9bf4badaa93'),
146  'Parameters should be sorted before is created' => array('b=v&a=v', '0f40b089cdad149aea99e9bf4badaa93')
147  );
148  }
149 
154  public function parametersRequireCacheHashWorks($params, $expected)
155  {
156  $this->assertEquals($expected, $this->subject->doParametersRequireCacheHash($params));
157  }
158 
163  {
164  return array(
165  'Empty parameter strings should not require anything.' => array('', false),
166  'Normal parameters aren\'t required.' => array('key=value', false),
167  'Configured "req1" to be required.' => array('req1=value', true),
168  'Configured "req1" to be required, should also work in combined context' => array('&key=value&req1=value', true),
169  'Configured "req1" to be required, should also work in combined context (key at the end)' => array('req1=value&key=value', true)
170  );
171  }
172 
180  public function canWhitelistParameters($params, $expected)
181  {
182  $method = new \ReflectionMethod(\TYPO3\CMS\Frontend\Page\CacheHashCalculator::class, 'setCachedParametersWhiteList');
183  $method->setAccessible(true);
184  $method->invoke($this->subject, array('whitep1', 'whitep2'));
185  $this->assertEquals($expected, $this->subject->generateForParameters($params));
186  }
187 
192  {
193  $oneParamHash = 'e2c0f2edf08be18bcff2f4272e11f66b';
194  $twoParamHash = 'f6f08c2e10a97d91b6ec61a6e2ddd0e7';
195  return array(
196  'Even with the whitelist enabled, empty parameters should not return an hash.' => array('', ''),
197  'Whitelisted parameters should have a hash.' => array('whitep1=value', $oneParamHash),
198  'Blacklisted parameter should not influence hash.' => array('whitep1=value&black=value', $oneParamHash),
199  'Multiple whitelisted parameters should work' => array('&whitep1=value&whitep2=value', $twoParamHash),
200  'The order should not influce the hash.' => array('whitep2=value&black=value&whitep1=value', $twoParamHash)
201  );
202  }
203 
208  public function canSkipParametersWithEmptyValues($params, $settings, $expected)
209  {
210  $this->subject->setConfiguration($settings);
211  $actual = $this->subject->getRelevantParameters($params);
212  $this->assertEquals($expected, array_keys($actual));
213  }
214 
219  {
220  return array(
221  'The default configuration does not allow to skip an empty key.' => array(
222  'key1=v&key2=&key3=',
223  array('excludedParametersIfEmpty' => array(), 'excludeAllEmptyParameters' => false),
224  array('encryptionKey', 'key1', 'key2', 'key3')
225  ),
226  'Due to the empty value, "key2" should be skipped(with equals sign' => array(
227  'key1=v&key2=&key3=',
228  array('excludedParametersIfEmpty' => array('key2'), 'excludeAllEmptyParameters' => false),
229  array('encryptionKey', 'key1', 'key3')
230  ),
231  'Due to the empty value, "key2" should be skipped(without equals sign)' => 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" and "key3" should be skipped' => array(
237  'key1=v&key2=&key3=',
238  array('excludedParametersIfEmpty' => array(), 'excludeAllEmptyParameters' => true),
239  array('encryptionKey', 'key1')
240  )
241  );
242  }
243 }