[TASK] Mark several methods within TemplateService as internal
[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 Prophecy\Prophecy\ObjectProphecy;
21 use TYPO3\CMS\Core\Context\Context;
22 use TYPO3\CMS\Core\Database\ConnectionPool;
23 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
24 use TYPO3\CMS\Core\Package\Package;
25 use TYPO3\CMS\Core\Package\PackageManager;
26 use TYPO3\CMS\Core\Tests\Unit\Utility\AccessibleProxies\ExtensionManagementUtilityAccessibleProxy;
27 use TYPO3\CMS\Core\TypoScript\TemplateService;
28 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
29 use TYPO3\CMS\Core\Utility\GeneralUtility;
30 use TYPO3\TestingFramework\Core\AccessibleObjectInterface;
31 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
32
33 /**
34 * Test case
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 * @var PackageManager|ObjectProphecy
55 */
56 protected $packageManagerProphecy;
57
58 /**
59 * Set up
60 */
61 protected function setUp(): void
62 {
63 $GLOBALS['SIM_ACCESS_TIME'] = time();
64 $GLOBALS['ACCESS_TIME'] = time();
65 $this->packageManagerProphecy = $this->prophesize(PackageManager::class);
66 $this->templateService = new TemplateService(new Context(), $this->packageManagerProphecy->reveal());
67 $this->templateServiceMock = $this->getAccessibleMock(
68 TemplateService::class,
69 ['dummy'],
70 [new Context(), $this->packageManagerProphecy->reveal()]
71 );
72 $this->backupPackageManager = ExtensionManagementUtilityAccessibleProxy::getPackageManager();
73 }
74
75 /**
76 * Tear down
77 */
78 public function tearDown(): void
79 {
80 ExtensionManagementUtilityAccessibleProxy::setPackageManager($this->backupPackageManager);
81 parent::tearDown();
82 }
83
84 /**
85 * @test
86 */
87 public function extensionStaticFilesAreNotProcessedIfNotExplicitlyRequested(): void
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 $this->packageManagerProphecy->getActivePackages()->shouldNotBeCalled();
95
96 $this->templateService->runThroughTemplates([], 0);
97 $this->assertFalse(
98 in_array('test.Core.TypoScript = 1', $this->templateService->config, true)
99 );
100 }
101
102 /**
103 * @test
104 */
105 public function extensionStaticsAreProcessedIfExplicitlyRequested(): void
106 {
107 $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
108 $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
109 $connectionPoolProphet->getQueryBuilderForTable(Argument::cetera())->willReturn($queryBuilderProphet->reveal());
110 GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
111
112 $mockPackage = $this->getMockBuilder(Package::class)
113 ->setMethods(['getPackagePath', 'getPackageKey'])
114 ->disableOriginalConstructor()
115 ->getMock();
116 $mockPackage->expects($this->any())->method('getPackagePath')->will($this->returnValue(__DIR__ . '/Fixtures/'));
117 $mockPackage->expects($this->any())->method('getPackageKey')->will($this->returnValue('core'));
118
119 $mockPackageManager = $this->getMockBuilder(PackageManager::class)
120 ->setMethods(['isPackageActive', 'getPackage'])
121 ->disableOriginalConstructor()
122 ->getMock();
123 $mockPackageManager->expects($this->any())->method('isPackageActive')->will($this->returnValue(true));
124 $mockPackageManager->expects($this->any())->method('getPackage')->will($this->returnValue($mockPackage));
125 ExtensionManagementUtility::setPackageManager($mockPackageManager);
126 $this->packageManagerProphecy->getActivePackages()->willReturn(['core' => $mockPackage]);
127
128 $this->templateService->setProcessExtensionStatics(true);
129 $this->templateService->runThroughTemplates([], 0);
130
131 $this->assertTrue(
132 in_array('test.Core.TypoScript = 1', $this->templateService->config, true)
133 );
134 }
135
136 /**
137 * @test
138 */
139 public function updateRootlineDataOverwritesOwnArrayData(): void
140 {
141 $originalRootline = [
142 0 => ['uid' => 2, 'title' => 'originalTitle'],
143 1 => ['uid' => 3, 'title' => 'originalTitle2'],
144 ];
145
146 $updatedRootline = [
147 0 => ['uid' => 1, 'title' => 'newTitle'],
148 1 => ['uid' => 2, 'title' => 'newTitle2'],
149 2 => ['uid' => 3, 'title' => 'newTitle3'],
150 ];
151
152 $expectedRootline = [
153 0 => ['uid' => 2, 'title' => 'newTitle2'],
154 1 => ['uid' => 3, 'title' => 'newTitle3'],
155 ];
156
157 $this->templateServiceMock->_set('rootLine', $originalRootline);
158 $this->templateServiceMock->updateRootlineData($updatedRootline);
159 $this->assertEquals($expectedRootline, $this->templateServiceMock->_get('rootLine'));
160 }
161
162 /**
163 * @test
164 */
165 public function updateRootlineDataWithInvalidNewRootlineThrowsException(): void
166 {
167 $originalRootline = [
168 0 => ['uid' => 2, 'title' => 'originalTitle'],
169 1 => ['uid' => 3, 'title' => 'originalTitle2'],
170 ];
171
172 $newInvalidRootline = [
173 0 => ['uid' => 1, 'title' => 'newTitle'],
174 1 => ['uid' => 2, 'title' => 'newTitle2'],
175 ];
176
177 $this->expectException(\RuntimeException::class);
178 $this->expectExceptionCode(1370419654);
179
180 $this->templateServiceMock->_set('rootLine', $originalRootline);
181 $this->templateServiceMock->updateRootlineData($newInvalidRootline);
182 }
183 }