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