TcaGroupTest.php 6.4 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 Prophecy\Prophecy\ObjectProphecy;
19
use TYPO3\CMS\Backend\Clipboard\Clipboard;
20
use TYPO3\CMS\Backend\Form\FormDataProvider\TcaGroup;
21
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
22
use TYPO3\CMS\Core\Database\RelationHandler;
23
24
use TYPO3\CMS\Core\Resource\Folder;
use TYPO3\CMS\Core\Resource\ResourceFactory;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27
28
29
30

/**
 * Test case
 */
31
class TcaGroupTest extends UnitTestCase
32
{
33
    /**
34
     * @var bool Reset singletons created by subject
35
     */
36
    protected $resetSingletonInstances = true;
37

38
39
40
41
42
43
44
45
46
    /**
     * @test
     */
    public function addDataReturnsFieldUnchangedIfFieldIsNotTypeGroup()
    {
        $input = [
            'databaseRow' => [
                'aField' => 'aValue',
            ],
47
            'processedTca' => [
48
49
50
51
52
53
54
55
56
57
                'columns' => [
                    'aField' => [
                        'config' => [
                            'type' => 'foo',
                        ],
                    ],
                ],
            ],
        ];
        $expected = $input;
58
        self::assertSame($expected, (new TcaGroup())->addData($input));
59
    }
60

61
62
63
    /**
     * @test
     */
64
    public function addDataThrowsExceptionWithTypeGroupAndNoValidInternalType()
65
66
    {
        $input = [
67
            'tableName' => 'aTable',
68
            'processedTca' => [
69
70
71
72
73
74
75
76
77
78
                'columns' => [
                    'aField' => [
                        'config' => [
                            'type' => 'group',
                            'internal_type' => 'foo',
                        ],
                    ],
                ],
            ],
        ];
79
80
        $this->expectException(\UnexpectedValueException::class);
        $this->expectExceptionCode(1438780511);
81
        (new TcaGroup())->addData($input);
82
    }
83

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
    /**
     * @test
     */
    public function addDataSetsFolderData()
    {
        $input = [
            'databaseRow' => [
                'aField' => '1:/aFolder/anotherFolder/',
            ],
            'processedTca' => [
                'columns' => [
                    'aField' => [
                        'config' => [
                            'type' => 'group',
                            'internal_type' => 'folder',
99
                            'maxitems' => 99999,
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
                        ],
                    ],
                ],
            ],
        ];

        /** @var Folder|ObjectProphecy $relationHandlerProphecy */
        $folderProphecy = $this->prophesize(Folder::class);

        /** @var ResourceFactory|ObjectProphecy $relationHandlerProphecy */
        $resourceFactoryProphecy = $this->prophesize(ResourceFactory::class);
        GeneralUtility::setSingletonInstance(ResourceFactory::class, $resourceFactoryProphecy->reveal());
        $resourceFactoryProphecy->retrieveFileOrFolderObject('1:/aFolder/anotherFolder/')
            ->shouldBeCalled()
            ->willReturn($folderProphecy->reveal());

        $expected = $input;
117
118
119
120
121
122
        $expected['databaseRow']['aField'] = [
            [
                'folder' => '1:/aFolder/anotherFolder/',
            ]
        ];
        $expected['processedTca']['columns']['aField']['config']['clipboardElements'] = [];
123
        self::assertSame($expected, (new TcaGroup())->addData($input));
124
125
    }

126
127
128
129
130
131
132
133
134
135
    /**
     * @test
     */
    public function addDataSetsDatabaseData()
    {
        $aFieldConfig = [
            'type' => 'group',
            'internal_type' => 'db',
            'MM' => 'mmTableName',
            'allowed' => 'aForeignTable',
136
            'maxitems' => 99999,
137
138
139
140
141
142
143
        ];
        $input = [
            'tableName' => 'aTable',
            'databaseRow' => [
                'uid' => 42,
                'aField' => '1,2',
            ],
144
            'processedTca' => [
145
146
147
148
149
150
151
                'columns' => [
                    'aField' => [
                        'config' => $aFieldConfig,
                    ],
                ],
            ],
        ];
152

153
154
155
        $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
        $GLOBALS['BE_USER'] = $backendUserProphecy->reveal();

156
157
158
159
160
        $clipboardProphecy = $this->prophesize(Clipboard::class);
        GeneralUtility::addInstance(Clipboard::class, $clipboardProphecy->reveal());
        $clipboardProphecy->initializeClipboard()->shouldBeCalled();
        $clipboardProphecy->elFromTable('aForeignTable')->shouldBeCalled()->willReturn([]);

161
162
163
164
165
        /** @var RelationHandler|ObjectProphecy $relationHandlerProphecy */
        $relationHandlerProphecy = $this->prophesize(RelationHandler::class);
        GeneralUtility::addInstance(RelationHandler::class, $relationHandlerProphecy->reveal());
        $relationHandlerProphecy->start('1,2', 'aForeignTable', 'mmTableName', 42, 'aTable', $aFieldConfig)->shouldBeCalled();
        $relationHandlerProphecy->getFromDB()->shouldBeCalled();
166
167
168
169
        $relationHandlerProphecy->getResolvedItemArray()->shouldBeCalled()->willReturn([
            [
                'table' => 'aForeignTable',
                'uid' => 1,
170
171
172
                'record' => [
                    'uid' => 1
                ]
173
174
175
176
            ],
            [
                'table' => 'aForeignTable',
                'uid' => 2,
177
178
179
                'record' => [
                    'uid' => 2
                ]
180
181
            ],
        ]);
182

183
        $expected = $input;
184
185
186
        $expected['databaseRow']['aField'] = [
            [
                'table' => 'aForeignTable',
187
                'uid' => 1,
188
                'title' => '',
189
190
191
                'row' => [
                    'uid' => 1
                ],
192
193
194
            ],
            [
                'table' => 'aForeignTable',
195
                'uid' => 2,
196
                'title' => '',
197
198
199
                'row' => [
                    'uid' => 2
                ],
200
201
202
            ]
        ];
        $expected['processedTca']['columns']['aField']['config']['clipboardElements'] = [];
203

204
        self::assertSame($expected, (new TcaGroup())->addData($input));
205
    }
206
}