[TASK] Deprecate $GLOBALS['TYPO3_LOADED_EXT']
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / TypoScript / TemplateServiceTest.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Tests\Unit\TypoScript;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use Prophecy\Argument;
20 use Prophecy\Prophecy\ObjectProphecy;
21 use TYPO3\CMS\Core\Context\Context;
22 use TYPO3\CMS\Core\Database\ConnectionPool;
23 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
24 use TYPO3\CMS\Core\Package\Package;
25 use TYPO3\CMS\Core\Package\PackageManager;
26 use TYPO3\CMS\Core\Tests\Unit\Utility\AccessibleProxies\ExtensionManagementUtilityAccessibleProxy;
27 use TYPO3\CMS\Core\TypoScript\TemplateService;
28 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
29 use TYPO3\CMS\Core\Utility\GeneralUtility;
30 use TYPO3\CMS\Frontend\Page\PageRepository;
31 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
32 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
33
34 /**
35 * Test case
36 */
37 class TemplateServiceTest extends UnitTestCase
38 {
39 /**
40 * @var TemplateService
41 */
42 protected $templateService;
43
44 /**
45 * @var \PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface|TemplateService
46 */
47 protected $templateServiceMock;
48
49 /**
50 * @var PackageManager
51 */
52 protected $backupPackageManager;
53
54 /**
55 * @var PackageManager|ObjectProphecy
56 */
57 protected $packageManagerProphecy;
58
59 /**
60 * Set up
61 */
62 protected function setUp(): void
63 {
64 $GLOBALS['SIM_ACCESS_TIME'] = time();
65 $GLOBALS['ACCESS_TIME'] = time();
66 $this->packageManagerProphecy = $this->prophesize(PackageManager::class);
67 $this->templateService = new TemplateService(new Context(), $this->packageManagerProphecy->reveal());
68 $this->templateServiceMock = $this->getAccessibleMock(
69 TemplateService::class,
70 ['dummy'],
71 [new Context(), $this->packageManagerProphecy->reveal()]
72 );
73 $this->backupPackageManager = ExtensionManagementUtilityAccessibleProxy::getPackageManager();
74 }
75
76 /**
77 * Tear down
78 */
79 public function tearDown(): void
80 {
81 ExtensionManagementUtilityAccessibleProxy::setPackageManager($this->backupPackageManager);
82 parent::tearDown();
83 }
84
85 /**
86 * @test
87 */
88 public function versionOlCallsVersionOlOfPageSelectClassWithGivenRow(): void
89 {
90 $row = ['foo'];
91 $GLOBALS['TSFE'] = new \stdClass();
92 $sysPageMock = $this->createMock(PageRepository::class);
93 $sysPageMock->expects($this->once())->method('versionOL')->with('sys_template', $row);
94 $GLOBALS['TSFE']->sys_page = $sysPageMock;
95 $this->templateService->versionOL($row);
96 }
97
98 /**
99 * @test
100 */
101 public function extensionStaticFilesAreNotProcessedIfNotExplicitlyRequested(): void
102 {
103 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
104 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
105 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
106 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
107
108 $identifier = $this->getUniqueId('test');
109 $this->packageManagerProphecy->getActivePackages()->shouldNotBeCalled();
110
111 $this->templateService->runThroughTemplates([], 0);
112 $this->assertFalse(
113 in_array('test.Core.TypoScript = 1', $this->templateService->config, true)
114 );
115 }
116
117 /**
118 * @test
119 */
120 public function extensionStaticsAreProcessedIfExplicitlyRequested(): void
121 {
122 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
123 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
124 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
125 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
126
127 $identifier = $this->getUniqueId('test');
128
129 $mockPackage = $this->getMockBuilder(Package::class)
130 ->setMethods(['getPackagePath', 'getPackageKey'])
131 ->disableOriginalConstructor()
132 ->getMock();
133 $mockPackage->expects($this->any())->method('getPackagePath')->will($this->returnValue(__DIR__ . '/Fixtures/'));
134 $mockPackage->expects($this->any())->method('getPackageKey')->will($this->returnValue('core'));
135
136 $mockPackageManager = $this->getMockBuilder(PackageManager::class)
137 ->setMethods(['isPackageActive', 'getPackage'])
138 ->disableOriginalConstructor()
139 ->getMock();
140 $mockPackageManager->expects($this->any())->method('isPackageActive')->will($this->returnValue(true));
141 $mockPackageManager->expects($this->any())->method('getPackage')->will($this->returnValue($mockPackage));
142 ExtensionManagementUtility::setPackageManager($mockPackageManager);
143 $this->packageManagerProphecy->getActivePackages()->willReturn(['core' => $mockPackage]);
144
145 $this->templateService->setProcessExtensionStatics(true);
146 $this->templateService->runThroughTemplates([], 0);
147
148 $this->assertTrue(
149 in_array('test.Core.TypoScript = 1', $this->templateService->config, true)
150 );
151 }
152
153 /**
154 * @test
155 */
156 public function updateRootlineDataOverwritesOwnArrayData(): void
157 {
158 $originalRootline = [
159 0 => ['uid' => 2, 'title' => 'originalTitle'],
160 1 => ['uid' => 3, 'title' => 'originalTitle2'],
161 ];
162
163 $updatedRootline = [
164 0 => ['uid' => 1, 'title' => 'newTitle'],
165 1 => ['uid' => 2, 'title' => 'newTitle2'],
166 2 => ['uid' => 3, 'title' => 'newTitle3'],
167 ];
168
169 $expectedRootline = [
170 0 => ['uid' => 2, 'title' => 'newTitle2'],
171 1 => ['uid' => 3, 'title' => 'newTitle3'],
172 ];
173
174 $this->templateServiceMock->_set('rootLine', $originalRootline);
175 $this->templateServiceMock->updateRootlineData($updatedRootline);
176 $this->assertEquals($expectedRootline, $this->templateServiceMock->_get('rootLine'));
177 }
178
179 /**
180 * @test
181 */
182 public function updateRootlineDataWithInvalidNewRootlineThrowsException(): void
183 {
184 $originalRootline = [
185 0 => ['uid' => 2, 'title' => 'originalTitle'],
186 1 => ['uid' => 3, 'title' => 'originalTitle2'],
187 ];
188
189 $newInvalidRootline = [
190 0 => ['uid' => 1, 'title' => 'newTitle'],
191 1 => ['uid' => 2, 'title' => 'newTitle2'],
192 ];
193
194 $this->expectException(\RuntimeException::class);
195 $this->expectExceptionCode(1370419654);
196
197 $this->templateServiceMock->_set('rootLine', $originalRootline);
198 $this->templateServiceMock->updateRootlineData($newInvalidRootline);
199 }
200 }