37430ce029f69b577d204ea8c9b6212f0925ba00
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Service / FlexFormServiceTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Service;
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\GeneralUtility;
18
19 /**
20 * Test case
21 */
22 class FlexFormServiceTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
24 /**
25 * @var array Backup of singletons
26 */
27 protected $backupSingletons = [];
28
29 /**
30 * Set up
31 */
32 protected function setUp()
33 {
34 $this->backupSingletons = GeneralUtility::getSingletonInstances();
35 }
36
37 /**
38 * Tear down
39 */
40 protected function tearDown()
41 {
42 GeneralUtility::resetSingletonInstances($this->backupSingletons);
43 parent::tearDown();
44 }
45
46 /**
47 * @test
48 */
49 public function convertFlexFormContentToArrayResolvesComplexArrayStructure()
50 {
51 $input = '<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
52 <T3FlexForms>
53 <data>
54 <sheet index="sDEF">
55 <language index="lDEF">
56 <field index="settings.foo">
57 <value index="vDEF">Foo-Value</value>
58 </field>
59 <field index="settings.bar">
60 <el index="el">
61 <section index="1">
62 <itemType index="_arrayContainer">
63 <el>
64 <field index="baz">
65 <value index="vDEF">Baz1-Value</value>
66 </field>
67 <field index="bum">
68 <value index="vDEF">Bum1-Value</value>
69 </field>
70 </el>
71 </itemType>
72 <itemType index="_TOGGLE">0</itemType>
73 </section>
74 <section index="2">
75 <itemType index="_arrayContainer">
76 <el>
77 <field index="baz">
78 <value index="vDEF">Baz2-Value</value>
79 </field>
80 <field index="bum">
81 <value index="vDEF">Bum2-Value</value>
82 </field>
83 </el>
84 </itemType>
85 <itemType index="_TOGGLE">0</itemType>
86 </section>
87 </el>
88 </field>
89 </language>
90 </sheet>
91 </data>
92 </T3FlexForms>';
93
94 $expected = [
95 'settings' => [
96 'foo' => 'Foo-Value',
97 'bar' => [
98 1 => [
99 'baz' => 'Baz1-Value',
100 'bum' => 'Bum1-Value'
101 ],
102 2 => [
103 'baz' => 'Baz2-Value',
104 'bum' => 'Bum2-Value'
105 ]
106 ]
107 ]
108 ];
109
110 // The subject calls xml2array statically, which calls getHash and setHash statically, which uses
111 // caches, those need to be mocked.
112 $cacheManagerMock = $this->createMock(\TYPO3\CMS\Core\Cache\CacheManager::class);
113 $cacheMock = $this->createMock(\TYPO3\CMS\Core\Cache\Frontend\FrontendInterface::class);
114 $cacheManagerMock->expects($this->any())->method('getCache')->will($this->returnValue($cacheMock));
115 GeneralUtility::setSingletonInstance(\TYPO3\CMS\Core\Cache\CacheManager::class, $cacheManagerMock);
116
117 $flexFormService = $this->getMockBuilder(\TYPO3\CMS\Extbase\Service\FlexFormService::class)
118 ->setMethods(['dummy'])
119 ->disableOriginalConstructor()
120 ->getMock();
121 $convertedFlexFormArray = $flexFormService->convertFlexFormContentToArray($input);
122 $this->assertSame($expected, $convertedFlexFormArray);
123 }
124 }