‪TYPO3CMS  ‪main
DatabaseEditRowTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
21 use PHPUnit\Framework\MockObject\MockObject;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30 
31 final class ‪DatabaseEditRowTest extends UnitTestCase
32 {
33  protected ‪DatabaseEditRow&MockObject ‪$subject;
34 
35  protected function ‪setUp(): void
36  {
37  parent::setUp();
38  $this->subject = $this->getMockBuilder(DatabaseEditRow::class)
39  ->onlyMethods(['getDatabaseRow'])
40  ->getMock();
41  }
42 
43  #[Test]
45  {
46  $input = [
47  'tableName' => 'tt_content',
48  'command' => 'edit',
49  'vanillaUid' => 10,
50  ];
51  $resultRow = [
52  'uid' => 10,
53  'pid' => 123,
54  ];
55  $this->subject->expects(self::once())->method('getDatabaseRow')->willReturn($resultRow);
56 
57  $result = $this->subject->addData($input);
58 
59  self::assertSame($resultRow, $result['databaseRow']);
60  }
61 
62  #[Test]
64  {
65  $input = [
66  'tableName' => 'tt_content',
67  'command' => 'edit',
68  'vanillaUid' => 10,
69  ];
70  $resultRow = [
71  'uid' => 10,
72  ];
73  $this->subject->expects(self::once())->method('getDatabaseRow')->willReturn($resultRow);
74 
75  $this->expectException(\UnexpectedValueException::class);
76  $this->expectExceptionCode(1437663061);
77 
78  $this->subject->addData($input);
79  }
80 
81  #[Test]
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  #[Test]
98  {
99  $input = [
100  'tableName' => 'tt_content',
101  'command' => 'edit',
102  'vanillaUid' => 10,
103  ];
104  $this->subject->expects(self::once())->method('getDatabaseRow')->willReturn([]);
105 
106  $this->expectException(DatabaseRecordException::class);
107  $this->expectExceptionCode(1437656081);
108 
109  $this->subject->addData($input);
110  }
111 
112  #[Test]
114  {
115  $input = [
116  'tableName' => 'tt_content',
117  'command' => 'edit',
118  'vanillaUid' => 10,
119  ];
120  $this->subject->expects(self::once())->method('getDatabaseRow')->willReturn([]);
121 
122  try {
123  $this->subject->addData($input);
124  } catch (‪DatabaseRecordException $e) {
125  self::assertSame('tt_content', $e->‪getTableName());
126  self::assertSame(10, $e->‪getUid());
127  }
128  }
129 
130  #[Test]
132  {
133  $connectionMock = $this->getMockBuilder(Connection::class)->disableOriginalConstructor()->getMock();
134  $connectionMock->method('getDatabasePlatform')->willReturn(new ‪MockMySQLPlatform());
135  $connectionPoolMock = $this->getMockBuilder(ConnectionPool::class)->disableOriginalConstructor()->getMock();
136  $connectionPoolMock->method('getConnectionForTable')->willReturn($connectionMock);
137  $connectionPoolMock->method('getConnectionByName')->willReturn($connectionMock);
138  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolMock);
139 
140  $this->expectException(DatabaseRecordWorkspaceDeletePlaceholderException::class);
141  $this->expectExceptionCode(1608658396);
142  ‪$GLOBALS['TCA']['tt_content']['ctrl']['versioningWS'] = 1;
143  $input = [
144  'tableName' => 'tt_content',
145  'command' => 'edit',
146  'vanillaUid' => 10,
147  ];
148  $resultRow = [
149  'uid' => 10,
150  'pid' => 123,
151  't3ver_state' => 2,
152  ];
153  $this->subject->expects(self::once())->method('getDatabaseRow')->willReturn($resultRow);
154  $this->subject->addData($input);
155  }
156 
157  #[Test]
159  {
160  $virtualRow = [
161  'uid' => 10,
162  'pid' => 123,
163  'title' => 'Title of the virtual record',
164  ];
165  $input = [
166  'tableName' => 'virtual_table',
167  'command' => 'edit',
168  'vanillaUid' => 10,
169  'databaseRow' => $virtualRow,
170  ];
171  $resultRow = $virtualRow;
172  $this->subject->expects(self::never())->method('getDatabaseRow');
173 
174  $result = $this->subject->addData($input);
175 
176  self::assertSame($resultRow, $result['databaseRow']);
177  }
178 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseEditRow
Definition: DatabaseEditRow.php:28
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\DatabaseEditRowTest\addDataThrowsExceptionIfRetrievedRowHasNoPid
‪addDataThrowsExceptionIfRetrievedRowHasNoPid()
Definition: DatabaseEditRowTest.php:63
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\DatabaseEditRowTest\addDataSkipsDatabaseLookupIfDatabaseRowIsPopulated
‪addDataSkipsDatabaseLookupIfDatabaseRowIsPopulated()
Definition: DatabaseEditRowTest.php:158
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\DatabaseEditRowTest\$subject
‪DatabaseEditRow &MockObject $subject
Definition: DatabaseEditRowTest.php:33
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\DatabaseEditRowTest\setUp
‪setUp()
Definition: DatabaseEditRowTest.php:35
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\DatabaseEditRowTest\addDataThrowsExceptionIfDatabaseFetchingReturnsNoRow
‪addDataThrowsExceptionIfDatabaseFetchingReturnsNoRow()
Definition: DatabaseEditRowTest.php:97
‪TYPO3\CMS\Backend\Form\Exception\DatabaseRecordException\getUid
‪int getUid()
Definition: DatabaseRecordException.php:64
‪TYPO3\CMS\Backend\Form\Exception\DatabaseRecordWorkspaceDeletePlaceholderException
Definition: DatabaseRecordWorkspaceDeletePlaceholderException.php:26
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\DatabaseEditRowTest\addDataThrowsWorkspaceDeletePlaceholderExceptionWithDeletePlaceholderRecord
‪addDataThrowsWorkspaceDeletePlaceholderExceptionWithDeletePlaceholderRecord()
Definition: DatabaseEditRowTest.php:131
‪TYPO3\CMS\Backend\Form\Exception\DatabaseRecordException\getTableName
‪string getTableName()
Definition: DatabaseRecordException.php:54
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\DatabaseEditRowTest\addDataThrowsExceptionIfGivenUidIsNotPositive
‪addDataThrowsExceptionIfGivenUidIsNotPositive()
Definition: DatabaseEditRowTest.php:82
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\DatabaseEditRowTest\addDataThrowsExceptionDatabaseRecordExceptionWithAdditionalInformationSet
‪addDataThrowsExceptionDatabaseRecordExceptionWithAdditionalInformationSet()
Definition: DatabaseEditRowTest.php:113
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:41
‪TYPO3\CMS\Backend\Form\Exception\DatabaseRecordException
Definition: DatabaseRecordException.php:24
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\DatabaseEditRowTest\addDataRetrievesRecordInformationFromDatabase
‪addDataRetrievesRecordInformationFromDatabase()
Definition: DatabaseEditRowTest.php:44
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\DatabaseEditRowTest
Definition: DatabaseEditRowTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Database\Mocks\MockPlatform\MockMySQLPlatform
Definition: MockMySQLPlatform.php:24
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider
Definition: DatabaseDefaultLanguagePageRowTest.php:18