[!!!][TASK] Remove support for non namespaced classes in Extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Scheduler / FieldProviderTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Scheduler;
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 TYPO3\CMS\Extbase\Mvc\Cli\Command;
18 use TYPO3\CMS\Extbase\Mvc\Cli\CommandManager;
19 use TYPO3\CMS\Extbase\Scheduler\FieldProvider;
20 use TYPO3\CMS\Extbase\Scheduler\Task;
21 use TYPO3\CMS\Extbase\Tests\Fixture\DummyController;
22 use TYPO3\CMS\Extbase\Tests\MockACommandController;
23 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
24
25 /**
26 * Test case
27 */
28 class FieldProviderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
29 {
30 /**
31 * @test
32 */
33 public function getCommandControllerActionFieldFetchesCorrectClassNames()
34 {
35
36 /** @var Command|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $command1 */
37 $command1 = $this->getAccessibleMock(Command::class, [], [], '', false);
38 $command1->expects($this->once())->method('isInternal')->will($this->returnValue(false));
39 $command1->expects($this->once())->method('isCliOnly')->will($this->returnValue(false));
40 $command1->expects($this->once())->method('getControllerClassName')->will($this->returnValue(MockACommandController::class));
41 $command1->expects($this->once())->method('getControllerCommandName')->will($this->returnValue('FuncA'));
42 $command1->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('extbase:mocka:funca'));
43
44 /** @var Command|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $command2 */
45 $command2 = $this->getAccessibleMock(Command::class, [], [], '', false);
46 $command2->expects($this->once())->method('isInternal')->will($this->returnValue(false));
47 $command2->expects($this->once())->method('isCliOnly')->will($this->returnValue(false));
48 $command2->expects($this->once())->method('getControllerClassName')->will($this->returnValue('Acme\\Mypkg\\Command\\MockBCommandController'));
49 $command2->expects($this->once())->method('getControllerCommandName')->will($this->returnValue('FuncB'));
50 $command2->expects($this->once())->method('getCommandIdentifier')->will($this->returnValue('mypkg:mockb:funcb'));
51
52 /** @var CommandManager|\PHPUnit_Framework_MockObject_MockObject $commandManager */
53 $commandManager = $this->getMockBuilder(CommandManager::class)
54 ->setMethods(['getAvailableCommands'])
55 ->getMock();
56 $commandManager->expects($this->any())->method('getAvailableCommands')->will($this->returnValue([$command1, $command2]));
57
58 /** @var FieldProvider|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $fieldProvider */
59 $fieldProvider = $this->getAccessibleMock(
60 FieldProvider::class,
61 ['getActionLabel'],
62 [],
63 '',
64 false
65 );
66 $fieldProvider->_set('commandManager', $commandManager);
67 $fieldProvider->expects($this->once())->method('getActionLabel')->will($this->returnValue('some label'));
68 $actualResult = $fieldProvider->_call('getCommandControllerActionField', []);
69 $this->assertContains('<option title="test" value="extbase:mocka:funca">Extbase MockA: FuncA</option>', $actualResult['code']);
70 $this->assertContains('<option title="test" value="mypkg:mockb:funcb">Mypkg MockB: FuncB</option>', $actualResult['code']);
71 }
72
73 /**
74 * @test
75 */
76 public function getCommandControllerActionFieldSkipsInternalCommands()
77 {
78 /** @var Command|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $command1 */
79 $command = $this->getAccessibleMock(Command::class, [], [], '', false);
80 $command->method('isInternal')->will($this->returnValue(true));
81 $command->method('isCliOnly')->will($this->returnValue(false));
82 $command->method('getControllerClassName')->will($this->returnValue(MockACommandController::class));
83 $command->method('getControllerCommandName')->will($this->returnValue('FuncA'));
84 $command->method('getCommandIdentifier')->will($this->returnValue('extbase:mocka:funca'));
85
86 /** @var CommandManager|\PHPUnit_Framework_MockObject_MockObject $commandManager */
87 $commandManager = $this->getMockBuilder(CommandManager::class)
88 ->setMethods(['getAvailableCommands'])
89 ->getMock();
90 $commandManager->expects($this->any())->method('getAvailableCommands')->will($this->returnValue([$command]));
91
92 /** @var FieldProvider|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $fieldProvider */
93 $fieldProvider = $this->getAccessibleMock(
94 FieldProvider::class,
95 ['getActionLabel'],
96 [],
97 '',
98 false
99 );
100 $fieldProvider->_set('commandManager', $commandManager);
101 $fieldProvider->expects($this->once())->method('getActionLabel')->will($this->returnValue('some label'));
102 $actualResult = $fieldProvider->_call('getCommandControllerActionField', []);
103 $this->assertNotContains('<option title="test" value="extbase:mocka:funca">Extbase MockA: FuncA</option>', $actualResult['code']);
104 }
105
106 /**
107 * @test
108 */
109 public function getCommandControllerActionFieldSkipsCliOnlyCommands()
110 {
111 /** @var Command|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $command1 */
112 $command = $this->getAccessibleMock(Command::class, [], [], '', false);
113 $command->method('isInternal')->will($this->returnValue(false));
114 $command->method('isCliOnly')->will($this->returnValue(true));
115 $command->method('getControllerClassName')->will($this->returnValue(MockACommandController::class));
116 $command->method('getControllerCommandName')->will($this->returnValue('FuncA'));
117 $command->method('getCommandIdentifier')->will($this->returnValue('extbase:mocka:funca'));
118
119 /** @var CommandManager|\PHPUnit_Framework_MockObject_MockObject $commandManager */
120 $commandManager = $this->getMockBuilder(CommandManager::class)
121 ->setMethods(['getAvailableCommands'])
122 ->getMock();
123 $commandManager->expects($this->any())->method('getAvailableCommands')->will($this->returnValue([$command]));
124
125 /** @var FieldProvider|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $fieldProvider */
126 $fieldProvider = $this->getAccessibleMock(
127 FieldProvider::class,
128 ['getActionLabel'],
129 [],
130 '',
131 false
132 );
133 $fieldProvider->_set('commandManager', $commandManager);
134 $fieldProvider->expects($this->once())->method('getActionLabel')->will($this->returnValue('some label'));
135 $actualResult = $fieldProvider->_call('getCommandControllerActionField', []);
136 $this->assertNotContains('<option title="test" value="extbase:mocka:funca">Extbase MockA: FuncA</option>', $actualResult['code']);
137 }
138
139 /**
140 * @test
141 */
142 public function constructResolvesExtensionNameFromNamespaced()
143 {
144 $mockController = new DummyController();
145 $expectedResult = 'Extbase';
146 $actualResult = $mockController->getExtensionName();
147 $this->assertSame($expectedResult, $actualResult);
148 }
149
150 /**
151 * @test
152 */
153 public function validateAdditionalFieldsReturnsTrue()
154 {
155 /** @var FieldProvider|\PHPUnit_Framework_MockObject_MockObject|\|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $fieldProvider */
156 $fieldProvider = $this->getAccessibleMock(
157 FieldProvider::class,
158 ['dummy'],
159 [],
160 '',
161 false
162 );
163 $submittedData = [];
164 /** @var SchedulerModuleController $schedulerModule */
165 $schedulerModule = $this->createMock(SchedulerModuleController::class);
166 $this->assertTrue($fieldProvider->validateAdditionalFields($submittedData, $schedulerModule));
167 }
168
169 /**
170 * @test
171 */
172 public function getAdditionalFieldsRendersRightHtml()
173 {
174 $this->markTestSkipped('Incomplete mocking in a complex scenario. This should be a functional test');
175
176 /** @var Command|\PHPUnit_Framework_MockObject_MockObject $command1 */
177 $command1 = $this->getAccessibleMock(Command::class, [], [], '', false);
178 $command1->expects($this->once())->method('isInternal')->will($this->returnValue(false));
179 $command1->expects($this->once())->method('getControllerClassName')->will($this->returnValue(MockACommandController::class));
180 $command1->expects($this->once())->method('getControllerCommandName')->will($this->returnValue('FuncA'));
181 $command1->expects($this->any())->method('getCommandIdentifier')->will($this->returnValue('extbase:mocka:funca'));
182 $command1->expects($this->once())->method('getArgumentDefinitions')->will($this->returnValue([]));
183
184 /** @var Command|\PHPUnit_Framework_MockObject_MockObject $command2 */
185 $command2 = $this->getAccessibleMock(Command::class, [], [], '', false);
186 $command2->expects($this->once())->method('isInternal')->will($this->returnValue(false));
187 $command2->expects($this->once())->method('getControllerClassName')->will($this->returnValue('Acme\\Mypkg\\Command\\MockBCommandController'));
188 $command2->expects($this->once())->method('getControllerCommandName')->will($this->returnValue('FuncB'));
189 $command2->expects($this->any())->method('getCommandIdentifier')->will($this->returnValue('mypkg:mockb:funcb'));
190
191 /** @var Command|\PHPUnit_Framework_MockObject_MockObject $command3 */
192 $command3 = $this->getAccessibleMock(Command::class, [], [], '', false);
193 $command3->expects($this->once())->method('isInternal')->will($this->returnValue(false));
194 $command3->expects($this->once())->method('getControllerClassName')->will($this->returnValue('Tx_Extbase_Command_MockCCommandController'));
195 $command3->expects($this->once())->method('getControllerCommandName')->will($this->returnValue('FuncC'));
196 $command3->expects($this->any())->method('getCommandIdentifier')->will($this->returnValue('extbase:mockc:funcc'));
197
198 /** @var CommandManager|\PHPUnit_Framework_MockObject_MockObject $commandManager */
199 $commandManager = $this->getMockBuilder(CommandManager::class)
200 ->setMethods(['getAvailableCommands'])
201 ->getMock();
202 $commandManager->expects($this->any())->method('getAvailableCommands')->will($this->returnValue([$command1, $command2, $command3]));
203
204 /** @var FieldProvider|\PHPUnit_Framework_MockObject_MockObject|\|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $fieldProvider */
205 $fieldProvider = $this->getAccessibleMock(
206 FieldProvider::class,
207 ['getActionLabel', 'getArgumentLabel', 'getCommandControllerActionArgumentFields'],
208 [],
209 '',
210 false
211 );
212 $fieldProvider->_set('commandManager', $commandManager);
213 $actionLabel = 'action label string';
214 $argumentLabel = 'argument label string';
215 $fieldProvider->expects($this->any())->method('getActionLabel')->will($this->returnValue($actionLabel));
216 $fieldProvider->expects($this->any())->method('getArgumentLabel')->will($this->returnValue($argumentLabel));
217 $argArray['arg'] = [
218 'code' => '<input type="text" name="tx_scheduler[task_extbase][arguments][arg]" value="1" /> ',
219 'label' => $argumentLabel
220 ];
221 $fieldProvider->expects($this->any())->method('getCommandControllerActionArgumentFields')->will($this->returnValue($argArray));
222 $expectedAdditionalFields = [
223 'action' => [
224 'code' => '<select name="tx_scheduler[task_extbase][action]">' . LF
225 . '<option title="test" value="extbase:mocka:funca" selected="selected">Extbase MockA: FuncA</option>' . LF
226 . '<option title="test" value="mypkg:mockb:funcb">Mypkg MockB: FuncB</option>' . LF
227 . '<option title="test" value="extbase:mockc:funcc">Extbase MockC: FuncC</option>' . LF
228 . '</select>',
229 'label' => $actionLabel
230 ],
231 'description' => [
232 'code' => '',
233 'label' => '<strong></strong>'
234 ],
235 'arg' => [
236 'code' => '<input type="text" name="tx_scheduler[task_extbase][arguments][arg]" value="1" /> ',
237 'label' => $argumentLabel
238 ]
239 ];
240
241 $taskInfo = [];
242 /** @var Task $task */
243 $task = new Task();
244 $task->setCommandIdentifier($command1->getCommandIdentifier());
245 /** @var SchedulerModuleController $schedulerModule */
246 $schedulerModule = $this->createMock(SchedulerModuleController::class);
247
248 $this->assertEquals($expectedAdditionalFields, $fieldProvider->getAdditionalFields($taskInfo, $task, $schedulerModule));
249 }
250 }