[BUGFIX] Show correct label for IRRE records
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / TcaGroupTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
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\Prophecy\ObjectProphecy;
18 use TYPO3\CMS\Backend\Form\FormDataProvider\TcaGroup;
19 use TYPO3\CMS\Core\Database\RelationHandler;
20 use TYPO3\CMS\Core\Resource\Folder;
21 use TYPO3\CMS\Core\Resource\ResourceFactory;
22 use TYPO3\CMS\Core\Tests\UnitTestCase;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25 /**
26 * Test case
27 */
28 class TcaGroupTest extends UnitTestCase
29 {
30 /**
31 * @var TcaGroup
32 */
33 protected $subject;
34
35 /**
36 * @var array
37 */
38 protected $singletonInstances;
39
40 protected function setUp()
41 {
42 $this->subject = new TcaGroup();
43 $this->singletonInstances = GeneralUtility::getSingletonInstances();
44 }
45
46 protected function tearDown()
47 {
48 GeneralUtility::resetSingletonInstances($this->singletonInstances);
49 parent::tearDown();
50 }
51
52 /**
53 * @test
54 */
55 public function addDataReturnsFieldUnchangedIfFieldIsNotTypeGroup()
56 {
57 $input = [
58 'databaseRow' => [
59 'aField' => 'aValue',
60 ],
61 'processedTca' => [
62 'columns' => [
63 'aField' => [
64 'config' => [
65 'type' => 'foo',
66 ],
67 ],
68 ],
69 ],
70 ];
71 $expected = $input;
72 $this->assertSame($expected, $this->subject->addData($input));
73 }
74
75 /**
76 * @test
77 */
78 public function addDataThrowsExceptionWithTypeGroupAndNoValidInternalType()
79 {
80 $input = [
81 'processedTca' => [
82 'columns' => [
83 'aField' => [
84 'config' => [
85 'type' => 'group',
86 'internal_type' => 'foo',
87 ],
88 ],
89 ],
90 ],
91 ];
92 $this->setExpectedException(\UnexpectedValueException::class, $this->anything(), 1438780511);
93 $this->subject->addData($input);
94 }
95
96 /**
97 * @test
98 */
99 public function addDataSetsFileData()
100 {
101 $input = [
102 'databaseRow' => [
103 'aField' => '/aDir/aFile.txt,/anotherDir/anotherFile.css',
104 ],
105 'processedTca' => [
106 'columns' => [
107 'aField' => [
108 'config' => [
109 'type' => 'group',
110 'internal_type' => 'file',
111 ],
112 ],
113 ],
114 ],
115 ];
116 $expected = $input;
117 $expected['databaseRow']['aField'] = '%2FaDir%2FaFile.txt|aFile.txt,%2FanotherDir%2FanotherFile.css|anotherFile.css';
118 $this->assertSame($expected, $this->subject->addData($input));
119 }
120
121 /**
122 * @test
123 */
124 public function addDataSetsFolderData()
125 {
126 $input = [
127 'databaseRow' => [
128 'aField' => '1:/aFolder/anotherFolder/',
129 ],
130 'processedTca' => [
131 'columns' => [
132 'aField' => [
133 'config' => [
134 'type' => 'group',
135 'internal_type' => 'folder',
136 ],
137 ],
138 ],
139 ],
140 ];
141
142 /** @var Folder|ObjectProphecy $relationHandlerProphecy */
143 $folderProphecy = $this->prophesize(Folder::class);
144 $folderProphecy->getIdentifier()->shouldBeCalled()->willReturn('anotherFolder');
145
146 /** @var ResourceFactory|ObjectProphecy $relationHandlerProphecy */
147 $resourceFactoryProphecy = $this->prophesize(ResourceFactory::class);
148 GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactoryProphecy->reveal());
149 $resourceFactoryProphecy->retrieveFileOrFolderObject('1:/aFolder/anotherFolder/')
150 ->shouldBeCalled()
151 ->willReturn($folderProphecy->reveal());
152
153 $expected = $input;
154 $expected['databaseRow']['aField'] = '1%3A%2FaFolder%2FanotherFolder%2F|anotherFolder';
155 $this->assertSame($expected, $this->subject->addData($input));
156 }
157
158 /**
159 * @test
160 */
161 public function addDataSetsDatabaseData()
162 {
163 $aFieldConfig = [
164 'type' => 'group',
165 'internal_type' => 'db',
166 'MM' => 'mmTableName',
167 'allowed' => 'aForeignTable',
168 ];
169 $input = [
170 'tableName' => 'aTable',
171 'databaseRow' => [
172 'uid' => 42,
173 'aField' => '1,2',
174 ],
175 'processedTca' => [
176 'columns' => [
177 'aField' => [
178 'config' => $aFieldConfig,
179 ],
180 ],
181 ],
182 ];
183
184 /** @var RelationHandler|ObjectProphecy $relationHandlerProphecy */
185 $relationHandlerProphecy = $this->prophesize(RelationHandler::class);
186 GeneralUtility::addInstance(RelationHandler::class, $relationHandlerProphecy->reveal());
187 $relationHandlerProphecy->start('1,2', 'aForeignTable', 'mmTableName', 42, 'aTable', $aFieldConfig)->shouldBeCalled();
188 $relationHandlerProphecy->getFromDB()->shouldBeCalled();
189 $relationHandlerProphecy->readyForInterface()->shouldBeCalled()->willReturn('1|aLabel,2|anotherLabel');
190
191 $expected = $input;
192 $expected['databaseRow']['aField'] = '1|aLabel,2|anotherLabel';
193
194 $this->assertSame($expected, $this->subject->addData($input));
195 }
196 }