[TASK] Use ServerRequestInterface in SchedulerModuleController
[Packages/TYPO3.CMS.git] / typo3 / sysext / recycler / Tests / Unit / Task / CleanerFieldProviderTest.php
1 <?php
2 namespace TYPO3\CMS\Recycler\Tests\Unit\Task;
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\Core\Localization\LanguageService;
18 use TYPO3\CMS\Core\Messaging\FlashMessage;
19 use TYPO3\CMS\Recycler\Task\CleanerFieldProvider;
20 use TYPO3\CMS\Recycler\Task\CleanerTask;
21 use TYPO3\CMS\Scheduler\Controller\SchedulerModuleController;
22
23 /**
24 * Testcase
25 */
26 class CleanerFieldProviderTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
27 {
28 /**
29 * @var CleanerFieldProvider
30 */
31 protected $subject;
32
33 /**
34 * Sets up an instance of \TYPO3\CMS\Recycler\Task\CleanerFieldProvider
35 */
36 protected function setUp()
37 {
38 $languageServiceMock = $this->getMockBuilder(LanguageService::class)
39 ->setMethods(['sL'])
40 ->disableOriginalConstructor()
41 ->getMock();
42 $languageServiceMock->expects($this->any())->method('sL')->will($this->returnValue('titleTest'));
43 $this->subject = $this->getMockBuilder(CleanerFieldProvider::class)
44 ->setMethods(['getLanguageService', 'addMessage'])
45 ->getMock();
46 $this->subject->expects($this->any())->method('getLanguageService')->willReturn($languageServiceMock);
47 }
48
49 /**
50 * @param array $mockedMethods
51 * @return \PHPUnit_Framework_MockObject_MockObject|SchedulerModuleController
52 */
53 protected function getScheduleModuleControllerMock($mockedMethods = [])
54 {
55 $languageServiceMock = $this->getMockBuilder(LanguageService::class)
56 ->setMethods(['sL'])
57 ->disableOriginalConstructor()
58 ->getMock();
59 $languageServiceMock->expects($this->any())->method('sL')->will($this->returnValue('titleTest'));
60
61 $mockedMethods = array_merge(['getLanguageService'], $mockedMethods);
62 $scheduleModuleMock = $this->getMockBuilder(SchedulerModuleController::class)
63 ->setMethods($mockedMethods)
64 ->disableOriginalConstructor()
65 ->getMock();
66 $scheduleModuleMock->expects($this->any())->method('getLanguageService')->willReturn($languageServiceMock);
67
68 return $scheduleModuleMock;
69 }
70
71 /**
72 * @return array
73 */
74 public function validateAdditionalFieldsLogsPeriodErrorDataProvider()
75 {
76 return [
77 ['abc'],
78 [$this->getMockBuilder(CleanerTask::class)->disableOriginalConstructor()->getMock()],
79 [null],
80 [''],
81 [0],
82 ['1234abc']
83 ];
84 }
85
86 /**
87 * @param mixed $period
88 * @test
89 * @dataProvider validateAdditionalFieldsLogsPeriodErrorDataProvider
90 */
91 public function validateAdditionalFieldsLogsPeriodError($period)
92 {
93 $submittedData = [
94 'RecyclerCleanerPeriod' => $period,
95 'RecyclerCleanerTCA' => ['pages']
96 ];
97
98 $scheduleModuleControllerMock = $this->getScheduleModuleControllerMock();
99 $this->subject->expects($this->atLeastOnce())
100 ->method('addMessage')
101 ->with($this->equalTo('titleTest'), FlashMessage::ERROR);
102
103 $this->subject->validateAdditionalFields($submittedData, $scheduleModuleControllerMock);
104 }
105
106 /**
107 * @return array
108 */
109 public function validateAdditionalFieldsDataProvider()
110 {
111 return [
112 ['abc'],
113 [$this->getMockBuilder(CleanerTask::class)->disableOriginalConstructor()->getMock()],
114 [null],
115 [123]
116 ];
117 }
118
119 /**
120 * @param mixed $table
121 * @test
122 * @dataProvider validateAdditionalFieldsDataProvider
123 */
124 public function validateAdditionalFieldsLogsTableError($table)
125 {
126 $submittedData = [
127 'RecyclerCleanerPeriod' => 14,
128 'RecyclerCleanerTCA' => $table
129 ];
130
131 $this->subject->validateAdditionalFields($submittedData, $this->getScheduleModuleControllerMock());
132 }
133
134 /**
135 * @test
136 */
137 public function validateAdditionalFieldsIsTrueIfValid()
138 {
139 $submittedData = [
140 'RecyclerCleanerPeriod' => 14,
141 'RecyclerCleanerTCA' => ['pages']
142 ];
143
144 $scheduleModuleControllerMock = $this->getScheduleModuleControllerMock();
145 $GLOBALS['TCA']['pages'] = ['foo' => 'bar'];
146 $this->assertTrue($this->subject->validateAdditionalFields($submittedData, $scheduleModuleControllerMock));
147 }
148
149 /**
150 * @test
151 */
152 public function saveAdditionalFieldsSavesFields()
153 {
154 $submittedData = [
155 'RecyclerCleanerPeriod' => 14,
156 'RecyclerCleanerTCA' => ['pages']
157 ];
158
159 $taskMock = $this->getMockBuilder(CleanerTask::class)->getMock();
160
161 $taskMock->expects($this->once())
162 ->method('setTcaTables')
163 ->with($this->equalTo(['pages']));
164
165 $taskMock->expects($this->once())
166 ->method('setPeriod')
167 ->with($this->equalTo(14));
168
169 $this->subject->saveAdditionalFields($submittedData, $taskMock);
170 }
171 }