weekly release 2.8dev
[moodle.git] / lib / tests / scheduled_task_test.php
CommitLineData
309ae892
DW
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/>.
16
17/**
0a5aa65b 18 * This file contains the unittests for scheduled tasks.
309ae892
DW
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 */
25
26defined('MOODLE_INTERNAL') || die();
0a5aa65b 27require_once(__DIR__ . '/fixtures/task_fixtures.php');
309ae892
DW
28
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 */
0a5aa65b 37class core_scheduled_task_testcase extends advanced_testcase {
309ae892
DW
38
39 /**
40 * Test the cron scheduling method
41 */
42 public function test_eval_cron_field() {
0a5aa65b 43 $testclass = new \core\task\scheduled_test_task();
309ae892
DW
44
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 }
52
53 public function test_get_next_scheduled_time() {
54 // Test job run at 1 am.
0a5aa65b 55 $testclass = new \core\task\scheduled_test_task();
309ae892
DW
56
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();
62
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 }
68
69 $this->assertEquals($oneam, $nexttime, 'Next scheduled time is 1am.');
70
0a5aa65b
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.');
75
309ae892 76 // Now test for job run every 10 minutes.
0a5aa65b 77 $testclass = new \core\task\scheduled_test_task();
309ae892
DW
78
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();
83
84 $minutes = ((intval(date('i') / 10))+1) * 10;
85 $nexttenminutes = mktime(date('H'), $minutes, 0);
86
87 $this->assertEquals($nexttenminutes, $nexttime, 'Next scheduled time is in 10 minutes.');
0a5aa65b
88
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.');
309ae892
DW
93 }
94
bbd9226c
DW
95 public function test_timezones() {
96 global $CFG, $USER;
97
98 // The timezones used in this test are chosen because they do not use DST - that would break the test.
99
100 $currenttimezonephp = date_default_timezone_get();
101 $currenttimezonecfg = null;
102 if (!empty($CFG->timezone)) {
103 $currenttimezonecfg = $CFG->timezone;
104 }
105 $userstimezone = null;
106 if (!empty($USER->timezone)) {
107 $userstimezone = $USER->timezone;
108 }
109
110 // We are testing a difference between $CFG->timezone and the php.ini timezone.
111 // GMT+8.
112 date_default_timezone_set('Australia/Perth');
113 // GMT-04:30.
114 $CFG->timezone = 'America/Caracas';
115
0a5aa65b 116 $testclass = new \core\task\scheduled_test_task();
bbd9226c
DW
117
118 // Scheduled tasks should always use servertime - so this is 03:30 GMT.
119 $testclass->set_hour('1');
120 $testclass->set_minute('0');
121
122 // Next valid time should be 1am of the next day.
123 $nexttime = $testclass->get_next_scheduled_time();
124
125 // GMT+05:45.
126 $USER->timezone = 'Asia/Kathmandu';
127 $userdate = userdate($nexttime);
128
129 // Should be displayed in user timezone.
130 // I used http://www.timeanddate.com/worldclock/fixedtime.html?msg=Moodle+Test&iso=20140314T01&p1=58
131 // to verify this time.
9be8583a 132 $this->assertContains('11:15 AM', core_text::strtoupper($userdate));
bbd9226c
DW
133
134 $CFG->timezone = $currenttimezonecfg;
135 date_default_timezone_set($currenttimezonephp);
136 }
137
309ae892
DW
138 public function test_get_next_scheduled_task() {
139 global $DB;
140
141 $this->resetAfterTest(true);
142 // Delete all existing scheduled tasks.
143 $DB->delete_records('task_scheduled');
144 // Add a scheduled task.
145
146 // A task that runs once per hour.
147 $record = new stdClass();
148 $record->blocking = true;
149 $record->minute = '0';
150 $record->hour = '0';
151 $record->dayofweek = '*';
152 $record->day = '*';
153 $record->month = '*';
154 $record->component = 'test_scheduled_task';
0a5aa65b 155 $record->classname = '\core\task\scheduled_test_task';
309ae892
DW
156
157 $DB->insert_record('task_scheduled', $record);
158 // And another one to test failures.
0a5aa65b
159 $record->classname = '\core\task\scheduled_test2_task';
160 $DB->insert_record('task_scheduled', $record);
161 // And disabled test.
162 $record->classname = '\core\task\scheduled_test3_task';
163 $record->disabled = 1;
309ae892 164 $DB->insert_record('task_scheduled', $record);
0a5aa65b 165
309ae892
DW
166 $now = time();
167
168 // Should get handed the first task.
169 $task = \core\task\manager::get_next_scheduled_task($now);
0a5aa65b 170 $this->assertInstanceOf('\core\task\scheduled_test_task', $task);
309ae892
DW
171 $task->execute();
172
173 \core\task\manager::scheduled_task_complete($task);
174 // Should get handed the second task.
175 $task = \core\task\manager::get_next_scheduled_task($now);
0a5aa65b 176 $this->assertInstanceOf('\core\task\scheduled_test2_task', $task);
309ae892
DW
177 $task->execute();
178
179 \core\task\manager::scheduled_task_failed($task);
180 // Should not get any task.
181 $task = \core\task\manager::get_next_scheduled_task($now);
182 $this->assertNull($task);
183
184 // Should get the second task (retry after delay).
185 $task = \core\task\manager::get_next_scheduled_task($now + 120);
0a5aa65b 186 $this->assertInstanceOf('\core\task\scheduled_test2_task', $task);
309ae892
DW
187 $task->execute();
188
189 \core\task\manager::scheduled_task_complete($task);
190
191 // Should not get any task.
192 $task = \core\task\manager::get_next_scheduled_task($now);
193 $this->assertNull($task);
194 }
195}