[FEATURE] Option to globally enable redirect hit count
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Mvc / Configuration / ConfigurationManagerTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Mvc\Configuration;
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\Configuration\Loader\FalYamlFileLoader;
19 use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader\Configuration;
20 use TYPO3\CMS\Core\Resource\File;
21 use TYPO3\CMS\Core\Resource\ResourceFactory;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Extbase\Object\ObjectManager;
24 use TYPO3\CMS\Form\Mvc\Configuration\ConfigurationManager;
25 use TYPO3\CMS\Form\Mvc\Configuration\Exception\ExtensionNameRequiredException;
26 use TYPO3\CMS\Form\Mvc\Configuration\Exception\NoConfigurationFoundException;
27 use TYPO3\CMS\Form\Mvc\Configuration\InheritancesResolverService;
28
29 /**
30 * Test case
31 */
32 class ConfigurationManagerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
33 {
34
35 /**
36 * @var array A backup of registered singleton instances
37 */
38 protected $singletonInstances = [];
39
40 /**
41 * Set up
42 */
43 public function setUp()
44 {
45 $this->singletonInstances = GeneralUtility::getSingletonInstances();
46 }
47
48 /**
49 * Tear down
50 */
51 public function tearDown()
52 {
53 GeneralUtility::resetSingletonInstances($this->singletonInstances);
54 parent::tearDown();
55 }
56
57 /**
58 * @test
59 */
60 public function getConfigurationFromYamlFileThrowsExceptionIfExtensionNameIsNotGiven()
61 {
62 $this->expectException(ExtensionNameRequiredException::class);
63 $this->expectExceptionCode(1471473377);
64
65 $mockConfigurationManager = $this->getAccessibleMock(ConfigurationManager::class, [
66 'dummy',
67 ], [], '', false);
68
69 $mockConfigurationManager->_call('getConfigurationFromYamlFile', '');
70 }
71
72 /**
73 * @test
74 */
75 public function getConfigurationFromYamlFileThrowsExceptionIfNoConfigurationIsFound()
76 {
77 $this->expectException(NoConfigurationFoundException::class);
78 $this->expectExceptionCode(1471473378);
79
80 $mockConfigurationManager = $this->getAccessibleMock(ConfigurationManager::class, [
81 'getYamlSettingsFromCache',
82 'getTypoScriptSettings',
83 ], [], '', false);
84
85 $mockConfigurationManager
86 ->expects($this->any())
87 ->method('getYamlSettingsFromCache')
88 ->willReturn([]);
89
90 $mockConfigurationManager
91 ->expects($this->any())
92 ->method('getTypoScriptSettings')
93 ->willReturn([]);
94
95 $objectManagerProphecy = $this->prophesize(ObjectManager::class);
96 $objectManagerProphecy
97 ->get(FalYamlFileLoader::class)
98 ->willReturn(new FalYamlFileLoader);
99
100 $configuration = new Configuration();
101 $objectManagerProphecy
102 ->get(Configuration::class)
103 ->willReturn($configuration);
104
105 $mockConfigurationManager->_set('objectManager', $objectManagerProphecy->reveal());
106
107 $input = 'form';
108 $expected = [];
109
110 $this->assertSame($expected, $mockConfigurationManager->_call('getConfigurationFromYamlFile', 'form'));
111 }
112
113 /**
114 * @test
115 */
116 public function getConfigurationFromYamlFile()
117 {
118 $mockConfigurationManager = $this->getAccessibleMock(ConfigurationManager::class, [
119 'getYamlSettingsFromCache',
120 'setYamlSettingsIntoCache',
121 'getTypoScriptSettings',
122 'overrideConfigurationByTypoScript',
123 ], [], '', false);
124
125 $objectManagerProphecy = $this->prophesize(ObjectManager::class);
126 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectManagerProphecy->reveal());
127
128 /** @var File|\Prophecy\Prophecy\ObjectProphecy */
129 $file1 = $this->prophesize(File::class);
130 $file1->getContents()->willReturn(file_get_contents(__DIR__ . '/Fixtures/File1.yaml'));
131 /** @var File|\Prophecy\Prophecy\ObjectProphecy */
132 $file2 = $this->prophesize(File::class);
133 $file2->getContents()->willReturn(file_get_contents(__DIR__ . '/Fixtures/File2.yaml'));
134 /** @var File|\Prophecy\Prophecy\ObjectProphecy */
135 $file3 = $this->prophesize(File::class);
136 $file3->getContents()->willReturn(file_get_contents(__DIR__ . '/Fixtures/File3.yaml'));
137 /** @var File|\Prophecy\Prophecy\ObjectProphecy */
138 $file4 = $this->prophesize(File::class);
139 $file4->getContents()->willReturn(file_get_contents(__DIR__ . '/Fixtures/File4.yaml'));
140
141 /** @var ResourceFactory|\Prophecy\Prophecy\ObjectProphecy */
142 $resourceFactory = $this->prophesize(ResourceFactory::class);
143 $resourceFactory->retrieveFileOrFolderObject('EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/File1.yaml')->willReturn($file1->reveal());
144 $resourceFactory->retrieveFileOrFolderObject('EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/File2.yaml')->willReturn($file2->reveal());
145 $resourceFactory->retrieveFileOrFolderObject('EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/File3.yaml')->willReturn($file3->reveal());
146 $resourceFactory->retrieveFileOrFolderObject('EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/File4.yaml')->willReturn($file4->reveal());
147
148 $configuration = new Configuration();
149 $objectManagerProphecy
150 ->get(Configuration::class)
151 ->willReturn($configuration);
152
153 $objectManagerProphecy
154 ->get(FalYamlFileLoader::class, Argument::type(Configuration::class))
155 ->willReturn(new FalYamlFileLoader($configuration, $resourceFactory->reveal()));
156
157 $objectManagerProphecy
158 ->get(InheritancesResolverService::class)
159 ->willReturn(new InheritancesResolverService);
160
161 $mockConfigurationManager->_set('objectManager', $objectManagerProphecy->reveal());
162
163 $mockConfigurationManager
164 ->expects($this->any())
165 ->method('getYamlSettingsFromCache')
166 ->willReturn([]);
167
168 $mockConfigurationManager
169 ->expects($this->any())
170 ->method('getTypoScriptSettings')
171 ->willReturn([
172 'configurationFile' => 'EXT:form/Tests/Unit/Mvc/Configuration/Fixtures/File1.yaml',
173 ]);
174
175 $mockConfigurationManager
176 ->expects($this->any())
177 ->method('setYamlSettingsIntoCache')
178 ->willReturn(null);
179
180 $mockConfigurationManager
181 ->expects($this->any())
182 ->method('overrideConfigurationByTypoScript')
183 ->willReturnArgument(0);
184
185 $input = 'form';
186 $expected = [
187 'config' => [
188 'value9' => 'File 3',
189 'value10' => 'File 4',
190 'value8' => 'File 3',
191 'value1' => 'File 1',
192 'value4' => 'File 1',
193 'value5' => 'File 1',
194 'value7' => 'File 2',
195 'value11' => [
196 'key1' => 'File 1',
197 'key2' => 'File 1',
198 ],
199 'value12' => [
200 'key1' => 'File 2',
201 ],
202 'value3' => 'File 1',
203 ],
204 'mixins' => [
205 'value11Mixin' => [
206 'key1' => 'File 1',
207 'key2' => 'File 1',
208 ],
209 'value12Mixin1' => [
210 'key1' => 'File 2',
211 ],
212 'value12Mixin2' => [
213 'key2' => 'File 2',
214 ],
215 ],
216 ];
217
218 $this->assertSame($expected, $mockConfigurationManager->_call('getConfigurationFromYamlFile', 'form'));
219 }
220 }