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