‪TYPO3CMS  ‪main
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 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
26 final class ‪CacheHashCalculatorTest extends UnitTestCase
27 {
29 
30  protected array ‪$configuration = [
31  'excludedParameters' => ['exclude1', 'exclude2'],
32  'cachedParametersWhiteList' => [],
33  'requireCacheHashPresenceParameters' => ['req1', 'req2'],
34  'excludedParametersIfEmpty' => [],
35  'excludeAllEmptyParameters' => false,
36  ];
37 
38  protected function ‪setUp(): void
39  {
40  parent::setUp();
41  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 't3lib_cacheHashTest';
42  $this->subject = new ‪CacheHashCalculator(new ‪CacheHashConfiguration($this->configuration));
43  }
44 
45  protected function ‪tearDown(): void
46  {
47  unset($this->subject);
48  parent::tearDown();
49  }
50 
51  #[DataProvider('cacheHashCalculationDataProvider')]
52  #[Test]
53  public function ‪cacheHashCalculationWorks(array $params, string $expected): void
54  {
55  self::assertEquals($expected, $this->subject->calculateCacheHash($params));
56  }
57 
58  public static function ‪cacheHashCalculationDataProvider(): array
59  {
60  return [
61  'Empty parameters should not return a hash' => [
62  [],
63  '',
64  ],
65  'Trivial key value combination should generate hash' => [
66  [
67  'encryptionKey' => 't3lib_cacheHashTest',
68  'key' => 'value',
69  ],
70  '5cfdcf826275558b3613dd51714a0a17',
71  ],
72  'Multiple parameters should generate hash' => [
73  [
74  'a' => 'v',
75  'b' => 'v',
76  'encryptionKey' => 't3lib_cacheHashTest',
77  ],
78  '0f40b089cdad149aea99e9bf4badaa93',
79  ],
80  ];
81  }
82 
83  #[DataProvider('getRelevantParametersDataprovider')]
84  #[Test]
85  public function ‪getRelevantParametersWorks(string $params, array $expected): void
86  {
87  $actual = $this->subject->getRelevantParameters($params);
88  self::assertEquals($expected, array_keys($actual));
89  }
90 
91  public static function ‪getRelevantParametersDataprovider(): array
92  {
93  return [
94  'Empty list should be passed through' => ['', []],
95  'Simple parameter should be passed through and the encryptionKey should be added' => [
96  'key=v&id=42',
97  ['encryptionKey', 'id', 'key'],
98  ],
99  'Simple parameter should be passed through' => [
100  'key1=v&key2=v&id=42',
101  ['encryptionKey', 'id', 'key1', 'key2'],
102  ],
103  'System and exclude parameters should be omitted' => [
104  'id=1&type=3&exclude1=x&no_cache=1',
105  [],
106  ],
107  'System and exclude parameters (except id) should be omitted, others should stay' => [
108  'id=1&type=3&key=x&no_cache=1',
109  ['encryptionKey', 'id', 'key'],
110  ],
111  'System and exclude parameters should be omitted and id is not required to be specified' => [
112  'type=3&no_cache=1',
113  [],
114  ],
115  ];
116  }
117 
118  #[DataProvider('canGenerateForParametersDataProvider')]
119  #[Test]
120  public function ‪canGenerateForParameters(string $params, string $expected): void
121  {
122  self::assertEquals($expected, $this->subject->generateForParameters($params));
123  }
124 
125  #[Test]
127  {
128  $this->expectException(\RuntimeException::class);
129  $this->expectExceptionCode(1467983513);
130  $this->subject->generateForParameters('&key=x');
131  }
132 
133  public static function ‪canGenerateForParametersDataProvider(): array
134  {
135  $knowHash = 'fac112f7e662c83c19b57142c3a921f5';
136  return [
137  'Empty parameters should not return an hash' => ['&id=42', ''],
138  'Querystring has no relevant parameters so we should not have a cacheHash' => ['&exclude1=val', ''],
139  'Querystring has only system parameters so we should not have a cacheHash' => ['&id=42&type=val', ''],
140  'Trivial key value combination should generate hash' => ['&id=42&key=value', $knowHash],
141  'Only the relevant parts should be taken into account' => ['&id=42&key=value&exclude1=val', $knowHash],
142  'Only the relevant parts should be taken into account(exclude2 before key)' => ['&id=42&exclude2=val&key=value', $knowHash],
143  'System parameters should not be taken into account (except id)' => ['&id=42&type=23&key=value', $knowHash],
144  'Trivial hash for sorted parameters should be right' => ['&id=42&a=v&b=v', '52c8a1299e20324f90377c43153c4987'],
145  'Parameters should be sorted before cHash is created' => ['&id=42&b=v&a=v', '52c8a1299e20324f90377c43153c4987'],
146  'Empty argument names are filtered out before cHash calculation' => ['&id=42&b=v&a=v&=dummy', '52c8a1299e20324f90377c43153c4987'],
147  ];
148  }
149 
150  #[DataProvider('parametersRequireCacheHashDataprovider')]
151  #[Test]
152  public function ‪parametersRequireCacheHashWorks(string $params, bool $expected): void
153  {
154  self::assertEquals($expected, $this->subject->doParametersRequireCacheHash($params));
155  }
156 
157  public static function ‪parametersRequireCacheHashDataprovider(): array
158  {
159  return [
160  'Empty parameter strings should not require anything.' => ['', false],
161  'Normal parameters aren\'t required.' => ['key=value', false],
162  'Configured "req1" to be required.' => ['req1=value', true],
163  'Configured "req1" to be required, should also work in combined context' => ['&key=value&req1=value', true],
164  'Configured "req1" to be required, should also work in combined context (key at the end)' => ['req1=value&key=value', true],
165  ];
166  }
167 
172  #[DataProvider('canWhitelistParametersDataProvider')]
173  #[Test]
174  public function ‪canWhitelistParameters(string $params, string $expected): void
175  {
176  $this->subject->setConfiguration([
177  'cachedParametersWhiteList' => ['whitep1', 'whitep2'],
178  ]);
179  self::assertEquals($expected, $this->subject->generateForParameters($params));
180  }
181 
182  public static function ‪canWhitelistParametersDataProvider(): array
183  {
184  $oneParamHash = 'eae50a13101afd53a9d2c543230eb5bb';
185  $twoParamHash = '701e2d2f1becc9d1b71d327e5cb1c3ed';
186  return [
187  'Even with the whitelist enabled, empty parameters should not return an hash.' => ['', ''],
188  'Whitelisted parameters should have a hash.' => ['&id=42&whitep1=value', $oneParamHash],
189  'Blacklisted parameter should not influence hash.' => ['&id=42&whitep1=value&black=value', $oneParamHash],
190  'Multiple whitelisted parameters should work' => ['&id=42&whitep1=value&whitep2=value', $twoParamHash],
191  'The order should not influce the hash.' => ['&id=42&whitep2=value&black=value&whitep1=value', $twoParamHash],
192  ];
193  }
194 
195  #[DataProvider('canSkipParametersWithEmptyValuesDataProvider')]
196  #[Test]
197  public function ‪canSkipParametersWithEmptyValues(string $params, array $settings, array $expected): void
198  {
199  $this->subject->setConfiguration($settings);
200  $actual = $this->subject->getRelevantParameters($params);
201  self::assertEquals($expected, array_keys($actual));
202  }
203 
204  public static function ‪canSkipParametersWithEmptyValuesDataProvider(): array
205  {
206  return [
207  'The default configuration does not allow to skip an empty key.' => [
208  '&id=42&key1=v&key2=&key3=',
209  ['excludedParametersIfEmpty' => [], 'excludeAllEmptyParameters' => false],
210  ['encryptionKey', 'id', 'key1', 'key2', 'key3'],
211  ],
212  'Due to the empty value, "key2" should be skipped (with equals sign)' => [
213  '&id=42&key1=v&key2=&key3=',
214  ['excludedParametersIfEmpty' => ['key2'], 'excludeAllEmptyParameters' => false],
215  ['encryptionKey', 'id', 'key1', 'key3'],
216  ],
217  'Due to the empty value, "key2" should be skipped (without equals sign)' => [
218  '&id=42&key1=v&key2&key3',
219  ['excludedParametersIfEmpty' => ['key2'], 'excludeAllEmptyParameters' => false],
220  ['encryptionKey', 'id', 'key1', 'key3'],
221  ],
222  'Due to the empty value, "key2" and "key3" should be skipped' => [
223  '&id=42&key1=v&key2=&key3=',
224  ['excludedParametersIfEmpty' => [], 'excludeAllEmptyParameters' => true],
225  ['encryptionKey', 'id', 'key1'],
226  ],
227  'Due to the empty value, "key1", "key2" and "key3" should be skipped (starting with "key")' => [
228  '&id=42&key1=v&key2=&key3=',
229  ['excludedParametersIfEmpty' => ['^key'], 'excludeAllEmptyParameters' => false],
230  ['encryptionKey', 'id', 'key1'],
231  ],
232  ];
233  }
234 }
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canSkipParametersWithEmptyValuesDataProvider
‪static canSkipParametersWithEmptyValuesDataProvider()
Definition: CacheHashCalculatorTest.php:204
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest
Definition: CacheHashCalculatorTest.php:27
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\parametersRequireCacheHashDataprovider
‪static parametersRequireCacheHashDataprovider()
Definition: CacheHashCalculatorTest.php:157
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canWhitelistParametersDataProvider
‪static canWhitelistParametersDataProvider()
Definition: CacheHashCalculatorTest.php:182
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\getRelevantParametersWorks
‪getRelevantParametersWorks(string $params, array $expected)
Definition: CacheHashCalculatorTest.php:85
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canGenerateForParameters
‪canGenerateForParameters(string $params, string $expected)
Definition: CacheHashCalculatorTest.php:120
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\$configuration
‪array $configuration
Definition: CacheHashCalculatorTest.php:30
‪TYPO3\CMS\Frontend\Tests\Unit\Page
Definition: CacheHashCalculatorTest.php:18
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration
Definition: CacheHashConfiguration.php:35
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\cacheHashCalculationDataProvider
‪static cacheHashCalculationDataProvider()
Definition: CacheHashCalculatorTest.php:58
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\cacheHashCalculationWorks
‪cacheHashCalculationWorks(array $params, string $expected)
Definition: CacheHashCalculatorTest.php:53
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canGenerateForParametersDataProvider
‪static canGenerateForParametersDataProvider()
Definition: CacheHashCalculatorTest.php:133
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\setUp
‪setUp()
Definition: CacheHashCalculatorTest.php:38
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator
Definition: CacheHashCalculator.php:25
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\parametersRequireCacheHashWorks
‪parametersRequireCacheHashWorks(string $params, bool $expected)
Definition: CacheHashCalculatorTest.php:152
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\getRelevantParametersDataprovider
‪static getRelevantParametersDataprovider()
Definition: CacheHashCalculatorTest.php:91
‪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:126
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\tearDown
‪tearDown()
Definition: CacheHashCalculatorTest.php:45
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canWhitelistParameters
‪canWhitelistParameters(string $params, string $expected)
Definition: CacheHashCalculatorTest.php:174
‪TYPO3\CMS\Frontend\Tests\Unit\Page\CacheHashCalculatorTest\canSkipParametersWithEmptyValues
‪canSkipParametersWithEmptyValues(string $params, array $settings, array $expected)
Definition: CacheHashCalculatorTest.php:197