‪TYPO3CMS  9.5
CacheHashCalculatorTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20 
24 class ‪CacheHashCalculatorTest extends UnitTestCase
25 {
29  protected ‪$subject;
30 
34  protected ‪$configuration = [
35  'excludedParameters' => ['exclude1', 'exclude2'],
36  'cachedParametersWhiteList' => [],
37  'requireCacheHashPresenceParameters' => ['req1', 'req2'],
38  'excludedParametersIfEmpty' => [],
39  'excludeAllEmptyParameters' => false
40  ];
41 
42  protected function ‪setUp()
43  {
44  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 't3lib_cacheHashTest';
45  $this->subject = new ‪CacheHashCalculator($this->configuration);
46  }
47 
48  protected function ‪tearDown()
49  {
50  unset($this->subject);
51  parent::tearDown();
52  }
53 
58  public function ‪cacheHashCalculationWorks($params, $expected)
59  {
60  self::assertEquals($expected, $this->subject->calculateCacheHash($params));
61  }
62 
66  public function ‪cacheHashCalculationDataProvider()
67  {
68  return [
69  'Empty parameters should not return a hash' => [
70  [],
71  ''
72  ],
73  'Trivial key value combination should generate hash' => [
74  [
75  'encryptionKey' => 't3lib_cacheHashTest',
76  'key' => 'value'
77  ],
78  '5cfdcf826275558b3613dd51714a0a17'
79  ],
80  'Multiple parameters should generate hash' => [
81  [
82  'a' => 'v',
83  'b' => 'v',
84  'encryptionKey' => 't3lib_cacheHashTest'
85  ],
86  '0f40b089cdad149aea99e9bf4badaa93'
87  ]
88  ];
89  }
90 
95  public function ‪getRelevantParametersWorks($params, $expected)
96  {
97  $actual = $this->subject->getRelevantParameters($params);
98  self::assertEquals($expected, array_keys($actual));
99  }
100 
104  public function ‪getRelevantParametersDataprovider()
105  {
106  return [
107  'Empty list should be passed through' => ['', []],
108  'Simple parameter should be passed through and the encryptionKey should be added' => [
109  'key=v&id=42',
110  ['encryptionKey', 'id', 'key']
111  ],
112  'Simple parameter should be passed through' => [
113  'key1=v&key2=v&id=42',
114  ['encryptionKey', 'id', 'key1', 'key2']
115  ],
116  'System and exclude parameters should be omitted' => [
117  'id=1&type=3&exclude1=x&no_cache=1',
118  []
119  ],
120  'System and exclude parameters (except id) should be omitted, others should stay' => [
121  'id=1&type=3&key=x&no_cache=1',
122  ['encryptionKey', 'id', 'key']
123  ],
124  'System and exclude parameters should be omitted and id is not required to be specified' => [
125  'type=3&no_cache=1',
126  []
127  ]
128  ];
129  }
130 
135  public function ‪canGenerateForParameters($params, $expected)
136  {
137  self::assertEquals($expected, $this->subject->generateForParameters($params));
138  }
139 
146  {
147  $this->subject->generateForParameters('&key=x');
148  }
149 
154  {
155  $knowHash = 'fac112f7e662c83c19b57142c3a921f5';
156  return [
157  'Empty parameters should not return an hash' => ['&id=42', ''],
158  'Querystring has no relevant parameters so we should not have a cacheHash' => ['&exclude1=val', ''],
159  'Querystring has only system parameters so we should not have a cacheHash' => ['&id=42&type=val', ''],
160  'Trivial key value combination should generate hash' => ['&id=42&key=value', $knowHash],
161  'Only the relevant parts should be taken into account' => ['&id=42&key=value&exclude1=val', $knowHash],
162  'Only the relevant parts should be taken into account(exclude2 before key)' => ['&id=42&exclude2=val&key=value', $knowHash],
163  'System parameters should not be taken into account (except id)' => ['&id=42&type=23&key=value', $knowHash],
164  'Admin panel parameters should not be taken into account' => ['&id=42&TSFE_ADMIN_PANEL[display]=7&key=value', $knowHash],
165  'Trivial hash for sorted parameters should be right' => ['&id=42&a=v&b=v', '52c8a1299e20324f90377c43153c4987'],
166  'Parameters should be sorted before cHash is created' => ['&id=42&b=v&a=v', '52c8a1299e20324f90377c43153c4987'],
167  'Empty argument names are filtered out before cHash calculation' => ['&id=42&b=v&a=v&=dummy', '52c8a1299e20324f90377c43153c4987']
168  ];
169  }
170 
175  public function ‪parametersRequireCacheHashWorks($params, $expected)
176  {
177  self::assertEquals($expected, $this->subject->doParametersRequireCacheHash($params));
178  }
179 
184  {
185  return [
186  'Empty parameter strings should not require anything.' => ['', false],
187  'Normal parameters aren\'t required.' => ['key=value', false],
188  'Configured "req1" to be required.' => ['req1=value', true],
189  'Configured "req1" to be required, should also work in combined context' => ['&key=value&req1=value', true],
190  'Configured "req1" to be required, should also work in combined context (key at the end)' => ['req1=value&key=value', true]
191  ];
192  }
193 
201  public function ‪canWhitelistParameters($params, $expected)
202  {
203  ‪$configuration = array_merge($this->configuration, [
204  'cachedParametersWhiteList' => ['whitep1', 'whitep2']
205  ]);
206  $this->subject = new CacheHashCalculator(‪$configuration);
207  self::assertEquals($expected, $this->subject->generateForParameters($params));
208  }
209 
213  public function ‪canWhitelistParametersDataProvider()
214  {
215  $oneParamHash = 'eae50a13101afd53a9d2c543230eb5bb';
216  $twoParamHash = '701e2d2f1becc9d1b71d327e5cb1c3ed';
217  return [
218  'Even with the whitelist enabled, empty parameters should not return an hash.' => ['', ''],
219  'Whitelisted parameters should have a hash.' => ['&id=42&whitep1=value', $oneParamHash],
220  'Blacklisted parameter should not influence hash.' => ['&id=42&whitep1=value&black=value', $oneParamHash],
221  'Multiple whitelisted parameters should work' => ['&id=42&whitep1=value&whitep2=value', $twoParamHash],
222  'The order should not influce the hash.' => ['&id=42&whitep2=value&black=value&whitep1=value', $twoParamHash]
223  ];
224  }
225 
230  public function ‪canSkipParametersWithEmptyValues($params, $settings, $expected)
231  {
232  $this->subject->setConfiguration($settings);
233  $actual = $this->subject->getRelevantParameters($params);
234  self::assertEquals($expected, array_keys($actual));
235  }
236 
241  {
242  return [
243  'The default configuration does not allow to skip an empty key.' => [
244  '&id=42&key1=v&key2=&key3=',
245  ['excludedParametersIfEmpty' => [], 'excludeAllEmptyParameters' => false],
246  ['encryptionKey', 'id', 'key1', 'key2', 'key3']
247  ],
248  'Due to the empty value, "key2" should be skipped(with equals sign' => [
249  '&id=42&key1=v&key2=&key3=',
250  ['excludedParametersIfEmpty' => ['key2'], 'excludeAllEmptyParameters' => false],
251  ['encryptionKey', 'id', 'key1', 'key3']
252  ],
253  'Due to the empty value, "key2" should be skipped(without equals sign)' => [
254  '&id=42&key1=v&key2&key3',
255  ['excludedParametersIfEmpty' => ['key2'], 'excludeAllEmptyParameters' => false],
256  ['encryptionKey', 'id', 'key1', 'key3']
257  ],
258  'Due to the empty value, "key2" and "key3" should be skipped' => [
259  '&id=42&key1=v&key2=&key3=',
260  ['excludedParametersIfEmpty' => [], 'excludeAllEmptyParameters' => true],
261  ['encryptionKey', 'id', 'key1']
262  ]
263  ];
264  }
265 }
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canWhitelistParametersDataProvider
‪array canWhitelistParametersDataProvider()
Definition: CacheHashCalculatorTest.php:211
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest
Definition: CacheHashCalculatorTest.php:25
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\parametersRequireCacheHashDataprovider
‪array parametersRequireCacheHashDataprovider()
Definition: CacheHashCalculatorTest.php:181
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\cacheHashCalculationDataProvider
‪array cacheHashCalculationDataProvider()
Definition: CacheHashCalculatorTest.php:64
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\cacheHashCalculationWorks
‪cacheHashCalculationWorks($params, $expected)
Definition: CacheHashCalculatorTest.php:56
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\$configuration
‪array $configuration
Definition: CacheHashCalculatorTest.php:32
‪TYPO3\CMS\Frontend\Tests\Unit\Page
Definition: CacheHashCalculatorTest.php:3
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canGenerateForParametersDataProvider
‪array canGenerateForParametersDataProvider()
Definition: CacheHashCalculatorTest.php:151
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canSkipParametersWithEmptyValues
‪canSkipParametersWithEmptyValues($params, $settings, $expected)
Definition: CacheHashCalculatorTest.php:228
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canWhitelistParameters
‪canWhitelistParameters($params, $expected)
Definition: CacheHashCalculatorTest.php:199
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\getRelevantParametersDataprovider
‪array getRelevantParametersDataprovider()
Definition: CacheHashCalculatorTest.php:102
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\getRelevantParametersWorks
‪getRelevantParametersWorks($params, $expected)
Definition: CacheHashCalculatorTest.php:93
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\setUp
‪setUp()
Definition: CacheHashCalculatorTest.php:40
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator
Definition: CacheHashCalculator.php:24
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canGenerateForParameters
‪canGenerateForParameters($params, $expected)
Definition: CacheHashCalculatorTest.php:133
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\parametersRequireCacheHashWorks
‪parametersRequireCacheHashWorks($params, $expected)
Definition: CacheHashCalculatorTest.php:173
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canSkipParametersWithEmptyValuesDataProvider
‪array canSkipParametersWithEmptyValuesDataProvider()
Definition: CacheHashCalculatorTest.php:238
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\$subject
‪CacheHashCalculator $subject
Definition: CacheHashCalculatorTest.php:28
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\generateForParametersThrowsExceptionWhenIdIsNotSpecified
‪generateForParametersThrowsExceptionWhenIdIsNotSpecified()
Definition: CacheHashCalculatorTest.php:143
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\tearDown
‪tearDown()
Definition: CacheHashCalculatorTest.php:46