b7563616564a65335c13dc8f36d23b738c996ace
[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 * Testcase for \TYPO3\CMS\Core\TypoScript\TemplateService
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->templateService->tt_track = false;
63 $this->templateServiceMock = $this->getAccessibleMock(
64 TemplateService::class,
65 ['dummy'],
66 [new Context()]
67 );
68 $this->templateServiceMock->tt_track = false;
69 $this->backupPackageManager = ExtensionManagementUtilityAccessibleProxy::getPackageManager();
70 }
71
72 /**
73 * Tear down
74 */
75 public function tearDown(): void
76 {
77 ExtensionManagementUtilityAccessibleProxy::setPackageManager($this->backupPackageManager);
78 parent::tearDown();
79 }
80
81 /**
82 * @test
83 */
84 public function versionOlCallsVersionOlOfPageSelectClassWithGivenRow(): void
85 {
86 $row = ['foo'];
87 $GLOBALS['TSFE'] = new \stdClass();
88 $sysPageMock = $this->createMock(PageRepository::class);
89 $sysPageMock->expects($this->once())->method('versionOL')->with('sys_template', $row);
90 $GLOBALS['TSFE']->sys_page = $sysPageMock;
91 $this->templateService->versionOL($row);
92 }
93
94 /**
95 * @test
96 */
97 public function extensionStaticFilesAreNotProcessedIfNotExplicitlyRequested(): void
98 {
99 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
100 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
101 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
102 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
103
104 $identifier = $this->getUniqueId('test');
105 $GLOBALS['TYPO3_LOADED_EXT'] = [
106 $identifier => [
107 'ext_typoscript_setup.txt' => ExtensionManagementUtility::extPath(
108 'core',
109 'Tests/Unit/TypoScript/Fixtures/ext_typoscript_setup.txt'
110 ),
111 ],
112 ];
113
114 $this->templateService->runThroughTemplates([], 0);
115 $this->assertFalse(
116 in_array('test.Core.TypoScript = 1', $this->templateService->config, true)
117 );
118 }
119
120 /**
121 * @test
122 */
123 public function extensionStaticsAreProcessedIfExplicitlyRequested(): void
124 {
125 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
126 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
127 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
128 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
129
130 $identifier = $this->getUniqueId('test');
131 $GLOBALS['TYPO3_LOADED_EXT'] = [
132 $identifier => [
133 'ext_typoscript_setup.txt' => ExtensionManagementUtility::extPath(
134 'core',
135 'Tests/Unit/TypoScript/Fixtures/ext_typoscript_setup.txt'
136 ),
137 'ext_typoscript_constants.txt' => ''
138 ],
139 ];
140
141 $mockPackage = $this->getMockBuilder(Package::class)
142 ->setMethods(['getPackagePath'])
143 ->disableOriginalConstructor()
144 ->getMock();
145 $mockPackage->expects($this->any())->method('getPackagePath')->will($this->returnValue(''));
146
147 $mockPackageManager = $this->getMockBuilder(PackageManager::class)
148 ->setMethods(['isPackageActive', 'getPackage'])
149 ->disableOriginalConstructor()
150 ->getMock();
151 $mockPackageManager->expects($this->any())->method('isPackageActive')->will($this->returnValue(true));
152 $mockPackageManager->expects($this->any())->method('getPackage')->will($this->returnValue($mockPackage));
153 ExtensionManagementUtility::setPackageManager($mockPackageManager);
154
155 $this->templateService->setProcessExtensionStatics(true);
156 $this->templateService->runThroughTemplates([], 0);
157
158 $this->assertTrue(
159 in_array('test.Core.TypoScript = 1', $this->templateService->config, true)
160 );
161 }
162
163 /**
164 * @test
165 */
166 public function updateRootlineDataOverwritesOwnArrayData(): void
167 {
168 $originalRootline = [
169 0 => ['uid' => 2, 'title' => 'originalTitle'],
170 1 => ['uid' => 3, 'title' => 'originalTitle2'],
171 ];
172
173 $updatedRootline = [
174 0 => ['uid' => 1, 'title' => 'newTitle'],
175 1 => ['uid' => 2, 'title' => 'newTitle2'],
176 2 => ['uid' => 3, 'title' => 'newTitle3'],
177 ];
178
179 $expectedRootline = [
180 0 => ['uid' => 2, 'title' => 'newTitle2'],
181 1 => ['uid' => 3, 'title' => 'newTitle3'],
182 ];
183
184 $this->templateServiceMock->_set('rootLine', $originalRootline);
185 $this->templateServiceMock->updateRootlineData($updatedRootline);
186 $this->assertEquals($expectedRootline, $this->templateServiceMock->_get('rootLine'));
187 }
188
189 /**
190 * @test
191 */
192 public function updateRootlineDataWithInvalidNewRootlineThrowsException(): void
193 {
194 $originalRootline = [
195 0 => ['uid' => 2, 'title' => 'originalTitle'],
196 1 => ['uid' => 3, 'title' => 'originalTitle2'],
197 ];
198
199 $newInvalidRootline = [
200 0 => ['uid' => 1, 'title' => 'newTitle'],
201 1 => ['uid' => 2, 'title' => 'newTitle2'],
202 ];
203
204 $this->expectException(\RuntimeException::class);
205 $this->expectExceptionCode(1370419654);
206
207 $this->templateServiceMock->_set('rootLine', $originalRootline);
208 $this->templateServiceMock->updateRootlineData($newInvalidRootline);
209 }
210
211 /**
212 * @test
213 */
214 public function getFileNameReturnsUrlCorrectly(): void
215 {
216 $this->assertSame('http://example.com', $this->templateService->getFileName('http://example.com'));
217 $this->assertSame('https://example.com', $this->templateService->getFileName('https://example.com'));
218 }
219
220 /**
221 * @test
222 */
223 public function getFileNameReturnsFileCorrectly(): void
224 {
225 $this->assertSame('typo3/index.php', $this->templateService->getFileName('typo3/index.php'));
226 }
227
228 /**
229 * @test
230 */
231 public function getFileNameReturnsNullIfDirectory(): void
232 {
233 $this->assertNull($this->templateService->getFileName('typo3/'));
234 }
235
236 /**
237 * @test
238 */
239 public function getFileNameReturnsNullWithInvalidFileName(): void
240 {
241 $this->assertNull($this->templateService->getFileName(' '));
242 $this->assertNull($this->templateService->getFileName('something/../else'));
243 }
244 }