[TASK] Shorten the extend class in all unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Domain / Finishers / SaveToDatabaseFinisherTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Domain\Finishers;
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\Form\Domain\Finishers\Exception\FinisherException;
18 use TYPO3\CMS\Form\Domain\Finishers\FinisherContext;
19 use TYPO3\CMS\Form\Domain\Finishers\SaveToDatabaseFinisher;
20 use TYPO3\CMS\Form\Domain\Model\FormElements\FormElementInterface;
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22
23 /**
24 * Test case
25 */
26 class SaveToDatabaseFinisherTest extends UnitTestCase
27 {
28
29 /**
30 * @test
31 */
32 public function throwExceptionOnInconsistentConfigurationThrowsExceptionOnInconsistentConfiguration()
33 {
34 $this->expectException(FinisherException::class);
35 $this->expectExceptionCode(1480469086);
36
37 $mockSaveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, [
38 'dummy'
39 ], [], '', false);
40
41 $mockSaveToDatabaseFinisher->_set('options', [
42 'mode' => 'update',
43 'whereClause' => '',
44 ]);
45
46 $mockSaveToDatabaseFinisher->_call('throwExceptionOnInconsistentConfiguration');
47 }
48
49 /**
50 * @test
51 */
52 public function prepareDataConvertsArrayValuesToCsv()
53 {
54 $elementsConfiguration = [
55 'foo' => [
56 'mapOnDatabaseColumn' => 'bar'
57 ]
58 ];
59
60 $saveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, ['getFormValues', 'getElementByIdentifier']);
61 $saveToDatabaseFinisher->method('getFormValues')->willReturn([
62 'foo' => [
63 'one',
64 'two'
65 ]
66 ]);
67 $saveToDatabaseFinisher->method('getElementByIdentifier')->willReturn($this->prophesize(FormElementInterface::class)->reveal());
68 $databaseData = $saveToDatabaseFinisher->_call('prepareData', $elementsConfiguration, []);
69
70 self::assertSame('one,two', $databaseData['bar']);
71 }
72
73 /**
74 * @test
75 */
76 public function executeInternalProcessesSingleTable()
77 {
78 $saveToDatabaseFinisher = $this->getMockBuilder(SaveToDatabaseFinisher::class)
79 ->setMethods(['process'])
80 ->getMock();
81 $this->inject($saveToDatabaseFinisher, 'options', [
82 'table' => 'tx_foo',
83 'databaseColumnMappings' => [
84 'foo' => 1,
85 ],
86 ]);
87
88 $saveToDatabaseFinisher->expects($this->once())->method('process')->with(0);
89
90 $saveToDatabaseFinisher->execute($this->prophesize(FinisherContext::class)->reveal());
91 }
92
93 /**
94 * @return array
95 */
96 public function skipIfValueIsEmptyDataProvider()
97 {
98 return [
99 'null value' => [
100 'value' => null,
101 'expectedEmpty' => true,
102 ],
103 'empty string' => [
104 'value' => '',
105 'expectedEmpty' => true,
106 ],
107 'false value' => [
108 'value' => false,
109 'expectedEmpty' => false,
110 ],
111 'space character' => [
112 'value' => ' ',
113 'expectedEmpty' => false,
114 ],
115 'zero' => [
116 'value' => 0,
117 'expectedEmpty' => false,
118 ],
119 'zero float' => [
120 'value' => 0.0,
121 'expectedEmpty' => false,
122 ],
123 ];
124 }
125
126 /**
127 * @test
128 * @dataProvider skipIfValueIsEmptyDataProvider
129 * @param mixed $value
130 * @param bool $expectedEmpty
131 */
132 public function skipIfValueIsEmptyDetectsEmptyValues($value, bool $expectedEmpty)
133 {
134 $elementsConfiguration = [
135 'foo' => [
136 'mapOnDatabaseColumn' => 'bar',
137 'skipIfValueIsEmpty' => true,
138 ]
139 ];
140
141 $saveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, ['getFormValues', 'getElementByIdentifier']);
142 $saveToDatabaseFinisher->method('getFormValues')->willReturn([
143 'foo' => $value
144 ]);
145 $saveToDatabaseFinisher->method('getElementByIdentifier')->willReturn($this->prophesize(FormElementInterface::class)->reveal());
146 $databaseData = $saveToDatabaseFinisher->_call('prepareData', $elementsConfiguration, []);
147
148 self:self::assertSame($expectedEmpty, empty($databaseData));
149 }
150
151 /**
152 * @test
153 */
154 public function executeInternalProcessesMultipleTables()
155 {
156 $saveToDatabaseFinisher = $this->getMockBuilder(SaveToDatabaseFinisher::class)
157 ->setMethods(['process'])
158 ->getMock();
159 $this->inject($saveToDatabaseFinisher, 'options', [
160 [
161 'table' => 'tx_foo',
162 'databaseColumnMappings' => [
163 'foo' => 1,
164 ],
165 ],
166 [
167 'table' => 'tx_bar',
168 'databaseColumnMappings' => [
169 'bar' => 1,
170 ],
171 ],
172 ]);
173
174 $saveToDatabaseFinisher->expects($this->exactly(2))->method('process')->withConsecutive([0], [1]);
175
176 $saveToDatabaseFinisher->execute($this->prophesize(FinisherContext::class)->reveal());
177 }
178
179 /**
180 * @test
181 */
182 public function prepareDataConvertsDateTimeToUnixTimestamp()
183 {
184 $elementsConfiguration = [
185 'date' => [
186 'mapOnDatabaseColumn' => 'date'
187 ]
188 ];
189
190 $saveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, ['getFormValues', 'getElementByIdentifier']);
191 $saveToDatabaseFinisher->method('getFormValues')->willReturn([
192 'date' => new \DateTime,
193 ]);
194 $saveToDatabaseFinisher->method('getElementByIdentifier')->willReturn($this->prophesize(FormElementInterface::class)->reveal());
195 $databaseData = $saveToDatabaseFinisher->_call('prepareData', $elementsConfiguration, []);
196
197 $expected = '#^([0-9]{10})$#';
198 $this->assertEquals(1, preg_match($expected, $databaseData['date']));
199 }
200
201 /**
202 * @test
203 */
204 public function prepareDataConvertsDateTimeToFormat()
205 {
206 $elementsConfiguration = [
207 'date' => [
208 'mapOnDatabaseColumn' => 'date',
209 'dateFormat' => 'Y.m.d',
210 ]
211 ];
212
213 $saveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, ['getFormValues', 'getElementByIdentifier']);
214 $saveToDatabaseFinisher->method('getFormValues')->willReturn([
215 'date' => new \DateTime('2018-06-12'),
216 ]);
217 $saveToDatabaseFinisher->method('getElementByIdentifier')->willReturn($this->prophesize(FormElementInterface::class)->reveal());
218 $databaseData = $saveToDatabaseFinisher->_call('prepareData', $elementsConfiguration, []);
219
220 self::assertSame('2018.06.12', $databaseData['date']);
221 }
222 }