8ffee53d27d96cb0a41c3b203a76a6ef142c6da7
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Functional / Relations / RelationTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Functional\Relations;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2014 Tymoteusz Motylewski <t.motylewski@gmail.com>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
29
30 class RelationTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
31
32 /**
33 * @var \ExtbaseTeam\BlogExample\Domain\Model\Blog
34 */
35 protected $blog;
36
37 /**
38 * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
39 */
40 protected $persistentManager;
41
42 protected $testExtensionsToLoad = array('typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example');
43
44 protected $coreExtensionsToLoad = array('extbase', 'fluid');
45
46 /**
47 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface The object manager
48 */
49 protected $objectManager;
50
51 /**
52 * Sets up this test suite.
53 */
54 public function setUp() {
55 parent::setUp();
56
57 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/pages.xml');
58 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Relations/Fixtures/blogs.xml');
59 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Relations/Fixtures/posts.xml');
60 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Relations/Fixtures/tags.xml');
61 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Relations/Fixtures/post-tag-mm.xml');
62
63 $this->objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
64 $this->persistentManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager');
65 /* @var $blogRepository \TYPO3\CMS\Extbase\Persistence\Repository */
66 $blogRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\BlogRepository');
67 $this->blog = $blogRepository->findByUid(1);
68 }
69
70 /**
71 * Tests adding object at the end of sorted 1:M relation (Blog:Posts)
72 *
73 * @test
74 */
75 public function attachPostToBlogAtTheEnd() {
76 $countPosts = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post');
77 $this->assertSame(10, $countPosts);
78
79 $newPostTitle = 'sdufhisdhuf';
80 $newPost = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Model\\Post');
81 $newPost->setBlog($this->blog);
82 $newPost->setTitle($newPostTitle);
83 $newPost->setContent('Bla Bla Bla');
84
85 $this->blog->addPost($newPost);
86 $this->updateAndPersistBlog();
87
88 $countPosts = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post');
89 $this->assertSame(11, $countPosts);
90
91 $post = $this->getDatabase()->exec_SELECTgetSingleRow('title,sorting', 'tx_blogexample_domain_model_post', 'blog =' . $this->blog->getUid(), '', 'sorting DESC');
92 $this->assertSame($newPostTitle, $post['title']);
93 $this->assertSame('11', $post['sorting']);
94 }
95
96 /**
97 * Tests removing object from the end of sorted 1:M relation (Blog:Posts)
98 *
99 * @test
100 */
101 public function removeLastPostFromBlog() {
102 $countPosts = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post');
103 $this->assertSame(10, $countPosts);
104
105 $post = $this->getDatabase()->exec_SELECTgetSingleRow('sorting', 'tx_blogexample_domain_model_post', 'blog =' . $this->blog->getUid(), '', 'sorting DESC');
106 $this->assertEquals(10, $post['sorting']);
107
108 $posts = $this->blog->getPosts();
109 $postsArray = $posts->toArray();
110 $latestPost = array_pop($postsArray);
111
112 $this->assertEquals(10, $latestPost->getUid());
113
114 $this->blog->removePost($latestPost);
115 $this->updateAndPersistBlog();
116
117 $countPosts = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post', 'deleted=0');
118 $this->assertEquals(9, $countPosts);
119
120 $post = $this->getDatabase()->exec_SELECTgetSingleRow('uid', 'tx_blogexample_domain_model_post', 'uid =' . $latestPost->getUid() . ' AND deleted=0');
121 $this->assertSame(NULL, $post['uid']);
122
123 $post = $this->getDatabase()->exec_SELECTgetSingleRow('title,sorting', 'tx_blogexample_domain_model_post', 'blog =' . $this->blog->getUid(), '', 'sorting DESC');
124 $this->assertSame('Post9', $post['title']);
125 $this->assertSame('9', $post['sorting']);
126 }
127
128 /**
129 * Tests adding object in the middle of the sorted 1:M relation (Blog:Posts)
130 *
131 * @test
132 */
133 public function addPostToBlogInTheMiddle() {
134 $countPosts = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post');
135 $this->assertSame(10, $countPosts);
136
137 $posts = clone $this->blog->getPosts();
138 $this->blog->getPosts()->removeAll($posts);
139 $counter = 1;
140 $newPostTitle = 'INSERTED POST at position 6';
141 foreach ($posts as $post) {
142 $this->blog->addPost($post);
143 if ($counter == 5) {
144 $newPost = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Model\\Post');
145 $newPost->setBlog($this->blog);
146 $newPost->setTitle($newPostTitle);
147 $newPost->setContent('Bla Bla Bla');
148 $this->blog->addPost($newPost);
149 }
150 $counter++;
151 }
152 $this->updateAndPersistBlog();
153
154 $countPosts = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post', 'deleted=0');
155 $this->assertSame(11, $countPosts);
156
157 //last post
158 $post = $this->getDatabase()->exec_SELECTgetSingleRow('title,sorting', 'tx_blogexample_domain_model_post', 'blog =' . $this->blog->getUid(), '', 'sorting DESC');
159 $this->assertSame('Post10', $post['title']);
160 $this->assertSame('11', $post['sorting']);
161
162 // check sorting of the post added in the middle
163 $post = $this->getDatabase()->exec_SELECTgetSingleRow('title,sorting', 'tx_blogexample_domain_model_post', 'uid=11');
164 $this->assertSame($newPostTitle, $post['title']);
165 $this->assertSame('6', $post['sorting']);
166 }
167
168 /**
169 * Tests removing object from the middle of sorted 1:M relation (Blog:Posts)
170 *
171 * @test
172 */
173 public function removeMiddlePostFromBlog() {
174 $countPosts = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post');
175 $this->assertSame(10, $countPosts);
176
177 $posts = clone $this->blog->getPosts();
178 $counter = 1;
179 foreach ($posts as $post) {
180 if ($counter == 5) {
181 $this->blog->removePost($post);
182 }
183 $counter++;
184 }
185 $this->updateAndPersistBlog();
186
187 $countPosts = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post', 'deleted=0');
188 $this->assertSame(9, $countPosts);
189
190 $post = $this->getDatabase()->exec_SELECTgetSingleRow('title,sorting', 'tx_blogexample_domain_model_post', 'blog ='.$this->blog->getUid(), '', 'sorting DESC');
191 $this->assertSame('Post10', $post['title']);
192 $this->assertSame('10', $post['sorting']);
193 }
194
195 /**
196 * Tests moving object from the end to the middle of the sorted 1:M relation (Blog:Posts)
197 *
198 * @test
199 */
200 public function movePostFromEndToTheMiddle() {
201 $countPosts = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post');
202 $this->assertSame(10, $countPosts);
203
204 $posts = clone $this->blog->getPosts();
205 $postsArray = $posts->toArray();
206 $latestPost = array_pop($postsArray);
207
208 $this->blog->getPosts()->removeAll($posts);
209 $counter = 0;
210 $postCount = $posts->count();
211 foreach ($posts as $post) {
212 if ($counter != ($postCount - 1)) {
213 $this->blog->addPost($post);
214 }
215 if ($counter == 4) {
216 $latestPost->setTitle('MOVED POST ' . $latestPost->getTitle());
217 $this->blog->addPost($latestPost);
218 }
219 $counter++;
220 }
221 $this->updateAndPersistBlog();
222
223 $countPosts = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post', 'deleted=0');
224 $this->assertSame(10, $countPosts);
225
226 $post = $this->getDatabase()->exec_SELECTgetSingleRow('title,sorting', 'tx_blogexample_domain_model_post', 'blog ='.$this->blog->getUid(), '', 'sorting DESC');
227 $this->assertSame('Post9', $post['title']);
228 $this->assertSame('10', $post['sorting']);
229
230 $post = $this->getDatabase()->exec_SELECTgetSingleRow('title,uid', 'tx_blogexample_domain_model_post', 'blog ='.$this->blog->getUid().' AND sorting=6');
231 $this->assertSame('MOVED POST Post10', $post['title']);
232 $this->assertSame('10', $post['uid']);
233 }
234
235 /**
236 * Tests adding object at the end of sorted M:M relation (Post:Tag)
237 *
238 * @test
239 */
240 public function attachTagToPostAtTheEnd() {
241 $count = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_tag');
242 $this->assertSame(10, $count);
243
244 $newTagTitle = 'sdufhisdhuf';
245 $newTag = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Model\\Tag', $newTagTitle);
246
247 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
248 $post = $postRepository->findByUid(1);
249 $post->addTag($newTag);
250
251 $postRepository->update($post);
252 $this->persistentManager->persistAll();
253
254 $count = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_tag');
255 $this->assertSame(11, $count);
256
257 $tag = $this->getDatabase()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid(), '', 'sorting DESC');
258 $this->assertSame('11', $tag['uid_foreign']);
259 }
260
261
262 /**
263 * Tests removing object from the end of sorted M:M relation (Post:Tag)
264 *
265 * @test
266 */
267 public function removeLastTagFromPost() {
268 $count = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_tag');
269 $this->assertSame(10, $count);
270
271 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
272 $post = $postRepository->findByUid(1);
273 $tags = $post->getTags();
274 $tagsArray = $tags->toArray();
275 $latestTag = array_pop($tagsArray);
276
277 $this->assertEquals(10, $latestTag->getUid());
278
279 $post->removeTag($latestTag);
280
281 $postRepository->update($post);
282 $this->persistentManager->persistAll();
283
284 $countPosts = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_tag', 'deleted=0' );
285 $this->assertEquals(10, $countPosts);
286
287 $tag = $this->getDatabase()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid(), '', 'sorting DESC');
288 $this->assertSame('9', $tag['uid_foreign']);
289
290 $tag = $this->getDatabase()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid().' AND uid_foreign='.$latestTag->getUid());
291 $this->assertSame(NULL, $tag['uid_foreign']);
292 }
293
294 /**
295 * Tests adding object in the middle of sorted M:M relation (Post:Tag)
296 *
297 * @test
298 */
299 public function addTagToPostInTheMiddle() {
300 $countTags = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_post_tag_mm', 'uid_local=1');
301 $this->assertSame(10, $countTags);
302
303 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
304 $post = $postRepository->findByUid(1);
305 $tags = clone $post->getTags();
306 $post->setTags(new ObjectStorage());
307
308 $counter = 1;
309 foreach ($tags as $tag) {
310 $post->addTag($tag);
311 if ($counter == 5) {
312 $newTag = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Model\\Tag', 'INSERTED TAG at position 6 : ' . strftime(''));
313 $post->addTag($newTag);
314 }
315 $counter++;
316 }
317
318 $postRepository->update($post);
319 $this->persistentManager->persistAll();
320
321 $countTags = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_post_tag_mm', 'uid_local=1');
322 $this->assertSame(11, $countTags);
323
324 $tag = $this->getDatabase()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid(), '', 'sorting DESC');
325 $this->assertSame('10', $tag['uid_foreign']);
326
327 $tag = $this->getDatabase()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid().' AND sorting=6');
328 $this->assertSame('11', $tag['uid_foreign']);
329 }
330
331
332 /**
333 * Tests removing object from the middle of the sorted M:M relation (Post:Tag)
334 *
335 * @test
336 */
337 public function removeMiddleTagFromPost() {
338 $countTags = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_post_tag_mm', 'uid_local=1');
339 $this->assertSame(10, $countTags);
340
341 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
342 $post = $postRepository->findByUid(1);
343 $tags = clone $post->getTags();
344 $counter = 1;
345 foreach ($tags as $tag) {
346 if ($counter == 5) {
347 $post->removeTag($tag);
348 }
349 $counter++;
350 }
351
352 $postRepository->update($post);
353 $this->persistentManager->persistAll();
354
355 $countTags = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_post_tag_mm', 'uid_local=1');
356 $this->assertSame(9, $countTags);
357
358 $tag = $this->getDatabase()->exec_SELECTgetSingleRow('uid_foreign,sorting', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid(), '', 'sorting DESC');
359 $this->assertSame('10', $tag['uid_foreign']);
360 $this->assertSame('10', $tag['sorting']);
361
362 $tag = $this->getDatabase()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid().' AND sorting=5');
363 $this->assertSame(NULL, $tag['uid_foreign']);
364 }
365
366 /**
367 * Tests moving object from the end to the middle of sorted M:M relation (Post:Tag)
368 *
369 * @test
370 */
371 public function moveTagFromEndToTheMiddle() {
372 $countTags = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_post_tag_mm', 'uid_local=1');
373 $this->assertSame(10, $countTags);
374
375 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
376 $post = $postRepository->findByUid(1);
377 $tags = clone $post->getTags();
378 $tagsArray = $tags->toArray();
379 $latestTag = array_pop($tagsArray);
380 $post->removeTag($latestTag);
381 $post->setTags(new ObjectStorage());
382
383 $counter = 1;
384 $tagCount = $tags->count();
385 foreach ($tags as $tag) {
386 if ($counter != $tagCount) {
387 $post->addTag($tag);
388 }
389 if ($counter == 5) {
390 $post->addTag($latestTag);
391 }
392 $counter++;
393 }
394 $post->addTag($latestTag);
395
396 $postRepository->update($post);
397 $this->persistentManager->persistAll();
398
399 $countTags = $this->getDatabase()->exec_SELECTcountRows('*', 'tx_blogexample_post_tag_mm', 'uid_local=1');
400 $this->assertSame(10, $countTags);
401
402 $tag = $this->getDatabase()->exec_SELECTgetSingleRow('uid_foreign,sorting', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid(), '', 'sorting DESC');
403 $this->assertSame('9', $tag['uid_foreign']);
404 $this->assertSame('10', $tag['sorting']);
405
406 $sorting = '6';
407 $tag = $this->getDatabase()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid().' AND sorting='.$sorting);
408 $this->assertSame('10', $tag['uid_foreign']);
409 }
410
411 /**
412 * Test if timestamp field is updated when updating a record
413 *
414 * @test
415 */
416 public function timestampFieldIsUpdatedOnPostSave() {
417 $rawPost = $this->getDatabase()->exec_SELECTgetSingleRow('*', 'tx_blogexample_domain_model_post', 'uid=1');
418
419 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
420 $post = $postRepository->findByUid(1);
421 $post->setTitle("newTitle");
422
423 $postRepository->update($post);
424 $this->persistentManager->persistAll();
425
426 $rawPost2 = $this->getDatabase()->exec_SELECTgetSingleRow('*', 'tx_blogexample_domain_model_post', 'uid=1');
427 $this->assertTrue($rawPost2['tstamp'] > $rawPost['tstamp']);
428 }
429
430 /**
431 * Helper method for persisting blog
432 */
433 protected function updateAndPersistBlog() {
434 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository $blogRepository */
435 $blogRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\BlogRepository');
436 $blogRepository->update($this->blog);
437 $this->persistentManager->persistAll();
438 }
439 }