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