[BUGFIX] FormEngine: Custom functions
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataCompilerTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form;
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 Prophecy\Prophecy\ObjectProphecy;
19 use TYPO3\CMS\Core\Tests\UnitTestCase;
20 use TYPO3\CMS\Backend\Form\FormDataCompiler;
21 use TYPO3\CMS\Backend\Form\FormDataGroupInterface;
22
23 /**
24 * Test case
25 */
26 class FormDataComplierTest extends UnitTestCase
27 {
28 /**
29 * @var FormDataCompiler
30 */
31 protected $subject;
32
33 /**
34 * @var FormDataGroupInterface | ObjectProphecy
35 */
36 protected $formDataGroupProphecy;
37
38 protected function setUp()
39 {
40 $this->formDataGroupProphecy = $this->prophesize(FormDataGroupInterface::class);
41 $this->subject = new FormDataCompiler($this->formDataGroupProphecy->reveal());
42 }
43
44 /**
45 * @test
46 */
47 public function compileThrowsExceptionIfInputContainsKeysNotValidInResult()
48 {
49 $input = [
50 'foo' => 'bar',
51 ];
52 $this->setExpectedException(\InvalidArgumentException::class, $this->anything(), 1440601540);
53 $this->subject->compile($input);
54 }
55
56 /**
57 * @test
58 */
59 public function compileThrowsExceptionAtUnknownCommand()
60 {
61 $input = [
62 'command' => 'unknownCommand',
63 ];
64 $this->setExpectedException(\InvalidArgumentException::class, $this->anything(), 1437653136);
65 $this->subject->compile($input);
66 }
67
68 /**
69 * @test
70 */
71 public function compileThrowsExceptionIfNoTableNameGiven()
72 {
73 $input = [
74 'tableName' => '',
75 ];
76 $this->setExpectedException(\InvalidArgumentException::class, $this->anything(), 1437654409);
77 $this->subject->compile($input);
78 }
79
80 /**
81 * @test
82 */
83 public function compileThrowsExceptionIfUidIsNotAnInteger()
84 {
85 $input = [
86 'vanillaUid' => 'foo123',
87 ];
88 $this->setExpectedException(\InvalidArgumentException::class, $this->anything(), 1437654247);
89 $this->subject->compile($input);
90 }
91
92 /**
93 * @test
94 */
95 public function compileThrowsExceptionIfCommandIsEditAndUidIsNegative()
96 {
97 $input = [
98 'command' => 'edit',
99 'vanillaUid' => -100,
100 ];
101 $this->setExpectedException(\InvalidArgumentException::class, $this->anything(), 1437654332);
102 $this->subject->compile($input);
103 }
104
105 /**
106 * @test
107 */
108 public function compileReturnsResultArrayWithInputDataSet()
109 {
110 $input = [
111 'tableName' => 'pages',
112 'vanillaUid' => 123,
113 'command' => 'edit',
114 ];
115 $this->formDataGroupProphecy->compile(Argument::cetera())->willReturnArgument(0);
116 $result = $this->subject->compile($input);
117 $this->assertEquals('pages', $result['tableName']);
118 $this->assertEquals(123, $result['vanillaUid']);
119 $this->assertEquals('edit', $result['command']);
120 }
121
122 /**
123 * @test
124 */
125 public function compileReturnsResultArrayWithAdditionalDataFormFormDataGroup()
126 {
127 $this->formDataGroupProphecy->compile(Argument::cetera())->will(function ($arguments) {
128 $result = $arguments[0];
129 $result['databaseRow'] = 'newData';
130 return $result;
131 });
132 $result = $this->subject->compile([]);
133 $this->assertEquals('newData', $result['databaseRow']);
134 }
135
136 /**
137 * @test
138 */
139 public function compileThrowsExceptionIfFormDataGroupDoesNotReturnArray()
140 {
141 $this->formDataGroupProphecy->compile(Argument::cetera())->willReturn(null);
142 $this->setExpectedException(\UnexpectedValueException::class, $this->anything(), 1446664764);
143 $this->subject->compile([]);
144 }
145
146 /**
147 * @test
148 */
149 public function compileThrowsExceptionIfFormDataGroupRemovedKeysFromResultArray()
150 {
151 $this->formDataGroupProphecy->compile(Argument::cetera())->will(function ($arguments) {
152 $result = $arguments[0];
153 unset($result['tableName']);
154 return $result;
155 });
156 $this->setExpectedException(\UnexpectedValueException::class, $this->anything(), 1438079402);
157 $this->subject->compile([]);
158 }
159
160 /**
161 * @test
162 */
163 public function compileThrowsExceptionIfFormDataGroupAddedKeysToResultArray()
164 {
165 $this->formDataGroupProphecy->compile(Argument::cetera())->will(function ($arguments) {
166 $result = $arguments[0];
167 $result['newKey'] = 'newData';
168 return $result;
169 });
170 $this->setExpectedException(\UnexpectedValueException::class, $this->anything(), 1438079402);
171 $this->subject->compile([]);
172 }
173 }