4a80d19a9a36ecedd595a8d020b00dee74bdbf3c
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / DatabaseParentPageRowTest.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\DatabaseParentPageRow;
19 use TYPO3\CMS\Core\Database\DatabaseConnection;
20
21 /**
22 * Test case
23 */
24 class DatabaseParentPageRowTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
25 {
26 /**
27 * @var DatabaseParentPageRow|\PHPUnit_Framework_MockObject_MockObject
28 */
29 protected $subject;
30
31 /**
32 * @var DatabaseConnection | ObjectProphecy
33 */
34 protected $dbProphecy;
35
36 protected function setUp()
37 {
38 $this->subject = $this->getMockBuilder(DatabaseParentPageRow::class)
39 ->setMethods(['getDatabaseRow'])
40 ->getMock();
41 }
42
43 /**
44 * @test
45 */
46 public function addDataFetchesParentPageRowOfRecordIfNeighbourGiven()
47 {
48 $input = [
49 'tableName' => 'tt_content',
50 'command' => 'new',
51 'vanillaUid' => -10,
52 ];
53 $parentPageRow = [
54 'uid' => 123,
55 'pid' => 321
56 ];
57
58 $this->subject->expects($this->at(0))
59 ->method('getDatabaseRow')
60 ->with($input['tableName'], 10)
61 ->willReturn(['pid' => 123]);
62
63 $this->subject->expects($this->at(1))
64 ->method('getDatabaseRow')
65 ->with('pages', 123)
66 ->willReturn($parentPageRow);
67
68 $result = $this->subject->addData($input);
69
70 $this->assertSame($parentPageRow, $result['parentPageRow']);
71 }
72
73 /**
74 * @test
75 */
76 public function addDataSetsNeigborRowIfNegativeUidGiven()
77 {
78 $input = [
79 'tableName' => 'tt_content',
80 'command' => 'new',
81 'vanillaUid' => -10,
82 ];
83 $neigborRow = [
84 'uid' => 10,
85 'pid' => 321
86 ];
87 $parentPageRow = [
88 'uid' => 123,
89 'pid' => 321
90 ];
91 $this->subject->expects($this->at(0))
92 ->method('getDatabaseRow')
93 ->with($input['tableName'], 10)
94 ->willReturn($neigborRow);
95
96 $this->subject->expects($this->at(1))
97 ->method('getDatabaseRow')
98 ->with('pages', 321)
99 ->willReturn($parentPageRow);
100
101 $result = $this->subject->addData($input);
102
103 $this->assertSame($neigborRow, $result['neighborRow']);
104 }
105
106 /**
107 * @test
108 */
109 public function addDataSetsParentPageRowToNullIfParentIsRoot()
110 {
111 $input = [
112 'tableName' => 'tt_content',
113 'command' => 'new',
114 'vanillaUid' => -10,
115 ];
116
117 $this->subject->expects($this->once())
118 ->method('getDatabaseRow')
119 ->with($input['tableName'], 10)
120 ->willReturn(['pid' => 0]);
121
122 $result = $this->subject->addData($input);
123
124 $this->assertNull($result['parentPageRow']);
125 }
126
127 /**
128 * @test
129 */
130 public function addDataSetsParentPageToGivenPageIdIfCommandIsNew()
131 {
132 $input = [
133 'tableName' => 'tt_content',
134 'command' => 'new',
135 'vanillaUid' => 123,
136 ];
137 $parentPageRow = [
138 'uid' => 123,
139 'pid' => 321
140 ];
141
142 $this->subject->expects($this->once())
143 ->method('getDatabaseRow')
144 ->with('pages', 123)
145 ->willReturn($parentPageRow);
146
147 $result = $this->subject->addData($input);
148
149 $this->assertSame($parentPageRow, $result['parentPageRow']);
150 }
151
152 /**
153 * @test
154 */
155 public function addDataSetsParentPageRowOnParentIfCommandIsEdit()
156 {
157 $input = [
158 'tableName' => 'tt_content',
159 'command' => 'edit',
160 'vanillaUid' => 123,
161 'databaseRow' => [
162 'uid' => 123,
163 'pid' => 321
164 ],
165 ];
166 $parentPageRow = [
167 'uid' => 321,
168 'pid' => 456
169 ];
170 $this->subject->expects($this->once())
171 ->method('getDatabaseRow')
172 ->with('pages', 321)
173 ->willReturn($parentPageRow);
174
175 $result = $this->subject->addData($input);
176
177 $this->assertSame($parentPageRow, $result['parentPageRow']);
178 }
179 }