[TASK] Re-work/simplify copyright header in PHP files - Part 8
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Functional / Persistence / RelationTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Functional\Persistence;
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 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
19
20 class RelationTest extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
21
22 /**
23 * @var int number of all records
24 */
25 protected $numberOfRecordsInFixture = 11;
26
27 /**
28 * @var \ExtbaseTeam\BlogExample\Domain\Model\Blog
29 */
30 protected $blog;
31
32 /**
33 * @var \TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager
34 */
35 protected $persistentManager;
36
37 protected $testExtensionsToLoad = array('typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example');
38
39 protected $coreExtensionsToLoad = array('extbase', 'fluid');
40
41 /**
42 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface The object manager
43 */
44 protected $objectManager;
45
46 /**
47 * Sets up this test suite.
48 */
49 public function setUp() {
50 parent::setUp();
51
52 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/pages.xml');
53 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/blogs.xml');
54 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/posts.xml');
55 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/tags.xml');
56 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/post-tag-mm.xml');
57 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/categories.xml');
58 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/category-mm.xml');
59
60 $this->objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
61 $this->persistentManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager');
62 /* @var $blogRepository \TYPO3\CMS\Extbase\Persistence\Repository */
63 $blogRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\BlogRepository');
64 $this->blog = $blogRepository->findByUid(1);
65 }
66
67 /**
68 * Tests adding object at the end of sorted 1:M relation (Blog:Posts)
69 *
70 * @test
71 */
72 public function attachPostToBlogAtTheEnd() {
73 $countPosts = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post');
74 $this->assertSame($this->numberOfRecordsInFixture, $countPosts);
75
76 $newPostTitle = 'sdufhisdhuf';
77 $newPost = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Model\\Post');
78 $newPost->setBlog($this->blog);
79 $newPost->setTitle($newPostTitle);
80 $newPost->setContent('Bla Bla Bla');
81
82 $this->blog->addPost($newPost);
83 $this->updateAndPersistBlog();
84
85 $countPosts = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post');
86 $this->assertSame(($this->numberOfRecordsInFixture + 1), $countPosts);
87
88 $post = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('title,sorting', 'tx_blogexample_domain_model_post', 'blog =' . $this->blog->getUid(), '', 'sorting DESC');
89 $this->assertSame($newPostTitle, $post['title']);
90 $this->assertSame((string)($this->numberOfRecordsInFixture), $post['sorting']);
91 }
92
93 /**
94 * Tests removing object from the end of sorted 1:M relation (Blog:Posts)
95 *
96 * @test
97 */
98 public function removeLastPostFromBlog() {
99 $countPosts = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post');
100 $this->assertSame($this->numberOfRecordsInFixture, $countPosts);
101
102 $post = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('sorting', 'tx_blogexample_domain_model_post', 'blog =' . $this->blog->getUid(), '', 'sorting DESC');
103 $this->assertEquals(10, $post['sorting']);
104
105 $posts = $this->blog->getPosts();
106 $postsArray = $posts->toArray();
107 $latestPost = array_pop($postsArray);
108
109 $this->assertEquals(10, $latestPost->getUid());
110
111 $this->blog->removePost($latestPost);
112 $this->updateAndPersistBlog();
113
114 $countPosts = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post', 'deleted=0');
115 $this->assertEquals(($this->numberOfRecordsInFixture - 1), $countPosts);
116
117 $post = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('uid', 'tx_blogexample_domain_model_post', 'uid =' . $latestPost->getUid() . ' AND deleted=0');
118 $this->assertSame(NULL, $post['uid']);
119
120 $post = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('title,sorting', 'tx_blogexample_domain_model_post', 'blog =' . $this->blog->getUid(), '', 'sorting DESC');
121 $this->assertSame('Post9', $post['title']);
122 $this->assertSame('9', $post['sorting']);
123 }
124
125 /**
126 * Tests adding object in the middle of the sorted 1:M relation (Blog:Posts)
127 *
128 * @test
129 */
130 public function addPostToBlogInTheMiddle() {
131 $countPosts = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post');
132 $this->assertSame($this->numberOfRecordsInFixture, $countPosts);
133
134 $posts = clone $this->blog->getPosts();
135 $this->blog->getPosts()->removeAll($posts);
136 $counter = 1;
137 $newPostTitle = 'INSERTED POST at position 6';
138 foreach ($posts as $post) {
139 $this->blog->addPost($post);
140 if ($counter == 5) {
141 $newPost = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Model\\Post');
142 $newPost->setBlog($this->blog);
143 $newPost->setTitle($newPostTitle);
144 $newPost->setContent('Bla Bla Bla');
145 $this->blog->addPost($newPost);
146 }
147 $counter++;
148 }
149 $this->updateAndPersistBlog();
150
151 $countPosts = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post', 'deleted=0');
152 $this->assertSame(($this->numberOfRecordsInFixture + 1), $countPosts);
153
154 //last post
155 $post = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('title,sorting', 'tx_blogexample_domain_model_post', 'blog =' . $this->blog->getUid(), '', 'sorting DESC');
156 $this->assertSame('Post10', $post['title']);
157 $this->assertSame('11', $post['sorting']);
158
159 // check sorting of the post added in the middle
160 $post = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('title,sorting', 'tx_blogexample_domain_model_post', 'uid=' . ($this->numberOfRecordsInFixture + 1));
161 $this->assertSame($newPostTitle, $post['title']);
162 $this->assertSame('6', $post['sorting']);
163 }
164
165 /**
166 * Tests removing object from the middle of sorted 1:M relation (Blog:Posts)
167 *
168 * @test
169 */
170 public function removeMiddlePostFromBlog() {
171 $countPosts = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post');
172 $this->assertSame($this->numberOfRecordsInFixture, $countPosts);
173
174 $posts = clone $this->blog->getPosts();
175 $counter = 1;
176 foreach ($posts as $post) {
177 if ($counter == 5) {
178 $this->blog->removePost($post);
179 }
180 $counter++;
181 }
182 $this->updateAndPersistBlog();
183
184 $countPosts = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post', 'deleted=0');
185 $this->assertSame(($this->numberOfRecordsInFixture - 1), $countPosts);
186
187 $post = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('title,sorting', 'tx_blogexample_domain_model_post', 'blog ='.$this->blog->getUid(), '', 'sorting DESC');
188 $this->assertSame('Post10', $post['title']);
189 $this->assertSame('10', $post['sorting']);
190 }
191
192 /**
193 * Tests moving object from the end to the middle of the sorted 1:M relation (Blog:Posts)
194 *
195 * @test
196 */
197 public function movePostFromEndToTheMiddle() {
198 $countPosts = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post');
199 $this->assertSame($this->numberOfRecordsInFixture, $countPosts);
200
201 $posts = clone $this->blog->getPosts();
202 $postsArray = $posts->toArray();
203 $latestPost = array_pop($postsArray);
204
205 $this->blog->getPosts()->removeAll($posts);
206 $counter = 0;
207 $postCount = $posts->count();
208 foreach ($posts as $post) {
209 if ($counter != ($postCount - 1)) {
210 $this->blog->addPost($post);
211 }
212 if ($counter == 4) {
213 $latestPost->setTitle('MOVED POST ' . $latestPost->getTitle());
214 $this->blog->addPost($latestPost);
215 }
216 $counter++;
217 }
218 $this->updateAndPersistBlog();
219
220 $countPosts = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_post', 'deleted=0');
221 $this->assertSame($this->numberOfRecordsInFixture, $countPosts);
222
223 $post = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('title,sorting', 'tx_blogexample_domain_model_post', 'blog ='.$this->blog->getUid(), '', 'sorting DESC');
224 $this->assertSame('Post9', $post['title']);
225 $this->assertSame('10', $post['sorting']);
226
227 $post = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('title,uid', 'tx_blogexample_domain_model_post', 'blog ='.$this->blog->getUid().' AND sorting=6');
228 $this->assertSame('MOVED POST Post10', $post['title']);
229 $this->assertSame('10', $post['uid']);
230 }
231
232 /**
233 * Tests adding object at the end of sorted M:M relation (Post:Tag)
234 *
235 * @test
236 */
237 public function attachTagToPostAtTheEnd() {
238 $count = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_tag');
239 $this->assertSame(10, $count);
240
241 $newTagTitle = 'sdufhisdhuf';
242 $newTag = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Model\\Tag', $newTagTitle);
243
244 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
245 $post = $postRepository->findByUid(1);
246 $post->addTag($newTag);
247
248 $postRepository->update($post);
249 $this->persistentManager->persistAll();
250
251 $count = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_tag');
252 $this->assertSame(11, $count);
253
254 $tag = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid(), '', 'sorting DESC');
255 $this->assertSame('11', $tag['uid_foreign']);
256 }
257
258
259 /**
260 * Tests removing object from the end of sorted M:M relation (Post:Tag)
261 *
262 * @test
263 */
264 public function removeLastTagFromPost() {
265 $count = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_tag');
266 $this->assertSame(10, $count);
267
268 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
269 $post = $postRepository->findByUid(1);
270 $tags = $post->getTags();
271 $tagsArray = $tags->toArray();
272 $latestTag = array_pop($tagsArray);
273
274 $this->assertEquals(10, $latestTag->getUid());
275
276 $post->removeTag($latestTag);
277
278 $postRepository->update($post);
279 $this->persistentManager->persistAll();
280
281 $countPosts = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_domain_model_tag', 'deleted=0' );
282 $this->assertEquals(10, $countPosts);
283
284 $tag = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid(), '', 'sorting DESC');
285 $this->assertSame('9', $tag['uid_foreign']);
286
287 $tag = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid().' AND uid_foreign='.$latestTag->getUid());
288 $this->assertSame(NULL, $tag['uid_foreign']);
289 }
290
291 /**
292 * Tests adding object in the middle of sorted M:M relation (Post:Tag)
293 *
294 * @test
295 */
296 public function addTagToPostInTheMiddle() {
297 $countTags = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_post_tag_mm', 'uid_local=1');
298 $this->assertSame(10, $countTags);
299
300 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
301 $post = $postRepository->findByUid(1);
302 $tags = clone $post->getTags();
303 $post->setTags(new ObjectStorage());
304
305 $counter = 1;
306 foreach ($tags as $tag) {
307 $post->addTag($tag);
308 if ($counter == 5) {
309 $newTag = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Model\\Tag', 'INSERTED TAG at position 6 : ' . strftime(''));
310 $post->addTag($newTag);
311 }
312 $counter++;
313 }
314
315 $postRepository->update($post);
316 $this->persistentManager->persistAll();
317
318 $countTags = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_post_tag_mm', 'uid_local=1');
319 $this->assertSame(11, $countTags);
320
321 $tag = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid(), '', 'sorting DESC');
322 $this->assertSame('10', $tag['uid_foreign']);
323
324 $tag = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid().' AND sorting=6');
325 $this->assertSame('11', $tag['uid_foreign']);
326 }
327
328
329 /**
330 * Tests removing object from the middle of the sorted M:M relation (Post:Tag)
331 *
332 * @test
333 */
334 public function removeMiddleTagFromPost() {
335 $countTags = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_post_tag_mm', 'uid_local=1');
336 $this->assertSame(10, $countTags);
337
338 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
339 $post = $postRepository->findByUid(1);
340 $tags = clone $post->getTags();
341 $counter = 1;
342 foreach ($tags as $tag) {
343 if ($counter == 5) {
344 $post->removeTag($tag);
345 }
346 $counter++;
347 }
348
349 $postRepository->update($post);
350 $this->persistentManager->persistAll();
351
352 $countTags = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_post_tag_mm', 'uid_local=1');
353 $this->assertSame(9, $countTags);
354
355 $tag = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('uid_foreign,sorting', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid(), '', 'sorting DESC');
356 $this->assertSame('10', $tag['uid_foreign']);
357 $this->assertSame('10', $tag['sorting']);
358
359 $tag = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid().' AND sorting=5');
360 $this->assertSame(NULL, $tag['uid_foreign']);
361 }
362
363 /**
364 * Tests moving object from the end to the middle of sorted M:M relation (Post:Tag)
365 *
366 * @test
367 */
368 public function moveTagFromEndToTheMiddle() {
369 $countTags = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_post_tag_mm', 'uid_local=1');
370 $this->assertSame(10, $countTags);
371
372 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
373 $post = $postRepository->findByUid(1);
374 $tags = clone $post->getTags();
375 $tagsArray = $tags->toArray();
376 $latestTag = array_pop($tagsArray);
377 $post->removeTag($latestTag);
378 $post->setTags(new ObjectStorage());
379
380 $counter = 1;
381 $tagCount = $tags->count();
382 foreach ($tags as $tag) {
383 if ($counter != $tagCount) {
384 $post->addTag($tag);
385 }
386 if ($counter == 5) {
387 $post->addTag($latestTag);
388 }
389 $counter++;
390 }
391 $post->addTag($latestTag);
392
393 $postRepository->update($post);
394 $this->persistentManager->persistAll();
395
396 $countTags = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'tx_blogexample_post_tag_mm', 'uid_local=1');
397 $this->assertSame(10, $countTags);
398
399 $tag = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('uid_foreign,sorting', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid(), '', 'sorting DESC');
400 $this->assertSame('9', $tag['uid_foreign']);
401 $this->assertSame('10', $tag['sorting']);
402
403 $sorting = '6';
404 $tag = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('uid_foreign', 'tx_blogexample_post_tag_mm', 'uid_local ='.$post->getUid().' AND sorting='.$sorting);
405 $this->assertSame('10', $tag['uid_foreign']);
406 }
407
408 /**
409 * Test if timestamp field is updated when updating a record
410 *
411 * @test
412 */
413 public function timestampFieldIsUpdatedOnPostSave() {
414 $rawPost = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', 'tx_blogexample_domain_model_post', 'uid=1');
415
416 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
417 $post = $postRepository->findByUid(1);
418 $post->setTitle("newTitle");
419
420 $postRepository->update($post);
421 $this->persistentManager->persistAll();
422
423 $rawPost2 = $this->getDatabaseConnection()->exec_SELECTgetSingleRow('*', 'tx_blogexample_domain_model_post', 'uid=1');
424 $this->assertTrue($rawPost2['tstamp'] > $rawPost['tstamp']);
425 }
426
427 /**
428 * Test query matching for mm relation without MM_match_fields defined
429 *
430 * @test
431 */
432 public function mmRelationWithoutMatchFieldIsResolved() {
433 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
434 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
435 $posts = $postRepository->findByTagAndBlog('Tag2', $this->blog);
436 $this->assertSame(1, count($posts));
437 }
438
439 /**
440 * @test
441 */
442 public function mmRelationWithMatchFieldIsResolvedFromLocalSide() {
443 $countCategories = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'sys_category_record_mm', 'uid_foreign=1 AND tablenames="tx_blogexample_domain_model_post" AND fieldname="categories"');
444 $this->assertSame(3, $countCategories);
445
446 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
447 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
448 $post = $postRepository->findByUid(1);
449 $this->assertSame(3, count($post->getCategories()));
450 }
451
452 /**
453 * Test query matching respects MM_match_fields
454 *
455 * @test
456 */
457 public function mmRelationWithMatchFieldIsResolvedFromForeignSide() {
458 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\PostRepository $postRepository */
459 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
460 $posts = $postRepository->findByCategory(1);
461 $this->assertSame(2, count($posts));
462
463 $posts = $postRepository->findByCategory(4);
464 $this->assertSame(0, count($posts));
465 }
466
467 /**
468 * @test
469 */
470 public function mmRelationWithMatchFieldIsCreatedFromLocalSide() {
471 $countCategories = $this->getDatabaseConnection()->exec_SELECTcountRows('*', 'sys_category_record_mm', 'uid_foreign=1 AND tablenames="tx_blogexample_domain_model_post" AND fieldname="categories"');
472 $this->assertSame(3, $countCategories);
473
474 $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
475 $post = $postRepository->findByUid(1);
476
477 $newCategory = $this->objectManager->get('\\TYPO3\\CMS\\Extbase\\Domain\\Model\\Category');
478 $newCategory->setTitle('New Category');
479
480 $post->addCategory($newCategory);
481
482 $postRepository->update($post);
483 $this->persistentManager->persistAll();
484
485 $countCategories = $this->getDatabaseConnection()->exec_SELECTcountRows(
486 '*',
487 'sys_category_record_mm',
488 'uid_foreign=1 AND tablenames="tx_blogexample_domain_model_post" AND fieldname="categories"'
489 );
490 $this->assertSame(4, $countCategories);
491 }
492
493 /**
494 * Helper method for persisting blog
495 */
496 protected function updateAndPersistBlog() {
497 /** @var \ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository $blogRepository */
498 $blogRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\BlogRepository');
499 $blogRepository->update($this->blog);
500 $this->persistentManager->persistAll();
501 }
502 }
503