0fd540d782e9ea800f538e5ccf1172b359fb04ad
[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 TYPO3\CMS\Core\Context\Context;
21 use TYPO3\CMS\Core\Database\ConnectionPool;
22 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
23 use TYPO3\CMS\Core\Package\Package;
24 use TYPO3\CMS\Core\Package\PackageManager;
25 use TYPO3\CMS\Core\Tests\Unit\Utility\AccessibleProxies\ExtensionManagementUtilityAccessibleProxy;
26 use TYPO3\CMS\Core\TypoScript\TemplateService;
27 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\CMS\Frontend\Page\PageRepository;
30 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32
33 /**
34 * Test case
35 */
36 class TemplateServiceTest extends UnitTestCase
37 {
38 /**
39 * @var TemplateService
40 */
41 protected $templateService;
42
43 /**
44 * @var \PHPUnit_Framework_MockObject_MockObject|AccessibleObjectInterface|TemplateService
45 */
46 protected $templateServiceMock;
47
48 /**
49 * @var PackageManager
50 */
51 protected $backupPackageManager;
52
53 /**
54 * Set up
55 */
56 protected function setUp(): void
57 {
58 $GLOBALS['TYPO3_LOADED_EXT'] = [];
59 $GLOBALS['SIM_ACCESS_TIME'] = time();
60 $GLOBALS['ACCESS_TIME'] = time();
61 $this->templateService = new TemplateService(new Context());
62 $this->templateServiceMock = $this->getAccessibleMock(
63 TemplateService::class,
64 ['dummy'],
65 [new Context()]
66 );
67 $this->backupPackageManager = ExtensionManagementUtilityAccessibleProxy::getPackageManager();
68 }
69
70 /**
71 * Tear down
72 */
73 public function tearDown(): void
74 {
75 ExtensionManagementUtilityAccessibleProxy::setPackageManager($this->backupPackageManager);
76 parent::tearDown();
77 }
78
79 /**
80 * @test
81 */
82 public function versionOlCallsVersionOlOfPageSelectClassWithGivenRow(): void
83 {
84 $row = ['foo'];
85 $GLOBALS['TSFE'] = new \stdClass();
86 $sysPageMock = $this->createMock(PageRepository::class);
87 $sysPageMock->expects($this->once())->method('versionOL')->with('sys_template', $row);
88 $GLOBALS['TSFE']->sys_page = $sysPageMock;
89 $this->templateService->versionOL($row);
90 }
91
92 /**
93 * @test
94 */
95 public function extensionStaticFilesAreNotProcessedIfNotExplicitlyRequested(): void
96 {
97 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
98 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
99 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
100 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
101
102 $identifier = $this->getUniqueId('test');
103 $GLOBALS['TYPO3_LOADED_EXT'] = [
104 $identifier => [
105 'ext_typoscript_setup.txt' => ExtensionManagementUtility::extPath(
106 'core',
107 'Tests/Unit/TypoScript/Fixtures/ext_typoscript_setup.txt'
108 ),
109 ],
110 ];
111
112 $this->templateService->runThroughTemplates([], 0);
113 $this->assertFalse(
114 in_array('test.Core.TypoScript = 1', $this->templateService->config, true)
115 );
116 }
117
118 /**
119 * @test
120 */
121 public function extensionStaticsAreProcessedIfExplicitlyRequested(): void
122 {
123 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
124 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
125 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
126 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
127
128 $identifier = $this->getUniqueId('test');
129 $GLOBALS['TYPO3_LOADED_EXT'] = [
130 $identifier => [
131 'ext_typoscript_setup.txt' => ExtensionManagementUtility::extPath(
132 'core',
133 'Tests/Unit/TypoScript/Fixtures/ext_typoscript_setup.txt'
134 ),
135 'ext_typoscript_constants.txt' => ''
136 ],
137 ];
138
139 $mockPackage = $this->getMockBuilder(Package::class)
140 ->setMethods(['getPackagePath'])
141 ->disableOriginalConstructor()
142 ->getMock();
143 $mockPackage->expects($this->any())->method('getPackagePath')->will($this->returnValue(''));
144
145 $mockPackageManager = $this->getMockBuilder(PackageManager::class)
146 ->setMethods(['isPackageActive', 'getPackage'])
147 ->disableOriginalConstructor()
148 ->getMock();
149 $mockPackageManager->expects($this->any())->method('isPackageActive')->will($this->returnValue(true));
150 $mockPackageManager->expects($this->any())->method('getPackage')->will($this->returnValue($mockPackage));
151 ExtensionManagementUtility::setPackageManager($mockPackageManager);
152
153 $this->templateService->setProcessExtensionStatics(true);
154 $this->templateService->runThroughTemplates([], 0);
155
156 $this->assertTrue(
157 in_array('test.Core.TypoScript = 1', $this->templateService->config, true)
158 );
159 }
160
161 /**
162 * @test
163 */
164 public function updateRootlineDataOverwritesOwnArrayData(): void
165 {
166 $originalRootline = [
167 0 => ['uid' => 2, 'title' => 'originalTitle'],
168 1 => ['uid' => 3, 'title' => 'originalTitle2'],
169 ];
170
171 $updatedRootline = [
172 0 => ['uid' => 1, 'title' => 'newTitle'],
173 1 => ['uid' => 2, 'title' => 'newTitle2'],
174 2 => ['uid' => 3, 'title' => 'newTitle3'],
175 ];
176
177 $expectedRootline = [
178 0 => ['uid' => 2, 'title' => 'newTitle2'],
179 1 => ['uid' => 3, 'title' => 'newTitle3'],
180 ];
181
182 $this->templateServiceMock->_set('rootLine', $originalRootline);
183 $this->templateServiceMock->updateRootlineData($updatedRootline);
184 $this->assertEquals($expectedRootline, $this->templateServiceMock->_get('rootLine'));
185 }
186
187 /**
188 * @test
189 */
190 public function updateRootlineDataWithInvalidNewRootlineThrowsException(): void
191 {
192 $originalRootline = [
193 0 => ['uid' => 2, 'title' => 'originalTitle'],
194 1 => ['uid' => 3, 'title' => 'originalTitle2'],
195 ];
196
197 $newInvalidRootline = [
198 0 => ['uid' => 1, 'title' => 'newTitle'],
199 1 => ['uid' => 2, 'title' => 'newTitle2'],
200 ];
201
202 $this->expectException(\RuntimeException::class);
203 $this->expectExceptionCode(1370419654);
204
205 $this->templateServiceMock->_set('rootLine', $originalRootline);
206 $this->templateServiceMock->updateRootlineData($newInvalidRootline);
207 }
208 }