TYPO3 CMS  TYPO3_8-7
NormalizeCommandTest.php
Go to the documentation of this file.
1 <?php
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 
19 
23 class NormalizeCommandTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
24 {
28  public static function normalizeValidDataProvider()
29  {
30  return [
31  '@weekly' => ['@weekly', '0 0 * * 7'],
32  ' @weekly ' => [' @weekly ', '0 0 * * 7'],
33  '* * * * *' => ['* * * * *', '* * * * *'],
34  '30 4 1,15 * 5' => ['30 4 1,15 * 5', '30 4 1,15 * 5'],
35  '5 0 * * *' => ['5 0 * * *', '5 0 * * *'],
36  '15 14 1 * *' => ['15 14 1 * *', '15 14 1 * *'],
37  '0 22 * * 1-5' => ['0 22 * * 1-5', '0 22 * * 1,2,3,4,5'],
38  '23 0-23/2 * * *' => ['23 0-23/2 * * *', '23 0,2,4,6,8,10,12,14,16,18,20,22 * * *'],
39  '5 4 * * sun' => ['5 4 * * sun', '5 4 * * 7'],
40  '0-3/2,7 0,4 20-22, feb,mar-jun/2,7 1-3,sun' => [
41  '0-3/2,7 0,4 20-22 feb,mar-jun/2,7 1-3,sun',
42  '0,2,7 0,4 20,21,22 2,3,5,7 1,2,3,7'
43  ],
44  '0-20/10 * * * *' => ['0-20/10 * * * *', '0,10,20 * * * *'],
45  '* * 2 * *' => ['* * 2 * *', '* * 2 * *'],
46  '* * 2,7 * *' => ['* * 2,7 * *', '* * 2,7 * *'],
47  '* * 2-4,10 * *' => ['* * 2-4,10 * *', '* * 2,3,4,10 * *'],
48  '* * */14 * *' => ['* * */14 * *', '* * 1,15,29 * *'],
49  '* * 2,4-6/2,*/14 * *' => ['* * 2,4-6/2,*/14 * *', '* * 1,2,4,6,15,29 * *'],
50  '* * * * 1' => ['* * * * 1', '* * * * 1'],
51  '0 0 * * 0' => ['0 0 * * 0', '0 0 * * 7'],
52  '0 0 * * 7' => ['0 0 * * 7', '0 0 * * 7'],
53  '* * 1,2 * 1' => ['* * 1,2 * 1', '* * 1,2 * 1']
54  ];
55  }
56 
63  public function normalizeConvertsCronCommand($expression, $expected)
64  {
65  $result = NormalizeCommand::normalize($expression);
66  $this->assertEquals($expected, $result);
67  }
68 
72  public static function validSpecialKeywordsDataProvider()
73  {
74  return [
75  '@yearly' => ['@yearly', '0 0 1 1 *'],
76  '@annually' => ['@annually', '0 0 1 1 *'],
77  '@monthly' => ['@monthly', '0 0 1 * *'],
78  '@weekly' => ['@weekly', '0 0 * * 0'],
79  '@daily' => ['@daily', '0 0 * * *'],
80  '@midnight' => ['@midnight', '0 0 * * *'],
81  '@hourly' => ['@hourly', '0 * * * *']
82  ];
83  }
84 
91  public function convertKeywordsToCronCommandConvertsValidKeywords($keyword, $expectedCronCommand)
92  {
94  $this->assertEquals($expectedCronCommand, $result);
95  }
96 
101  {
102  $invalidKeyword = 'foo';
104  $this->assertEquals($invalidKeyword, $result);
105  }
106 
111  {
112  return [
113  '1-2 * * * *' => ['1-2 * * * *', '1,2 * * * *'],
114  '* 1-2 * * *' => ['* 1-2 * * *', '* 1,2 * * *'],
115  '* * 1-2 * *' => ['* * 1-2 * *', '* * 1,2 * *'],
116  '* * * 1-2 *' => ['* * * 1-2 *', '* * * 1,2 *'],
117  '* * * * 1-2' => ['* * * * 1-2', '* * * * 1,2']
118  ];
119  }
120 
127  public function normalizeFieldsConvertsField($expression, $expected)
128  {
130  $this->assertEquals($expected, $result);
131  }
132 
137  {
138  return [
139  '* monthField' => ['*', true, '*'],
140  'string 1 monthField' => ['1', true, '1'],
141  'jan' => ['jan', true, '1'],
142  'feb/2' => ['feb/2', true, '2'],
143  'jan-feb/2' => ['jan-feb/2', true, '1'],
144  '1-2 monthField' => ['1-2', true, '1,2'],
145  '1-3/2,feb,may,6' => ['1-3/2,feb,may,6', true, '1,2,3,5,6'],
146  '*/4' => ['*/4', true, '1,5,9'],
147  '* !monthField' => ['*', false, '*'],
148  'string 1, !monthField' => ['1', false, '1'],
149  'fri' => ['fri', false, '5'],
150  'sun' => ['sun', false, '7'],
151  'string 0 for sunday' => ['0', false, '7'],
152  '0,1' => ['0,1', false, '1,7'],
153  '*/3' => ['*/3', false, '1,4,7'],
154  'tue/2' => ['tue/2', false, '2'],
155  '1-2 !monthField' => ['1-2', false, '1,2'],
156  'tue-fri/2' => ['tue-fri/2', false, '2,4'],
157  '1-3/2,tue,fri,6' => ['1-3/2,tue,fri,6', false, '1,2,3,5,6']
158  ];
159  }
160 
169  $expression,
170  $isMonthField,
171  $expected
172  ) {
173  $result = NormalizeCommandAccessibleProxy::normalizeMonthAndWeekdayField($expression, $isMonthField);
174  $this->assertSame($expected, $result);
175  }
176 
181  {
182  return [
183  'mon' => ['mon', true, 1291083486],
184  '1-2/mon' => ['1-2/mon', true, 1291414957],
185  '0,1' => ['0,1', true, 1291083486],
186  'feb' => ['feb', false, 1291163589],
187  '1-2/feb' => ['1-2/feb', false, 1291414957],
188  '0-fri/2,7' => ['0-fri/2,7', false, 1291237145]
189  ];
190  }
191 
200  $expression,
201  $isMonthField,
202  $expectedExceptionCode
203  ) {
204  $this->expectException(\InvalidArgumentException::class);
205  $this->expectExceptionCode($expectedExceptionCode);
207  }
208 
212  public static function normalizeIntegerFieldValidDataProvider()
213  {
214  return [
215  '*' => ['*', '*'],
216  'string 2' => ['2', '2'],
217  'integer 3' => [3, '3'],
218  'list of values' => ['1,2,3', '1,2,3'],
219  'unsorted list of values' => ['3,1,5', '1,3,5'],
220  'duplicate values' => ['0-2/2,2', '0,2'],
221  'additional field between steps' => ['1-3/2,2', '1,2,3'],
222  '2-4' => ['2-4', '2,3,4'],
223  'simple step 4/4' => ['4/4', '4'],
224  'step 2-7/5' => ['2-7/5', '2,7'],
225  'steps 4-12/4' => ['4-12/4', '4,8,12'],
226  '0-59/20' => ['0-59/20', '0,20,40'],
227  '*/20' => ['*/20', '0,20,40']
228  ];
229  }
230 
237  public function normalizeIntegerFieldReturnsNormalizedListForValidExpression($expression, $expected)
238  {
240  $this->assertSame($expected, $result);
241  }
242 
247  {
248  return [
249  'string foo' => ['foo', 0, 59, 1291429389],
250  'empty string' => ['', 0, 59, 1291429389],
251  '4-3' => ['4-3', 0, 59, 1291237145],
252  '/2' => ['/2', 0, 59, 1291234985],
253  '/' => ['/', 0, 59, 1291234985],
254  'left bound too low' => ['2-4', 3, 4, 1291470084],
255  'right bound too high' => ['2-4', 2, 3, 1291470170],
256  'left and right bound' => ['2-5', 2, 4, 1291470170],
257  'element in list is lower than allowed' => ['2,1,4', 2, 4, 1291470084],
258  'element in list is higher than allowed' => ['2,5,4', 1, 4, 1291470170]
259  ];
260  }
261 
271  $expression,
272  $lowerBound,
273  $upperBound,
274  $expectedExceptionCode
275  ) {
276  $this->expectException(\InvalidArgumentException::class);
277  $this->expectExceptionCode($expectedExceptionCode);
278 
279  NormalizeCommandAccessibleProxy::normalizeIntegerField($expression, $lowerBound, $upperBound);
280  }
281 
286  {
287  $result = NormalizeCommandAccessibleProxy::splitFields('12,13 * 1-12/2,14 jan fri');
288  $expectedResult = [
289  0 => '12,13',
290  1 => '*',
291  2 => '1-12/2,14',
292  3 => 'jan',
293  4 => 'fri'
294  ];
295  $this->assertSame($expectedResult, $result);
296  }
297 
301  public static function invalidCronCommandFieldsDataProvider()
302  {
303  return [
304  'empty string' => [''],
305  'foo' => ['foo'],
306  'integer 4' => [4],
307  'four fields' => ['* * * *'],
308  'six fields' => ['* * * * * *']
309  ];
310  }
311 
318  {
319  $this->expectException(\InvalidArgumentException::class);
320  $this->expectExceptionCode(1291227373);
322  }
323 
327  public static function validRangeDataProvider()
328  {
329  return [
330  'single value' => ['3', '3'],
331  'integer 3' => [3, '3'],
332  '0-0' => ['0-0', '0'],
333  '4-4' => ['4-4', '4'],
334  '0-3' => ['0-3', '0,1,2,3'],
335  '4-5' => ['4-5', '4,5']
336  ];
337  }
338 
346  {
348  $this->assertSame($expected, $result);
349  }
350 
354  public static function invalidRangeDataProvider()
355  {
356  return [
357  'empty string' => ['', 1291234985],
358  'string' => ['foo', 1291237668],
359  'single dash' => ['-', 1291237668],
360  'left part is string' => ['foo-5', 1291237668],
361  'right part is string' => ['5-foo', 1291237668],
362  'range of strings' => ['foo-bar', 1291237668],
363  'string five minus' => ['5-', 1291237668],
364  'string minus five' => ['-5', 1291237668],
365  'more than one dash' => ['2-3-4', 1291234986],
366  'left part bigger than right part' => ['6-3', 1291237145]
367  ];
368  }
369 
376  public function convertRangeToListOfValuesThrowsExceptionForInvalidRanges($range, $expectedExceptionCode)
377  {
378  $this->expectException(\InvalidArgumentException::class);
379  $this->expectExceptionCode($expectedExceptionCode);
381  }
382 
386  public static function validStepsDataProvider()
387  {
388  return [
389  '2/2' => ['2/2', '2'],
390  '2,3,4/2' => ['2,3,4/2', '2,4'],
391  '1,2,3,4,5,6,7/3' => ['1,2,3,4,5,6,7/3', '1,4,7'],
392  '0,1,2,3,4,5,6/3' => ['0,1,2,3,4,5,6/3', '0,3,6']
393  ];
394  }
395 
402  public function reduceListOfValuesByStepValueReturnsCorrectListOfValues($stepExpression, $expected)
403  {
405  $this->assertSame($expected, $result);
406  }
407 
411  public static function invalidStepsDataProvider()
412  {
413  return [
414  'empty string' => ['', 1291234987],
415  'slash only' => ['/', 1291414955],
416  'left part empty' => ['/2', 1291414955],
417  'right part empty' => ['2/', 1291414956],
418  'multiples slashes' => ['1/2/3', 1291242168],
419  '2-2' => ['2-2', 1291414956],
420  '2.3/2' => ['2.3/2', 1291414958],
421  '2,3,4/2.3' => ['2,3,4/2.3', 1291414957],
422  '2,3,4/2,3' => ['2,3,4/2,3', 1291414957]
423  ];
424  }
425 
433  $stepExpression,
434  $expectedExceptionCode
435  ) {
436  $this->expectException(\InvalidArgumentException::class);
437  $this->expectExceptionCode($expectedExceptionCode);
438 
440  }
441 
446  {
448  $this->assertSame('2', $result);
449  }
450 
455  {
457  $this->assertSame('5', $result);
458  }
459 
464  {
466  $this->assertSame('2', $result);
467  }
468 
472  public static function validMonthNamesDataProvider()
473  {
474  return [
475  'jan' => ['jan', 1],
476  'feb' => ['feb', 2],
477  'MaR' => ['MaR', 3],
478  'aPr' => ['aPr', 4],
479  'MAY' => ['MAY', 5],
480  'jun' => ['jun', 6],
481  'jul' => ['jul', 7],
482  'aug' => ['aug', 8],
483  'sep' => ['sep', 9],
484  'oct' => ['oct', 10],
485  'nov' => ['nov', 11],
486  'dec' => ['dec', 12],
487  'string 7' => ['7', 7],
488  'integer 7' => [7, 7],
489  'string 07' => ['07', 7],
490  'integer 07' => [7, 7]
491  ];
492  }
493 
500  public function normalizeMonthConvertsName($monthName, $expectedInteger)
501  {
503  $this->assertEquals($expectedInteger, $result);
504  }
505 
512  public function normalizeMonthReturnsInteger($monthName, $expectedInteger)
513  {
515  $this->assertInternalType(\PHPUnit\Framework\Constraint\IsType::TYPE_INT, $result);
516  }
517 
521  public static function invalidMonthNamesDataProvider()
522  {
523  return [
524  'sep-' => ['sep-', 1291083486],
525  '-September-' => ['-September-', 1291083486],
526  ',sep' => [',sep', 1291083486],
527  ',September,' => [',September,', 1291083486],
528  'sep/' => ['sep/', 1291083486],
529  '/sep' => ['/sep', 1291083486],
530  '/September/' => ['/September/', 1291083486],
531  'foo' => ['foo', 1291083486],
532  'Tuesday' => ['Tuesday', 1291083486],
533  'Tue' => ['Tue', 1291083486],
534  'string 0' => ['0', 1291083486],
535  'integer 0' => [0, 1291083486],
536  'string seven' => ['seven', 1291083486],
537  'string 13' => ['13', 1291083486],
538  'integer 13' => [13, 1291083486],
539  'integer 100' => [100, 1291083486],
540  'integer 2010' => [2010, 1291083486],
541  'string minus 7' => ['-7', 1291083486],
542  'negative integer 7' => [-7, 1291083486]
543  ];
544  }
545 
553  $invalidMonthName,
554  $expectedExceptionCode
555  ) {
556  $this->expectException(\InvalidArgumentException::class);
557  $this->expectExceptionCode($expectedExceptionCode);
558 
560  }
561 
565  public static function validWeekdayDataProvider()
566  {
567  return [
568  'string 1' => ['1', 1],
569  'string 2' => ['2', 2],
570  'string 02' => ['02', 2],
571  'integer 02' => [2, 2],
572  'string 3' => ['3', 3],
573  'string 4' => ['4', 4],
574  'string 5' => ['5', 5],
575  'integer 5' => [5, 5],
576  'string 6' => ['6', 6],
577  'string 7' => ['7', 7],
578  'string 0' => ['0', 7],
579  'integer 0' => [0, 7],
580  'mon' => ['mon', 1],
581  'monday' => ['monday', 1],
582  'tue' => ['tue', 2],
583  'tuesday' => ['tuesday', 2],
584  'WED' => ['WED', 3],
585  'WEDnesday' => ['WEDnesday', 3],
586  'tHu' => ['tHu', 4],
587  'Thursday' => ['Thursday', 4],
588  'fri' => ['fri', 5],
589  'friday' => ['friday', 5],
590  'sat' => ['sat', 6],
591  'saturday' => ['saturday', 6],
592  'sun' => ['sun', 7],
593  'sunday' => ['sunday', 7]
594  ];
595  }
596 
603  public function normalizeWeekdayConvertsName($weekday, $expectedInteger)
604  {
606  $this->assertEquals($expectedInteger, $result);
607  }
608 
615  public function normalizeWeekdayReturnsInteger($weekday, $expectedInteger)
616  {
618  $this->assertInternalType(\PHPUnit\Framework\Constraint\IsType::TYPE_INT, $result);
619  }
620 
624  public static function invalidWeekdayDataProvider()
625  {
626  return [
627  '-fri' => ['-fri'],
628  'fri-' => ['fri-'],
629  '-friday-' => ['-friday-'],
630  '/fri' => ['/fri'],
631  'fri/' => ['fri/'],
632  '/friday/' => ['/friday/'],
633  ',fri' => [',fri'],
634  ',friday,' => [',friday,'],
635  'string minus 1' => ['-1'],
636  'integer -1' => [-1],
637  'string seven' => ['seven'],
638  'string 8' => ['8'],
639  'string 29' => ['29'],
640  'string 2010' => ['2010'],
641  'Jan' => ['Jan'],
642  'January' => ['January'],
643  'MARCH' => ['MARCH']
644  ];
645  }
646 
653  {
654  $this->expectException(\InvalidArgumentException::class);
655  $this->expectExceptionCode(1291163589);
656 
658  }
659 }
normalizeMonthAndWeekdayFieldReturnsNormalizedListForValidExpression( $expression, $isMonthField, $expected)
reduceListOfValuesByStepValueThrowsExceptionForInvalidStepExpressions( $stepExpression, $expectedExceptionCode)
convertRangeToListOfValuesThrowsExceptionForInvalidRanges($range, $expectedExceptionCode)
normalizeMonthThrowsExceptionForInvalidMonthRepresentation( $invalidMonthName, $expectedExceptionCode)
normalizeMonthAndWeekdayFieldThrowsExceptionForInvalidExpression( $expression, $isMonthField, $expectedExceptionCode)
normalizeIntegerFieldThrowsExceptionForInvalidExpressions( $expression, $lowerBound, $upperBound, $expectedExceptionCode)