TYPO3 CMS  TYPO3_8-7
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 
30 
38 class TcaSelectTreeItemsTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
39 {
43  protected $subject;
44 
48  protected $singletonInstances = [];
49 
53  public function setUp()
54  {
55  $this->singletonInstances = GeneralUtility::getSingletonInstances();
56  $this->subject = new TcaSelectTreeItems();
57  }
58 
59  protected function tearDown()
60  {
62  GeneralUtility::resetSingletonInstances($this->singletonInstances);
63  parent::tearDown();
64  }
65 
70  protected function mockDatabaseConnection()
71  {
72  $connectionProphet = $this->prophesize(Connection::class);
73  $connectionProphet->quote(Argument::cetera())->will(function ($arguments) {
74  return "'" . $arguments[0] . "'";
75  });
76  $connectionProphet->quoteIdentifier(Argument::cetera())->will(function ($arguments) {
77  return '`' . $arguments[0] . '`';
78  });
79 
81  $statementProphet = $this->prophesize(Statement::class);
82  $statementProphet->fetch()->shouldBeCalled();
83 
84  $restrictionProphet = $this->prophesize(DefaultRestrictionContainer::class);
85  $restrictionProphet->removeAll()->willReturn($restrictionProphet->reveal());
86  $restrictionProphet->add(Argument::cetera())->willReturn($restrictionProphet->reveal());
87 
88  $queryBuilderProphet = $this->prophesize(QueryBuilder::class);
89  $queryBuilderProphet->expr()->willReturn(
90  GeneralUtility::makeInstance(ExpressionBuilder::class, $connectionProphet->reveal())
91  );
92  $queryBuilderProphet->getRestrictions()->willReturn($restrictionProphet->reveal());
93  $queryBuilderProphet->quoteIdentifier(Argument::cetera())->will(function ($arguments) {
94  return '`' . $arguments[0] . '`';
95  });
96 
97  $connectionPoolProphet = $this->prophesize(ConnectionPool::class);
98  $connectionPoolProphet->getConnectionForTable('foreignTable')
99  ->willReturn($connectionProphet->reveal());
100  $connectionPoolProphet->getQueryBuilderForTable('foreignTable')
101  ->shouldBeCalled()
102  ->willReturn($queryBuilderProphet->reveal());
103 
104  $queryBuilderProphet->select('foreignTable.uid')
105  ->shouldBeCalled()
106  ->willReturn($queryBuilderProphet->reveal());
107  $queryBuilderProphet->from('foreignTable')
108  ->shouldBeCalled()
109  ->willReturn($queryBuilderProphet->reveal());
110  $queryBuilderProphet->from('pages')
111  ->shouldBeCalled()
112  ->willReturn($queryBuilderProphet->reveal());
113  $queryBuilderProphet->where('')
114  ->shouldBeCalled()
115  ->willReturn($queryBuilderProphet->reveal());
116  $queryBuilderProphet->andWhere(' 1=1')
117  ->shouldBeCalled()
118  ->willReturn($queryBuilderProphet->reveal());
119  $queryBuilderProphet->andWhere('`pages.uid` = `foreignTable.pid`')
120  ->shouldBeCalled()
121  ->willReturn($queryBuilderProphet->reveal());
122  $queryBuilderProphet->execute()
123  ->shouldBeCalled()
124  ->willReturn($statementProphet->reveal());
125 
126  // Two instances are needed due to the push/pop behavior of addInstance()
127  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
128  GeneralUtility::addInstance(ConnectionPool::class, $connectionPoolProphet->reveal());
129  }
130 
134  public function addDataAddsTreeConfigurationForExtJs()
135  {
136  $GLOBALS['TCA']['foreignTable'] = [];
137 
139  $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
140  $GLOBALS['BE_USER'] = $backendUserProphecy->reveal();
141  $backendUserProphecy->getPagePermsClause(Argument::cetera())->willReturn(' 1=1');
142 
143  $this->mockDatabaseConnection();
144 
146  $treeDataProviderProphecy = $this->prophesize(DatabaseTreeDataProvider::class);
147  GeneralUtility::addInstance(DatabaseTreeDataProvider::class, $treeDataProviderProphecy->reveal());
148 
150  $tableConfigurationTreeProphecy = $this->prophesize(TableConfigurationTree::class);
151  GeneralUtility::addInstance(TableConfigurationTree::class, $tableConfigurationTreeProphecy->reveal());
152  $tableConfigurationTreeProphecy->setDataProvider(Argument::cetera())->shouldBeCalled();
153  $tableConfigurationTreeProphecy->setNodeRenderer(Argument::cetera())->shouldBeCalled();
154  $tableConfigurationTreeProphecy->render()->shouldBeCalled()->willReturn(['fake', 'tree', 'data']);
155 
156  $input = [
157  'tableName' => 'aTable',
158  'databaseRow' => [
159  'aField' => '1'
160  ],
161  'processedTca' => [
162  'columns' => [
163  'aField' => [
164  'config' => [
165  'type' => 'select',
166  'renderType' => 'selectTree',
167  'treeConfig' => [
168  'childrenField' => 'childrenField'
169  ],
170  'foreign_table' => 'foreignTable',
171  'items' => [],
172  'maxitems' => 1
173  ],
174  ],
175  ],
176  ],
177  'selectTreeCompileItems' => true,
178  ];
179 
180  $expected = $input;
181  $expected['databaseRow']['aField'] = ['1'];
182  $expected['processedTca']['columns']['aField']['config']['items'] = [
183  'fake', 'tree', 'data',
184  ];
185  $this->assertEquals($expected, $this->subject->addData($input));
186  }
187 
191  public function addDataHandsPageTsConfigSettingsOverToTableConfigurationTree()
192  {
193  $GLOBALS['TCA']['foreignTable'] = [];
194 
196  $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
197  $GLOBALS['BE_USER'] = $backendUserProphecy->reveal();
198  $backendUserProphecy->getPagePermsClause(Argument::cetera())->willReturn(' 1=1');
199 
200  $this->mockDatabaseConnection();
201 
203  $treeDataProviderProphecy = $this->prophesize(DatabaseTreeDataProvider::class);
204  GeneralUtility::addInstance(DatabaseTreeDataProvider::class, $treeDataProviderProphecy->reveal());
205 
207  $tableConfigurationTreeProphecy = $this->prophesize(TableConfigurationTree::class);
208  GeneralUtility::addInstance(TableConfigurationTree::class, $tableConfigurationTreeProphecy->reveal());
209  $tableConfigurationTreeProphecy->render()->willReturn([]);
210  $tableConfigurationTreeProphecy->setDataProvider(Argument::cetera())->shouldBeCalled();
211  $tableConfigurationTreeProphecy->setNodeRenderer(Argument::cetera())->shouldBeCalled();
212 
213  $input = [
214  'tableName' => 'aTable',
215  'databaseRow' => [
216  'aField' => '1'
217  ],
218  'processedTca' => [
219  'columns' => [
220  'aField' => [
221  'config' => [
222  'type' => 'select',
223  'renderType' => 'selectTree',
224  'treeConfig' => [
225  'childrenField' => 'childrenField'
226  ],
227  'foreign_table' => 'foreignTable',
228  'items' => [],
229  'maxitems' => 1
230  ],
231  ],
232  ],
233  ],
234  'pageTsConfig' => [
235  'TCEFORM.' => [
236  'aTable.' => [
237  'aField.' => [
238  'config.' => [
239  'treeConfig.' => [
240  'rootUid' => '42',
241  'appearance.' => [
242  'expandAll' => 1,
243  'maxLevels' => 4,
244  'nonSelectableLevels' => '0,1',
245  ],
246  ],
247  ],
248  ],
249  ],
250  ],
251  ],
252  'selectTreeCompileItems' => true,
253  ];
254 
255  $this->subject->addData($input);
256 
257  $treeDataProviderProphecy->setRootUid(42)->shouldHaveBeenCalled();
258  $treeDataProviderProphecy->setExpandAll(true)->shouldHaveBeenCalled();
259  $treeDataProviderProphecy->setLevelMaximum(4)->shouldHaveBeenCalled();
260  $treeDataProviderProphecy->setNonSelectableLevelList('0,1')->shouldHaveBeenCalled();
261  }
262 }
static addInstance($className, $instance)
static makeInstance($className,... $constructorArguments)
static resetSingletonInstances(array $newSingletonInstances)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']