MDL-25499: Centralise management of all types of cron tasks
[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
85 public function test_get_next_scheduled_task() {
86 global $DB;
87
88 $this->resetAfterTest(true);
89 // Delete all existing scheduled tasks.
90 $DB->delete_records('task_scheduled');
91 // Add a scheduled task.
92
93 // A task that runs once per hour.
94 $record = new stdClass();
95 $record->blocking = true;
96 $record->minute = '0';
97 $record->hour = '0';
98 $record->dayofweek = '*';
99 $record->day = '*';
100 $record->month = '*';
101 $record->component = 'test_scheduled_task';
102 $record->classname = '\\testable_scheduled_task';
103
104 $DB->insert_record('task_scheduled', $record);
105 // And another one to test failures.
106 $record->classname = '\\testable_scheduled_task2';
107 $DB->insert_record('task_scheduled', $record);
108 $now = time();
109
110 // Should get handed the first task.
111 $task = \core\task\manager::get_next_scheduled_task($now);
112 $this->assertNotNull($task);
113 $task->execute();
114
115 \core\task\manager::scheduled_task_complete($task);
116 // Should get handed the second task.
117 $task = \core\task\manager::get_next_scheduled_task($now);
118 $this->assertNotNull($task);
119 $task->execute();
120
121 \core\task\manager::scheduled_task_failed($task);
122 // Should not get any task.
123 $task = \core\task\manager::get_next_scheduled_task($now);
124 $this->assertNull($task);
125
126 // Should get the second task (retry after delay).
127 $task = \core\task\manager::get_next_scheduled_task($now + 120);
128 $this->assertNotNull($task);
129 $task->execute();
130
131 \core\task\manager::scheduled_task_complete($task);
132
133 // Should not get any task.
134 $task = \core\task\manager::get_next_scheduled_task($now);
135 $this->assertNull($task);
136 }
137}
138
139class testable_scheduled_task extends \core\task\scheduled_task {
140 public function get_name() {
141 return "Test task";
142 }
143
144 public function execute() {
145 }
146}
147
148class testable_scheduled_task2 extends \core\task\scheduled_task {
149 public function get_name() {
150 return "Test task 2";
151 }
152
153 public function execute() {
154 }
155}
156