[!!!][TASK] Move TYPO3_DB to extension
[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 TYPO3\CMS\Backend\Form\Exception\DatabaseRecordException;
18 use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseEditRow;
19
20 /**
21 * Test case
22 */
23 class DatabaseEditRowTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
24 {
25 /**
26 * @var DatabaseEditRow|\PHPUnit_Framework_MockObject_MockObject
27 */
28 protected $subject;
29
30 protected function setUp()
31 {
32 $this->subject = $this->getMockBuilder(DatabaseEditRow::class)
33 ->setMethods(['getDatabaseRow'])
34 ->getMock();
35 }
36
37 /**
38 * @test
39 */
40 public function addDataRetrievesRecordInformationFromDatabase()
41 {
42 $input = [
43 'tableName' => 'tt_content',
44 'command' => 'edit',
45 'vanillaUid' => 10,
46 ];
47 $resultRow = [
48 'uid' => 10,
49 'pid' => 123
50 ];
51 $this->subject->expects($this->once())->method('getDatabaseRow')->willReturn($resultRow);
52
53 $result = $this->subject->addData($input);
54
55 $this->assertSame($resultRow, $result['databaseRow']);
56 }
57
58 /**
59 * @test
60 */
61 public function addDataThrowsExceptionIfRetrievedRowHasNoPid()
62 {
63 $input = [
64 'tableName' => 'tt_content',
65 'command' => 'edit',
66 'vanillaUid' => 10,
67 ];
68 $resultRow = [
69 'uid' => 10,
70 ];
71 $this->subject->expects($this->once())->method('getDatabaseRow')->willReturn($resultRow);
72
73 $this->expectException(\UnexpectedValueException::class);
74 $this->expectExceptionCode(1437663061);
75
76 $this->subject->addData($input);
77 }
78
79 /**
80 * @test
81 */
82 public function addDataThrowsExceptionIfGivenUidIsNotPositive()
83 {
84 $input = [
85 'tableName' => 'tt_content',
86 'command' => 'edit',
87 'vanillaUid' => -10,
88 ];
89
90 $this->expectException(\InvalidArgumentException::class);
91 $this->expectExceptionCode(1437656456);
92
93 $this->subject->addData($input);
94 }
95
96 /**
97 * @test
98 */
99 public function addDataThrowsExceptionIfDatabaseFetchingReturnsNoRow()
100 {
101 $input = [
102 'tableName' => 'tt_content',
103 'command' => 'edit',
104 'vanillaUid' => 10,
105 ];
106 $this->subject->expects($this->once())->method('getDatabaseRow')->willReturn([]);
107
108 $this->expectException(DatabaseRecordException::class);
109 $this->expectExceptionCode(1437656081);
110
111 $this->subject->addData($input);
112 }
113
114 /**
115 * @test
116 */
117 public function addDataThrowsExceptionDatabaseRecordExceptionWithAdditionalInformationSet()
118 {
119 $input = [
120 'tableName' => 'tt_content',
121 'command' => 'edit',
122 'vanillaUid' => 10,
123 ];
124 $this->subject->expects($this->once())->method('getDatabaseRow')->willReturn([]);
125
126 try {
127 $this->subject->addData($input);
128 } catch (DatabaseRecordException $e) {
129 $this->assertSame('tt_content', $e->getTableName());
130 $this->assertSame(10, $e->getUid());
131 }
132 }
133
134 /**
135 * @test
136 */
137 public function addDataSkipsDatabaseLookupIfDatabaseRowIsPopulated()
138 {
139 $virtualRow = [
140 'uid' => 10,
141 'pid' => 123,
142 'title' => 'Title of the virtual record'
143 ];
144 $input = [
145 'tableName' => 'virtual_table',
146 'command' => 'edit',
147 'vanillaUid' => 10,
148 'databaseRow' => $virtualRow
149 ];
150 $resultRow = $virtualRow;
151 $this->subject->expects($this->never())->method('getDatabaseRow');
152
153 $result = $this->subject->addData($input);
154
155 $this->assertSame($resultRow, $result['databaseRow']);
156 }
157 }