‪TYPO3CMS  10.4
CacheHashCalculatorTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 class ‪CacheHashCalculatorTest extends UnitTestCase
28 {
32  protected ‪$subject;
33 
37  protected ‪$configuration = [
38  'excludedParameters' => ['exclude1', 'exclude2'],
39  'cachedParametersWhiteList' => [],
40  'requireCacheHashPresenceParameters' => ['req1', 'req2'],
41  'excludedParametersIfEmpty' => [],
42  'excludeAllEmptyParameters' => false
43  ];
44 
45  protected function ‪setUp(): void
46  {
47  parent::setUp();
48  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 't3lib_cacheHashTest';
49  $this->subject = new ‪CacheHashCalculator(new ‪CacheHashConfiguration($this->configuration));
50  }
51 
52  protected function ‪tearDown(): void
53  {
54  unset($this->subject);
55  parent::tearDown();
56  }
57 
62  public function ‪cacheHashCalculationWorks($params, $expected)
63  {
64  self::assertEquals($expected, $this->subject->calculateCacheHash($params));
65  }
66 
70  public function ‪cacheHashCalculationDataProvider()
71  {
72  return [
73  'Empty parameters should not return a hash' => [
74  [],
75  ''
76  ],
77  'Trivial key value combination should generate hash' => [
78  [
79  'encryptionKey' => 't3lib_cacheHashTest',
80  'key' => 'value'
81  ],
82  '5cfdcf826275558b3613dd51714a0a17'
83  ],
84  'Multiple parameters should generate hash' => [
85  [
86  'a' => 'v',
87  'b' => 'v',
88  'encryptionKey' => 't3lib_cacheHashTest'
89  ],
90  '0f40b089cdad149aea99e9bf4badaa93'
91  ]
92  ];
93  }
94 
99  public function ‪getRelevantParametersWorks($params, $expected)
100  {
101  $actual = $this->subject->getRelevantParameters($params);
102  self::assertEquals($expected, array_keys($actual));
103  }
104 
108  public function ‪getRelevantParametersDataprovider()
109  {
110  return [
111  'Empty list should be passed through' => ['', []],
112  'Simple parameter should be passed through and the encryptionKey should be added' => [
113  'key=v&id=42',
114  ['encryptionKey', 'id', 'key']
115  ],
116  'Simple parameter should be passed through' => [
117  'key1=v&key2=v&id=42',
118  ['encryptionKey', 'id', 'key1', 'key2']
119  ],
120  'System and exclude parameters should be omitted' => [
121  'id=1&type=3&exclude1=x&no_cache=1',
122  []
123  ],
124  'System and exclude parameters (except id) should be omitted, others should stay' => [
125  'id=1&type=3&key=x&no_cache=1',
126  ['encryptionKey', 'id', 'key']
127  ],
128  'System and exclude parameters should be omitted and id is not required to be specified' => [
129  'type=3&no_cache=1',
130  []
131  ]
132  ];
133  }
134 
139  public function ‪canGenerateForParameters($params, $expected)
140  {
141  self::assertEquals($expected, $this->subject->generateForParameters($params));
142  }
143 
148  {
149  $this->expectException(\RuntimeException::class);
150  $this->expectExceptionCode(1467983513);
151  $this->subject->generateForParameters('&key=x');
152  }
153 
158  {
159  $knowHash = 'fac112f7e662c83c19b57142c3a921f5';
160  return [
161  'Empty parameters should not return an hash' => ['&id=42', ''],
162  'Querystring has no relevant parameters so we should not have a cacheHash' => ['&exclude1=val', ''],
163  'Querystring has only system parameters so we should not have a cacheHash' => ['&id=42&type=val', ''],
164  'Trivial key value combination should generate hash' => ['&id=42&key=value', $knowHash],
165  'Only the relevant parts should be taken into account' => ['&id=42&key=value&exclude1=val', $knowHash],
166  'Only the relevant parts should be taken into account(exclude2 before key)' => ['&id=42&exclude2=val&key=value', $knowHash],
167  'System parameters should not be taken into account (except id)' => ['&id=42&type=23&key=value', $knowHash],
168  'Trivial hash for sorted parameters should be right' => ['&id=42&a=v&b=v', '52c8a1299e20324f90377c43153c4987'],
169  'Parameters should be sorted before cHash is created' => ['&id=42&b=v&a=v', '52c8a1299e20324f90377c43153c4987'],
170  'Empty argument names are filtered out before cHash calculation' => ['&id=42&b=v&a=v&=dummy', '52c8a1299e20324f90377c43153c4987']
171  ];
172  }
173 
178  public function ‪parametersRequireCacheHashWorks($params, $expected)
179  {
180  self::assertEquals($expected, $this->subject->doParametersRequireCacheHash($params));
181  }
182 
187  {
188  return [
189  'Empty parameter strings should not require anything.' => ['', false],
190  'Normal parameters aren\'t required.' => ['key=value', false],
191  'Configured "req1" to be required.' => ['req1=value', true],
192  'Configured "req1" to be required, should also work in combined context' => ['&key=value&req1=value', true],
193  'Configured "req1" to be required, should also work in combined context (key at the end)' => ['req1=value&key=value', true]
194  ];
195  }
196 
204  public function ‪canWhitelistParameters($params, $expected)
205  {
206  $this->subject->setConfiguration([
207  'cachedParametersWhiteList' => ['whitep1', 'whitep2'],
208  ]);
209  self::assertEquals($expected, $this->subject->generateForParameters($params));
210  }
211 
215  public function ‪canWhitelistParametersDataProvider()
216  {
217  $oneParamHash = 'eae50a13101afd53a9d2c543230eb5bb';
218  $twoParamHash = '701e2d2f1becc9d1b71d327e5cb1c3ed';
219  return [
220  'Even with the whitelist enabled, empty parameters should not return an hash.' => ['', ''],
221  'Whitelisted parameters should have a hash.' => ['&id=42&whitep1=value', $oneParamHash],
222  'Blacklisted parameter should not influence hash.' => ['&id=42&whitep1=value&black=value', $oneParamHash],
223  'Multiple whitelisted parameters should work' => ['&id=42&whitep1=value&whitep2=value', $twoParamHash],
224  'The order should not influce the hash.' => ['&id=42&whitep2=value&black=value&whitep1=value', $twoParamHash]
225  ];
226  }
227 
232  public function ‪canSkipParametersWithEmptyValues($params, $settings, $expected)
233  {
234  $this->subject->setConfiguration($settings);
235  $actual = $this->subject->getRelevantParameters($params);
236  self::assertEquals($expected, array_keys($actual));
237  }
238 
243  {
244  return [
245  'The default configuration does not allow to skip an empty key.' => [
246  '&id=42&key1=v&key2=&key3=',
247  ['excludedParametersIfEmpty' => [], 'excludeAllEmptyParameters' => false],
248  ['encryptionKey', 'id', 'key1', 'key2', 'key3']
249  ],
250  'Due to the empty value, "key2" should be skipped (with equals sign)' => [
251  '&id=42&key1=v&key2=&key3=',
252  ['excludedParametersIfEmpty' => ['key2'], 'excludeAllEmptyParameters' => false],
253  ['encryptionKey', 'id', 'key1', 'key3']
254  ],
255  'Due to the empty value, "key2" should be skipped (without equals sign)' => [
256  '&id=42&key1=v&key2&key3',
257  ['excludedParametersIfEmpty' => ['key2'], 'excludeAllEmptyParameters' => false],
258  ['encryptionKey', 'id', 'key1', 'key3']
259  ],
260  'Due to the empty value, "key2" and "key3" should be skipped' => [
261  '&id=42&key1=v&key2=&key3=',
262  ['excludedParametersIfEmpty' => [], 'excludeAllEmptyParameters' => true],
263  ['encryptionKey', 'id', 'key1']
264  ],
265  'Due to the empty value, "key1", "key2" and "key3" should be skipped (starting with "key")' => [
266  '&id=42&key1=v&key2=&key3=',
267  ['excludedParametersIfEmpty' => ['^key'], 'excludeAllEmptyParameters' => false],
268  ['encryptionKey', 'id', 'key1']
269  ],
270  ];
271  }
272 }
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canWhitelistParametersDataProvider
‪array canWhitelistParametersDataProvider()
Definition: CacheHashCalculatorTest.php:213
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest
Definition: CacheHashCalculatorTest.php:28
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\parametersRequireCacheHashDataprovider
‪array parametersRequireCacheHashDataprovider()
Definition: CacheHashCalculatorTest.php:184
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\cacheHashCalculationDataProvider
‪array cacheHashCalculationDataProvider()
Definition: CacheHashCalculatorTest.php:68
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\cacheHashCalculationWorks
‪cacheHashCalculationWorks($params, $expected)
Definition: CacheHashCalculatorTest.php:60
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\$configuration
‪array $configuration
Definition: CacheHashCalculatorTest.php:35
‪TYPO3\CMS\Frontend\Tests\Unit\Page
Definition: CacheHashCalculatorTest.php:18
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canGenerateForParametersDataProvider
‪array canGenerateForParametersDataProvider()
Definition: CacheHashCalculatorTest.php:155
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canSkipParametersWithEmptyValues
‪canSkipParametersWithEmptyValues($params, $settings, $expected)
Definition: CacheHashCalculatorTest.php:230
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canWhitelistParameters
‪canWhitelistParameters($params, $expected)
Definition: CacheHashCalculatorTest.php:202
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\getRelevantParametersDataprovider
‪array getRelevantParametersDataprovider()
Definition: CacheHashCalculatorTest.php:106
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration
Definition: CacheHashConfiguration.php:35
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\getRelevantParametersWorks
‪getRelevantParametersWorks($params, $expected)
Definition: CacheHashCalculatorTest.php:97
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\setUp
‪setUp()
Definition: CacheHashCalculatorTest.php:43
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator
Definition: CacheHashCalculator.php:25
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canGenerateForParameters
‪canGenerateForParameters($params, $expected)
Definition: CacheHashCalculatorTest.php:137
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\parametersRequireCacheHashWorks
‪parametersRequireCacheHashWorks($params, $expected)
Definition: CacheHashCalculatorTest.php:176
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canSkipParametersWithEmptyValuesDataProvider
‪array canSkipParametersWithEmptyValuesDataProvider()
Definition: CacheHashCalculatorTest.php:240
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\$subject
‪CacheHashCalculator $subject
Definition: CacheHashCalculatorTest.php:31
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\generateForParametersThrowsExceptionWhenIdIsNotSpecified
‪generateForParametersThrowsExceptionWhenIdIsNotSpecified()
Definition: CacheHashCalculatorTest.php:145
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\tearDown
‪tearDown()
Definition: CacheHashCalculatorTest.php:50