[BUGFIX] Field is not added to TCA if part of fieldname exists
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / RegistryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit;
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
17 /**
18 * Testcase for TYPO3\CMS\Core\Registry
19 */
20 class RegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * @var \TYPO3\CMS\Core\Registry
24 */
25 protected $registry;
26
27 /**
28 * Sets up this testcase
29 */
30 protected function setUp()
31 {
32 $GLOBALS['TYPO3_DB'] = $this->createMock(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
33 $GLOBALS['TYPO3_DB']->expects($this->any())->method('fullQuoteStr')->will($this->onConsecutiveCalls('\'tx_phpunit\'', '\'someKey\'', '\'tx_phpunit\'', '\'someKey\''));
34 $this->registry = new \TYPO3\CMS\Core\Registry();
35 }
36
37 /**
38 * @test
39 */
40 public function getThrowsExceptionForInvalidNamespacesUsingNoNamespace()
41 {
42 $this->expectException(\InvalidArgumentException::class);
43 $this->expectExceptionCode(1249755131);
44
45 $this->registry->get('', 'someKey');
46 }
47
48 /**
49 * @test
50 */
51 public function getThrowsExceptionForInvalidNamespacesUsingTooShortNamespace()
52 {
53 $this->expectException(\InvalidArgumentException::class);
54 $this->expectExceptionCode(1249755131);
55
56 $this->registry->get('t', 'someKey');
57 }
58
59 /**
60 * @test
61 */
62 public function getRetrievesTheCorrectEntry()
63 {
64 $testKey = 'TYPO3\\CMS\\Core\\Registry_testcase.testData.getRetrievesTheCorrectEntry';
65 $testValue = 'getRetrievesTheCorrectEntry';
66 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue(array(
67 array('entry_key' => $testKey, 'entry_value' => serialize($testValue))
68 )));
69 $this->assertEquals($this->registry->get('tx_phpunit', $testKey), $testValue, 'The actual data did not match the expected data.');
70 }
71
72 /**
73 * @test
74 */
75 public function getLazyLoadsEntriesOfOneNamespace()
76 {
77 $testKey1 = 'TYPO3\\CMS\\Core\\Registry_testcase.testData.getLazyLoadsEntriesOfOneNamespace1';
78 $testValue1 = 'getLazyLoadsEntriesOfOneNamespace1';
79 $testKey2 = 'TYPO3\\CMS\\Core\\Registry_testcase.testData.getLazyLoadsEntriesOfOneNamespace2';
80 $testValue2 = 'getLazyLoadsEntriesOfOneNamespace2';
81 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue(array(
82 array('entry_key' => $testKey1, 'entry_value' => serialize($testValue1)),
83 array('entry_key' => $testKey2, 'entry_value' => serialize($testValue2))
84 )));
85 $this->assertEquals($this->registry->get('tx_phpunit', $testKey1), $testValue1, 'The actual data did not match the expected data.');
86 $this->assertEquals($this->registry->get('tx_phpunit', $testKey2), $testValue2, 'The actual data did not match the expected data.');
87 }
88
89 /**
90 * @test
91 */
92 public function getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound()
93 {
94 $defaultValue = 'getReturnsTheDefaultValueIfTheRequestedKeyWasNotFound';
95 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTgetRows')->with('*', 'sys_registry', 'entry_namespace = \'tx_phpunit\'')->will($this->returnValue(array(
96 array('entry_key' => 'foo', 'entry_value' => 'bar')
97 )));
98 $this->assertEquals($defaultValue, $this->registry->get('tx_phpunit', 'someNonExistingKey', $defaultValue), 'A value other than the default value was returned.');
99 }
100
101 /**
102 * @test
103 */
104 public function setThrowsAnExceptionOnEmptyNamespace()
105 {
106 $this->expectException(\InvalidArgumentException::class);
107 $this->expectExceptionCode(1249755131);
108
109 $this->registry->set('', 'someKey', 'someValue');
110 }
111
112 /**
113 * @test
114 */
115 public function setThrowsAnExceptionOnWrongNamespace()
116 {
117 $this->expectException(\InvalidArgumentException::class);
118 $this->expectExceptionCode(1249755131);
119
120 $this->registry->set('t', 'someKey', 'someValue');
121 }
122
123 /**
124 * @test
125 */
126 public function setAllowsValidNamespaces()
127 {
128 $registry = $this->getMockBuilder(\TYPO3\CMS\Core\Registry::class)
129 ->setMethods(array('loadEntriesByNamespace'))
130 ->getMock();
131 $registry->set('tx_thisIsValid', 'someKey', 'someValue');
132 $registry->set('thisIsValid', 'someKey', 'someValue');
133 $registry->set('user_soIsThis', 'someKey', 'someValue');
134 $registry->set('core', 'someKey', 'someValue');
135 }
136
137 /**
138 * @test
139 */
140 public function setReallySavesTheGivenValueToTheDatabase()
141 {
142 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_INSERTquery')->with('sys_registry', array(
143 'entry_namespace' => 'tx_phpunit',
144 'entry_key' => 'someKey',
145 'entry_value' => serialize('someValue')
146 ));
147 $registry = $this->getMockBuilder(\TYPO3\CMS\Core\Registry::class)
148 ->setMethods(array('loadEntriesByNamespace'))
149 ->getMock();
150 $registry->set('tx_phpunit', 'someKey', 'someValue');
151 }
152
153 /**
154 * @test
155 */
156 public function setUpdatesExistingKeys()
157 {
158 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_SELECTquery')->with('uid', 'sys_registry', 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'')->will($this->returnValue('DBResource'));
159 $GLOBALS['TYPO3_DB']->expects($this->once())->method('sql_num_rows')->with('DBResource')->will($this->returnValue(1));
160 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_UPDATEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'', array(
161 'entry_value' => serialize('someValue')
162 ));
163 $GLOBALS['TYPO3_DB']->expects($this->never())->method('exec_INSERTquery');
164 $registry = $this->getMockBuilder(\TYPO3\CMS\Core\Registry::class)
165 ->setMethods(array('loadEntriesByNamespace'))
166 ->getMock();
167 $registry->set('tx_phpunit', 'someKey', 'someValue');
168 }
169
170 /**
171 * @test
172 */
173 public function removeThrowsAnExceptionOnWrongNamespace()
174 {
175 $this->expectException(\InvalidArgumentException::class);
176 $this->expectExceptionCode(1249755131);
177
178 $this->registry->remove('t', 'someKey');
179 }
180
181 /**
182 * @test
183 */
184 public function removeReallyRemovesTheEntryFromTheDatabase()
185 {
186 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\' AND entry_key = \'someKey\'');
187 $this->registry->remove('tx_phpunit', 'someKey');
188 }
189
190 /**
191 * @test
192 */
193 public function removeUnsetsValueFromTheInternalEntriesCache()
194 {
195 $registry = $this->getMockBuilder(\TYPO3\CMS\Core\Registry::class)
196 ->setMethods(array('loadEntriesByNamespace'))
197 ->getMock();
198 $registry->set('tx_phpunit', 'someKey', 'someValue');
199 $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
200 $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
201 $registry->remove('tx_phpunit', 'someKey');
202 $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
203 $this->assertEquals('someOtherValue', $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
204 $this->assertEquals('someValueInOtherNamespace', $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
205 }
206
207 /**
208 * @test
209 */
210 public function removeAllByNamespaceThrowsAnExceptionOnWrongNamespace()
211 {
212 $this->expectException(\InvalidArgumentException::class);
213 $this->expectExceptionCode(1249755131);
214
215 $this->registry->removeAllByNamespace('');
216 }
217
218 /**
219 * @test
220 */
221 public function removeAllByNamespaceReallyRemovesAllEntriesOfTheSpecifiedNamespaceFromTheDatabase()
222 {
223 $GLOBALS['TYPO3_DB']->expects($this->once())->method('exec_DELETEquery')->with('sys_registry', 'entry_namespace = \'tx_phpunit\'');
224 $this->registry->removeAllByNamespace('tx_phpunit');
225 }
226
227 /**
228 * @test
229 */
230 public function removeAllByNamespaceUnsetsValuesOfTheSpecifiedNamespaceFromTheInternalEntriesCache()
231 {
232 $registry = $this->getMockBuilder(\TYPO3\CMS\Core\Registry::class)
233 ->setMethods(array('loadEntriesByNamespace'))
234 ->getMock();
235 $registry->set('tx_phpunit', 'someKey', 'someValue');
236 $registry->set('tx_phpunit', 'someOtherKey', 'someOtherValue');
237 $registry->set('tx_otherNamespace', 'someKey', 'someValueInOtherNamespace');
238 $registry->removeAllByNamespace('tx_phpunit');
239 $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
240 $this->assertEquals('defaultValue', $registry->get('tx_phpunit', 'someOtherKey', 'defaultValue'), 'A value other than the default value was returned, thus the entry was still present.');
241 $this->assertEquals('someValueInOtherNamespace', $registry->get('tx_otherNamespace', 'someKey', 'defaultValue'), 'A value other than the stored value was returned, thus the entry was removed.');
242 }
243 }