1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\CronTask\Tests; |
4
|
|
|
|
5
|
|
|
use Cron\CronExpression; |
6
|
|
|
use SilverStripe\CronTask\Controllers\CronTask; |
7
|
|
|
use SilverStripe\CronTask\Controllers\CronTaskController; |
8
|
|
|
use SilverStripe\CronTask\CronTaskStatus; |
9
|
|
|
use SilverStripe\Dev\FunctionalTest; |
10
|
|
|
use SilverStripe\ORM\FieldType\DBDate; |
11
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @package crontask |
15
|
|
|
*/ |
16
|
|
|
class CronTaskControllerTest extends FunctionalTest |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritDoc} |
20
|
|
|
* @var bool |
21
|
|
|
*/ |
22
|
|
|
protected $usesDatabase = true; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
*/ |
27
|
|
|
protected function setUp() |
28
|
|
|
{ |
29
|
|
|
parent::setUp(); |
30
|
|
|
CronTaskTest\TestCron::$times_run = 0; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Tests CronTaskController::isTaskDue |
35
|
|
|
*/ |
36
|
|
|
public function testIsTaskDue() |
37
|
|
|
{ |
38
|
|
|
$runner = CronTaskController::create(); |
39
|
|
|
$task = new CronTaskTest\TestCron(); |
40
|
|
|
$cron = CronExpression::factory($task->getSchedule()); |
41
|
|
|
|
42
|
|
|
// Assuming first run, match the exact time (seconds are ignored) |
43
|
|
|
DBDatetime::set_mock_now('2010-06-20 13:00:10'); |
44
|
|
|
$this->assertTrue($runner->isTaskDue($task, $cron)); |
45
|
|
|
|
46
|
|
|
// Assume first run, do not match time just before or just after schedule |
47
|
|
|
DBDatetime::set_mock_now('2010-06-20 13:01:10'); |
48
|
|
|
$this->assertFalse($runner->isTaskDue($task, $cron)); |
49
|
|
|
DBDatetime::set_mock_now('2010-06-20 12:59:50'); |
50
|
|
|
$this->assertFalse($runner->isTaskDue($task, $cron)); |
51
|
|
|
|
52
|
|
|
// Mock a run and test that subsequent runs are properly scheduled |
53
|
|
|
DBDatetime::set_mock_now('2010-06-20 13:30:10'); |
54
|
|
|
CronTaskStatus::update_status('SilverStripe\\CronTask\\Tests\\CronTaskTest\\TestCron', true); |
55
|
|
|
|
56
|
|
|
// Job prior to next hour mark should not run |
57
|
|
|
DBDatetime::set_mock_now('2010-06-20 13:40:00'); |
58
|
|
|
$this->assertFalse($runner->isTaskDue($task, $cron)); |
59
|
|
|
|
60
|
|
|
// Jobs just after the next hour mark should run |
61
|
|
|
DBDatetime::set_mock_now('2010-06-20 14:10:00'); |
62
|
|
|
$this->assertTrue($runner->isTaskDue($task, $cron)); |
63
|
|
|
|
64
|
|
|
// Jobs somehow delayed a whole day should be run |
65
|
|
|
DBDatetime::set_mock_now('2010-06-21 13:40:00'); |
66
|
|
|
$this->assertTrue($runner->isTaskDue($task, $cron)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Test CronTaskController::runTask |
71
|
|
|
*/ |
72
|
|
|
public function testRunTask() |
73
|
|
|
{ |
74
|
|
|
$runner = CronTaskController::create(); |
75
|
|
|
$runner->setQuiet(true); |
|
|
|
|
76
|
|
|
$task = new CronTaskTest\TestCron(); |
77
|
|
|
|
78
|
|
|
// Assuming first run, match the exact time (seconds are ignored) |
79
|
|
|
$this->assertEquals(0, CronTaskTest\TestCron::$times_run); |
80
|
|
|
DBDatetime::set_mock_now('2010-06-20 13:00:10'); |
81
|
|
|
$runner->runTask($task); |
82
|
|
|
$this->assertEquals(1, CronTaskTest\TestCron::$times_run); |
83
|
|
|
|
84
|
|
|
// Test that re-requsting the task in the same minute do not retrigger another run |
85
|
|
|
DBDatetime::set_mock_now('2010-06-20 13:00:40'); |
86
|
|
|
$runner->runTask($task); |
87
|
|
|
$this->assertEquals(1, CronTaskTest\TestCron::$times_run); |
88
|
|
|
|
89
|
|
|
// Job prior to next hour mark should not run |
90
|
|
|
DBDatetime::set_mock_now('2010-06-20 13:40:00'); |
91
|
|
|
$runner->runTask($task); |
92
|
|
|
$this->assertEquals(1, CronTaskTest\TestCron::$times_run); |
93
|
|
|
|
94
|
|
|
// Jobs just after the next hour mark should run |
95
|
|
|
DBDatetime::set_mock_now('2010-06-20 14:10:00'); |
96
|
|
|
$runner->runTask($task); |
97
|
|
|
$this->assertEquals(2, CronTaskTest\TestCron::$times_run); |
98
|
|
|
|
99
|
|
|
// Jobs run on the exact next expected date should run |
100
|
|
|
DBDatetime::set_mock_now('2010-06-20 15:00:00'); |
101
|
|
|
$runner->runTask($task); |
102
|
|
|
$this->assertEquals(3, CronTaskTest\TestCron::$times_run); |
103
|
|
|
|
104
|
|
|
// Jobs somehow delayed a whole day should be run |
105
|
|
|
DBDatetime::set_mock_now('2010-06-21 13:40:00'); |
106
|
|
|
$runner->runTask($task); |
107
|
|
|
$this->assertEquals(4, CronTaskTest\TestCron::$times_run); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
// normal cron output includes the current date/time - we check for that |
112
|
|
|
// the exact output here could vary depending on what other modules are installed |
113
|
|
|
public function testDefaultQuietFlagOutput() |
114
|
|
|
{ |
115
|
|
|
$this->loginWithPermission('ADMIN'); |
116
|
|
|
$this->expectOutputRegex('#' . DBDatetime::now()->Format(DBDate::ISO_DATE) . '#'); |
117
|
|
|
$this->get('dev/cron?debug=1'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// with the flag set we want no output |
121
|
|
|
public function testQuietFlagOnOutput() |
122
|
|
|
{ |
123
|
|
|
$this->loginWithPermission('ADMIN'); |
124
|
|
|
$this->expectOutputString(''); |
125
|
|
|
$this->get('dev/cron?quiet=1'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.