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