TYPO3 CMS  TYPO3_7-6
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 
20 
25 {
29  public static function normalizeValidDataProvider()
30  {
31  return [
32  '@weekly' => ['@weekly', '0 0 * * 7'],
33  ' @weekly ' => [' @weekly ', '0 0 * * 7'],
34  '* * * * *' => ['* * * * *', '* * * * *'],
35  '30 4 1,15 * 5' => ['30 4 1,15 * 5', '30 4 1,15 * 5'],
36  '5 0 * * *' => ['5 0 * * *', '5 0 * * *'],
37  '15 14 1 * *' => ['15 14 1 * *', '15 14 1 * *'],
38  '0 22 * * 1-5' => ['0 22 * * 1-5', '0 22 * * 1,2,3,4,5'],
39  '23 0-23/2 * * *' => ['23 0-23/2 * * *', '23 0,2,4,6,8,10,12,14,16,18,20,22 * * *'],
40  '5 4 * * sun' => ['5 4 * * sun', '5 4 * * 7'],
41  '0-3/2,7 0,4 20-22, feb,mar-jun/2,7 1-3,sun' => ['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'],
42  '0-20/10 * * * *' => ['0-20/10 * * * *', '0,10,20 * * * *'],
43  '* * 2 * *' => ['* * 2 * *', '* * 2 * *'],
44  '* * 2,7 * *' => ['* * 2,7 * *', '* * 2,7 * *'],
45  '* * 2-4,10 * *' => ['* * 2-4,10 * *', '* * 2,3,4,10 * *'],
46  '* * */14 * *' => ['* * */14 * *', '* * 1,15,29 * *'],
47  '* * 2,4-6/2,*/14 * *' => ['* * 2,4-6/2,*/14 * *', '* * 1,2,4,6,15,29 * *'],
48  '* * * * 1' => ['* * * * 1', '* * * * 1'],
49  '0 0 * * 0' => ['0 0 * * 0', '0 0 * * 7'],
50  '0 0 * * 7' => ['0 0 * * 7', '0 0 * * 7'],
51  '* * 1,2 * 1' => ['* * 1,2 * 1', '* * 1,2 * 1']
52  ];
53  }
54 
61  public function normalizeConvertsCronCommand($expression, $expected)
62  {
63  $result = NormalizeCommand::normalize($expression);
64  $this->assertEquals($expected, $result);
65  }
66 
70  public static function validSpecialKeywordsDataProvider()
71  {
72  return [
73  '@yearly' => ['@yearly', '0 0 1 1 *'],
74  '@annually' => ['@annually', '0 0 1 1 *'],
75  '@monthly' => ['@monthly', '0 0 1 * *'],
76  '@weekly' => ['@weekly', '0 0 * * 0'],
77  '@daily' => ['@daily', '0 0 * * *'],
78  '@midnight' => ['@midnight', '0 0 * * *'],
79  '@hourly' => ['@hourly', '0 * * * *']
80  ];
81  }
82 
89  public function convertKeywordsToCronCommandConvertsValidKeywords($keyword, $expectedCronCommand)
90  {
92  $this->assertEquals($expectedCronCommand, $result);
93  }
94 
99  {
100  $invalidKeyword = 'foo';
102  $this->assertEquals($invalidKeyword, $result);
103  }
104 
109  {
110  return [
111  '1-2 * * * *' => ['1-2 * * * *', '1,2 * * * *'],
112  '* 1-2 * * *' => ['* 1-2 * * *', '* 1,2 * * *'],
113  '* * 1-2 * *' => ['* * 1-2 * *', '* * 1,2 * *'],
114  '* * * 1-2 *' => ['* * * 1-2 *', '* * * 1,2 *'],
115  '* * * * 1-2' => ['* * * * 1-2', '* * * * 1,2']
116  ];
117  }
118 
125  public function normalizeFieldsConvertsField($expression, $expected)
126  {
128  $this->assertEquals($expected, $result);
129  }
130 
135  {
136  return [
137  '* monthField' => ['*', true, '*'],
138  'string 1 monthField' => ['1', true, '1'],
139  'jan' => ['jan', true, '1'],
140  'feb/2' => ['feb/2', true, '2'],
141  'jan-feb/2' => ['jan-feb/2', true, '1'],
142  '1-2 monthField' => ['1-2', true, '1,2'],
143  '1-3/2,feb,may,6' => ['1-3/2,feb,may,6', true, '1,2,3,5,6'],
144  '*/4' => ['*/4', true, '1,5,9'],
145  '* !monthField' => ['*', false, '*'],
146  'string 1, !monthField' => ['1', false, '1'],
147  'fri' => ['fri', false, '5'],
148  'sun' => ['sun', false, '7'],
149  'string 0 for sunday' => ['0', false, '7'],
150  '0,1' => ['0,1', false, '1,7'],
151  '*/3' => ['*/3', false, '1,4,7'],
152  'tue/2' => ['tue/2', false, '2'],
153  '1-2 !monthField' => ['1-2', false, '1,2'],
154  'tue-fri/2' => ['tue-fri/2', false, '2,4'],
155  '1-3/2,tue,fri,6' => ['1-3/2,tue,fri,6', false, '1,2,3,5,6']
156  ];
157  }
158 
166  public function normalizeMonthAndWeekdayFieldReturnsNormalizedListForValidExpression($expression, $isMonthField, $expected)
167  {
168  $result = NormalizeCommandAccessibleProxy::normalizeMonthAndWeekdayField($expression, $isMonthField);
169  $this->assertSame($expected, $result);
170  }
171 
176  {
177  return [
178  'mon' => ['mon', true],
179  '1-2/mon' => ['1-2/mon', true],
180  '0,1' => ['0,1', true],
181  'feb' => ['feb', false],
182  '1-2/feb' => ['1-2/feb', false],
183  '0-fri/2,7' => ['0-fri/2,7', false, '2,4,7']
184  ];
185  }
186 
194  public function normalizeMonthAndWeekdayFieldThrowsExceptionForInvalidExpression($expression, $isMonthField)
195  {
197  }
198 
202  public static function normalizeIntegerFieldValidDataProvider()
203  {
204  return [
205  '*' => ['*', '*'],
206  'string 2' => ['2', '2'],
207  'integer 3' => [3, '3'],
208  'list of values' => ['1,2,3', '1,2,3'],
209  'unsorted list of values' => ['3,1,5', '1,3,5'],
210  'duplicate values' => ['0-2/2,2', '0,2'],
211  'additional field between steps' => ['1-3/2,2', '1,2,3'],
212  '2-4' => ['2-4', '2,3,4'],
213  'simple step 4/4' => ['4/4', '4'],
214  'step 2-7/5' => ['2-7/5', '2,7'],
215  'steps 4-12/4' => ['4-12/4', '4,8,12'],
216  '0-59/20' => ['0-59/20', '0,20,40'],
217  '*/20' => ['*/20', '0,20,40']
218  ];
219  }
220 
227  public function normalizeIntegerFieldReturnsNormalizedListForValidExpression($expression, $expected)
228  {
230  $this->assertSame($expected, $result);
231  }
232 
237  {
238  return [
239  'string foo' => ['foo', 0, 59],
240  'empty string' => ['', 0, 59],
241  '4-3' => ['4-3', 0, 59],
242  '/2' => ['/2', 0, 59],
243  '/' => ['/', 0, 59],
244  'left bound too low' => ['2-4', 3, 4],
245  'right bound too high' => ['2-4', 2, 3],
246  'left and right bound' => ['2-5', 2, 4],
247  'element in list is lower than allowed' => ['2,1,4', 2, 4],
248  'element in list is higher than allowed' => ['2,5,4', 1, 4]
249  ];
250  }
251 
260  public function normalizeIntegerFieldThrowsExceptionForInvalidExpressions($expression, $lowerBound, $upperBound)
261  {
262  NormalizeCommandAccessibleProxy::normalizeIntegerField($expression, $lowerBound, $upperBound);
263  }
264 
269  {
270  $result = NormalizeCommandAccessibleProxy::splitFields('12,13 * 1-12/2,14 jan fri');
271  $expectedResult = [
272  0 => '12,13',
273  1 => '*',
274  2 => '1-12/2,14',
275  3 => 'jan',
276  4 => 'fri'
277  ];
278  $this->assertSame($expectedResult, $result);
279  }
280 
284  public static function invalidCronCommandFieldsDataProvider()
285  {
286  return [
287  'empty string' => [''],
288  'foo' => ['foo'],
289  'integer 4' => [4],
290  'four fields' => ['* * * *'],
291  'six fields' => ['* * * * * *']
292  ];
293  }
294 
302  {
304  }
305 
309  public static function validRangeDataProvider()
310  {
311  return [
312  'single value' => ['3', '3'],
313  'integer 3' => [3, '3'],
314  '0-0' => ['0-0', '0'],
315  '4-4' => ['4-4', '4'],
316  '0-3' => ['0-3', '0,1,2,3'],
317  '4-5' => ['4-5', '4,5']
318  ];
319  }
320 
328  {
330  $this->assertSame($expected, $result);
331  }
332 
336  public static function invalidRangeDataProvider()
337  {
338  return [
339  'empty string' => [''],
340  'string' => ['foo'],
341  'single dash' => ['-'],
342  'left part is string' => ['foo-5'],
343  'right part is string' => ['5-foo'],
344  'range of strings' => ['foo-bar'],
345  'string five minus' => ['5-'],
346  'string minus five' => ['-5'],
347  'more than one dash' => ['2-3-4'],
348  'left part bigger than right part' => ['6-3']
349  ];
350  }
351 
359  {
361  }
362 
366  public static function validStepsDataProvider()
367  {
368  return [
369  '2/2' => ['2/2', '2'],
370  '2,3,4/2' => ['2,3,4/2', '2,4'],
371  '1,2,3,4,5,6,7/3' => ['1,2,3,4,5,6,7/3', '1,4,7'],
372  '0,1,2,3,4,5,6/3' => ['0,1,2,3,4,5,6/3', '0,3,6']
373  ];
374  }
375 
382  public function reduceListOfValuesByStepValueReturnsCorrectListOfValues($stepExpression, $expected)
383  {
385  $this->assertSame($expected, $result);
386  }
387 
391  public static function invalidStepsDataProvider()
392  {
393  return [
394  'empty string' => [''],
395  'slash only' => ['/'],
396  'left part empty' => ['/2'],
397  'right part empty' => ['2/'],
398  'multiples slashes' => ['1/2/3'],
399  '2-2' => ['2-2'],
400  '2.3/2' => ['2.3/2'],
401  '2,3,4/2.3' => ['2,3,4/2.3'],
402  '2,3,4/2,3' => ['2,3,4/2,3']
403  ];
404  }
405 
413  {
415  }
416 
421  {
423  $this->assertSame('2', $result);
424  }
425 
430  {
432  $this->assertSame('5', $result);
433  }
434 
439  {
441  $this->assertSame('2', $result);
442  }
443 
447  public static function validMonthNamesDataProvider()
448  {
449  return [
450  'jan' => ['jan', 1],
451  'feb' => ['feb', 2],
452  'MaR' => ['MaR', 3],
453  'aPr' => ['aPr', 4],
454  'MAY' => ['MAY', 5],
455  'jun' => ['jun', 6],
456  'jul' => ['jul', 7],
457  'aug' => ['aug', 8],
458  'sep' => ['sep', 9],
459  'oct' => ['oct', 10],
460  'nov' => ['nov', 11],
461  'dec' => ['dec', 12],
462  'string 7' => ['7', 7],
463  'integer 7' => [7, 7],
464  'string 07' => ['07', 7],
465  'integer 07' => [7, 7]
466  ];
467  }
468 
475  public function normalizeMonthConvertsName($monthName, $expectedInteger)
476  {
478  $this->assertEquals($expectedInteger, $result);
479  }
480 
487  public function normalizeMonthReturnsInteger($monthName, $expectedInteger)
488  {
490  $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
491  }
492 
496  public static function invalidMonthNamesDataProvider()
497  {
498  return [
499  'sep-' => ['sep-'],
500  '-September-' => ['-September-'],
501  ',sep' => [',sep'],
502  ',September,' => [',September,'],
503  'sep/' => ['sep/'],
504  '/sep' => ['/sep'],
505  '/September/' => ['/September/'],
506  'foo' => ['foo'],
507  'Tuesday' => ['Tuesday'],
508  'Tue' => ['Tue'],
509  'string 0' => ['0'],
510  'integer 0' => [0],
511  'string seven' => ['seven'],
512  'string 13' => ['13'],
513  'integer 13' => [13],
514  'integer 100' => [100],
515  'integer 2010' => [2010],
516  'string minus 7' => ['-7'],
517  'negative integer 7' => [-7]
518  ];
519  }
520 
528  {
530  }
531 
535  public static function validWeekdayDataProvider()
536  {
537  return [
538  'string 1' => ['1', 1],
539  'string 2' => ['2', 2],
540  'string 02' => ['02', 2],
541  'integer 02' => [2, 2],
542  'string 3' => ['3', 3],
543  'string 4' => ['4', 4],
544  'string 5' => ['5', 5],
545  'integer 5' => [5, 5],
546  'string 6' => ['6', 6],
547  'string 7' => ['7', 7],
548  'string 0' => ['0', 7],
549  'integer 0' => [0, 7],
550  'mon' => ['mon', 1],
551  'monday' => ['monday', 1],
552  'tue' => ['tue', 2],
553  'tuesday' => ['tuesday', 2],
554  'WED' => ['WED', 3],
555  'WEDnesday' => ['WEDnesday', 3],
556  'tHu' => ['tHu', 4],
557  'Thursday' => ['Thursday', 4],
558  'fri' => ['fri', 5],
559  'friday' => ['friday', 5],
560  'sat' => ['sat', 6],
561  'saturday' => ['saturday', 6],
562  'sun' => ['sun', 7],
563  'sunday' => ['sunday', 7]
564  ];
565  }
566 
573  public function normalizeWeekdayConvertsName($weekday, $expectedInteger)
574  {
576  $this->assertEquals($expectedInteger, $result);
577  }
578 
585  public function normalizeWeekdayReturnsInteger($weekday, $expectedInteger)
586  {
588  $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
589  }
590 
594  public static function invalidWeekdayDataProvider()
595  {
596  return [
597  '-fri' => ['-fri'],
598  'fri-' => ['fri-'],
599  '-friday-' => ['-friday-'],
600  '/fri' => ['/fri'],
601  'fri/' => ['fri/'],
602  '/friday/' => ['/friday/'],
603  ',fri' => [',fri'],
604  ',friday,' => [',friday,'],
605  'string minus 1' => ['-1'],
606  'integer -1' => [-1],
607  'string seven' => ['seven'],
608  'string 8' => ['8'],
609  'string 29' => ['29'],
610  'string 2010' => ['2010'],
611  'Jan' => ['Jan'],
612  'January' => ['January'],
613  'MARCH' => ['MARCH']
614  ];
615  }
616 
624  {
626  }
627 }
normalizeMonthAndWeekdayFieldReturnsNormalizedListForValidExpression($expression, $isMonthField, $expected)
normalizeMonthAndWeekdayFieldThrowsExceptionForInvalidExpression($expression, $isMonthField)
normalizeIntegerFieldThrowsExceptionForInvalidExpressions($expression, $lowerBound, $upperBound)