[BUGFIX] Consider property clean if lazy loaded proxy is untouched
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Functional / Persistence / IsDirtyTest.php
1 <?php
2
3 namespace TYPO3\CMS\Extbase\Tests\Functional\Persistence;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use ExtbaseTeam\BlogExample\Domain\Model\Administrator;
19 use ExtbaseTeam\BlogExample\Domain\Repository\AdministratorRepository;
20 use ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Extbase\Object\ObjectManager;
23 use TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy;
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25
26 class IsDirtyTest extends FunctionalTestCase
27 {
28
29 /**
30 * @var array
31 */
32 protected $testExtensionsToLoad = ['typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example'];
33
34 /**
35 * @var array
36 */
37 protected $coreExtensionsToLoad = ['extbase', 'fluid'];
38
39 /**
40 * @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface The object manager
41 */
42 protected $objectManager;
43
44 /**
45 * @var \ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository
46 */
47 protected $blogRepository;
48
49 /**
50 * @var \ExtbaseTeam\BlogExample\Domain\Repository\AdministratorRepository
51 */
52 protected $adminRepository;
53
54 /**
55 * Sets up this test suite.
56 */
57 protected function setUp()
58 {
59 parent::setUp();
60
61 $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
62 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/blogs.xml');
63 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/posts.xml');
64 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/post-post-mm.xml');
65 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/tags.xml');
66 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/tags-mm.xml');
67 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/post-tag-mm.xml');
68 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/persons.xml');
69 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/fe_users.xml');
70 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/extbase/Tests/Functional/Persistence/Fixtures/fe_groups.xml');
71
72 $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
73 $this->blogRepository = $this->objectManager->get(BlogRepository::class);
74 $this->adminRepository = $this->objectManager->get(AdministratorRepository::class);
75 }
76
77 /**
78 * @test
79 */
80 public function objectFetchedFromDbIsNotDirty()
81 {
82 $blog = $this->blogRepository->findByUid(3);
83 $this->assertFalse($blog->_isDirty());
84 }
85
86 /**
87 * @test
88 */
89 public function lazyLoadingProxyReplacedByRealInstanceIsNotDirty()
90 {
91 $blog = $this->blogRepository->findByUid(3);
92 $this->assertInstanceOf(LazyLoadingProxy::class, $blog->getAdministrator()); // precondition
93
94 $admin = $this->adminRepository->findByUid(3);
95 $this->assertInstanceOf(Administrator::class, $admin); // precondition
96
97 $blog->setAdministrator($admin);
98 $this->assertFalse($blog->_isDirty());
99 }
100
101 /**
102 * @test
103 */
104 public function lazyLoadingProxyReplacedByWrongInstanceIsDirty()
105 {
106 $blog = $this->blogRepository->findByUid(3);
107 $this->assertInstanceOf(LazyLoadingProxy::class, $blog->getAdministrator()); //precondition
108
109 $blog->setAdministrator(new Administrator());
110 $this->assertTrue($blog->_isDirty());
111 }
112
113 /**
114 * @test
115 */
116 public function realInstanceReplacedByLazyLoadingProxyIsNotDirty()
117 {
118 $blog = $this->blogRepository->findByUid(3);
119 $lazyLoadingProxy = $blog->getAdministrator();
120 $this->assertInstanceOf(LazyLoadingProxy::class, $lazyLoadingProxy); //precondition
121
122 $admin = $this->adminRepository->findByUid(3);
123 $this->assertInstanceOf(Administrator::class, $admin); // precondition
124
125 $blog->setAdministrator($admin);
126 $blog->_memorizeCleanState();
127
128 $blog->_setProperty('administrator', $lazyLoadingProxy);
129 $this->assertFalse($blog->_isDirty());
130 }
131
132 /**
133 * @test
134 */
135 public function lazyLoadingProxyByWrongLazyLoadingProxyIsDirtyAndUpdated()
136 {
137 $blogOne = $this->blogRepository->findByUid(3);
138 $this->assertInstanceOf(LazyLoadingProxy::class, $blogOne->getAdministrator()); //precondition
139
140 $blogTwo = $this->blogRepository->findByUid(2);
141 $this->assertInstanceOf(LazyLoadingProxy::class, $blogTwo->getAdministrator()); //precondition
142
143 $blogOne->_setProperty('administrator', $blogTwo->getAdministrator());
144 $this->assertTrue($blogOne->_isDirty());
145
146 $this->blogRepository->update($blogOne);
147
148 $updatedBlogOne = $this->blogRepository->findByUid(3);
149 $this->assertSame($updatedBlogOne->getAdministrator()->getUid(), $blogTwo->getAdministrator()->getUid());
150 }
151 }