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