TYPO3 CMS  TYPO3_7-6
CacheHashCalculatorTest.php
Go to the documentation of this file.
1 <?php
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 
21 {
25  protected $subject;
26 
27  protected function setUp()
28  {
29  $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = 't3lib_cacheHashTest';
30  $this->subject = $this->getMock(\TYPO3\CMS\Frontend\Page\CacheHashCalculator::class, ['foo']);
31  $this->subject->setConfiguration([
32  'excludedParameters' => ['exclude1', 'exclude2'],
33  'cachedParametersWhiteList' => [],
34  'requireCacheHashPresenceParameters' => ['req1', 'req2'],
35  'excludedParametersIfEmpty' => [],
36  'includePageId' => true,
37  'excludeAllEmptyParameters' => false
38  ]);
39  }
40 
45  public function cacheHashCalculationWorks($params, $expected)
46  {
47  $this->assertEquals($expected, $this->subject->calculateCacheHash($params));
48  }
49 
54  {
55  return [
56  'Empty parameters should not return a hash' => [
57  [],
58  ''
59  ],
60  'Trivial key value combination should generate hash' => [
61  [
62  'encryptionKey' => 't3lib_cacheHashTest',
63  'key' => 'value'
64  ],
65  '5cfdcf826275558b3613dd51714a0a17'
66  ],
67  'Multiple parameters should generate hash' => [
68  [
69  'a' => 'v',
70  'b' => 'v',
71  'encryptionKey' => 't3lib_cacheHashTest'
72  ],
73  '0f40b089cdad149aea99e9bf4badaa93'
74  ]
75  ];
76  }
77 
82  public function getRelevantParametersWorks($params, $expected)
83  {
84  $actual = $this->subject->getRelevantParameters($params);
85  $this->assertEquals($expected, array_keys($actual));
86  }
87 
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 
122  public function canGenerateForParameters($params, $expected)
123  {
124  $this->assertEquals($expected, $this->subject->generateForParameters($params));
125  }
126 
133  {
134  $this->subject->generateForParameters('&key=x');
135  }
136 
141  {
142  $knowHash = 'fac112f7e662c83c19b57142c3a921f5';
143  return [
144  'Empty parameters should not return an hash' => ['&id=42', ''],
145  'Querystring has no relevant parameters so we should not have a cacheHash' => ['&exclude1=val', ''],
146  'Querystring has only system parameters so we should not have a cacheHash' => ['&id=42&type=val', ''],
147  'Trivial key value combination should generate hash' => ['&id=42&key=value', $knowHash],
148  'Only the relevant parts should be taken into account' => ['&id=42&key=value&exclude1=val', $knowHash],
149  'Only the relevant parts should be taken into account(exclude2 before key)' => ['&id=42&exclude2=val&key=value', $knowHash],
150  'System parameters should not be taken into account (except id)' => ['&id=42&type=23&key=value', $knowHash],
151  'Admin panel parameters should not be taken into account' => ['&id=42&TSFE_ADMIN_PANEL[display]=7&key=value', $knowHash],
152  'Trivial hash for sorted parameters should be right' => ['&id=42&a=v&b=v', '52c8a1299e20324f90377c43153c4987'],
153  'Parameters should be sorted before cHash is created' => ['&id=42&b=v&a=v', '52c8a1299e20324f90377c43153c4987'],
154  'Empty argument names are filtered out before cHash calculation' => ['&id=42&b=v&a=v&=dummy', '52c8a1299e20324f90377c43153c4987']
155  ];
156  }
157 
162  public function parametersRequireCacheHashWorks($params, $expected)
163  {
164  $this->assertEquals($expected, $this->subject->doParametersRequireCacheHash($params));
165  }
166 
171  {
172  return [
173  'Empty parameter strings should not require anything.' => ['', false],
174  'Normal parameters aren\'t required.' => ['key=value', false],
175  'Configured "req1" to be required.' => ['req1=value', true],
176  'Configured "req1" to be required, should also work in combined context' => ['&key=value&req1=value', true],
177  'Configured "req1" to be required, should also work in combined context (key at the end)' => ['req1=value&key=value', true]
178  ];
179  }
180 
188  public function canWhitelistParameters($params, $expected)
189  {
190  $method = new \ReflectionMethod(\TYPO3\CMS\Frontend\Page\CacheHashCalculator::class, 'setCachedParametersWhiteList');
191  $method->setAccessible(true);
192  $method->invoke($this->subject, ['whitep1', 'whitep2']);
193  $this->assertEquals($expected, $this->subject->generateForParameters($params));
194  }
195 
200  {
201  $oneParamHash = 'eae50a13101afd53a9d2c543230eb5bb';
202  $twoParamHash = '701e2d2f1becc9d1b71d327e5cb1c3ed';
203  return [
204  'Even with the whitelist enabled, empty parameters should not return an hash.' => ['', ''],
205  'Whitelisted parameters should have a hash.' => ['&id=42&whitep1=value', $oneParamHash],
206  'Blacklisted parameter should not influence hash.' => ['&id=42&whitep1=value&black=value', $oneParamHash],
207  'Multiple whitelisted parameters should work' => ['&id=42&whitep1=value&whitep2=value', $twoParamHash],
208  'The order should not influce the hash.' => ['&id=42&whitep2=value&black=value&whitep1=value', $twoParamHash]
209  ];
210  }
211 
216  public function canSkipParametersWithEmptyValues($params, $settings, $expected)
217  {
218  $this->subject->setConfiguration($settings);
219  $actual = $this->subject->getRelevantParameters($params);
220  $this->assertEquals($expected, array_keys($actual));
221  }
222 
227  {
228  return [
229  'The default configuration does not allow to skip an empty key.' => [
230  '&id=42&key1=v&key2=&key3=',
231  ['excludedParametersIfEmpty' => [], 'excludeAllEmptyParameters' => false],
232  ['encryptionKey', 'id', 'key1', 'key2', 'key3']
233  ],
234  'Due to the empty value, "key2" should be skipped(with equals sign' => [
235  '&id=42&key1=v&key2=&key3=',
236  ['excludedParametersIfEmpty' => ['key2'], 'excludeAllEmptyParameters' => false],
237  ['encryptionKey', 'id', 'key1', 'key3']
238  ],
239  'Due to the empty value, "key2" should be skipped(without equals sign)' => [
240  '&id=42&key1=v&key2&key3',
241  ['excludedParametersIfEmpty' => ['key2'], 'excludeAllEmptyParameters' => false],
242  ['encryptionKey', 'id', 'key1', 'key3']
243  ],
244  'Due to the empty value, "key2" and "key3" should be skipped' => [
245  '&id=42&key1=v&key2=&key3=',
246  ['excludedParametersIfEmpty' => [], 'excludeAllEmptyParameters' => true],
247  ['encryptionKey', 'id', 'key1']
248  ]
249  ];
250  }
251 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']