‪TYPO3CMS  10.4
TcaSelectTreeItemsTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Doctrine\DBAL\Driver\Statement;
19 use Prophecy\Argument;
20 use Prophecy\Prophecy\ObjectProphecy;
34 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35 
43 class ‪TcaSelectTreeItemsTest extends UnitTestCase
44 {
48  protected ‪$resetSingletonInstances = true;
49 
54  protected function ‪mockDatabaseConnection()
55  {
56  $connectionProphet = $this->prophesize(Connection::class);
57  $connectionProphet->quote(Argument::cetera())->will(function ($arguments) {
58  return "'" . $arguments[0] . "'";
59  });
60  $connectionProphet->quoteIdentifier(Argument::cetera())->will(function ($arguments) {
61  return '`' . $arguments[0] . '`';
62  });
63 
65  $statementProphet = $this->prophesize(Statement::class);
66  $statementProphet->fetch()->shouldBeCalled();
67 
68  $restrictionProphet = $this->prophesize(DefaultRestrictionContainer::class);
69  $restrictionProphet->removeAll()->willReturn($restrictionProphet->reveal());
70  $restrictionProphet->add(Argument::cetera())->willReturn($restrictionProphet->reveal());
71 
72  $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
73  $queryBuilderProphet->expr()->willReturn(
74  GeneralUtility::makeInstance(ExpressionBuilder::class, $connectionProphet->reveal())
75  );
76  $queryBuilderProphet->getRestrictions()->willReturn($restrictionProphet->reveal());
77  $queryBuilderProphet->quoteIdentifier(Argument::cetera())->will(function ($arguments) {
78  return '`' . $arguments[0] . '`';
79  });
80 
81  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
82  $connectionPoolProphet->getConnectionForTable('foreignTable')
83  ->willReturn($connectionProphet->reveal());
84  $connectionPoolProphet->getQueryBuilderForTable('foreignTable')
85  ->shouldBeCalled()
86  ->willReturn($queryBuilderProphet->reveal());
87 
88  $queryBuilderProphet->select('foreignTable.uid')
89  ->shouldBeCalled()
90  ->willReturn($queryBuilderProphet->reveal());
91  $queryBuilderProphet->from('foreignTable')
92  ->shouldBeCalled()
93  ->willReturn($queryBuilderProphet->reveal());
94  $queryBuilderProphet->from('pages')
95  ->shouldBeCalled()
96  ->willReturn($queryBuilderProphet->reveal());
97  $queryBuilderProphet->where('')
98  ->shouldBeCalled()
99  ->willReturn($queryBuilderProphet->reveal());
100  $queryBuilderProphet->andWhere(' 1=1')
101  ->shouldBeCalled()
102  ->willReturn($queryBuilderProphet->reveal());
103  $queryBuilderProphet->andWhere('`pages.uid` = `foreignTable.pid`')
104  ->shouldBeCalled()
105  ->willReturn($queryBuilderProphet->reveal());
106  $queryBuilderProphet->execute()
107  ->shouldBeCalled()
108  ->willReturn($statementProphet->reveal());
109 
110  // Two instances are needed due to the push/pop behavior of addInstance()
111  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
112  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
113  }
114 
119  {
120  ‪$GLOBALS['TCA']['foreignTable'] = [];
121 
122  $iconFactoryProphecy = $this->prophesize(IconFactory::class);
123  GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
124  GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
125 
126  $fileRepositoryProphecy = $this->prophesize(FileRepository::class);
127  $fileRepositoryProphecy->findByRelation(Argument::cetera())->shouldNotBeCalled();
128  GeneralUtility::setSingletonInstance(FileRepository::class, $fileRepositoryProphecy->reveal());
129 
131  $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
132  ‪$GLOBALS['BE_USER'] = $backendUserProphecy->reveal();
133  $backendUserProphecy->getPagePermsClause(Argument::cetera())->willReturn(' 1=1');
134 
135  $languageService = $this->prophesize(LanguageService::class);
136  ‪$GLOBALS['LANG'] = $languageService->reveal();
137  $languageService->sL(Argument::cetera())->willReturnArgument(0);
138 
139  $this->‪mockDatabaseConnection();
140 
142  $treeDataProviderProphecy = $this->prophesize(DatabaseTreeDataProvider::class);
143  GeneralUtility::addInstance(DatabaseTreeDataProvider::class, $treeDataProviderProphecy->reveal());
144 
146  $tableConfigurationTreeProphecy = $this->prophesize(TableConfigurationTree::class);
147  GeneralUtility::addInstance(TableConfigurationTree::class, $tableConfigurationTreeProphecy->reveal());
148  $tableConfigurationTreeProphecy->setDataProvider(Argument::cetera())->shouldBeCalled();
149  $tableConfigurationTreeProphecy->setNodeRenderer(Argument::cetera())->shouldBeCalled();
150  $tableConfigurationTreeProphecy->render()->shouldBeCalled()->willReturn(['fake', 'tree', 'data']);
151 
152  $input = [
153  'tableName' => 'aTable',
154  'effectivePid' => 42,
155  'databaseRow' => [
156  'uid' => 5,
157  'aField' => '1'
158  ],
159  'processedTca' => [
160  'columns' => [
161  'aField' => [
162  'config' => [
163  'type' => 'select',
164  'renderType' => 'selectTree',
165  'treeConfig' => [
166  'childrenField' => 'childrenField'
167  ],
168  'foreign_table' => 'foreignTable',
169  'items' => [],
170  'maxitems' => 1
171  ],
172  ],
173  ],
174  ],
175  'selectTreeCompileItems' => true,
176  ];
177 
178  $expected = $input;
179  $expected['databaseRow']['aField'] = ['1'];
180  $expected['processedTca']['columns']['aField']['config']['items'] = [
181  'fake', 'tree', 'data',
182  ];
183  self::assertEquals($expected, (new ‪TcaSelectTreeItems())->addData($input));
184  }
185 
190  {
191  ‪$GLOBALS['TCA']['foreignTable'] = [];
192 
193  $iconFactoryProphecy = $this->prophesize(IconFactory::class);
194  GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
195  GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
196 
197  $fileRepositoryProphecy = $this->prophesize(FileRepository::class);
198  $fileRepositoryProphecy->findByRelation(Argument::cetera())->shouldNotBeCalled();
199  GeneralUtility::setSingletonInstance(FileRepository::class, $fileRepositoryProphecy->reveal());
200 
202  $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
203  ‪$GLOBALS['BE_USER'] = $backendUserProphecy->reveal();
204  $backendUserProphecy->getPagePermsClause(Argument::cetera())->willReturn(' 1=1');
205 
206  $languageService = $this->prophesize(LanguageService::class);
207  ‪$GLOBALS['LANG'] = $languageService->reveal();
208  $languageService->sL(Argument::cetera())->willReturnArgument(0);
209 
210  $this->‪mockDatabaseConnection();
211 
213  $treeDataProviderProphecy = $this->prophesize(DatabaseTreeDataProvider::class);
214  GeneralUtility::addInstance(DatabaseTreeDataProvider::class, $treeDataProviderProphecy->reveal());
215 
217  $tableConfigurationTreeProphecy = $this->prophesize(TableConfigurationTree::class);
218  GeneralUtility::addInstance(TableConfigurationTree::class, $tableConfigurationTreeProphecy->reveal());
219  $tableConfigurationTreeProphecy->render()->willReturn([]);
220  $tableConfigurationTreeProphecy->setDataProvider(Argument::cetera())->shouldBeCalled();
221  $tableConfigurationTreeProphecy->setNodeRenderer(Argument::cetera())->shouldBeCalled();
222 
223  $input = [
224  'tableName' => 'aTable',
225  'effectivePid' => 42,
226  'databaseRow' => [
227  'uid' => 5,
228  'aField' => '1'
229  ],
230  'processedTca' => [
231  'columns' => [
232  'aField' => [
233  'config' => [
234  'type' => 'select',
235  'renderType' => 'selectTree',
236  'treeConfig' => [
237  'childrenField' => 'childrenField'
238  ],
239  'foreign_table' => 'foreignTable',
240  'items' => [
241  [ 'static item foo', 1, 'foo-icon' ],
242  [ 'static item bar', 2, 'bar-icon' ],
243  ],
244  'maxitems' => 1
245  ],
246  ],
247  ],
248  ],
249  'pageTsConfig' => [
250  'TCEFORM.' => [
251  'aTable.' => [
252  'aField.' => [
253  'config.' => [
254  'treeConfig.' => [
255  'rootUid' => '42',
256  'appearance.' => [
257  'expandAll' => 1,
258  'maxLevels' => 4,
259  'nonSelectableLevels' => '0,1',
260  ],
261  ],
262  ],
263  'altLabels.' => [
264  1 => 'alt static item foo',
265  2 => 'alt static item bar'
266  ],
267  'altIcons.' => [
268  1 => 'foo-alt-icon',
269  2 => 'bar-alt-icon'
270  ],
271  ],
272  ],
273  ],
274  ],
275  'selectTreeCompileItems' => true,
276  ];
277 
278  $result = (new ‪TcaSelectTreeItems())->addData($input);
279 
280  $treeDataProviderProphecy->setRootUid(42)->shouldHaveBeenCalled();
281  $treeDataProviderProphecy->setExpandAll(true)->shouldHaveBeenCalled();
282  $treeDataProviderProphecy->setLevelMaximum(4)->shouldHaveBeenCalled();
283  $treeDataProviderProphecy->setNonSelectableLevelList('0,1')->shouldHaveBeenCalled();
284 
285  $resultItems = $result['processedTca']['columns']['aField']['config']['items'];
286  self::assertEquals('alt static item foo', $resultItems[0]['name']);
287  self::assertEquals('foo-alt-icon', $resultItems[0]['icon']);
288  self::assertEquals('alt static item bar', $resultItems[1]['name']);
289  self::assertEquals('bar-alt-icon', $resultItems[1]['icon']);
290  }
291 }
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaSelectTreeItemsTest\addDataHandsPageTsConfigSettingsOverToTableConfigurationTree
‪addDataHandsPageTsConfigSettingsOverToTableConfigurationTree()
Definition: TcaSelectTreeItemsTest.php:188
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaSelectTreeItemsTest\addDataAddsTreeConfigurationForSelectTreeElement
‪addDataAddsTreeConfigurationForSelectTreeElement()
Definition: TcaSelectTreeItemsTest.php:117
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:35
‪TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider
Definition: DatabaseTreeDataProvider.php:36
‪TYPO3\CMS\Core\Tree\TableConfiguration\TableConfigurationTree
Definition: TableConfigurationTree.php:24
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:33
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:52
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaSelectTreeItems
Definition: TcaSelectTreeItems.php:39
‪TYPO3\CMS\Core\Resource\FileRepository
Definition: FileRepository.php:33
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaSelectTreeItemsTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: TcaSelectTreeItemsTest.php:47
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaSelectTreeItemsTest
Definition: TcaSelectTreeItemsTest.php:44
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaSelectTreeItemsTest\mockDatabaseConnection
‪mockDatabaseConnection()
Definition: TcaSelectTreeItemsTest.php:53
‪TYPO3\CMS\Core\Database\Query\Restriction\DefaultRestrictionContainer
Definition: DefaultRestrictionContainer.php:24
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider
Definition: DatabaseDefaultLanguagePageRowTest.php:18