c7dd0aae5c09c3b80415b7a8d0d7dfdbf008e0f2
[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 * Subject is not notice free, disable E_NOTICES
31 */
32 protected static $suppressNotices = true;
33
34 /**
35 * @var \TYPO3\CMS\Core\TypoScript\TemplateService
36 */
37 protected $templateService;
38
39 /**
40 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|\TYPO3\CMS\Core\TypoScript\TemplateService
41 */
42 protected $templateServiceMock;
43
44 /**
45 * @var \TYPO3\CMS\Core\Package\PackageManager
46 */
47 protected $backupPackageManager;
48
49 /**
50 * Set up
51 */
52 protected function setUp()
53 {
54 $GLOBALS['TYPO3_LOADED_EXT'] = [];
55 $this->templateService = new \TYPO3\CMS\Core\TypoScript\TemplateService();
56 $this->templateService->tt_track = false;
57 $this->templateServiceMock = $this->getAccessibleMock(\TYPO3\CMS\Core\TypoScript\TemplateService::class, ['dummy']);
58 $this->templateServiceMock->tt_track = false;
59 $this->backupPackageManager = ExtensionManagementUtilityAccessibleProxy::getPackageManager();
60 }
61
62 /**
63 * Tear down
64 */
65 public function tearDown()
66 {
67 ExtensionManagementUtilityAccessibleProxy::setPackageManager($this->backupPackageManager);
68 parent::tearDown();
69 }
70
71 /**
72 * @test
73 */
74 public function versionOlCallsVersionOlOfPageSelectClassWithGivenRow()
75 {
76 $row = ['foo'];
77 $GLOBALS['TSFE'] = new \stdClass();
78 $sysPageMock = $this->createMock(\TYPO3\CMS\Frontend\Page\PageRepository::class);
79 $sysPageMock->expects($this->once())->method('versionOL')->with('sys_template', $row);
80 $GLOBALS['TSFE']->sys_page = $sysPageMock;
81 $this->templateService->versionOL($row);
82 }
83
84 /**
85 * @test
86 */
87 public function extensionStaticFilesAreNotProcessedIfNotExplicitlyRequested()
88 {
89 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
90 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
91 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
92 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
93
94 $identifier = $this->getUniqueId('test');
95 $GLOBALS['TYPO3_LOADED_EXT'] = [
96 $identifier => [
97 'ext_typoscript_setup.txt' => ExtensionManagementUtility::extPath(
98 'core',
99 'Tests/Unit/TypoScript/Fixtures/ext_typoscript_setup.txt'
100 ),
101 ],
102 ];
103
104 $this->templateService->runThroughTemplates([], 0);
105 $this->assertFalse(
106 in_array('test.Core.TypoScript = 1', $this->templateService->config)
107 );
108 }
109
110 /**
111 * @test
112 */
113 public function extensionStaticsAreProcessedIfExplicitlyRequested()
114 {
115 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
116 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
117 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
118 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
119
120 $identifier = $this->getUniqueId('test');
121 $GLOBALS['TYPO3_LOADED_EXT'] = [
122 $identifier => [
123 'ext_typoscript_setup.txt' => ExtensionManagementUtility::extPath(
124 'core',
125 'Tests/Unit/TypoScript/Fixtures/ext_typoscript_setup.txt'
126 ),
127 'ext_typoscript_constants.txt' => ''
128 ],
129 ];
130
131 $mockPackage = $this->getMockBuilder(\TYPO3\CMS\Core\Package\Package::class)
132 ->setMethods(['getPackagePath'])
133 ->disableOriginalConstructor()
134 ->getMock();
135 $mockPackage->expects($this->any())->method('getPackagePath')->will($this->returnValue(''));
136
137 $mockPackageManager = $this->getMockBuilder(\TYPO3\CMS\Core\Package\PackageManager::class)
138 ->setMethods(['isPackageActive', 'getPackage'])
139 ->disableOriginalConstructor()
140 ->getMock();
141 $mockPackageManager->expects($this->any())->method('isPackageActive')->will($this->returnValue(true));
142 $mockPackageManager->expects($this->any())->method('getPackage')->will($this->returnValue($mockPackage));
143 ExtensionManagementUtility::setPackageManager($mockPackageManager);
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)
150 );
151 }
152
153 /**
154 * @test
155 */
156 public function updateRootlineDataOverwritesOwnArrayData()
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()
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
201 /**
202 * @test
203 */
204 public function getFileNameReturnsUrlCorrectly()
205 {
206 $this->assertSame('http://example.com', $this->templateService->getFileName('http://example.com'));
207 $this->assertSame('https://example.com', $this->templateService->getFileName('https://example.com'));
208 }
209
210 /**
211 * @test
212 */
213 public function getFileNameReturnsFileCorrectly()
214 {
215 $this->assertSame('typo3/index.php', $this->templateService->getFileName('typo3/index.php'));
216 }
217
218 /**
219 * @test
220 */
221 public function getFileNameReturnsNullIfDirectory()
222 {
223 $this->assertNull($this->templateService->getFileName(__DIR__));
224 }
225
226 /**
227 * @test
228 */
229 public function getFileNameReturnsNullWithInvalidFileName()
230 {
231 $this->assertNull($this->templateService->getFileName(' '));
232 $this->assertNull($this->templateService->getFileName('something/../else'));
233 }
234 }