[TASK] Unit tests: Do not rely on CacheManager instance
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / RootlineUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
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\Cache\CacheManager;
18 use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\RootlineUtility;
21 use TYPO3\CMS\Frontend\Page\PageRepository;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Test case
26 */
27 class RootlineUtilityTest extends UnitTestCase
28 {
29 /**
30 * Subject is not notice free, disable E_NOTICES
31 */
32 protected static $suppressNotices = true;
33
34 /**
35 * @var RootlineUtility|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\PHPUnit_Framework_MockObject_MockObject
36 */
37 protected $subject;
38
39 /**
40 * @var PageRepository|\PHPUnit_Framework_MockObject_MockObject
41 */
42 protected $pageContextMock;
43
44 protected function setUp()
45 {
46 $cacheManagerProphecy = $this->prophesize(CacheManager::class);
47 GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerProphecy->reveal());
48 $cacheFrontendProphecy = $this->prophesize(FrontendInterface::class);
49 $cacheManagerProphecy->getCache('cache_rootline')->willReturn($cacheFrontendProphecy->reveal());
50
51 $this->pageContextMock = $this->createMock(PageRepository::class);
52 $this->subject = $this->getAccessibleMock(RootlineUtility::class, ['enrichWithRelationFields'], [1, '', $this->pageContextMock]);
53 }
54
55 protected function tearDown()
56 {
57 RootlineUtility::purgeCaches();
58 GeneralUtility::purgeInstances();
59 parent::tearDown();
60 }
61
62 /**
63 * Tests that $subsetCandidate is completely part of $superset
64 * and keys match.
65 *
66 * @see (A ^ B) = A <=> A c B
67 * @param array $subsetCandidate
68 * @param array $superset
69 */
70 protected function assertIsSubset(array $subsetCandidate, array $superset)
71 {
72 $this->assertSame($subsetCandidate, array_intersect_assoc($subsetCandidate, $superset));
73 }
74
75 /**
76 * @test
77 */
78 public function isMountedPageWithoutMountPointsReturnsFalse()
79 {
80 $this->subject->__construct(1);
81 $this->assertFalse($this->subject->isMountedPage());
82 }
83
84 /**
85 * @test
86 */
87 public function isMountedPageWithMatchingMountPointParameterReturnsTrue()
88 {
89 $this->subject->__construct(1, '1-99');
90 $this->assertTrue($this->subject->isMountedPage());
91 }
92
93 /**
94 * @test
95 */
96 public function isMountedPageWithNonMatchingMountPointParameterReturnsFalse()
97 {
98 $this->subject->__construct(1, '99-99');
99 $this->assertFalse($this->subject->isMountedPage());
100 }
101
102 /**
103 * @test
104 */
105 public function processMountedPageWithNonMountedPageThrowsException()
106 {
107 $this->expectException(\RuntimeException::class);
108 $this->expectExceptionCode(1343464100);
109
110 $this->subject->__construct(1, '1-99');
111 $this->subject->_call('processMountedPage', ['uid' => 1], ['uid' => 99, 'doktype' => PageRepository::DOKTYPE_DEFAULT]);
112 }
113
114 /**
115 * @test
116 */
117 public function processMountedPageWithMountedPageNotThrowsException()
118 {
119 $this->subject->__construct(1, '1-99');
120 $this->assertNotEmpty($this->subject->_call('processMountedPage', ['uid' => 1], ['uid' => 99, 'doktype' => PageRepository::DOKTYPE_MOUNTPOINT, 'mount_pid' => 1]));
121 }
122
123 /**
124 * @test
125 */
126 public function processMountedPageWithMountedPageAddsMountedFromParameter()
127 {
128 $this->subject->__construct(1, '1-99');
129 $result = $this->subject->_call('processMountedPage', ['uid' => 1], ['uid' => 99, 'doktype' => PageRepository::DOKTYPE_MOUNTPOINT, 'mount_pid' => 1]);
130 $this->assertTrue(isset($result['_MOUNTED_FROM']));
131 $this->assertSame(1, $result['_MOUNTED_FROM']);
132 }
133
134 /**
135 * @test
136 */
137 public function processMountedPageWithMountedPageAddsMountPointParameterToReturnValue()
138 {
139 $this->subject->__construct(1, '1-99');
140 $result = $this->subject->_call('processMountedPage', ['uid' => 1], ['uid' => 99, 'doktype' => PageRepository::DOKTYPE_MOUNTPOINT, 'mount_pid' => 1]);
141 $this->assertTrue(isset($result['_MP_PARAM']));
142 $this->assertSame('1-99', $result['_MP_PARAM']);
143 }
144
145 /**
146 * @test
147 */
148 public function processMountedPageForMountPageIsOverlayAddsMountOLParameter()
149 {
150 $this->subject->__construct(1, '1-99');
151 $result = $this->subject->_call('processMountedPage', ['uid' => 1], ['uid' => 99, 'doktype' => PageRepository::DOKTYPE_MOUNTPOINT, 'mount_pid' => 1, 'mount_pid_ol' => 1]);
152 $this->assertTrue(isset($result['_MOUNT_OL']));
153 $this->assertSame(true, $result['_MOUNT_OL']);
154 }
155
156 /**
157 * @test
158 */
159 public function processMountedPageForMountPageIsOverlayAddsDataInformationAboutMountPage()
160 {
161 $this->subject->__construct(1, '1-99');
162 $result = $this->subject->_call('processMountedPage', ['uid' => 1], ['uid' => 99, 'doktype' => PageRepository::DOKTYPE_MOUNTPOINT, 'mount_pid' => 1, 'mount_pid_ol' => 1, 'pid' => 5, 'title' => 'TestCase']);
163 $this->assertTrue(isset($result['_MOUNT_PAGE']));
164 $this->assertSame(['uid' => 99, 'pid' => 5, 'title' => 'TestCase'], $result['_MOUNT_PAGE']);
165 }
166
167 /**
168 * @test
169 */
170 public function processMountedPageForMountPageWithoutOverlayReplacesMountedPageWithMountPage()
171 {
172 $mountPointPageData = ['uid' => 99, 'doktype' => PageRepository::DOKTYPE_MOUNTPOINT, 'mount_pid' => 1, 'mount_pid_ol' => 0];
173 $this->subject->__construct(1, '1-99');
174 $result = $this->subject->_call('processMountedPage', ['uid' => 1], $mountPointPageData);
175 $this->assertIsSubset($mountPointPageData, $result);
176 }
177
178 /**
179 * @test
180 */
181 public function columnHasRelationToResolveDetectsGroupFieldAsLocal()
182 {
183 $this->assertFalse($this->subject->_call('columnHasRelationToResolve', [
184 'type' => 'group'
185 ]));
186 }
187
188 /**
189 * @test
190 */
191 public function columnHasRelationToResolveDetectsGroupFieldWithMMAsRemote2()
192 {
193 $this->assertTrue($this->subject->_call('columnHasRelationToResolve', [
194 'config' => [
195 'type' => 'group',
196 'MM' => 'tx_xyz'
197 ]
198 ]));
199 }
200
201 /**
202 * @test
203 */
204 public function columnHasRelationToResolveDetectsInlineFieldAsLocal()
205 {
206 $this->assertFalse($this->subject->_call('columnHasRelationToResolve', [
207 'config' => [
208 'type' => 'inline'
209 ]
210 ]));
211 }
212
213 /**
214 * @test
215 */
216 public function columnHasRelationToResolveDetectsInlineFieldWithForeignKeyAsRemote()
217 {
218 $this->assertTrue($this->subject->_call('columnHasRelationToResolve', [
219 'config' => [
220 'type' => 'inline',
221 'foreign_field' => 'xyz'
222 ]
223 ]));
224 }
225
226 /**
227 * @test
228 */
229 public function columnHasRelationToResolveDetectsInlineFieldWithFMMAsRemote()
230 {
231 $this->assertTrue($this->subject->_call('columnHasRelationToResolve', [
232 'config' => [
233 'type' => 'inline',
234 'MM' => 'xyz'
235 ]
236 ]));
237 }
238
239 /**
240 * @test
241 */
242 public function columnHasRelationToResolveDetectsSelectFieldAsLocal()
243 {
244 $this->assertFalse($this->subject->_call('columnHasRelationToResolve', [
245 'config' => [
246 'type' => 'select'
247 ]
248 ]));
249 }
250
251 /**
252 * @test
253 */
254 public function columnHasRelationToResolveDetectsSelectFieldWithMMAsRemote()
255 {
256 $this->assertTrue($this->subject->_call('columnHasRelationToResolve', [
257 'config' => [
258 'type' => 'select',
259 'MM' => 'xyz'
260 ]
261 ]));
262 }
263
264 /**
265 * @test
266 */
267 public function getCacheIdentifierContainsAllContextParameters()
268 {
269 $this->pageContextMock->sys_language_uid = 8;
270 $this->pageContextMock->versioningWorkspaceId = 15;
271 $this->pageContextMock->versioningPreview = true;
272 $this->subject->__construct(42, '47-11', $this->pageContextMock);
273 $this->assertSame('42_47-11_8_15_1', $this->subject->getCacheIdentifier());
274 $this->pageContextMock->versioningPreview = false;
275 $this->subject->__construct(42, '47-11', $this->pageContextMock);
276 $this->assertSame('42_47-11_8_15_0', $this->subject->getCacheIdentifier());
277 $this->pageContextMock->versioningWorkspaceId = 0;
278 $this->subject->__construct(42, '47-11', $this->pageContextMock);
279 $this->assertSame('42_47-11_8_0_0', $this->subject->getCacheIdentifier());
280 }
281
282 /**
283 * @test
284 */
285 public function getCacheIdentifierReturnsValidIdentifierWithCommasInMountPointParameter()
286 {
287 /** @var \TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend $cacheFrontendMock */
288 $cacheFrontendMock = $this->getMockForAbstractClass(\TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend::class, [], '', false);
289 $this->pageContextMock->sys_language_uid = 8;
290 $this->pageContextMock->versioningWorkspaceId = 15;
291 $this->pageContextMock->versioningPreview = true;
292 $this->subject->__construct(42, '47-11,48-12', $this->pageContextMock);
293 $this->assertTrue($cacheFrontendMock->isValidEntryIdentifier($this->subject->getCacheIdentifier()));
294 }
295 }