[BUGFIX] EXT:form - Cover empty strings for "skipIfValueIsEmpty"
[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
22 /**
23 * Test case
24 */
25 class SaveToDatabaseFinisherTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
26 {
27
28 /**
29 * @test
30 */
31 public function throwExceptionOnInconsistentConfigurationThrowsExceptionOnInconsistentConfiguration()
32 {
33 $this->expectException(FinisherException::class);
34 $this->expectExceptionCode(1480469086);
35
36 $mockSaveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, [
37 'dummy'
38 ], [], '', false);
39
40 $mockSaveToDatabaseFinisher->_set('options', [
41 'mode' => 'update',
42 'whereClause' => '',
43 ]);
44
45 $mockSaveToDatabaseFinisher->_call('throwExceptionOnInconsistentConfiguration');
46 }
47
48 /**
49 * @test
50 */
51 public function prepareDataConvertsArrayValuesToCsv()
52 {
53 $elementsConfiguration = [
54 'foo' => [
55 'mapOnDatabaseColumn' => 'bar'
56 ]
57 ];
58
59 $saveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, ['getFormValues', 'getElementByIdentifier']);
60 $saveToDatabaseFinisher->method('getFormValues')->willReturn([
61 'foo' => [
62 'one',
63 'two'
64 ]
65 ]);
66 $saveToDatabaseFinisher->method('getElementByIdentifier')->willReturn($this->prophesize(FormElementInterface::class)->reveal());
67 $databaseData = $saveToDatabaseFinisher->_call('prepareData', $elementsConfiguration, []);
68
69 self::assertSame('one,two', $databaseData['bar']);
70 }
71
72 /**
73 * @test
74 */
75 public function executeInternalProcessesSingleTable()
76 {
77 $saveToDatabaseFinisher = $this->getMockBuilder(SaveToDatabaseFinisher::class)
78 ->setMethods(['process'])
79 ->getMock();
80 $this->inject($saveToDatabaseFinisher, 'options', [
81 'table' => 'tx_foo',
82 'databaseColumnMappings' => [
83 'foo' => 1,
84 ],
85 ]);
86
87 $saveToDatabaseFinisher->expects($this->once())->method('process')->with(0);
88
89 $saveToDatabaseFinisher->execute($this->prophesize(FinisherContext::class)->reveal());
90 }
91
92 /**
93 * @return array
94 */
95 public function skipIfValueIsEmptyDataProvider()
96 {
97 return [
98 'null value' => [
99 'value' => null,
100 'expectedEmpty' => true,
101 ],
102 'empty string' => [
103 'value' => '',
104 'expectedEmpty' => true,
105 ],
106 'false value' => [
107 'value' => false,
108 'expectedEmpty' => false,
109 ],
110 'space character' => [
111 'value' => ' ',
112 'expectedEmpty' => false,
113 ],
114 'zero' => [
115 'value' => 0,
116 'expectedEmpty' => false,
117 ],
118 'zero float' => [
119 'value' => 0.0,
120 'expectedEmpty' => false,
121 ],
122 ];
123 }
124
125 /**
126 * @test
127 * @dataProvider skipIfValueIsEmptyDataProvider
128 * @param mixed $value
129 * @param bool $expectedEmpty
130 */
131 public function skipIfValueIsEmptyDetectsEmptyValues($value, bool $expectedEmpty)
132 {
133 $elementsConfiguration = [
134 'foo' => [
135 'mapOnDatabaseColumn' => 'bar',
136 'skipIfValueIsEmpty' => true,
137 ]
138 ];
139
140 $saveToDatabaseFinisher = $this->getAccessibleMock(SaveToDatabaseFinisher::class, ['getFormValues', 'getElementByIdentifier']);
141 $saveToDatabaseFinisher->method('getFormValues')->willReturn([
142 'foo' => $value
143 ]);
144 $saveToDatabaseFinisher->method('getElementByIdentifier')->willReturn($this->prophesize(FormElementInterface::class)->reveal());
145 $databaseData = $saveToDatabaseFinisher->_call('prepareData', $elementsConfiguration, []);
146
147 self:self::assertSame($expectedEmpty, empty($databaseData));
148 }
149
150 /**
151 * @test
152 */
153 public function executeInternalProcessesMultipleTables()
154 {
155 $saveToDatabaseFinisher = $this->getMockBuilder(SaveToDatabaseFinisher::class)
156 ->setMethods(['process'])
157 ->getMock();
158 $this->inject($saveToDatabaseFinisher, 'options', [
159 [
160 'table' => 'tx_foo',
161 'databaseColumnMappings' => [
162 'foo' => 1,
163 ],
164 ],
165 [
166 'table' => 'tx_bar',
167 'databaseColumnMappings' => [
168 'bar' => 1,
169 ],
170 ],
171 ]);
172
173 $saveToDatabaseFinisher->expects($this->exactly(2))->method('process')->withConsecutive([0], [1]);
174
175 $saveToDatabaseFinisher->execute($this->prophesize(FinisherContext::class)->reveal());
176 }
177 }