Merge branch 'MDL-47808-master' of git://github.com/andrewnicols/moodle
[moodle.git] / lib / tests / scheduled_task_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * This file contains the unittests for scheduled tasks.
19  *
20  * @package   core
21  * @category  phpunit
22  * @copyright 2013 Damyon Wiese
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
27 require_once(__DIR__ . '/fixtures/task_fixtures.php');
29 /**
30  * Test class for scheduled task.
31  *
32  * @package core
33  * @category task
34  * @copyright 2013 Damyon Wiese
35  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class core_scheduled_task_testcase extends advanced_testcase {
39     /**
40      * Test the cron scheduling method
41      */
42     public function test_eval_cron_field() {
43         $testclass = new \core\task\scheduled_test_task();
45         $this->assertEquals(20, count($testclass->eval_cron_field('*/3', 0, 59)));
46         $this->assertEquals(31, count($testclass->eval_cron_field('1,*/2', 0, 59)));
47         $this->assertEquals(15, count($testclass->eval_cron_field('1-10,5-15', 0, 59)));
48         $this->assertEquals(13, count($testclass->eval_cron_field('1-10,5-15/2', 0, 59)));
49         $this->assertEquals(3, count($testclass->eval_cron_field('1,2,3,1,2,3', 0, 59)));
50         $this->assertEquals(1, count($testclass->eval_cron_field('-1,10,80', 0, 59)));
51     }
53     public function test_get_next_scheduled_time() {
54         // Test job run at 1 am.
55         $testclass = new \core\task\scheduled_test_task();
57         // All fields default to '*'.
58         $testclass->set_hour('1');
59         $testclass->set_minute('0');
60         // Next valid time should be 1am of the next day.
61         $nexttime = $testclass->get_next_scheduled_time();
63         $oneam = mktime(1, 0, 0);
64         // Make it 1 am tomorrow if the time is after 1am.
65         if ($oneam < time()) {
66             $oneam += 86400;
67         }
69         $this->assertEquals($oneam, $nexttime, 'Next scheduled time is 1am.');
71         // Disabled flag does not affect next time.
72         $testclass->set_disabled(true);
73         $nexttime = $testclass->get_next_scheduled_time();
74         $this->assertEquals($oneam, $nexttime, 'Next scheduled time is 1am.');
76         // Now test for job run every 10 minutes.
77         $testclass = new \core\task\scheduled_test_task();
79         // All fields default to '*'.
80         $testclass->set_minute('*/10');
81         // Next valid time should be next 10 minute boundary.
82         $nexttime = $testclass->get_next_scheduled_time();
84         $minutes = ((intval(date('i') / 10))+1) * 10;
85         $nexttenminutes = mktime(date('H'), $minutes, 0);
87         $this->assertEquals($nexttenminutes, $nexttime, 'Next scheduled time is in 10 minutes.');
89         // Disabled flag does not affect next time.
90         $testclass->set_disabled(true);
91         $nexttime = $testclass->get_next_scheduled_time();
92         $this->assertEquals($nexttenminutes, $nexttime, 'Next scheduled time is in 10 minutes.');
94         // Test hourly job executed on Sundays only.
95         $testclass = new \core\task\scheduled_test_task();
96         $testclass->set_minute('0');
97         $testclass->set_day_of_week('7');
99         $nexttime = $testclass->get_next_scheduled_time();
101         $this->assertEquals(7, date('N', $nexttime));
102         $this->assertEquals(0, date('i', $nexttime));
104         // Test monthly job
105         $testclass = new \core\task\scheduled_test_task();
106         $testclass->set_minute('32');
107         $testclass->set_hour('0');
108         $testclass->set_day('1');
110         $nexttime = $testclass->get_next_scheduled_time();
112         $this->assertEquals(32, date('i', $nexttime));
113         $this->assertEquals(0, date('G', $nexttime));
114         $this->assertEquals(1, date('j', $nexttime));
115     }
117     public function test_timezones() {
118         global $CFG, $USER;
120         // The timezones used in this test are chosen because they do not use DST - that would break the test.
122         $currenttimezonephp = date_default_timezone_get();
123         $currenttimezonecfg = null;
124         if (!empty($CFG->timezone)) {
125             $currenttimezonecfg = $CFG->timezone;
126         }
127         $userstimezone = null;
128         if (!empty($USER->timezone)) {
129             $userstimezone = $USER->timezone;
130         }
132         // We are testing a difference between $CFG->timezone and the php.ini timezone.
133         // GMT+8.
134         date_default_timezone_set('Australia/Perth');
135         // GMT-04:30.
136         $CFG->timezone = 'America/Caracas';
138         $testclass = new \core\task\scheduled_test_task();
140         // Scheduled tasks should always use servertime - so this is 03:30 GMT.
141         $testclass->set_hour('1');
142         $testclass->set_minute('0');
144         // Next valid time should be 1am of the next day.
145         $nexttime = $testclass->get_next_scheduled_time();
147         // GMT+05:45.
148         $USER->timezone = 'Asia/Kathmandu';
149         $userdate = userdate($nexttime);
151         // Should be displayed in user timezone.
152         // I used http://www.timeanddate.com/worldclock/fixedtime.html?msg=Moodle+Test&iso=20140314T01&p1=58
153         // to verify this time.
154         $this->assertContains('11:15 AM', core_text::strtoupper($userdate));
156         $CFG->timezone = $currenttimezonecfg;
157         date_default_timezone_set($currenttimezonephp);
158     }
160     public function test_get_next_scheduled_task() {
161         global $DB;
163         $this->resetAfterTest(true);
164         // Delete all existing scheduled tasks.
165         $DB->delete_records('task_scheduled');
166         // Add a scheduled task.
168         // A task that runs once per hour.
169         $record = new stdClass();
170         $record->blocking = true;
171         $record->minute = '0';
172         $record->hour = '0';
173         $record->dayofweek = '*';
174         $record->day = '*';
175         $record->month = '*';
176         $record->component = 'test_scheduled_task';
177         $record->classname = '\core\task\scheduled_test_task';
179         $DB->insert_record('task_scheduled', $record);
180         // And another one to test failures.
181         $record->classname = '\core\task\scheduled_test2_task';
182         $DB->insert_record('task_scheduled', $record);
183         // And disabled test.
184         $record->classname = '\core\task\scheduled_test3_task';
185         $record->disabled = 1;
186         $DB->insert_record('task_scheduled', $record);
188         $now = time();
190         // Should get handed the first task.
191         $task = \core\task\manager::get_next_scheduled_task($now);
192         $this->assertInstanceOf('\core\task\scheduled_test_task', $task);
193         $task->execute();
195         \core\task\manager::scheduled_task_complete($task);
196         // Should get handed the second task.
197         $task = \core\task\manager::get_next_scheduled_task($now);
198         $this->assertInstanceOf('\core\task\scheduled_test2_task', $task);
199         $task->execute();
201         \core\task\manager::scheduled_task_failed($task);
202         // Should not get any task.
203         $task = \core\task\manager::get_next_scheduled_task($now);
204         $this->assertNull($task);
206         // Should get the second task (retry after delay).
207         $task = \core\task\manager::get_next_scheduled_task($now + 120);
208         $this->assertInstanceOf('\core\task\scheduled_test2_task', $task);
209         $task->execute();
211         \core\task\manager::scheduled_task_complete($task);
213         // Should not get any task.
214         $task = \core\task\manager::get_next_scheduled_task($now);
215         $this->assertNull($task);
216     }
218     public function test_get_broken_scheduled_task() {
219         global $DB;
221         $this->resetAfterTest(true);
222         // Delete all existing scheduled tasks.
223         $DB->delete_records('task_scheduled');
224         // Add a scheduled task.
226         // A broken task that runs all the time.
227         $record = new stdClass();
228         $record->blocking = true;
229         $record->minute = '*';
230         $record->hour = '*';
231         $record->dayofweek = '*';
232         $record->day = '*';
233         $record->month = '*';
234         $record->component = 'test_scheduled_task';
235         $record->classname = '\core\task\scheduled_test_task_broken';
237         $DB->insert_record('task_scheduled', $record);
239         $now = time();
240         // Should not get any task.
241         $task = \core\task\manager::get_next_scheduled_task($now);
242         $this->assertDebuggingCalled();
243         $this->assertNull($task);
244     }
246     /**
247      * Tests the use of 'R' syntax in time fields of tasks to get
248      * tasks be configured with a non-uniform time.
249      */
250     public function test_random_time_specification() {
252         // Testing non-deterministic things in a unit test is not really
253         // wise, so we just test the values have changed within allowed bounds.
254         $testclass = new \core\task\scheduled_test_task();
256         // The test task defaults to '*'.
257         $this->assertInternalType('string', $testclass->get_minute());
258         $this->assertInternalType('string', $testclass->get_hour());
260         // Set a random value.
261         $testclass->set_minute('R');
262         $testclass->set_hour('R');
264         // Verify the minute has changed within allowed bounds.
265         $minute = $testclass->get_minute();
266         $this->assertInternalType('int', $minute);
267         $this->assertGreaterThanOrEqual(0, $minute);
268         $this->assertLessThanOrEqual(59, $minute);
270         // Verify the hour has changed within allowed bounds.
271         $hour = $testclass->get_hour();
272         $this->assertInternalType('int', $hour);
273         $this->assertGreaterThanOrEqual(0, $hour);
274         $this->assertLessThanOrEqual(23, $hour);
275     }