‪TYPO3CMS  9.5
TcaSelectTreeItemsTest.php
Go to the documentation of this file.
1 <?php
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 Doctrine\DBAL\Driver\Statement;
18 use Prophecy\Argument;
19 use Prophecy\Prophecy\ObjectProphecy;
33 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
34 
42 class ‪TcaSelectTreeItemsTest extends UnitTestCase
43 {
47  protected ‪$resetSingletonInstances = true;
48 
53  protected function ‪mockDatabaseConnection()
54  {
55  $connectionProphet = $this->prophesize(Connection::class);
56  $connectionProphet->quote(Argument::cetera())->will(function ($arguments) {
57  return "'" . $arguments[0] . "'";
58  });
59  $connectionProphet->quoteIdentifier(Argument::cetera())->will(function ($arguments) {
60  return '`' . $arguments[0] . '`';
61  });
62 
64  $statementProphet = $this->prophesize(Statement::class);
65  $statementProphet->fetch()->shouldBeCalled();
66 
67  $restrictionProphet = $this->prophesize(DefaultRestrictionContainer::class);
68  $restrictionProphet->removeAll()->willReturn($restrictionProphet->reveal());
69  $restrictionProphet->add(Argument::cetera())->willReturn($restrictionProphet->reveal());
70 
71  $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
72  $queryBuilderProphet->expr()->willReturn(
73  GeneralUtility::makeInstance(ExpressionBuilder::class, $connectionProphet->reveal())
74  );
75  $queryBuilderProphet->getRestrictions()->willReturn($restrictionProphet->reveal());
76  $queryBuilderProphet->quoteIdentifier(Argument::cetera())->will(function ($arguments) {
77  return '`' . $arguments[0] . '`';
78  });
79 
80  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
81  $connectionPoolProphet->getConnectionForTable('foreignTable')
82  ->willReturn($connectionProphet->reveal());
83  $connectionPoolProphet->getQueryBuilderForTable('foreignTable')
84  ->shouldBeCalled()
85  ->willReturn($queryBuilderProphet->reveal());
86 
87  $queryBuilderProphet->select('foreignTable.uid')
88  ->shouldBeCalled()
89  ->willReturn($queryBuilderProphet->reveal());
90  $queryBuilderProphet->from('foreignTable')
91  ->shouldBeCalled()
92  ->willReturn($queryBuilderProphet->reveal());
93  $queryBuilderProphet->from('pages')
94  ->shouldBeCalled()
95  ->willReturn($queryBuilderProphet->reveal());
96  $queryBuilderProphet->where('')
97  ->shouldBeCalled()
98  ->willReturn($queryBuilderProphet->reveal());
99  $queryBuilderProphet->andWhere(' 1=1')
100  ->shouldBeCalled()
101  ->willReturn($queryBuilderProphet->reveal());
102  $queryBuilderProphet->andWhere('`pages.uid` = `foreignTable.pid`')
103  ->shouldBeCalled()
104  ->willReturn($queryBuilderProphet->reveal());
105  $queryBuilderProphet->execute()
106  ->shouldBeCalled()
107  ->willReturn($statementProphet->reveal());
108 
109  // Two instances are needed due to the push/pop behavior of addInstance()
110  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
111  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
112  }
113 
118  {
119  ‪$GLOBALS['TCA']['foreignTable'] = [];
120 
121  $iconFactoryProphecy = $this->prophesize(IconFactory::class);
122  GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
123  GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
124 
125  $fileRepositoryProphecy = $this->prophesize(FileRepository::class);
126  $fileRepositoryProphecy->findByRelation(Argument::cetera())->shouldNotBeCalled();
127  GeneralUtility::setSingletonInstance(FileRepository::class, $fileRepositoryProphecy->reveal());
128 
130  $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
131  ‪$GLOBALS['BE_USER'] = $backendUserProphecy->reveal();
132  $backendUserProphecy->getPagePermsClause(Argument::cetera())->willReturn(' 1=1');
133 
134  $languageService = $this->prophesize(LanguageService::class);
135  ‪$GLOBALS['LANG'] = $languageService->reveal();
136  $languageService->sL(Argument::cetera())->willReturnArgument(0);
137 
138  $this->‪mockDatabaseConnection();
139 
141  $treeDataProviderProphecy = $this->prophesize(DatabaseTreeDataProvider::class);
142  GeneralUtility::addInstance(DatabaseTreeDataProvider::class, $treeDataProviderProphecy->reveal());
143 
145  $tableConfigurationTreeProphecy = $this->prophesize(TableConfigurationTree::class);
146  GeneralUtility::addInstance(TableConfigurationTree::class, $tableConfigurationTreeProphecy->reveal());
147  $tableConfigurationTreeProphecy->setDataProvider(Argument::cetera())->shouldBeCalled();
148  $tableConfigurationTreeProphecy->setNodeRenderer(Argument::cetera())->shouldBeCalled();
149  $tableConfigurationTreeProphecy->render()->shouldBeCalled()->willReturn(['fake', 'tree', 'data']);
150 
151  $input = [
152  'tableName' => 'aTable',
153  'effectivePid' => 42,
154  'databaseRow' => [
155  'uid' => 5,
156  'aField' => '1'
157  ],
158  'processedTca' => [
159  'columns' => [
160  'aField' => [
161  'config' => [
162  'type' => 'select',
163  'renderType' => 'selectTree',
164  'treeConfig' => [
165  'childrenField' => 'childrenField'
166  ],
167  'foreign_table' => 'foreignTable',
168  'items' => [],
169  'maxitems' => 1
170  ],
171  ],
172  ],
173  ],
174  'selectTreeCompileItems' => true,
175  ];
176 
177  $expected = $input;
178  $expected['databaseRow']['aField'] = ['1'];
179  $expected['processedTca']['columns']['aField']['config']['items'] = [
180  'fake', 'tree', 'data',
181  ];
182  $this->assertEquals($expected, (new ‪TcaSelectTreeItems)->addData($input));
183  }
184 
189  {
190  ‪$GLOBALS['TCA']['foreignTable'] = [];
191 
192  $iconFactoryProphecy = $this->prophesize(IconFactory::class);
193  GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
194  GeneralUtility::addInstance(IconFactory::class, $iconFactoryProphecy->reveal());
195 
196  $fileRepositoryProphecy = $this->prophesize(FileRepository::class);
197  $fileRepositoryProphecy->findByRelation(Argument::cetera())->shouldNotBeCalled();
198  GeneralUtility::setSingletonInstance(FileRepository::class, $fileRepositoryProphecy->reveal());
199 
201  $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
202  ‪$GLOBALS['BE_USER'] = $backendUserProphecy->reveal();
203  $backendUserProphecy->getPagePermsClause(Argument::cetera())->willReturn(' 1=1');
204 
205  $languageService = $this->prophesize(LanguageService::class);
206  ‪$GLOBALS['LANG'] = $languageService->reveal();
207  $languageService->sL(Argument::cetera())->willReturnArgument(0);
208 
209  $this->‪mockDatabaseConnection();
210 
212  $treeDataProviderProphecy = $this->prophesize(DatabaseTreeDataProvider::class);
213  GeneralUtility::addInstance(DatabaseTreeDataProvider::class, $treeDataProviderProphecy->reveal());
214 
216  $tableConfigurationTreeProphecy = $this->prophesize(TableConfigurationTree::class);
217  GeneralUtility::addInstance(TableConfigurationTree::class, $tableConfigurationTreeProphecy->reveal());
218  $tableConfigurationTreeProphecy->render()->willReturn([]);
219  $tableConfigurationTreeProphecy->setDataProvider(Argument::cetera())->shouldBeCalled();
220  $tableConfigurationTreeProphecy->setNodeRenderer(Argument::cetera())->shouldBeCalled();
221 
222  $input = [
223  'tableName' => 'aTable',
224  'effectivePid' => 42,
225  'databaseRow' => [
226  'uid' => 5,
227  'aField' => '1'
228  ],
229  'processedTca' => [
230  'columns' => [
231  'aField' => [
232  'config' => [
233  'type' => 'select',
234  'renderType' => 'selectTree',
235  'treeConfig' => [
236  'childrenField' => 'childrenField'
237  ],
238  'foreign_table' => 'foreignTable',
239  'items' => [],
240  'maxitems' => 1
241  ],
242  ],
243  ],
244  ],
245  'pageTsConfig' => [
246  'TCEFORM.' => [
247  'aTable.' => [
248  'aField.' => [
249  'config.' => [
250  'treeConfig.' => [
251  'rootUid' => '42',
252  'appearance.' => [
253  'expandAll' => 1,
254  'maxLevels' => 4,
255  'nonSelectableLevels' => '0,1',
256  ],
257  ],
258  ],
259  ],
260  ],
261  ],
262  ],
263  'selectTreeCompileItems' => true,
264  ];
265 
266  (new ‪TcaSelectTreeItems)->addData($input);
267 
268  $treeDataProviderProphecy->setRootUid(42)->shouldHaveBeenCalled();
269  $treeDataProviderProphecy->setExpandAll(true)->shouldHaveBeenCalled();
270  $treeDataProviderProphecy->setLevelMaximum(4)->shouldHaveBeenCalled();
271  $treeDataProviderProphecy->setNonSelectableLevelList('0,1')->shouldHaveBeenCalled();
272  }
273 }
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaSelectTreeItemsTest\addDataHandsPageTsConfigSettingsOverToTableConfigurationTree
‪addDataHandsPageTsConfigSettingsOverToTableConfigurationTree()
Definition: TcaSelectTreeItemsTest.php:187
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaSelectTreeItemsTest\addDataAddsTreeConfigurationForSelectTreeElement
‪addDataAddsTreeConfigurationForSelectTreeElement()
Definition: TcaSelectTreeItemsTest.php:116
‪TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder
Definition: ExpressionBuilder.php:33
‪TYPO3\CMS\Core\Tree\TableConfiguration\DatabaseTreeDataProvider
Definition: DatabaseTreeDataProvider.php:30
‪TYPO3\CMS\Core\Tree\TableConfiguration\TableConfigurationTree
Definition: TableConfigurationTree.php:21
‪TYPO3\CMS\Core\Imaging\IconFactory
Definition: IconFactory.php:31
‪TYPO3\CMS\Core\Database\Query\QueryBuilder
Definition: QueryBuilder.php:47
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaSelectTreeItems
Definition: TcaSelectTreeItems.php:38
‪TYPO3\CMS\Core\Resource\FileRepository
Definition: FileRepository.php:32
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:45
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:31
‪$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:46
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaSelectTreeItemsTest
Definition: TcaSelectTreeItemsTest.php:43
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:29
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaSelectTreeItemsTest\mockDatabaseConnection
‪mockDatabaseConnection()
Definition: TcaSelectTreeItemsTest.php:52
‪TYPO3\CMS\Core\Database\Query\Restriction\DefaultRestrictionContainer
Definition: DefaultRestrictionContainer.php:22
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider
Definition: DatabaseDefaultLanguagePageRowTest.php:3