d0178e6098cdf754d31ee3d8e588ea7a312ccae0
[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 TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Testcase for \TYPO3\CMS\Core\TypoScript\TemplateService
22 */
23 class TemplateServiceTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
24
25 /**
26 * @var \TYPO3\CMS\Core\TypoScript\TemplateService
27 */
28 protected $templateService;
29
30 /**
31 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\TYPO3\CMS\Core\TypoScript\TemplateService
32 */
33 protected $templateServiceMock;
34
35 /**
36 * Sets up this test case.
37 *
38 * @return void
39 */
40 protected function setUp() {
41 $GLOBALS['TYPO3_LOADED_EXT'] = array();
42 $this->templateService = new \TYPO3\CMS\Core\TypoScript\TemplateService();
43 $this->templateService->tt_track = FALSE;
44 $this->templateServiceMock = $this->getAccessibleMock(\TYPO3\CMS\Core\TypoScript\TemplateService::class, array('dummy'));
45 $this->templateServiceMock->tt_track = FALSE;
46 }
47
48 /**
49 * @test
50 */
51 public function versionOlCallsVersionOlOfPageSelectClassWithGivenRow() {
52 $row = array('foo');
53 $GLOBALS['TSFE'] = new \stdClass();
54 $sysPageMock = $this->getMock(\TYPO3\CMS\Frontend\Page\PageRepository::class);
55 $sysPageMock->expects($this->once())->method('versionOL')->with('sys_template', $row);
56 $GLOBALS['TSFE']->sys_page = $sysPageMock;
57 $this->templateService->versionOL($row);
58 }
59
60 /**
61 * @test
62 */
63 public function extensionStaticFilesAreNotProcessedIfNotExplicitlyRequested() {
64 $identifier = $this->getUniqueId('test');
65 $GLOBALS['TYPO3_LOADED_EXT'] = array(
66 $identifier => array(
67 'ext_typoscript_setup.txt' => ExtensionManagementUtility::extPath(
68 'core', 'Tests/Unit/TypoScript/Fixtures/ext_typoscript_setup.txt'
69 ),
70 ),
71 );
72
73 $this->templateService->runThroughTemplates(array(), 0);
74 $this->assertFalse(
75 in_array('test.Core.TypoScript = 1', $this->templateService->config)
76 );
77 }
78
79 /**
80 * @test
81 */
82 public function extensionStaticsAreProcessedIfExplicitlyRequested() {
83 $identifier = $this->getUniqueId('test');
84 $GLOBALS['TYPO3_LOADED_EXT'] = array(
85 $identifier => array(
86 'ext_typoscript_setup.txt' => ExtensionManagementUtility::extPath(
87 'core', 'Tests/Unit/TypoScript/Fixtures/ext_typoscript_setup.txt'
88 ),
89 'ext_typoscript_constants.txt' => ''
90 ),
91 );
92
93 $mockPackage = $this->getMock(\TYPO3\CMS\Core\Package\Package::class, array('getPackagePath'), array(), '', FALSE);
94 $mockPackage->expects($this->any())->method('getPackagePath')->will($this->returnValue(''));
95
96 $mockPackageManager = $this->getMock(\TYPO3\CMS\Core\Package\PackageManager::class, array('isPackageActive' , 'getPackage'));
97 $mockPackageManager->expects($this->any())->method('isPackageActive')->will($this->returnValue(TRUE));
98 $mockPackageManager->expects($this->any())->method('getPackage')->will($this->returnValue($mockPackage));
99 ExtensionManagementUtility::setPackageManager($mockPackageManager);
100
101 $this->templateService->setProcessExtensionStatics(TRUE);
102 $this->templateService->runThroughTemplates(array(), 0);
103
104 $this->assertTrue(
105 in_array('test.Core.TypoScript = 1', $this->templateService->config)
106 );
107
108 ExtensionManagementUtility::setPackageManager(GeneralUtility::makeInstance(\TYPO3\CMS\Core\Package\PackageManager::class));
109 }
110
111 /**
112 * @test
113 */
114 public function updateRootlineDataOverwritesOwnArrayData() {
115 $originalRootline = array(
116 0 => array('uid' => 2, 'title' => 'originalTitle'),
117 1 => array('uid' => 3, 'title' => 'originalTitle2'),
118 );
119
120 $updatedRootline = array(
121 0 => array('uid' => 1, 'title' => 'newTitle'),
122 1 => array('uid' => 2, 'title' => 'newTitle2'),
123 2 => array('uid' => 3, 'title' => 'newTitle3'),
124 );
125
126 $expectedRootline = array(
127 0 => array('uid' => 2, 'title' => 'newTitle2'),
128 1 => array('uid' => 3, 'title' => 'newTitle3'),
129 );
130
131 $this->templateServiceMock->_set('rootLine', $originalRootline);
132 $this->templateServiceMock->updateRootlineData($updatedRootline);
133 $this->assertEquals($expectedRootline, $this->templateServiceMock->_get('rootLine'));
134 }
135
136 /**
137 * @test
138 * @expectedException \RuntimeException
139 */
140 public function updateRootlineDataWithInvalidNewRootlineThrowsException() {
141 $originalRootline = array(
142 0 => array('uid' => 2, 'title' => 'originalTitle'),
143 1 => array('uid' => 3, 'title' => 'originalTitle2'),
144 );
145
146 $newInvalidRootline = array(
147 0 => array('uid' => 1, 'title' => 'newTitle'),
148 1 => array('uid' => 2, 'title' => 'newTitle2'),
149 );
150
151 $this->templateServiceMock->_set('rootLine', $originalRootline);
152 $this->templateServiceMock->updateRootlineData($newInvalidRootline);
153 }
154
155 /**
156 * @test
157 */
158 public function getFileNameReturnsUrlCorrectly() {
159 $this->assertSame('http://example.com', $this->templateService->getFileName('http://example.com'));
160 $this->assertSame('https://example.com', $this->templateService->getFileName('https://example.com'));
161 }
162
163 /**
164 * @test
165 */
166 public function getFileNameReturnsFileCorrectly() {
167 $this->assertSame('typo3/index.php', $this->templateService->getFileName('typo3/index.php'));
168 }
169
170 /**
171 * @test
172 */
173 public function getFileNameReturnsNullIfDirectory() {
174 $this->assertNull($this->templateService->getFileName(__DIR__));
175 }
176
177 /**
178 * @test
179 */
180 public function getFileNameReturnsNullWithInvalidFileName() {
181 $this->assertNull($this->templateService->getFileName(' '));
182 $this->assertNull($this->templateService->getFileName('something/../else'));
183 }
184
185 }