TYPO3 CMS  TYPO3_6-2
RelationTest.php
Go to the documentation of this file.
1 <?php
3 
19 
21 
25  protected $numberOfRecordsInFixture = 11;
26 
30  protected $blog;
31 
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 
44  protected $objectManager;
45 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
432  public function mmRelationWithoutMatchFieldIsResolved() {
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 
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 
447  $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
448  $post = $postRepository->findByUid(1);
449  $this->assertSame(3, count($post->getCategories()));
450  }
451 
457  public function mmRelationWithMatchFieldIsResolvedFromForeignSide() {
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 
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 
498  public function adjustingMmRelationWithTablesnameAndFieldnameFieldDoNotTouchOtherRelations() {
500  $postRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\PostRepository');
502  $post = $postRepository->findByUid(1);
503  // Move category down
504  foreach ($post->getCategories() as $category) {
505  $post->removeCategory($category);
506  $post->addCategory($category);
507  break;
508  }
509  $postRepository->update($post);
510  $this->persistentManager->persistAll();
511 
512  // re-fetch Post and Blog
513  $newBlogCategoryCount = $this->getDatabaseConnection()->exec_SELECTcountRows(
514  'uid_local',
515  'sys_category_record_mm',
516  'tablenames = "tx_blogexample_domain_model_blog"
517  AND fieldname = "categories"
518  AND uid_foreign = ' . $this->blog->getUid() . ''
519  );
520 
521  $this->assertSame($this->blog->getCategories()->count(), $newBlogCategoryCount);
522  }
523 
527  protected function updateAndPersistBlog() {
529  $blogRepository = $this->objectManager->get('ExtbaseTeam\\BlogExample\\Domain\\Repository\\BlogRepository');
530  $blogRepository->update($this->blog);
531  $this->persistentManager->persistAll();
532  }
533 }
534