TYPO3 CMS  TYPO3_6-2
NormalizeCommandTest.php
Go to the documentation of this file.
1 <?php
3 
22 
28  protected function getAccessibleProxy() {
29  $className = $this->getUniqueId('NormalizeCommand');
30  $fullClassName = __NAMESPACE__ . '\\' . $className;
31  if (!class_exists($className, FALSE)) {
32  eval(
33  'namespace ' . __NAMESPACE__ . ';' .
34  'class ' . $className . ' extends \\TYPO3\\CMS\\Scheduler\\CronCommand\\NormalizeCommand {' .
35  ' public static function convertKeywordsToCronCommand($cronCommand) {' .
36  ' return parent::convertKeywordsToCronCommand($cronCommand);' .
37  ' }' .
38  ' public static function normalizeFields($cronCommand) {' .
39  ' return parent::normalizeFields($cronCommand);' .
40  ' }' .
41  ' public static function normalizeMonthAndWeekdayField($expression, $isMonthField = TRUE) {' .
42  ' return parent::normalizeMonthAndWeekdayField($expression, $isMonthField);' .
43  ' }' .
44  ' public static function normalizeIntegerField($expression, $lowerBound = 0, $upperBound = 59) {' .
45  ' return parent::normalizeIntegerField($expression, $lowerBound, $upperBound);' .
46  ' }' .
47  ' public static function splitFields($cronCommand) {' .
48  ' return parent::splitFields($cronCommand);' .
49  ' }' .
50  ' public static function convertRangeToListOfValues($range) {' .
51  ' return parent::convertRangeToListOfValues($range);' .
52  ' }' .
53  ' public static function reduceListOfValuesByStepValue($stepExpression) {' .
54  ' return parent::reduceListOfValuesByStepValue($stepExpression);' .
55  ' }' .
56  ' public static function normalizeMonthAndWeekday($expression, $isMonth = TRUE) {' .
57  ' return parent::normalizeMonthAndWeekday($expression, $isMonth);' .
58  ' }' .
59  ' public static function normalizeMonth($month) {' .
60  ' return parent::normalizeMonth($month);' .
61  ' }' .
62  ' public static function normalizeWeekday($weekday) {' .
63  ' return parent::normalizeWeekday($weekday);' .
64  ' }' .
65  '}'
66  );
67  }
68  return $fullClassName;
69  }
70 
74  static public function normalizeValidDataProvider() {
75  return array(
76  '@weekly' => array('@weekly', '0 0 * * 7'),
77  ' @weekly ' => array(' @weekly ', '0 0 * * 7'),
78  '* * * * *' => array('* * * * *', '* * * * *'),
79  '30 4 1,15 * 5' => array('30 4 1,15 * 5', '30 4 1,15 * 5'),
80  '5 0 * * *' => array('5 0 * * *', '5 0 * * *'),
81  '15 14 1 * *' => array('15 14 1 * *', '15 14 1 * *'),
82  '0 22 * * 1-5' => array('0 22 * * 1-5', '0 22 * * 1,2,3,4,5'),
83  '23 0-23/2 * * *' => array('23 0-23/2 * * *', '23 0,2,4,6,8,10,12,14,16,18,20,22 * * *'),
84  '5 4 * * sun' => array('5 4 * * sun', '5 4 * * 7'),
85  '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'),
86  '0-20/10 * * * *' => array('0-20/10 * * * *', '0,10,20 * * * *'),
87  '* * 2 * *' => array('* * 2 * *', '* * 2 * *'),
88  '* * 2,7 * *' => array('* * 2,7 * *', '* * 2,7 * *'),
89  '* * 2-4,10 * *' => array('* * 2-4,10 * *', '* * 2,3,4,10 * *'),
90  '* * */14 * *' => array('* * */14 * *', '* * 1,15,29 * *'),
91  '* * 2,4-6/2,*/14 * *' => array('* * 2,4-6/2,*/14 * *', '* * 1,2,4,6,15,29 * *'),
92  '* * * * 1' => array('* * * * 1', '* * * * 1'),
93  '0 0 * * 0' => array('0 0 * * 0', '0 0 * * 7'),
94  '0 0 * * 7' => array('0 0 * * 7', '0 0 * * 7'),
95  '* * 1,2 * 1' => array('* * 1,2 * 1', '* * 1,2 * 1')
96  );
97  }
98 
105  public function normalizeConvertsCronCommand($expression, $expected) {
107  $this->assertEquals($expected, $result);
108  }
109 
113  static public function validSpecialKeywordsDataProvider() {
114  return array(
115  '@yearly' => array('@yearly', '0 0 1 1 *'),
116  '@annually' => array('@annually', '0 0 1 1 *'),
117  '@monthly' => array('@monthly', '0 0 1 * *'),
118  '@weekly' => array('@weekly', '0 0 * * 0'),
119  '@daily' => array('@daily', '0 0 * * *'),
120  '@midnight' => array('@midnight', '0 0 * * *'),
121  '@hourly' => array('@hourly', '0 * * * *')
122  );
123  }
124 
131  public function convertKeywordsToCronCommandConvertsValidKeywords($keyword, $expectedCronCommand) {
132  $accessibleProxyClassName = $this->getAccessibleProxy();
133  $result = $accessibleProxyClassName::convertKeywordsToCronCommand($keyword);
134  $this->assertEquals($expectedCronCommand, $result);
135  }
136 
141  $invalidKeyword = 'foo';
142  $accessibleProxyClassName = $this->getAccessibleProxy();
143  $result = $accessibleProxyClassName::convertKeywordsToCronCommand($invalidKeyword);
144  $this->assertEquals($invalidKeyword, $result);
145  }
146 
151  return array(
152  '1-2 * * * *' => array('1-2 * * * *', '1,2 * * * *'),
153  '* 1-2 * * *' => array('* 1-2 * * *', '* 1,2 * * *'),
154  '* * 1-2 * *' => array('* * 1-2 * *', '* * 1,2 * *'),
155  '* * * 1-2 *' => array('* * * 1-2 *', '* * * 1,2 *'),
156  '* * * * 1-2' => array('* * * * 1-2', '* * * * 1,2')
157  );
158  }
159 
166  public function normalizeFieldsConvertsField($expression, $expected) {
167  $accessibleProxyClassName = $this->getAccessibleProxy();
168  $result = $accessibleProxyClassName::normalizeFields($expression);
169  $this->assertEquals($expected, $result);
170  }
171 
176  return array(
177  '* monthField' => array('*', TRUE, '*'),
178  'string 1 monthField' => array('1', TRUE, '1'),
179  'jan' => array('jan', TRUE, '1'),
180  'feb/2' => array('feb/2', TRUE, '2'),
181  'jan-feb/2' => array('jan-feb/2', TRUE, '1'),
182  '1-2 monthField' => array('1-2', TRUE, '1,2'),
183  '1-3/2,feb,may,6' => array('1-3/2,feb,may,6', TRUE, '1,2,3,5,6'),
184  '*/4' => array('*/4', TRUE, '1,5,9'),
185  '* !monthField' => array('*', FALSE, '*'),
186  'string 1, !monthField' => array('1', FALSE, '1'),
187  'fri' => array('fri', FALSE, '5'),
188  'sun' => array('sun', FALSE, '7'),
189  'string 0 for sunday' => array('0', FALSE, '7'),
190  '0,1' => array('0,1', FALSE, '1,7'),
191  '*/3' => array('*/3', FALSE, '1,4,7'),
192  'tue/2' => array('tue/2', FALSE, '2'),
193  '1-2 !monthField' => array('1-2', FALSE, '1,2'),
194  'tue-fri/2' => array('tue-fri/2', FALSE, '2,4'),
195  '1-3/2,tue,fri,6' => array('1-3/2,tue,fri,6', FALSE, '1,2,3,5,6')
196  );
197  }
198 
206  public function normalizeMonthAndWeekdayFieldReturnsNormalizedListForValidExpression($expression, $isMonthField, $expected) {
207  $accessibleProxyClassName = $this->getAccessibleProxy();
208  $result = $accessibleProxyClassName::normalizeMonthAndWeekdayField($expression, $isMonthField);
209  $this->assertSame($expected, $result);
210  }
211 
216  return array(
217  'mon' => array('mon', TRUE),
218  '1-2/mon' => array('1-2/mon', TRUE),
219  '0,1' => array('0,1', TRUE),
220  'feb' => array('feb', FALSE),
221  '1-2/feb' => array('1-2/feb', FALSE),
222  '0-fri/2,7' => array('0-fri/2,7', FALSE, '2,4,7')
223  );
224  }
225 
233  public function normalizeMonthAndWeekdayFieldThrowsExceptionForInvalidExpression($expression, $isMonthField) {
234  $accessibleProxyClassName = $this->getAccessibleProxy();
235  $result = $accessibleProxyClassName::normalizeMonthAndWeekdayField($expression, $isMonthField);
236  }
237 
241  static public function normalizeIntegerFieldValidDataProvider() {
242  return array(
243  '*' => array('*', '*'),
244  'string 2' => array('2', '2'),
245  'integer 3' => array(3, '3'),
246  'list of values' => array('1,2,3', '1,2,3'),
247  'unsorted list of values' => array('3,1,5', '1,3,5'),
248  'duplicate values' => array('0-2/2,2', '0,2'),
249  'additional field between steps' => array('1-3/2,2', '1,2,3'),
250  '2-4' => array('2-4', '2,3,4'),
251  'simple step 4/4' => array('4/4', '4'),
252  'step 2-7/5' => array('2-7/5', '2,7'),
253  'steps 4-12/4' => array('4-12/4', '4,8,12'),
254  '0-59/20' => array('0-59/20', '0,20,40'),
255  '*/20' => array('*/20', '0,20,40')
256  );
257  }
258 
265  public function normalizeIntegerFieldReturnsNormalizedListForValidExpression($expression, $expected) {
266  $accessibleProxyClassName = $this->getAccessibleProxy();
267  $result = $accessibleProxyClassName::normalizeIntegerField($expression);
268  $this->assertSame($expected, $result);
269  }
270 
274  static public function normalizeIntegerFieldInvalidDataProvider() {
275  return array(
276  'string foo' => array('foo', 0, 59),
277  'empty string' => array('', 0, 59),
278  '4-3' => array('4-3', 0, 59),
279  '/2' => array('/2', 0, 59),
280  '/' => array('/', 0, 59),
281  'left bound too low' => array('2-4', 3, 4),
282  'right bound too high' => array('2-4', 2, 3),
283  'left and right bound' => array('2-5', 2, 4),
284  'element in list is lower than allowed' => array('2,1,4', 2, 4),
285  'element in list is higher than allowed' => array('2,5,4', 1, 4)
286  );
287  }
288 
297  public function normalizeIntegerFieldThrowsExceptionForInvalidExpressions($expression, $lowerBound, $upperBound) {
298  $accessibleProxyClassName = $this->getAccessibleProxy();
299  $accessibleProxyClassName::normalizeIntegerField($expression, $lowerBound, $upperBound);
300  }
301 
306  $accessibleProxyClassName = $this->getAccessibleProxy();
307  $result = $accessibleProxyClassName::splitFields('12,13 * 1-12/2,14 jan fri');
308  $expectedResult = array(
309  0 => '12,13',
310  1 => '*',
311  2 => '1-12/2,14',
312  3 => 'jan',
313  4 => 'fri'
314  );
315  $this->assertSame($expectedResult, $result);
316  }
317 
321  static public function invalidCronCommandFieldsDataProvider() {
322  return array(
323  'empty string' => array(''),
324  'foo' => array('foo'),
325  'integer 4' => array(4),
326  'four fields' => array('* * * *'),
327  'six fields' => array('* * * * * *')
328  );
329  }
330 
338  $accessibleProxyClassName = $this->getAccessibleProxy();
339  $accessibleProxyClassName::splitFields($cronCommand);
340  }
341 
345  static public function validRangeDataProvider() {
346  return array(
347  'single value' => array('3', '3'),
348  'integer 3' => array(3, '3'),
349  '0-0' => array('0-0', '0'),
350  '4-4' => array('4-4', '4'),
351  '0-3' => array('0-3', '0,1,2,3'),
352  '4-5' => array('4-5', '4,5')
353  );
354  }
355 
363  $accessibleProxyClassName = $this->getAccessibleProxy();
364  $result = $accessibleProxyClassName::convertRangeToListOfValues($range);
365  $this->assertSame($expected, $result);
366  }
367 
371  static public function invalidRangeDataProvider() {
372  return array(
373  'empty string' => array(''),
374  'string' => array('foo'),
375  'single dash' => array('-'),
376  'left part is string' => array('foo-5'),
377  'right part is string' => array('5-foo'),
378  'range of strings' => array('foo-bar'),
379  'string five minus' => array('5-'),
380  'string minus five' => array('-5'),
381  'more than one dash' => array('2-3-4'),
382  'left part bigger than right part' => array('6-3')
383  );
384  }
385 
393  $accessibleProxyClassName = $this->getAccessibleProxy();
394  $accessibleProxyClassName::convertRangeToListOfValues($range);
395  }
396 
400  static public function validStepsDataProvider() {
401  return array(
402  '2/2' => array('2/2', '2'),
403  '2,3,4/2' => array('2,3,4/2', '2,4'),
404  '1,2,3,4,5,6,7/3' => array('1,2,3,4,5,6,7/3', '1,4,7'),
405  '0,1,2,3,4,5,6/3' => array('0,1,2,3,4,5,6/3', '0,3,6')
406  );
407  }
408 
415  public function reduceListOfValuesByStepValueReturnsCorrectListOfValues($stepExpression, $expected) {
416  $accessibleProxyClassName = $this->getAccessibleProxy();
417  $result = $accessibleProxyClassName::reduceListOfValuesByStepValue($stepExpression);
418  $this->assertSame($expected, $result);
419  }
420 
424  static public function invalidStepsDataProvider() {
425  return array(
426  'empty string' => array(''),
427  'slash only' => array('/'),
428  'left part empty' => array('/2'),
429  'right part empty' => array('2/'),
430  'multiples slashes' => array('1/2/3'),
431  '2-2' => array('2-2'),
432  '2.3/2' => array('2.3/2'),
433  '2,3,4/2.3' => array('2,3,4/2.3'),
434  '2,3,4/2,3' => array('2,3,4/2,3')
435  );
436  }
437 
445  $accessibleProxyClassName = $this->getAccessibleProxy();
446  $accessibleProxyClassName::reduceListOfValuesByStepValue($stepExpression);
447  }
448 
453  $accessibleProxyClassName = $this->getAccessibleProxy();
454  $result = $accessibleProxyClassName::normalizeMonthAndWeekday('feb', TRUE);
455  $this->assertSame('2', $result);
456  }
457 
462  $accessibleProxyClassName = $this->getAccessibleProxy();
463  $result = $accessibleProxyClassName::normalizeMonthAndWeekday('fri', FALSE);
464  $this->assertSame('5', $result);
465  }
466 
471  $accessibleProxyClassName = $this->getAccessibleProxy();
472  $result = $accessibleProxyClassName::normalizeMonthAndWeekday('2');
473  $this->assertSame('2', $result);
474  }
475 
479  static public function validMonthNamesDataProvider() {
480  return array(
481  'jan' => array('jan', 1),
482  'feb' => array('feb', 2),
483  'MaR' => array('MaR', 3),
484  'aPr' => array('aPr', 4),
485  'MAY' => array('MAY', 5),
486  'jun' => array('jun', 6),
487  'jul' => array('jul', 7),
488  'aug' => array('aug', 8),
489  'sep' => array('sep', 9),
490  'oct' => array('oct', 10),
491  'nov' => array('nov', 11),
492  'dec' => array('dec', 12),
493  'string 7' => array('7', 7),
494  'integer 7' => array(7, 7),
495  'string 07' => array('07', 7),
496  'integer 07' => array(7, 7)
497  );
498  }
499 
506  public function normalizeMonthConvertsName($monthName, $expectedInteger) {
507  $accessibleProxyClassName = $this->getAccessibleProxy();
508  $result = $accessibleProxyClassName::normalizeMonth($monthName);
509  $this->assertEquals($expectedInteger, $result);
510  }
511 
518  public function normalizeMonthReturnsInteger($monthName, $expectedInteger) {
519  $accessibleProxyClassName = $this->getAccessibleProxy();
520  $result = $accessibleProxyClassName::normalizeMonth($monthName);
521  $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
522  }
523 
527  static public function invalidMonthNamesDataProvider() {
528  return array(
529  'sep-' => array('sep-'),
530  '-September-' => array('-September-'),
531  ',sep' => array(',sep'),
532  ',September,' => array(',September,'),
533  'sep/' => array('sep/'),
534  '/sep' => array('/sep'),
535  '/September/' => array('/September/'),
536  'foo' => array('foo'),
537  'Tuesday' => array('Tuesday'),
538  'Tue' => array('Tue'),
539  'string 0' => array('0'),
540  'integer 0' => array(0),
541  'string seven' => array('seven'),
542  'string 13' => array('13'),
543  'integer 13' => array(13),
544  'integer 100' => array(100),
545  'integer 2010' => array(2010),
546  'string minus 7' => array('-7'),
547  'negative integer 7' => array(-7)
548  );
549  }
550 
558  $accessibleProxyClassName = $this->getAccessibleProxy();
559  $accessibleProxyClassName::normalizeMonth($invalidMonthName);
560  }
561 
565  static public function validWeekdayDataProvider() {
566  return array(
567  'string 1' => array('1', 1),
568  'string 2' => array('2', 2),
569  'string 02' => array('02', 2),
570  'integer 02' => array(2, 2),
571  'string 3' => array('3', 3),
572  'string 4' => array('4', 4),
573  'string 5' => array('5', 5),
574  'integer 5' => array(5, 5),
575  'string 6' => array('6', 6),
576  'string 7' => array('7', 7),
577  'string 0' => array('0', 7),
578  'integer 0' => array(0, 7),
579  'mon' => array('mon', 1),
580  'monday' => array('monday', 1),
581  'tue' => array('tue', 2),
582  'tuesday' => array('tuesday', 2),
583  'WED' => array('WED', 3),
584  'WEDnesday' => array('WEDnesday', 3),
585  'tHu' => array('tHu', 4),
586  'Thursday' => array('Thursday', 4),
587  'fri' => array('fri', 5),
588  'friday' => array('friday', 5),
589  'sat' => array('sat', 6),
590  'saturday' => array('saturday', 6),
591  'sun' => array('sun', 7),
592  'sunday' => array('sunday', 7)
593  );
594  }
595 
602  public function normalizeWeekdayConvertsName($weekday, $expectedInteger) {
603  $accessibleProxyClassName = $this->getAccessibleProxy();
604  $result = $accessibleProxyClassName::normalizeWeekday($weekday);
605  $this->assertEquals($expectedInteger, $result);
606  }
607 
614  public function normalizeWeekdayReturnsInteger($weekday, $expectedInteger) {
615  $accessibleProxyClassName = $this->getAccessibleProxy();
616  $result = $accessibleProxyClassName::normalizeWeekday($weekday);
617  $this->assertInternalType(\PHPUnit_Framework_Constraint_IsType::TYPE_INT, $result);
618  }
619 
623  static public function invalidWeekdayDataProvider() {
624  return array(
625  '-fri' => array('-fri'),
626  'fri-' => array('fri-'),
627  '-friday-' => array('-friday-'),
628  '/fri' => array('/fri'),
629  'fri/' => array('fri/'),
630  '/friday/' => array('/friday/'),
631  ',fri' => array(',fri'),
632  ',friday,' => array(',friday,'),
633  'string minus 1' => array('-1'),
634  'integer -1' => array(-1),
635  'string seven' => array('seven'),
636  'string 8' => array('8'),
637  'string 8' => array('8'),
638  'string 29' => array('29'),
639  'string 2010' => array('2010'),
640  'Jan' => array('Jan'),
641  'January' => array('January'),
642  'MARCH' => array('MARCH')
643  );
644  }
645 
653  $accessibleProxyClassName = $this->getAccessibleProxy();
654  $accessibleProxyClassName::normalizeWeekday($weekday);
655  }
656 
657 }
normalizeMonthAndWeekdayFieldReturnsNormalizedListForValidExpression($expression, $isMonthField, $expected)
normalizeMonthAndWeekdayFieldThrowsExceptionForInvalidExpression($expression, $isMonthField)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
normalizeIntegerFieldThrowsExceptionForInvalidExpressions($expression, $lowerBound, $upperBound)