[BUGFIX] Exception editing inline mm with deleted child child
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / DatabaseEditRowTest.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\Argument;
18 use Prophecy\Prophecy\ObjectProphecy;
19 use TYPO3\CMS\Backend\Form\Exception\DatabaseRecordException;
20 use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseEditRow;
21 use TYPO3\CMS\Core\Database\DatabaseConnection;
22 use TYPO3\CMS\Core\Tests\UnitTestCase;
23
24 /**
25 * Test case
26 */
27 class DatabaseEditRowTest extends UnitTestCase
28 {
29 /**
30 * @var DatabaseEditRow
31 */
32 protected $subject;
33
34 /**
35 * @var DatabaseConnection | ObjectProphecy
36 */
37 protected $dbProphecy;
38
39 protected function setUp()
40 {
41 $this->dbProphecy = $this->prophesize(DatabaseConnection::class);
42 $GLOBALS['TYPO3_DB'] = $this->dbProphecy->reveal();
43
44 $this->subject = new DatabaseEditRow();
45 }
46
47 /**
48 * @test
49 */
50 public function addDataRetrievesRecordInformationFromDatabase()
51 {
52 $input = [
53 'tableName' => 'tt_content',
54 'command' => 'edit',
55 'vanillaUid' => 10,
56 ];
57 $resultRow = [
58 'uid' => 10,
59 'pid' => 123
60 ];
61 $this->dbProphecy->quoteStr($input['tableName'], $input['tableName'])->willReturn($input['tableName']);
62 $this->dbProphecy->exec_SELECTgetSingleRow('*', 'tt_content', 'uid=' . $input['vanillaUid'])->willReturn($resultRow);
63 $this->dbProphecy->exec_SELECTgetSingleRow(Argument::cetera())->willReturn([]);
64
65 $result = $this->subject->addData($input);
66
67 $this->assertSame($resultRow, $result['databaseRow']);
68 }
69
70 /**
71 * @test
72 */
73 public function addDataThrowsExceptionIfRetrievedRowHasNoPid()
74 {
75 $input = [
76 'tableName' => 'tt_content',
77 'command' => 'edit',
78 'vanillaUid' => 10,
79 ];
80 $resultRow = [
81 'uid' => 10,
82 ];
83 $this->dbProphecy->quoteStr($input['tableName'], $input['tableName'])->willReturn($input['tableName']);
84 $this->dbProphecy->exec_SELECTgetSingleRow('*', 'tt_content', 'uid=' . $input['vanillaUid'])->willReturn($resultRow);
85
86 $this->setExpectedException(\UnexpectedValueException::class, $this->anything(), 1437663061);
87
88 $this->subject->addData($input);
89 }
90
91 /**
92 * @test
93 */
94 public function addDataThrowsExceptionIfGivenUidIsNotPositive()
95 {
96 $input = [
97 'tableName' => 'tt_content',
98 'command' => 'edit',
99 'vanillaUid' => -10,
100 ];
101
102 $this->setExpectedException(\InvalidArgumentException::class, $this->anything(), 1437656456);
103
104 $this->subject->addData($input);
105 }
106
107 /**
108 * @test
109 */
110 public function addDataThrowsExceptionIfNoRecordForEditingCouldBeRetrievedFromDatabase()
111 {
112 $input = [
113 'tableName' => 'tt_content',
114 'command' => 'edit',
115 'vanillaUid' => 10,
116 ];
117
118 $this->setExpectedException(\RuntimeException::class, $this->anything(), 1437655862);
119
120 $this->subject->addData($input);
121 }
122
123 /**
124 * @test
125 */
126 public function addDataThrowsExceptionIfDatabaseFetchingReturnsFalse()
127 {
128 $input = [
129 'tableName' => 'tt_content',
130 'command' => 'edit',
131 'vanillaUid' => 10,
132 ];
133 $this->dbProphecy->quoteStr(Argument::cetera())->willReturn($input['tableName']);
134 $this->dbProphecy->exec_SELECTgetSingleRow(Argument::cetera())->willReturn(false);
135
136 $this->setExpectedException(DatabaseRecordException::class, $this->anything(), 1437656081);
137
138 $this->subject->addData($input);
139 }
140
141 /**
142 * @test
143 */
144 public function addDataThrowsExceptionDatabaseRecordExceptionWithAdditionalInformationSet()
145 {
146 $input = [
147 'tableName' => 'tt_content',
148 'command' => 'edit',
149 'vanillaUid' => 10,
150 ];
151 $this->dbProphecy->quoteStr(Argument::cetera())->willReturn($input['tableName']);
152 $this->dbProphecy->exec_SELECTgetSingleRow(Argument::cetera())->willReturn(false);
153
154 try {
155 $this->subject->addData($input);
156 } catch (DatabaseRecordException $e) {
157 $this->assertSame('tt_content', $e->getTableName());
158 $this->assertSame(10, $e->getUid());
159 }
160 }
161
162 /**
163 * @test
164 */
165 public function addDataThrowsExceptionIfDatabaseFetchingReturnsInvalidRowResultData()
166 {
167 $input = [
168 'tableName' => 'tt_content',
169 'command' => 'edit',
170 'vanillaUid' => 10,
171 ];
172 $this->dbProphecy->quoteStr(Argument::cetera())->willReturn($input['tableName']);
173 $this->dbProphecy->exec_SELECTgetSingleRow(Argument::cetera())->willReturn('invalid result data');
174
175 $this->setExpectedException(\UnexpectedValueException::class, $this->anything(), 1437656323);
176
177 $this->subject->addData($input);
178 }
179 }