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