* renamed TcaMapper to ObjectRelationalMapper
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / TX_EXTMVC_Persistence_Session_testcase.php
1 <?php
2 declare(ENCODING = 'utf-8');
3
4 /* *
5 * This script belongs to the FLOW3 framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 require_once(PATH_tslib . 'class.tslib_content.php');
25 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/TX_EXTMVC_Persistence_Session.php');
26 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/DomainObject/TX_EXTMVC_DomainObject_Entity.php');
27 require_once(t3lib_extMgm::extPath('extmvc') . 'Tests/Fixtures/TX_EXTMVC_Tests_Fixtures_Entity.php');
28
29 class TX_EXTMVC_Persistence_Session_testcase extends tx_phpunit_testcase {
30
31 public function setUp() {
32 // global $TCA;
33 global $_EXTKEY;
34
35 $this->cObj = t3lib_div::makeInstance('tslib_cObj');
36 // $GLOBALS['TSFE']->fe_user = $this->getMock('tslib_feUserAuth');
37 $GLOBALS['TSFE'] = $this->getMock('tslib_fe', array('includeTCA'));
38 // $TCA = $this->setupTca();
39 $GLOBALS['TSFE']->expects($this->any())
40 ->method('includeTCA')
41 ->will($this->returnValue(NULL));
42
43
44 $GLOBALS['TSFE']->fe_user->user['uid'] = 999;
45 $GLOBALS['TSFE']->id = 42;
46 }
47
48 public function setupTCA() {
49 global $TCA;
50 $TCA['tx_blogexample_domain_blog'] = array (
51 'ctrl' => array (
52 'title' => 'LLL:EXT:blogexample/Resources/Language/locallang_db.xml:tx_blogexample_domain_blog',
53 'label' => 'name',
54 'tstamp' => 'tstamp',
55 'prependAtCopy' => 'LLL:EXT:lang/locallang_general.xml:LGL.prependAtCopy',
56 'delete' => 'deleted',
57 'enablecolumns' => array (
58 'disabled' => 'hidden'
59 ),
60 'iconfile' => t3lib_extMgm::extRelPath($_EXTKEY).'Resources/Icons/icon_tx_blogexample_domain_blog.gif'
61 ),
62 'interface' => array(
63 'showRecordFieldList' => 'hidden, name, description, logo, posts'
64 ),
65 'columns' => array(
66 'hidden' => array(
67 'exclude' => 1,
68 'label' => 'LLL:EXT:lang/locallang_general.xml:LGL.hidden',
69 'config' => array(
70 'type' => 'check'
71 )
72 ),
73 'name' => array(
74 'exclude' => 0,
75 'label' => 'LLL:EXT:blogexample/Resources/Language/locallang_db.xml:tx_blogexample_domain_blog.name',
76 'config' => array(
77 'type' => 'input',
78 'size' => 20,
79 'eval' => 'trim,required',
80 'max' => 256
81 )
82 ),
83 'description' => array(
84 'exclude' => 1,
85 'label' => 'LLL:EXT:blogexample/Resources/Language/locallang_db.xml:tx_blogexample_domain_blog.description',
86 'config' => array(
87 'type' => 'text',
88 'eval' => 'required',
89 'rows' => 30,
90 'cols' => 80,
91 )
92 ),
93 'logo' => array(
94 'exclude' => 1,
95 'label' => 'LLL:EXT:blogexample/Resources/Language/locallang_db.xml:tx_blogexample_domain_blog.logo',
96 'config' => array(
97 'type' => 'group',
98 'internal_type' => 'file',
99 'allowed' => $GLOBALS['TYPO3_CONF_VARS']['GFX']['imagefile_ext'],
100 'max_size' => 3000,
101 'uploadfolder' => 'uploads/pics',
102 'show_thumbs' => 1,
103 'size' => 1,
104 'maxitems' => 1,
105 'minitems' => 0
106 )
107 ),
108 'posts' => array(
109 'exclude' => 1,
110 'label' => 'LLL:EXT:blogexample/Resources/Language/locallang_db.xml:tx_blogexample_domain_blog.posts',
111 'config' => array(
112 'type' => 'inline',
113 // TODO is 'foreign_class' in $TCA the best way?
114 'foreign_class' => 'TX_Blogexample_Domain_Post',
115 'foreign_table' => 'tx_blogexample_domain_post',
116 'foreign_field' => 'blog',
117 'foreign_table_field' => 'blog_table',
118 'appearance' => array(
119 'newRecordLinkPosition' => 'bottom',
120 'collapseAll' => 1,
121 'expandSingle' => 1,
122 ),
123 )
124 ),
125 ),
126 'types' => array(
127 '1' => array('showitem' => 'hidden, name, description, logo, posts')
128 ),
129 'palettes' => array(
130 '1' => array('showitem' => '')
131 )
132 );
133 }
134
135 public function test_NewSessionIsEmpty() {
136 $session = new TX_EXTMVC_Persistence_Session;
137 $addedObjects = $session->getAddedObjects();
138 $removedObjects = $session->getRemovedObjects();
139 $reconstitutedObjects = $session->getReconstitutedObjects();
140 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
141 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
142 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
143 }
144
145 public function test_ObjectCanBeRegisteredAsAdded() {
146 $session = new TX_EXTMVC_Persistence_Session;
147 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
148 $session->registerAddedObject($entity);
149 $addedObjects = $session->getAddedObjects();
150 $removedObjects = $session->getRemovedObjects();
151 $reconstitutedObjects = $session->getReconstitutedObjects();
152
153 $this->assertEquals(1, count($addedObjects), 'The added objects storage holds 0 or more than 1 objects.');
154 $this->assertSame($entity, $addedObjects[0], 'The returned object was not the same as the registered one.');
155 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
156 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
157 }
158
159 public function test_ObjectCanBeRegisteredAsRemoved() {
160 $session = new TX_EXTMVC_Persistence_Session;
161 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
162 $session->registerRemovedObject($entity);
163 $addedObjects = $session->getAddedObjects();
164 $removedObjects = $session->getRemovedObjects();
165 $reconstitutedObjects = $session->getReconstitutedObjects();
166
167 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
168 $this->assertEquals(1, count($removedObjects), 'The removed objects storage holds 0 or more than 1 objects.');
169 $this->assertSame($entity, $removedObjects[0], 'The returned object was not the same as the registered one.');
170 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
171 }
172
173 public function test_ObjectCanBeRegisteredAsReconstituted() {
174 $session = new TX_EXTMVC_Persistence_Session;
175 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
176 $session->registerReconstitutedObject($entity);
177 $addedObjects = $session->getAddedObjects();
178 $removedObjects = $session->getRemovedObjects();
179 $reconstitutedObjects = $session->getReconstitutedObjects();
180
181 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
182 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
183 $this->assertEquals(1, count($reconstitutedObjects), 'The reconstituted objects storage holds 0 or more than 1 objects.');
184 $this->assertSame($entity, $reconstitutedObjects[0], 'The returned object was not the same as the registered one.');
185 }
186
187 public function test_ObjectCanBeUnregisteredAsAdded() {
188 $session = new TX_EXTMVC_Persistence_Session;
189 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
190 $session->registerAddedObject($entity);
191 $session->unregisterAddedObject($entity);
192 $addedObjects = $session->getAddedObjects();
193 $removedObjects = $session->getRemovedObjects();
194 $reconstitutedObjects = $session->getReconstitutedObjects();
195
196 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
197 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
198 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
199 }
200
201 public function test_ObjectCanBeUnregisteredAsRemoved() {
202 $session = new TX_EXTMVC_Persistence_Session;
203 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
204 $session->registerRemovedObject($entity);
205 $session->unregisterRemovedObject($entity);
206 $addedObjects = $session->getAddedObjects();
207 $removedObjects = $session->getRemovedObjects();
208 $reconstitutedObjects = $session->getReconstitutedObjects();
209
210 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
211 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
212 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
213 }
214
215 public function test_ObjectCanBeUnregisteredAsReconstituted() {
216 $session = new TX_EXTMVC_Persistence_Session;
217 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
218 $session->registerReconstitutedObject($entity);
219 $session->unregisterReconstitutedObject($entity);
220 $addedObjects = $session->getAddedObjects();
221 $removedObjects = $session->getRemovedObjects();
222 $reconstitutedObjects = $session->getReconstitutedObjects();
223
224 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
225 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
226 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
227 }
228 public function test_ObjectCanBeRemovedAfterBeingAdded() {
229 $session = new TX_EXTMVC_Persistence_Session;
230 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
231 $session->registerAddedObject($entity);
232 $session->registerRemovedObject($entity);
233 $addedObjects = $session->getAddedObjects();
234 $removedObjects = $session->getRemovedObjects();
235 $reconstitutedObjects = $session->getReconstitutedObjects();
236
237 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
238 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
239 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
240 }
241
242 public function test_AnObjectCanBeRemovedAfterBeingAdded() {
243 $session = new TX_EXTMVC_Persistence_Session;
244 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
245 $session->registerAddedObject($entity);
246 $session->registerRemovedObject($entity);
247 $addedObjects = $session->getAddedObjects();
248 $removedObjects = $session->getRemovedObjects();
249 $reconstitutedObjects = $session->getReconstitutedObjects();
250
251 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
252 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
253 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
254 }
255
256 public function test_TryingToRegisterReconstitutedObjectsAsAddedResultsInAnException() {
257 $this->setExpectedException('InvalidArgumentException');
258 $session = new TX_EXTMVC_Persistence_Session;
259 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
260 $session->registerReconstitutedObject($entity);
261 $session->registerAddedObject($entity);
262 }
263
264 public function test_TryingToRegisterAddedObjectsAsReconstitutedResultsInAnException() {
265 $this->setExpectedException('InvalidArgumentException');
266 $session = new TX_EXTMVC_Persistence_Session;
267 $entity1 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
268 $session->registerAddedObject($entity1);
269 $session->registerReconstitutedObject($entity1);
270 }
271
272 public function test_SessionCanBeCleared() {
273 $session = new TX_EXTMVC_Persistence_Session;
274 $entity1 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
275 $entity2 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
276 $entity3 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
277 $session->registerAddedObject($entity1);
278 $session->registerRemovedObject($entity2);
279 $session->registerReconstitutedObject($entity3);
280 $session->registerAggregateRootClassName('TX_EXTMVC_DomainObject_Entity');
281 $session->clear();
282 $addedObjects = $session->getAddedObjects();
283 $removedObjects = $session->getRemovedObjects();
284 $reconstitutedObjects = $session->getReconstitutedObjects();
285 $aggregateRootClassNames = $session->getAggregateRootClassNames();
286
287 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
288 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
289 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
290 $this->assertEquals(0, count($aggregateRootClassName), 'The aggregate root class name was not empty.');
291 }
292
293 public function test_ObjectCanBeUnregistered() {
294 $session = new TX_EXTMVC_Persistence_Session;
295 $entity1 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
296 $entity2 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
297 $entity3 = $this->getMock('TX_EXTMVC_DomainObject_Entity');
298 $session->registerAddedObject($entity1);
299 $session->registerRemovedObject($entity2);
300 $session->registerReconstitutedObject($entity3);
301 $session->unregisterObject($entity1);
302 $session->unregisterObject($entity2);
303 $session->unregisterObject($entity3);
304 $addedObjects = $session->getAddedObjects();
305 $removedObjects = $session->getRemovedObjects();
306 $reconstitutedObjects = $session->getReconstitutedObjects();
307
308 $this->assertEquals(0, count($addedObjects), 'The added objects storage was not empty.');
309 $this->assertEquals(0, count($removedObjects), 'The removed objects storage was not empty.');
310 $this->assertEquals(0, count($reconstitutedObjects), 'The reconstituted objects storage was not empty.');
311 }
312
313 public function test_DirtyEntitiesAreReturned() {
314 $session = new TX_EXTMVC_Persistence_Session;
315 $entity = $this->getMock('TX_EXTMVC_DomainObject_Entity');
316 $entity->expects($this->any())
317 ->method('_isDirty')
318 ->will($this->returnValue(TRUE));
319 $session->registerReconstitutedObject($entity);
320 $dirtyObjects = $session->getDirtyObjects();
321 $this->assertEquals(1, count($dirtyObjects), 'There is more than one dirty object.');
322 $this->assertEquals($entity, $dirtyObjects[0], 'The entity doesn\'t equal to the dirty object retrieved from the session.');
323 }
324
325
326 }
327 ?>