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