Fixed bug #17490: After introducing the locking in #17289 no CSRF token will ever...
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_registryTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 Bastian Waidelich <bastian@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * Testcase for class t3lib_Registry
28 *
29 * @author Bastian Waidelich <bastian@typo3.org>
30 * @package TYPO3
31 * @subpackage t3lib
32 */
33 class t3lib_RegistryTest extends tx_phpunit_testcase {
34
35 /**
36 * @var t3lib_Registry
37 */
38 protected $registry;
39
40 /**
41 * @var t3lib_DB
42 */
43 protected $typo3DbBackup;
44
45 /**
46 * Sets up this testcase
47 */
48 public function setUp() {
49 $this->typo3DbBackup = $GLOBALS['TYPO3_DB'];
50 $GLOBALS['TYPO3_DB'] = $this->getMock('t3lib_DB', array());
51 $GLOBALS['TYPO3_DB']->expects($this->any())
52 ->method('fullQuoteStr')
53 ->will($this->onConsecutiveCalls('\'tx_phpunit\'', '\'someKey\'', '\'tx_phpunit\'', '\'someKey\''));
54
55 $this->registry = new t3lib_Registry();
56 }
57
58 /**
59 * Tears down this testcase
60 */
61 public function tearDown() {
62 $GLOBALS['TYPO3_DB'] = $this->typo3DbBackup;
63 }
64
65 /**
66 * @test
67 * @expectedException InvalidArgumentException
68 */
69 public function getThrowsExceptionForInvalidNamespaces() {
70 $this->registry->get('invalidNamespace', 'someKey');
71 }
72
73 /**
74 * @test
75 */
76 public function getRetrievesTheCorrectEntry() {
77 $testKey = 't3lib_Registry_testcase.testData.getRetrievesTheCorrectEntry';
78 $testValue = 'getRetrievesTheCorrectEntry';
79
80 $GLOBALS['TYPO3_DB']->expects($this->once())
81 ->method('exec_SELECTgetRows')
82 ->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')
83 ->will(
84 $this->returnValue(
85 array(
86 array('entry_key' => $testKey, 'entry_value' => serialize($testValue))
87 )
88 )
89 );
90
91 $this->assertEquals(
92 $this->registry->get('tx_phpunit', $testKey),
93 $testValue,
94 'The actual data did not match the expected data.'
95 );
96 }
97
98 /**
99 * @test
100 */
101 public function getLazyLoadsEntriesOfOneNamespace() {
102 $testKey1 = 't3lib_Registry_testcase.testData.getLazyLoadsEntriesOfOneNamespace1';
103 $testValue1 = 'getLazyLoadsEntriesOfOneNamespace1';
104 $testKey2 = 't3lib_Registry_testcase.testData.getLazyLoadsEntriesOfOneNamespace2';
105 $testValue2 = 'getLazyLoadsEntriesOfOneNamespace2';
106
107 $GLOBALS['TYPO3_DB']->expects($this->once())
108 ->method('exec_SELECTgetRows')
109 ->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')
110 ->will(
111 $this->returnValue(
112 array(
113 array('entry_key' => $testKey1, 'entry_value' => serialize($testValue1)),
114 array('entry_key' => $testKey2, 'entry_value' => serialize($testValue2))
115 )
116 )
117 );
118
119 $this->assertEquals(
120 $this->registry->get('tx_phpunit', $testKey1),
121 $testValue1,
122 'The actual data did not match the expected data.'
123 );
124 $this->assertEquals(
125 $this->registry->get('tx_phpunit', $testKey2),
126 $testValue2,
127 'The actual data did not match the expected data.'
128 );
129 }
130
131 /**
132 * @test
133 */
134 public function getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound() {
135 $defaultValue = 'getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound';
136
137 $GLOBALS['TYPO3_DB']->expects($this->once())
138 ->method('exec_SELECTgetRows')
139 ->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')
140 ->will(
141 $this->returnValue(
142 array(
143 array('entry_key' => 'foo', 'entry_value' => 'bar'),
144 )
145 )
146 );
147
148 $this->assertEquals(
149 $defaultValue,
150 $this->registry->get('tx_phpunit', 'someNonExistingKey', $defaultValue),
151 'A value other than the default value was returned.'
152 );
153 }
154
155 /**
156 * @test
157 * @expectedException InvalidArgumentException
158 */
159 public function setThrowsAnExceptionOnEmptyNamespace() {
160 $this->registry->set('', 'someKey', 'someValue');
161 }
162
163 /**
164 * @test
165 * @expectedException InvalidArgumentException
166 */
167 public function setThrowsAnExceptionOnWrongNamespace() {
168 $this->registry->set('invalidNamespace', 'someKey', 'someValue');
169 }
170
171 /**
172 * @test
173 */
174 public function setAllowsValidNamespaces() {
175 $this->registry->set('tx_thisIsValid', 'someKey', 'someValue');
176 $this->registry->set('user_soIsThis', 'someKey', 'someValue');
177 $this->registry->set('core', 'someKey', 'someValue');
178 }
179
180 /**
181 * @test
182 */
183 public function setReallySavesTheGivenValueToTheDatabase() {
184 $GLOBALS['TYPO3_DB']->expects($this->once())
185 ->method('exec_INSERTquery')
186 ->with(
187 'sys_registry',
188 array(
189 'entry_namespace' => 'tx_phpunit',
190 'entry_key' => 'someKey',
191 'entry_value' => serialize('someValue')
192 )
193 );
194
195 $this->registry->set('tx_phpunit', 'someKey', 'someValue');
196 }
197
198 /**
199 * @test
200 */
201 public function setUpdatesExistingKeys() {
202 $GLOBALS['TYPO3_DB']->expects($this->once())
203 ->method('exec_SELECTquery')
204 ->with(
205 'uid',
206 'sys_registry',
207 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\''
208 )
209 ->will(
210 $this->returnValue('DBResource')
211 );
212
213 $GLOBALS['TYPO3_DB']->expects($this->once())
214 ->method('sql_num_rows')
215 ->with('DBResource')
216 ->will(
217 $this->returnValue(1)
218 );
219
220 $GLOBALS['TYPO3_DB']->expects($this->once())
221 ->method('exec_UPDATEquery')
222 ->with(
223 'sys_registry',
224 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'',
225 array(
226 'entry_value' => serialize('someValue')
227 )
228 );
229
230 $GLOBALS['TYPO3_DB']->expects($this->never())
231 ->method('exec_INSERTquery');
232
233 $this->registry->set('tx_phpunit', 'someKey', 'someValue');
234 }
235
236 /**
237 * @test
238 */
239 public function setStoresValueInTheInternalEntriesCache() {
240 $registry = $this->getMock('t3lib_Registry', array('loadEntriesByNamespace'));
241 $registry->expects($this->never())
242 ->method('loadEntriesByNamespace');
243
244 $registry->set('tx_phpunit', 'someKey', 'someValue');
245
246 $this->assertEquals(
247 'someValue',
248 $registry->get('tx_phpunit', 'someKey'),
249 'The actual data did not match the expected data.'
250 );
251 }
252
253 /**
254 * @test
255 * @expectedException InvalidArgumentException
256 */
257 public function removeThrowsAnExceptionOnWrongNamespace() {
258 $this->registry->remove('coreInvalid', 'someKey');
259 }
260
261 /**
262 * @test
263 */
264 public function removeReallyRemovesTheEntryFromTheDatabase() {
265 $GLOBALS['TYPO3_DB']->expects($this->once())
266 ->method('exec_DELETEquery')
267 ->with(
268 'sys_registry',
269 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\''
270 );
271
272 $this->registry->remove('tx_phpunit', 'someKey');
273 }
274
275 /**
276 * @test
277 */
278 public function removeUnsetsValueFromTheInternalEntriesCache() {
279 $registry = $this->getMock('t3lib_Registry', array('loadEntriesByNamespace'));
280 $registry->set('tx_phpunit', 'someKey', 'someValue');
281 $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
282 $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
283 $registry->remove('tx_phpunit', 'someKey');
284
285 $this->assertEquals(
286 'defaultValue',
287 $registry->get('tx_phpunit', 'someKey', 'defaultValue'),
288 'A value other than the default value was returned, thus the entry was still present.'
289 );
290
291 $this->assertEquals(
292 'someOtherValue',
293 $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'),
294 'A value other than the stored value was returned, thus the entry was removed.'
295 );
296
297 $this->assertEquals(
298 'someValueInOtherNamespace',
299 $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'),
300 'A value other than the stored value was returned, thus the entry was removed.'
301 );
302 }
303
304 /**
305 * @test
306 * @expectedException InvalidArgumentException
307 */
308 public function removeAllByNamespaceThrowsAnExceptionOnWrongNamespace() {
309 $this->registry->removeAllByNamespace('');
310 }
311
312 /**
313 * @test
314 */
315 public function removeAllByNamespaceReallyRemovesAllEntriesOfTheSpecifiedNamespaceFromTheDatabase() {
316 $GLOBALS['TYPO3_DB']->expects($this->once())
317 ->method('exec_DELETEquery')
318 ->with(
319 'sys_registry',
320 'entry_namespace = \'tx_phpunit\''
321 );
322
323 $this->registry->removeAllByNamespace('tx_phpunit');
324 }
325
326 /**
327 * @test
328 */
329 public function removeAllByNamespaceUnsetsValuesOfTheSpecifiedNamespaceFromTheInternalEntriesCache() {
330 $registry = $this->getMock('t3lib_Registry', array('loadEntriesByNamespace'));
331 $registry->set('tx_phpunit', 'someKey', 'someValue');
332 $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
333 $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
334 $registry->removeAllByNamespace('tx_phpunit');
335
336 $this->assertEquals(
337 'defaultValue',
338 $registry->get('tx_phpunit', 'someKey', 'defaultValue'),
339 'A value other than the default value was returned, thus the entry was still present.'
340 );
341
342 $this->assertEquals(
343 'defaultValue',
344 $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'),
345 'A value other than the default value was returned, thus the entry was still present.'
346 );
347
348 $this->assertEquals(
349 'someValueInOtherNamespace',
350 $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'),
351 'A value other than the stored value was returned, thus the entry was removed.'
352 );
353 }
354 }
355
356 ?>