DatabaseEditRowTest.php 4.99 KB
Newer Older
1
<?php
2

3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * This file is part of the TYPO3 CMS project.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 *
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
 *
 * The TYPO3 project - inspiring people to share!
 */

16
17
namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;

18
use TYPO3\CMS\Backend\Form\Exception\DatabaseRecordException;
19
use TYPO3\CMS\Backend\Form\Exception\DatabaseRecordWorkspaceDeletePlaceholderException;
20
use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseEditRow;
21
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22
23
24
25

/**
 * Test case
 */
26
class DatabaseEditRowTest extends UnitTestCase
27
28
{
    /**
29
     * @var DatabaseEditRow|\PHPUnit\Framework\MockObject\MockObject
30
31
32
     */
    protected $subject;

33
    protected function setUp(): void
34
    {
35
36
37
        $this->subject = $this->getMockBuilder(DatabaseEditRow::class)
            ->setMethods(['getDatabaseRow'])
            ->getMock();
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    }

    /**
     * @test
     */
    public function addDataRetrievesRecordInformationFromDatabase()
    {
        $input = [
            'tableName' => 'tt_content',
            'command' => 'edit',
            'vanillaUid' => 10,
        ];
        $resultRow = [
            'uid' => 10,
            'pid' => 123
        ];
54
        $this->subject->expects(self::once())->method('getDatabaseRow')->willReturn($resultRow);
55
56
57

        $result = $this->subject->addData($input);

58
        self::assertSame($resultRow, $result['databaseRow']);
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    }

    /**
     * @test
     */
    public function addDataThrowsExceptionIfRetrievedRowHasNoPid()
    {
        $input = [
            'tableName' => 'tt_content',
            'command' => 'edit',
            'vanillaUid' => 10,
        ];
        $resultRow = [
            'uid' => 10,
        ];
74
        $this->subject->expects(self::once())->method('getDatabaseRow')->willReturn($resultRow);
75

76
77
        $this->expectException(\UnexpectedValueException::class);
        $this->expectExceptionCode(1437663061);
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

        $this->subject->addData($input);
    }

    /**
     * @test
     */
    public function addDataThrowsExceptionIfGivenUidIsNotPositive()
    {
        $input = [
            'tableName' => 'tt_content',
            'command' => 'edit',
            'vanillaUid' => -10,
        ];

93
94
        $this->expectException(\InvalidArgumentException::class);
        $this->expectExceptionCode(1437656456);
95
96
97
98
99
100
101

        $this->subject->addData($input);
    }

    /**
     * @test
     */
102
    public function addDataThrowsExceptionIfDatabaseFetchingReturnsNoRow()
103
104
105
106
107
108
    {
        $input = [
            'tableName' => 'tt_content',
            'command' => 'edit',
            'vanillaUid' => 10,
        ];
109
        $this->subject->expects(self::once())->method('getDatabaseRow')->willReturn([]);
110

111
112
        $this->expectException(DatabaseRecordException::class);
        $this->expectExceptionCode(1437656081);
113
114
115
116

        $this->subject->addData($input);
    }

117
118
119
120
121
122
123
124
125
126
    /**
     * @test
     */
    public function addDataThrowsExceptionDatabaseRecordExceptionWithAdditionalInformationSet()
    {
        $input = [
            'tableName' => 'tt_content',
            'command' => 'edit',
            'vanillaUid' => 10,
        ];
127
        $this->subject->expects(self::once())->method('getDatabaseRow')->willReturn([]);
128
129
130
131

        try {
            $this->subject->addData($input);
        } catch (DatabaseRecordException $e) {
132
133
            self::assertSame('tt_content', $e->getTableName());
            self::assertSame(10, $e->getUid());
134
135
        }
    }
136

137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    /**
     * @test
     */
    public function addDataThrowsWorkspaceDeletePlaceholderExeptionWithDeletePlaceholderRecord()
    {
        $this->expectException(DatabaseRecordWorkspaceDeletePlaceholderException::class);
        $this->expectExceptionCode(1608658396);
        $GLOBALS['TCA']['tt_content']['ctrl']['versioningWS'] = 1;
        $input = [
            'tableName' => 'tt_content',
            'command' => 'edit',
            'vanillaUid' => 10,
        ];
        $resultRow = [
            'uid' => 10,
            'pid' => 123,
            't3ver_state' => 2,
        ];
        $this->subject->expects(self::once())->method('getDatabaseRow')->willReturn($resultRow);
        $this->subject->addData($input);
    }

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    /**
     * @test
     */
    public function addDataSkipsDatabaseLookupIfDatabaseRowIsPopulated()
    {
        $virtualRow = [
            'uid' => 10,
            'pid' => 123,
            'title' => 'Title of the virtual record'
        ];
        $input = [
            'tableName' => 'virtual_table',
            'command' => 'edit',
            'vanillaUid' => 10,
            'databaseRow' => $virtualRow
        ];
        $resultRow = $virtualRow;
176
        $this->subject->expects(self::never())->method('getDatabaseRow');
177
178
179

        $result = $this->subject->addData($input);

180
        self::assertSame($resultRow, $result['databaseRow']);
181
    }
182
}