[TASK] Removes eval() in scheduler unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Tests / Unit / CronCommand / NormalizeCommandTest.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Tests\Unit\CronCommand;
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\Tests\UnitTestCase;
18 use TYPO3\CMS\Scheduler\Tests\Unit\CronCommand\AccessibleProxies\NormalizeCommandAccessibleProxy;
19 use TYPO3\CMS\Scheduler\CronCommand\NormalizeCommand;
20
21 /**
22 * Test case
23 *
24 * @author Christian Kuhn <lolli@schwarzbu.ch>
25 */
26 class NormalizeCommandTest extends UnitTestCase {
27
28 /**
29 * @return array
30 */
31 static public function normalizeValidDataProvider() {
32 return array(
33 '@weekly' => array('@weekly', '0 0 * * 7'),
34 ' @weekly ' => array(' @weekly ', '0 0 * * 7'),
35 '* * * * *' => array('* * * * *', '* * * * *'),
36 '30 4 1,15 * 5' => array('30 4 1,15 * 5', '30 4 1,15 * 5'),
37 '5 0 * * *' => array('5 0 * * *', '5 0 * * *'),
38 '15 14 1 * *' => array('15 14 1 * *', '15 14 1 * *'),
39 '0 22 * * 1-5' => array('0 22 * * 1-5', '0 22 * * 1,2,3,4,5'),
40 '23 0-23/2 * * *' => array('23 0-23/2 * * *', '23 0,2,4,6,8,10,12,14,16,18,20,22 * * *'),
41 '5 4 * * sun' => array('5 4 * * sun', '5 4 * * 7'),
42 '0-3/2,7 0,4 20-22, feb,mar-jun/2,7 1-3,sun' => array('0-3/2,7 0,4 20-22 feb,mar-jun/2,7 1-3,sun', '0,2,7 0,4 20,21,22 2,3,5,7 1,2,3,7'),
43 '0-20/10 * * * *' => array('0-20/10 * * * *', '0,10,20 * * * *'),
44 '* * 2 * *' => array('* * 2 * *', '* * 2 * *'),
45 '* * 2,7 * *' => array('* * 2,7 * *', '* * 2,7 * *'),
46 '* * 2-4,10 * *' => array('* * 2-4,10 * *', '* * 2,3,4,10 * *'),
47 '* * */14 * *' => array('* * */14 * *', '* * 1,15,29 * *'),
48 '* * 2,4-6/2,*/14 * *' => array('* * 2,4-6/2,*/14 * *', '* * 1,2,4,6,15,29 * *'),
49 '* * * * 1' => array('* * * * 1', '* * * * 1'),
50 '0 0 * * 0' => array('0 0 * * 0', '0 0 * * 7'),
51 '0 0 * * 7' => array('0 0 * * 7', '0 0 * * 7'),
52 '* * 1,2 * 1' => array('* * 1,2 * 1', '* * 1,2 * 1')
53 );
54 }
55
56 /**
57 * @test
58 * @dataProvider normalizeValidDataProvider
59 * @param string $expression Cron command to test
60 * @param string $expected Expected result (normalized cron command syntax)
61 */
62 public function normalizeConvertsCronCommand($expression, $expected) {
63 $result = NormalizeCommand::normalize($expression);
64 $this->assertEquals($expected, $result);
65 }
66
67 /**
68 * @return array
69 */
70 static public function validSpecialKeywordsDataProvider() {
71 return array(
72 '@yearly' => array('@yearly', '0 0 1 1 *'),
73 '@annually' => array('@annually', '0 0 1 1 *'),
74 '@monthly' => array('@monthly', '0 0 1 * *'),
75 '@weekly' => array('@weekly', '0 0 * * 0'),
76 '@daily' => array('@daily', '0 0 * * *'),
77 '@midnight' => array('@midnight', '0 0 * * *'),
78 '@hourly' => array('@hourly', '0 * * * *')
79 );
80 }
81
82 /**
83 * @test
84 * @dataProvider validSpecialKeywordsDataProvider
85 * @param string $keyword Cron command keyword
86 * @param string $expectedCronCommand Expected result (normalized cron command syntax)
87 */
88 public function convertKeywordsToCronCommandConvertsValidKeywords($keyword, $expectedCronCommand) {
89 $result = NormalizeCommandAccessibleProxy::convertKeywordsToCronCommand($keyword);
90 $this->assertEquals($expectedCronCommand, $result);
91 }
92
93 /**
94 * @test
95 */
96 public function convertKeywordsToCronCommandReturnsUnchangedCommandIfKeywordWasNotFound() {
97 $invalidKeyword = 'foo';
98 $result = NormalizeCommandAccessibleProxy::convertKeywordsToCronCommand($invalidKeyword);
99 $this->assertEquals($invalidKeyword, $result);
100 }
101
102 /**
103 * @return array
104 */
105 public function normalizeFieldsValidDataProvider() {
106 return array(
107 '1-2 * * * *' => array('1-2 * * * *', '1,2 * * * *'),
108 '* 1-2 * * *' => array('* 1-2 * * *', '* 1,2 * * *'),
109 '* * 1-2 * *' => array('* * 1-2 * *', '* * 1,2 * *'),
110 '* * * 1-2 *' => array('* * * 1-2 *', '* * * 1,2 *'),
111 '* * * * 1-2' => array('* * * * 1-2', '* * * * 1,2')
112 );
113 }
114
115 /**
116 * @test
117 * @dataProvider normalizeFieldsValidDataProvider
118 * @param string $expression Cron command to normalize
119 * @param string $expected Expected result (normalized cron command syntax)
120 */
121 public function normalizeFieldsConvertsField($expression, $expected) {
122 $result = NormalizeCommandAccessibleProxy::normalizeFields($expression);
123 $this->assertEquals($expected, $result);
124 }
125
126 /**
127 * @return array
128 */
129 static public function normalizeMonthAndWeekdayFieldValidDataProvider() {
130 return array(
131 '* monthField' => array('*', TRUE, '*'),
132 'string 1 monthField' => array('1', TRUE, '1'),
133 'jan' => array('jan', TRUE, '1'),
134 'feb/2' => array('feb/2', TRUE, '2'),
135 'jan-feb/2' => array('jan-feb/2', TRUE, '1'),
136 '1-2 monthField' => array('1-2', TRUE, '1,2'),
137 '1-3/2,feb,may,6' => array('1-3/2,feb,may,6', TRUE, '1,2,3,5,6'),
138 '*/4' => array('*/4', TRUE, '1,5,9'),
139 '* !monthField' => array('*', FALSE, '*'),
140 'string 1, !monthField' => array('1', FALSE, '1'),
141 'fri' => array('fri', FALSE, '5'),
142 'sun' => array('sun', FALSE, '7'),
143 'string 0 for sunday' => array('0', FALSE, '7'),
144 '0,1' => array('0,1', FALSE, '1,7'),
145 '*/3' => array('*/3', FALSE, '1,4,7'),
146 'tue/2' => array('tue/2', FALSE, '2'),
147 '1-2 !monthField' => array('1-2', FALSE, '1,2'),
148 'tue-fri/2' => array('tue-fri/2', FALSE, '2,4'),
149 '1-3/2,tue,fri,6' => array('1-3/2,tue,fri,6', FALSE, '1,2,3,5,6')
150 );
151 }
152
153 /**
154 * @test
155 * @dataProvider normalizeMonthAndWeekdayFieldValidDataProvider
156 * @param string $expression Cron command partial expression for month and weekday fields
157 * @param bool $isMonthField Flag to designate month field or not
158 * @param string $expected Expected result (normalized months or weekdays)
159 */
160 public function normalizeMonthAndWeekdayFieldReturnsNormalizedListForValidExpression($expression, $isMonthField, $expected) {
161 $result = NormalizeCommandAccessibleProxy::normalizeMonthAndWeekdayField($expression, $isMonthField);
162 $this->assertSame($expected, $result);
163 }
164
165 /**
166 * @return array
167 */
168 static public function normalizeMonthAndWeekdayFieldInvalidDataProvider() {
169 return array(
170 'mon' => array('mon', TRUE),
171 '1-2/mon' => array('1-2/mon', TRUE),
172 '0,1' => array('0,1', TRUE),
173 'feb' => array('feb', FALSE),
174 '1-2/feb' => array('1-2/feb', FALSE),
175 '0-fri/2,7' => array('0-fri/2,7', FALSE, '2,4,7')
176 );
177 }
178
179 /**
180 * @test
181 * @dataProvider normalizeMonthAndWeekdayFieldInvalidDataProvider
182 * @expectedException \InvalidArgumentException
183 * @param string $expression Cron command partial expression for month and weekday fields (invalid)
184 * @param bool $isMonthField Flag to designate month field or not
185 */
186 public function normalizeMonthAndWeekdayFieldThrowsExceptionForInvalidExpression($expression, $isMonthField) {
187 NormalizeCommandAccessibleProxy::normalizeMonthAndWeekdayField($expression, $isMonthField);
188 }
189
190 /**
191 * @return array
192 */
193 static public function normalizeIntegerFieldValidDataProvider() {
194 return array(
195 '*' => array('*', '*'),
196 'string 2' => array('2', '2'),
197 'integer 3' => array(3, '3'),
198 'list of values' => array('1,2,3', '1,2,3'),
199 'unsorted list of values' => array('3,1,5', '1,3,5'),
200 'duplicate values' => array('0-2/2,2', '0,2'),
201 'additional field between steps' => array('1-3/2,2', '1,2,3'),
202 '2-4' => array('2-4', '2,3,4'),
203 'simple step 4/4' => array('4/4', '4'),
204 'step 2-7/5' => array('2-7/5', '2,7'),
205 'steps 4-12/4' => array('4-12/4', '4,8,12'),
206 '0-59/20' => array('0-59/20', '0,20,40'),
207 '*/20' => array('*/20', '0,20,40')
208 );
209 }
210
211 /**
212 * @test
213 * @dataProvider normalizeIntegerFieldValidDataProvider
214 * @param string $expression Cron command partial integer expression
215 * @param string $expected Expected result (normalized integer or integer list)
216 */
217 public function normalizeIntegerFieldReturnsNormalizedListForValidExpression($expression, $expected) {
218 $result = NormalizeCommandAccessibleProxy::normalizeIntegerField($expression);
219 $this->assertSame($expected, $result);
220 }
221
222 /**
223 * @return array
224 */
225 static public function normalizeIntegerFieldInvalidDataProvider() {
226 return array(
227 'string foo' => array('foo', 0, 59),
228 'empty string' => array('', 0, 59),
229 '4-3' => array('4-3', 0, 59),
230 '/2' => array('/2', 0, 59),
231 '/' => array('/', 0, 59),
232 'left bound too low' => array('2-4', 3, 4),
233 'right bound too high' => array('2-4', 2, 3),
234 'left and right bound' => array('2-5', 2, 4),
235 'element in list is lower than allowed' => array('2,1,4', 2, 4),
236 'element in list is higher than allowed' => array('2,5,4', 1, 4)
237 );
238 }
239
240 /**
241 * @test
242 * @dataProvider normalizeIntegerFieldInvalidDataProvider
243 * @expectedException \InvalidArgumentException
244 * @param string $expression Cron command partial integer expression (invalid)
245 * @param int $lowerBound Lower limit
246 * @param int $upperBound Upper limit
247 */
248 public function normalizeIntegerFieldThrowsExceptionForInvalidExpressions($expression, $lowerBound, $upperBound) {
249 NormalizeCommandAccessibleProxy::normalizeIntegerField($expression, $lowerBound, $upperBound);
250 }
251
252 /**
253 * @test
254 */
255 public function splitFieldsReturnsIntegerArrayWithFieldsSplitByWhitespace() {
256 $result = NormalizeCommandAccessibleProxy::splitFields('12,13 * 1-12/2,14 jan fri');
257 $expectedResult = array(
258 0 => '12,13',
259 1 => '*',
260 2 => '1-12/2,14',
261 3 => 'jan',
262 4 => 'fri'
263 );
264 $this->assertSame($expectedResult, $result);
265 }
266
267 /**
268 * @return array
269 */
270 static public function invalidCronCommandFieldsDataProvider() {
271 return array(
272 'empty string' => array(''),
273 'foo' => array('foo'),
274 'integer 4' => array(4),
275 'four fields' => array('* * * *'),
276 'six fields' => array('* * * * * *')
277 );
278 }
279
280 /**
281 * @test
282 * @expectedException \InvalidArgumentException
283 * @dataProvider invalidCronCommandFieldsDataProvider
284 * @param string $cronCommand Invalid cron command
285 */
286 public function splitFieldsThrowsExceptionIfCronCommandDoesNotContainFiveFields($cronCommand) {
287 NormalizeCommandAccessibleProxy::splitFields($cronCommand);
288 }
289
290 /**
291 * @return array
292 */
293 static public function validRangeDataProvider() {
294 return array(
295 'single value' => array('3', '3'),
296 'integer 3' => array(3, '3'),
297 '0-0' => array('0-0', '0'),
298 '4-4' => array('4-4', '4'),
299 '0-3' => array('0-3', '0,1,2,3'),
300 '4-5' => array('4-5', '4,5')
301 );
302 }
303
304 /**
305 * @test
306 * @dataProvider validRangeDataProvider
307 * @param string $range Cron command range expression
308 * @param string $expected Expected result (normalized range)
309 */
310 public function convertRangeToListOfValuesReturnsCorrectListForValidRanges($range, $expected) {
311 $result = NormalizeCommandAccessibleProxy::convertRangeToListOfValues($range);
312 $this->assertSame($expected, $result);
313 }
314
315 /**
316 * @return array
317 */
318 static public function invalidRangeDataProvider() {
319 return array(
320 'empty string' => array(''),
321 'string' => array('foo'),
322 'single dash' => array('-'),
323 'left part is string' => array('foo-5'),
324 'right part is string' => array('5-foo'),
325 'range of strings' => array('foo-bar'),
326 'string five minus' => array('5-'),
327 'string minus five' => array('-5'),
328 'more than one dash' => array('2-3-4'),
329 'left part bigger than right part' => array('6-3')
330 );
331 }
332
333 /**
334 * @test
335 * @dataProvider invalidRangeDataProvider
336 * @expectedException \InvalidArgumentException
337 * @param string $range Cron command range expression (invalid)
338 */
339 public function convertRangeToListOfValuesThrowsExceptionForInvalidRanges($range) {
340 NormalizeCommandAccessibleProxy::convertRangeToListOfValues($range);
341 }
342
343 /**
344 * @return array
345 */
346 static public function validStepsDataProvider() {
347 return array(
348 '2/2' => array('2/2', '2'),
349 '2,3,4/2' => array('2,3,4/2', '2,4'),
350 '1,2,3,4,5,6,7/3' => array('1,2,3,4,5,6,7/3', '1,4,7'),
351 '0,1,2,3,4,5,6/3' => array('0,1,2,3,4,5,6/3', '0,3,6')
352 );
353 }
354
355 /**
356 * @test
357 * @dataProvider validStepsDataProvider
358 * @param string $stepExpression Cron command step expression
359 * @param string $expected Expected result (normalized range)
360 */
361 public function reduceListOfValuesByStepValueReturnsCorrectListOfValues($stepExpression, $expected) {
362 $result = NormalizeCommandAccessibleProxy::reduceListOfValuesByStepValue($stepExpression);
363 $this->assertSame($expected, $result);
364 }
365
366 /**
367 * @return array
368 */
369 static public function invalidStepsDataProvider() {
370 return array(
371 'empty string' => array(''),
372 'slash only' => array('/'),
373 'left part empty' => array('/2'),
374 'right part empty' => array('2/'),
375 'multiples slashes' => array('1/2/3'),
376 '2-2' => array('2-2'),
377 '2.3/2' => array('2.3/2'),
378 '2,3,4/2.3' => array('2,3,4/2.3'),
379 '2,3,4/2,3' => array('2,3,4/2,3')
380 );
381 }
382
383 /**
384 * @test
385 * @dataProvider invalidStepsDataProvider
386 * @expectedException \InvalidArgumentException
387 * @param string $stepExpression Cron command step expression (invalid)
388 */
389 public function reduceListOfValuesByStepValueThrowsExceptionForInvalidStepExpressions($stepExpression) {
390 NormalizeCommandAccessibleProxy::reduceListOfValuesByStepValue($stepExpression);
391 }
392
393 /**
394 * @test
395 */
396 public function normalizeMonthAndWeekdayNormalizesAMonth() {
397 $result = NormalizeCommandAccessibleProxy::normalizeMonthAndWeekday('feb', TRUE);
398 $this->assertSame('2', $result);
399 }
400
401 /**
402 * @test
403 */
404 public function normalizeMonthAndWeekdayNormalizesAWeekday() {
405 $result = NormalizeCommandAccessibleProxy::normalizeMonthAndWeekday('fri', FALSE);
406 $this->assertSame('5', $result);
407 }
408
409 /**
410 * @test
411 */
412 public function normalizeMonthAndWeekdayLeavesValueUnchanged() {
413 $result = NormalizeCommandAccessibleProxy::normalizeMonthAndWeekday('2');
414 $this->assertSame('2', $result);
415 }
416
417 /**
418 * @return array
419 */
420 static public function validMonthNamesDataProvider() {
421 return array(
422 'jan' => array('jan', 1),
423 'feb' => array('feb', 2),
424 'MaR' => array('MaR', 3),
425 'aPr' => array('aPr', 4),
426 'MAY' => array('MAY', 5),
427 'jun' => array('jun', 6),
428 'jul' => array('jul', 7),
429 'aug' => array('aug', 8),
430 'sep' => array('sep', 9),
431 'oct' => array('oct', 10),
432 'nov' => array('nov', 11),
433 'dec' => array('dec', 12),
434 'string 7' => array('7', 7),
435 'integer 7' => array(7, 7),
436 'string 07' => array('07', 7),
437 'integer 07' => array(7, 7)
438 );
439 }
440
441 /**
442 * @test
443 * @dataProvider validMonthNamesDataProvider
444 * @param string $monthName Month name
445 * @param int $expectedInteger Number of the month
446 */
447 public function normalizeMonthConvertsName($monthName, $expectedInteger) {
448 $result = NormalizeCommandAccessibleProxy::normalizeMonth($monthName);
449 $this->assertEquals($expectedInteger, $result);
450 }
451
452 /**
453 * @test
454 * @dataProvider validMonthNamesDataProvider
455 * @param string $monthName Month name
456 * @param int $expectedInteger Number of the month (not used)
457 */
458 public function normalizeMonthReturnsInteger($monthName, $expectedInteger) {
459 $result = NormalizeCommandAccessibleProxy::normalizeMonth($monthName);
460 $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
461 }
462
463 /**
464 * @return array
465 */
466 static public function invalidMonthNamesDataProvider() {
467 return array(
468 'sep-' => array('sep-'),
469 '-September-' => array('-September-'),
470 ',sep' => array(',sep'),
471 ',September,' => array(',September,'),
472 'sep/' => array('sep/'),
473 '/sep' => array('/sep'),
474 '/September/' => array('/September/'),
475 'foo' => array('foo'),
476 'Tuesday' => array('Tuesday'),
477 'Tue' => array('Tue'),
478 'string 0' => array('0'),
479 'integer 0' => array(0),
480 'string seven' => array('seven'),
481 'string 13' => array('13'),
482 'integer 13' => array(13),
483 'integer 100' => array(100),
484 'integer 2010' => array(2010),
485 'string minus 7' => array('-7'),
486 'negative integer 7' => array(-7)
487 );
488 }
489
490 /**
491 * @test
492 * @expectedException \InvalidArgumentException
493 * @dataProvider invalidMonthNamesDataProvider
494 * @param string $invalidMonthName Month name (invalid)
495 */
496 public function normalizeMonthThrowsExceptionForInvalidMonthRepresentation($invalidMonthName) {
497 NormalizeCommandAccessibleProxy::normalizeMonth($invalidMonthName);
498 }
499
500 /**
501 * @return array
502 */
503 static public function validWeekdayDataProvider() {
504 return array(
505 'string 1' => array('1', 1),
506 'string 2' => array('2', 2),
507 'string 02' => array('02', 2),
508 'integer 02' => array(2, 2),
509 'string 3' => array('3', 3),
510 'string 4' => array('4', 4),
511 'string 5' => array('5', 5),
512 'integer 5' => array(5, 5),
513 'string 6' => array('6', 6),
514 'string 7' => array('7', 7),
515 'string 0' => array('0', 7),
516 'integer 0' => array(0, 7),
517 'mon' => array('mon', 1),
518 'monday' => array('monday', 1),
519 'tue' => array('tue', 2),
520 'tuesday' => array('tuesday', 2),
521 'WED' => array('WED', 3),
522 'WEDnesday' => array('WEDnesday', 3),
523 'tHu' => array('tHu', 4),
524 'Thursday' => array('Thursday', 4),
525 'fri' => array('fri', 5),
526 'friday' => array('friday', 5),
527 'sat' => array('sat', 6),
528 'saturday' => array('saturday', 6),
529 'sun' => array('sun', 7),
530 'sunday' => array('sunday', 7)
531 );
532 }
533
534 /**
535 * @test
536 * @dataProvider validWeekdayDataProvider
537 * @param string $weekday Weekday expression
538 * @param int $expectedInteger Number of weekday
539 */
540 public function normalizeWeekdayConvertsName($weekday, $expectedInteger) {
541 $result = NormalizeCommandAccessibleProxy::normalizeWeekday($weekday);
542 $this->assertEquals($expectedInteger, $result);
543 }
544
545 /**
546 * @test
547 * @dataProvider validWeekdayDataProvider
548 * @param string $weekday Weekday expression
549 * @param int $expectedInteger Number of weekday (not used)
550 */
551 public function normalizeWeekdayReturnsInteger($weekday, $expectedInteger) {
552 $result = NormalizeCommandAccessibleProxy::normalizeWeekday($weekday);
553 $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
554 }
555
556 /**
557 * @return array
558 */
559 static public function invalidWeekdayDataProvider() {
560 return array(
561 '-fri' => array('-fri'),
562 'fri-' => array('fri-'),
563 '-friday-' => array('-friday-'),
564 '/fri' => array('/fri'),
565 'fri/' => array('fri/'),
566 '/friday/' => array('/friday/'),
567 ',fri' => array(',fri'),
568 ',friday,' => array(',friday,'),
569 'string minus 1' => array('-1'),
570 'integer -1' => array(-1),
571 'string seven' => array('seven'),
572 'string 8' => array('8'),
573 'string 29' => array('29'),
574 'string 2010' => array('2010'),
575 'Jan' => array('Jan'),
576 'January' => array('January'),
577 'MARCH' => array('MARCH')
578 );
579 }
580
581 /**
582 * @test
583 * @dataProvider invalidWeekdayDataProvider
584 * @expectedException \InvalidArgumentException
585 * @param string $weekday Weekday expression (invalid)
586 */
587 public function normalizeWeekdayThrowsExceptionForInvalidWeekdayRepresentation($weekday) {
588 NormalizeCommandAccessibleProxy::normalizeWeekday($weekday);
589 }
590
591 }