1 |
efrain |
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 |
namespace core\task;
|
|
|
18 |
|
|
|
19 |
defined('MOODLE_INTERNAL') || die();
|
|
|
20 |
|
|
|
21 |
// We need to keep this here because there is a provider
|
|
|
22 |
// needing \core\task\adhoc_test_task and cannot move it
|
|
|
23 |
// to setUpBeforeClass() or similar. Whenever we allow to
|
|
|
24 |
// autoload fixtures, this can be removed.
|
|
|
25 |
require_once(__DIR__ . '/../fixtures/task_fixtures.php');
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* This file contains the unit tests for the task manager.
|
|
|
29 |
*
|
|
|
30 |
* @package core
|
|
|
31 |
* @category test
|
|
|
32 |
* @copyright 2019 Brendan Heywood <brendan@catalyst-au.net>
|
|
|
33 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
34 |
* @covers \core\task\manager
|
|
|
35 |
*/
|
|
|
36 |
final class manager_test extends \advanced_testcase {
|
|
|
37 |
/**
|
|
|
38 |
* Data provider for test_get_candidate_adhoc_tasks.
|
|
|
39 |
*
|
|
|
40 |
* @return array
|
|
|
41 |
*/
|
|
|
42 |
public static function get_candidate_adhoc_tasks_provider(): array {
|
|
|
43 |
return [
|
|
|
44 |
[
|
|
|
45 |
'concurrencylimit' => 5,
|
|
|
46 |
'limit' => 100,
|
|
|
47 |
'pertasklimits' => [],
|
|
|
48 |
'tasks' => [
|
|
|
49 |
new adhoc_test_task(time() - 20, null),
|
|
|
50 |
new adhoc_test_task(time() - 20, null),
|
|
|
51 |
new adhoc_test_task(time() - 20, null),
|
|
|
52 |
new adhoc_test_task(time() - 20, null),
|
|
|
53 |
new adhoc_test_task(time() - 20, null),
|
|
|
54 |
],
|
|
|
55 |
'expected' => [
|
|
|
56 |
adhoc_test_task::class,
|
|
|
57 |
adhoc_test_task::class,
|
|
|
58 |
adhoc_test_task::class,
|
|
|
59 |
adhoc_test_task::class,
|
|
|
60 |
adhoc_test_task::class,
|
|
|
61 |
],
|
|
|
62 |
],
|
|
|
63 |
[
|
|
|
64 |
'concurrencylimit' => 5,
|
|
|
65 |
'limit' => 100,
|
|
|
66 |
'pertasklimits' => [],
|
|
|
67 |
'tasks' => [
|
|
|
68 |
new adhoc_test_task(time() - 20, time()),
|
|
|
69 |
new adhoc_test_task(time() - 20, null),
|
|
|
70 |
new adhoc_test_task(time() - 20, null),
|
|
|
71 |
new adhoc_test_task(time() - 20, null),
|
|
|
72 |
new adhoc_test_task(time() - 20, null),
|
|
|
73 |
],
|
|
|
74 |
'expected' => [
|
|
|
75 |
adhoc_test_task::class,
|
|
|
76 |
adhoc_test_task::class,
|
|
|
77 |
adhoc_test_task::class,
|
|
|
78 |
adhoc_test_task::class,
|
|
|
79 |
],
|
|
|
80 |
],
|
|
|
81 |
[
|
|
|
82 |
'concurrencylimit' => 1,
|
|
|
83 |
'limit' => 100,
|
|
|
84 |
'pertasklimits' => [],
|
|
|
85 |
'tasks' => [
|
|
|
86 |
new adhoc_test_task(time() - 20, time()),
|
|
|
87 |
new adhoc_test_task(time() - 20, null),
|
|
|
88 |
new adhoc_test_task(time() - 20, null),
|
|
|
89 |
new adhoc_test_task(time() - 20, null),
|
|
|
90 |
new adhoc_test_task(time() - 20, null),
|
|
|
91 |
],
|
|
|
92 |
'expected' => [],
|
|
|
93 |
],
|
|
|
94 |
[
|
|
|
95 |
'concurrencylimit' => 2,
|
|
|
96 |
'limit' => 100,
|
|
|
97 |
'pertasklimits' => [],
|
|
|
98 |
'tasks' => [
|
|
|
99 |
new adhoc_test_task(time() - 20, time()),
|
|
|
100 |
new adhoc_test_task(time() - 20, time()),
|
|
|
101 |
new adhoc_test_task(time() - 20, null),
|
|
|
102 |
new adhoc_test_task(time() - 20, null),
|
|
|
103 |
new adhoc_test_task(time() - 20, null),
|
|
|
104 |
],
|
|
|
105 |
'expected' => [],
|
|
|
106 |
],
|
|
|
107 |
[
|
|
|
108 |
'concurrencylimit' => 2,
|
|
|
109 |
'limit' => 100,
|
|
|
110 |
'pertasklimits' => [],
|
|
|
111 |
'tasks' => [
|
|
|
112 |
new adhoc_test_task(time() - 20, time()),
|
|
|
113 |
new adhoc_test_task(time() - 20, time()),
|
|
|
114 |
new adhoc_test2_task(time() - 20, time()),
|
|
|
115 |
new adhoc_test2_task(time() - 20, time()),
|
|
|
116 |
new adhoc_test3_task(time() - 20, null),
|
|
|
117 |
],
|
|
|
118 |
'expected' => [adhoc_test3_task::class],
|
|
|
119 |
],
|
|
|
120 |
[
|
|
|
121 |
'concurrencylimit' => 2,
|
|
|
122 |
'limit' => 2,
|
|
|
123 |
'pertasklimits' => [],
|
|
|
124 |
'tasks' => [
|
|
|
125 |
new adhoc_test_task(time() - 20, null),
|
|
|
126 |
new adhoc_test_task(time() - 20, null),
|
|
|
127 |
new adhoc_test_task(time() - 20, null),
|
|
|
128 |
new adhoc_test2_task(time() - 20, null),
|
|
|
129 |
],
|
|
|
130 |
'expected' => [
|
|
|
131 |
adhoc_test_task::class,
|
|
|
132 |
adhoc_test_task::class,
|
|
|
133 |
],
|
|
|
134 |
],
|
|
|
135 |
[
|
|
|
136 |
'concurrencylimit' => 2,
|
|
|
137 |
'limit' => 2,
|
|
|
138 |
'pertasklimits' => [],
|
|
|
139 |
'tasks' => [
|
|
|
140 |
new adhoc_test_task(time() - 20, time()),
|
|
|
141 |
new adhoc_test_task(time() - 20, time()),
|
|
|
142 |
new adhoc_test_task(time() - 20, null),
|
|
|
143 |
new adhoc_test2_task(time() - 20, null),
|
|
|
144 |
],
|
|
|
145 |
'expected' => [
|
|
|
146 |
adhoc_test2_task::class,
|
|
|
147 |
],
|
|
|
148 |
],
|
|
|
149 |
[
|
|
|
150 |
'concurrencylimit' => 3,
|
|
|
151 |
'limit' => 100,
|
|
|
152 |
'pertasklimits' => [],
|
|
|
153 |
'tasks' => [
|
|
|
154 |
new adhoc_test_task(time() - 20, time()),
|
|
|
155 |
new adhoc_test_task(time() - 20, time()),
|
|
|
156 |
new adhoc_test_task(time() - 20, null),
|
|
|
157 |
new adhoc_test2_task(time() - 20, time()),
|
|
|
158 |
new adhoc_test2_task(time() - 20, time()),
|
|
|
159 |
new adhoc_test2_task(time() - 20, null),
|
|
|
160 |
new adhoc_test3_task(time() - 20, time()),
|
|
|
161 |
new adhoc_test3_task(time() - 20, time()),
|
|
|
162 |
new adhoc_test3_task(time() - 20, null),
|
|
|
163 |
new adhoc_test4_task(time() - 20, time()),
|
|
|
164 |
new adhoc_test4_task(time() - 20, time()),
|
|
|
165 |
new adhoc_test4_task(time() - 20, null),
|
|
|
166 |
new adhoc_test5_task(time() - 20, time()),
|
|
|
167 |
new adhoc_test5_task(time() - 20, time()),
|
|
|
168 |
new adhoc_test5_task(time() - 20, null),
|
|
|
169 |
],
|
|
|
170 |
'expected' => [
|
|
|
171 |
adhoc_test_task::class,
|
|
|
172 |
adhoc_test2_task::class,
|
|
|
173 |
adhoc_test3_task::class,
|
|
|
174 |
adhoc_test4_task::class,
|
|
|
175 |
adhoc_test5_task::class,
|
|
|
176 |
],
|
|
|
177 |
],
|
|
|
178 |
[
|
|
|
179 |
'concurrencylimit' => 3,
|
|
|
180 |
'limit' => 100,
|
|
|
181 |
'pertasklimits' => [
|
|
|
182 |
'adhoc_test_task' => 2,
|
|
|
183 |
'adhoc_test2_task' => 2,
|
|
|
184 |
'adhoc_test3_task' => 2,
|
|
|
185 |
'adhoc_test4_task' => 2,
|
|
|
186 |
'adhoc_test5_task' => 2,
|
|
|
187 |
],
|
|
|
188 |
'tasks' => [
|
|
|
189 |
new adhoc_test_task(time() - 20, time()),
|
|
|
190 |
new adhoc_test_task(time() - 20, time()),
|
|
|
191 |
new adhoc_test_task(time() - 20, null),
|
|
|
192 |
new adhoc_test2_task(time() - 20, time()),
|
|
|
193 |
new adhoc_test2_task(time() - 20, time()),
|
|
|
194 |
new adhoc_test2_task(time() - 20, null),
|
|
|
195 |
new adhoc_test3_task(time() - 20, time()),
|
|
|
196 |
new adhoc_test3_task(time() - 20, time()),
|
|
|
197 |
new adhoc_test3_task(time() - 20, null),
|
|
|
198 |
new adhoc_test4_task(time() - 20, time()),
|
|
|
199 |
new adhoc_test4_task(time() - 20, time()),
|
|
|
200 |
new adhoc_test4_task(time() - 20, null),
|
|
|
201 |
new adhoc_test5_task(time() - 20, time()),
|
|
|
202 |
new adhoc_test5_task(time() - 20, time()),
|
|
|
203 |
new adhoc_test5_task(time() - 20, null),
|
|
|
204 |
],
|
|
|
205 |
'expected' => [],
|
|
|
206 |
],
|
|
|
207 |
];
|
|
|
208 |
}
|
|
|
209 |
|
|
|
210 |
/**
|
|
|
211 |
* Test that the candidate adhoc tasks are returned in the right order.
|
|
|
212 |
*
|
|
|
213 |
* @dataProvider get_candidate_adhoc_tasks_provider
|
|
|
214 |
*
|
|
|
215 |
* @param int $concurrencylimit The max number of runners each task can consume
|
|
|
216 |
* @param int $limit SQL limit
|
|
|
217 |
* @param array $pertasklimits Per-task limits
|
|
|
218 |
* @param array $tasks Array of tasks to put in DB and retrieve
|
|
|
219 |
* @param array $expected Array of expected classnames
|
|
|
220 |
*/
|
|
|
221 |
public function test_get_candidate_adhoc_tasks(
|
|
|
222 |
int $concurrencylimit,
|
|
|
223 |
int $limit,
|
|
|
224 |
array $pertasklimits,
|
|
|
225 |
array $tasks,
|
|
|
226 |
array $expected
|
|
|
227 |
): void {
|
|
|
228 |
$this->resetAfterTest();
|
|
|
229 |
|
|
|
230 |
foreach ($tasks as $task) {
|
|
|
231 |
manager::queue_adhoc_task($task);
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
$candidates = manager::get_candidate_adhoc_tasks(time(), $limit, $concurrencylimit, $pertasklimits);
|
|
|
235 |
$this->assertEquals(
|
|
|
236 |
array_map(
|
|
|
237 |
function (string $classname): string {
|
|
|
238 |
return '\\' . $classname;
|
|
|
239 |
},
|
|
|
240 |
$expected
|
|
|
241 |
),
|
|
|
242 |
array_column($candidates, 'classname')
|
|
|
243 |
);
|
|
|
244 |
}
|
|
|
245 |
|
|
|
246 |
/**
|
|
|
247 |
* Test that adhoc tasks are set as failed when shutdown is called during execution.
|
|
|
248 |
*/
|
|
|
249 |
public function test_adhoc_task_running_will_fail_when_shutdown(): void {
|
|
|
250 |
$this->resetAfterTest();
|
|
|
251 |
$this->preventResetByRollback();
|
|
|
252 |
|
|
|
253 |
$task1 = new adhoc_test_task();
|
|
|
254 |
$task1->set_next_run_time(time() - 20);
|
|
|
255 |
manager::queue_adhoc_task($task1);
|
|
|
256 |
|
|
|
257 |
$next1 = manager::get_next_adhoc_task(time());
|
|
|
258 |
\core\task\manager::adhoc_task_starting($next1);
|
|
|
259 |
|
|
|
260 |
self::assertEmpty(manager::get_failed_adhoc_tasks());
|
|
|
261 |
|
|
|
262 |
// Trigger shutdown handler.
|
|
|
263 |
\core_shutdown_manager::shutdown_handler();
|
|
|
264 |
|
|
|
265 |
$failedtasks = manager::get_failed_adhoc_tasks();
|
|
|
266 |
|
|
|
267 |
self::assertCount(1, $failedtasks);
|
|
|
268 |
self::assertEquals($next1->get_id(), $failedtasks[0]->get_id());
|
|
|
269 |
}
|
|
|
270 |
|
|
|
271 |
/**
|
|
|
272 |
* Test that scheduled tasks are set as failed when shutdown is called during execution.
|
|
|
273 |
*/
|
|
|
274 |
public function test_scheduled_task_running_will_fail_when_shutdown(): void {
|
|
|
275 |
global $DB;
|
|
|
276 |
|
|
|
277 |
$this->resetAfterTest();
|
|
|
278 |
$this->preventResetByRollback();
|
|
|
279 |
|
|
|
280 |
// Disable all the tasks, so we can insert our own and be sure it's the only one being run.
|
|
|
281 |
$DB->set_field('task_scheduled', 'disabled', 1);
|
|
|
282 |
|
|
|
283 |
$task1 = new scheduled_test_task();
|
|
|
284 |
$task1->set_minute('*');
|
|
|
285 |
$task1->set_next_run_time(time() - HOURSECS);
|
|
|
286 |
$DB->insert_record('task_scheduled', manager::record_from_scheduled_task($task1));
|
|
|
287 |
|
|
|
288 |
$next1 = \core\task\manager::get_next_scheduled_task(time());
|
|
|
289 |
\core\task\manager::scheduled_task_starting($next1);
|
|
|
290 |
|
|
|
291 |
$running = manager::get_running_tasks();
|
|
|
292 |
$this->assertCount(1, $running);
|
|
|
293 |
|
|
|
294 |
// Trigger shutdown handler.
|
|
|
295 |
\core_shutdown_manager::shutdown_handler();
|
|
|
296 |
|
|
|
297 |
$running = manager::get_running_tasks();
|
|
|
298 |
$this->assertCount(0, $running);
|
|
|
299 |
|
|
|
300 |
$scheduledtask1 = manager::get_scheduled_task(scheduled_test_task::class);
|
|
|
301 |
self::assertGreaterThan($next1->get_fail_delay(), $scheduledtask1->get_fail_delay());
|
|
|
302 |
}
|
11 |
efrain |
303 |
|
|
|
304 |
public function test_get_next_adhoc_task_will_respect_failed_tasks(): void {
|
|
|
305 |
// Create three tasks, one is burned on the first get_next_adhoc_task() call to build up the cache,
|
|
|
306 |
// the second will be set to failed and the third is required to make the "uniquetasksinqueue" query
|
|
|
307 |
// within the get_next_adhoc_task() function not returning a different count of remaining unique tasks.
|
|
|
308 |
manager::queue_adhoc_task(new adhoc_test_task());
|
|
|
309 |
manager::queue_adhoc_task(new adhoc_test_task());
|
|
|
310 |
manager::queue_adhoc_task(new adhoc_test_task());
|
|
|
311 |
$timestart = time();
|
|
|
312 |
|
|
|
313 |
$candidates = manager::get_candidate_adhoc_tasks($timestart, 4, null);
|
|
|
314 |
$this->assertEquals(count($candidates), 3);
|
|
|
315 |
$task1 = manager::adhoc_task_from_record(array_shift($candidates));
|
|
|
316 |
$task2 = manager::adhoc_task_from_record(array_shift($candidates));
|
|
|
317 |
$task3 = manager::adhoc_task_from_record(array_shift($candidates));
|
|
|
318 |
|
|
|
319 |
// Build up the cache by getting the first task.
|
|
|
320 |
$task = manager::get_next_adhoc_task($timestart);
|
|
|
321 |
// Release the lock by completing the task to avoid "A lock was created but not released" error if the assertion fails.
|
|
|
322 |
manager::adhoc_task_complete($task);
|
|
|
323 |
$this->assertEquals($task->get_id(), $task1->get_id());
|
|
|
324 |
|
|
|
325 |
// Make $task2 failed...
|
|
|
326 |
try {
|
|
|
327 |
// Expecting "Error: Call to a member function release() on null" as the task was not locked before.
|
|
|
328 |
manager::adhoc_task_failed($task2);
|
|
|
329 |
} catch (\Throwable $t) {
|
|
|
330 |
// Ignoring "Call to a member function release() on null" and throw anything else.
|
|
|
331 |
if ($t->getMessage() != "Call to a member function release() on null") {
|
|
|
332 |
throw $t;
|
|
|
333 |
}
|
|
|
334 |
}
|
|
|
335 |
$task = manager::get_next_adhoc_task($timestart);
|
|
|
336 |
// Release the lock by completing the task to avoid "A lock was created but not released" error if the assertion fails.
|
|
|
337 |
manager::adhoc_task_complete($task);
|
|
|
338 |
// Task $task2 should not be returned because it has failed meanwhile and
|
|
|
339 |
// therefore has its nextruntime in the future...
|
|
|
340 |
$this->assertNotEquals($task->get_id(), $task2->get_id());
|
|
|
341 |
|
|
|
342 |
// Just to make sure check that the complete queue is as expected.
|
|
|
343 |
$this->assertEquals($task->get_id(), $task3->get_id());
|
|
|
344 |
// Now the queue should be empty...
|
|
|
345 |
$task = manager::get_next_adhoc_task($timestart);
|
|
|
346 |
$this->assertNull($task);
|
|
|
347 |
|
|
|
348 |
$this->resetAfterTest();
|
|
|
349 |
}
|
1441 |
ariadna |
350 |
|
|
|
351 |
/**
|
|
|
352 |
* Test verifying \core\task\manager behaviour for scheduled tasks when dealing with deprecated plugin types.
|
|
|
353 |
*
|
|
|
354 |
* This only verifies that existing tasks will not be listed, or returned for execution via existing APIs, like:
|
|
|
355 |
* - {@see \core\task\manager::get_all_scheduled_tasks}
|
|
|
356 |
* - {@see \core\task\manager::get_next_scheduled_task}
|
|
|
357 |
*
|
|
|
358 |
* I.e. Nothing prevents task->execute() from running if called directly.
|
|
|
359 |
*
|
|
|
360 |
* @return void
|
|
|
361 |
*/
|
|
|
362 |
public function test_scheduled_tasks_deprecated_plugintype(): void {
|
|
|
363 |
$this->resetAfterTest();
|
|
|
364 |
global $DB, $CFG;
|
|
|
365 |
|
|
|
366 |
$fakepluginroot = $CFG->libdir . '/tests/fixtures/fakeplugins/fake/fullfeatured';
|
|
|
367 |
require_once($fakepluginroot . '/classes/plugininfo/fullsubtype.php');
|
|
|
368 |
require_once($fakepluginroot . '/classes/plugininfo/fulldeprecatedsubtype.php');
|
|
|
369 |
require_once($fakepluginroot . '/fullsubtype/example/classes/task/scheduled_test.php');
|
|
|
370 |
require_once($fakepluginroot . '/fulldeprecatedsubtype/test/classes/task/scheduled_test.php');
|
|
|
371 |
|
|
|
372 |
// Inject stub plugininfo instances into a stub plugin manager, then inject that into the static cache via reflection.
|
|
|
373 |
// When the manager code calls core_plugin_manager::instance(), it'll get back the stub.
|
|
|
374 |
$stubavailableplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fullsubtype::class);
|
|
|
375 |
$stubavailableplugininfo->method('is_deprecated')->willReturn(false);
|
|
|
376 |
$stubavailableplugininfo->component = "fullsubtype_example";
|
|
|
377 |
$stubdeprecatedplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fulldeprecatedsubtype::class);
|
|
|
378 |
$stubdeprecatedplugininfo->method('is_deprecated')->willReturn(true);
|
|
|
379 |
$stubdeprecatedplugininfo->component = "fulldeprecatedsubtype_test";
|
|
|
380 |
|
|
|
381 |
$stubpluginman = $this->createStub(\core_plugin_manager::class);
|
|
|
382 |
$stubpluginman
|
|
|
383 |
->method('get_plugin_info')
|
|
|
384 |
->will($this->returnValueMap([
|
|
|
385 |
['fullsubtype_example', $stubavailableplugininfo],
|
|
|
386 |
['fulldeprecatedsubtype_test', $stubdeprecatedplugininfo],
|
|
|
387 |
]));
|
|
|
388 |
|
|
|
389 |
$pluginman = new \ReflectionClass(\core_plugin_manager::class);
|
|
|
390 |
$pluginman->setStaticPropertyValue('singletoninstance', $stubpluginman);
|
|
|
391 |
|
|
|
392 |
$DB->delete_records('task_scheduled');
|
|
|
393 |
|
|
|
394 |
// Non-deprecated plugin type: is listed and is returned during scheduling.
|
|
|
395 |
$scheduledtask = new \fullsubtype_example\task\scheduled_test();
|
|
|
396 |
$DB->insert_record('task_scheduled', \core\task\manager::record_from_scheduled_task($scheduledtask));
|
|
|
397 |
$records = $DB->get_records('task_scheduled');
|
|
|
398 |
$this->assertCount(1, $records);
|
|
|
399 |
|
|
|
400 |
$this->assertInstanceOf(
|
|
|
401 |
\fullsubtype_example\task\scheduled_test::class,
|
|
|
402 |
\core\task\manager::get_all_scheduled_tasks()[0]
|
|
|
403 |
);
|
|
|
404 |
$now = time();
|
|
|
405 |
$task = \core\task\manager::get_next_scheduled_task($now);
|
|
|
406 |
$this->assertInstanceOf(\fullsubtype_example\task\scheduled_test::class, $task);
|
|
|
407 |
manager::scheduled_task_complete($task);
|
|
|
408 |
|
|
|
409 |
// Deprecated plugin type: isn't listed and isn't returned during scheduling.
|
|
|
410 |
$DB->delete_records('task_scheduled');
|
|
|
411 |
$scheduledtask = new \fulldeprecatedsubtype_test\task\scheduled_test();
|
|
|
412 |
$DB->insert_record('task_scheduled', \core\task\manager::record_from_scheduled_task($scheduledtask));
|
|
|
413 |
$records = $DB->get_records('task_scheduled');
|
|
|
414 |
$this->assertCount(1, $records);
|
|
|
415 |
|
|
|
416 |
$this->assertEmpty(\core\task\manager::get_all_scheduled_tasks());
|
|
|
417 |
$this->assertNull(\core\task\manager::get_next_scheduled_task($now));
|
|
|
418 |
|
|
|
419 |
// Task can still be executed directly.
|
|
|
420 |
$this->expectExceptionMessage('task->execute() called');
|
|
|
421 |
$scheduledtask->execute();
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
/**
|
|
|
425 |
* Test verifying \core\task\manager behaviour for adhoc tasks when dealing with deprecated plugin types.
|
|
|
426 |
*
|
|
|
427 |
* This only verifies that new tasks cannot be queued via:
|
|
|
428 |
* - {@see \core\task\manager::queue_adhoc_task}
|
|
|
429 |
* - {@see \core\task\manager::get_next_adhoc_task()}
|
|
|
430 |
*
|
|
|
431 |
* I.e. Nothing prevents task->execute() from running if called directly.
|
|
|
432 |
*
|
|
|
433 |
* @return void
|
|
|
434 |
*/
|
|
|
435 |
public function test_queue_adhoc_task_deprecated_plugintype(): void {
|
|
|
436 |
$this->resetAfterTest();
|
|
|
437 |
global $DB, $CFG;
|
|
|
438 |
|
|
|
439 |
$fakepluginroot = $CFG->libdir . '/tests/fixtures/fakeplugins/fake/fullfeatured';
|
|
|
440 |
require_once($fakepluginroot . '/classes/plugininfo/fullsubtype.php');
|
|
|
441 |
require_once($fakepluginroot . '/classes/plugininfo/fulldeprecatedsubtype.php');
|
|
|
442 |
require_once($fakepluginroot . '/classes/plugininfo/fulldeletedsubtype.php');
|
|
|
443 |
require_once($fakepluginroot . '/fullsubtype/example/classes/task/adhoc_test.php');
|
|
|
444 |
require_once($fakepluginroot . '/fulldeprecatedsubtype/test/classes/task/adhoc_test.php');
|
|
|
445 |
require_once($fakepluginroot . '/fulldeletedsubtype/demo/classes/task/adhoc_test.php');
|
|
|
446 |
|
|
|
447 |
// Inject stub plugininfo instances into a stub plugin manager, then inject that into the static cache via reflection.
|
|
|
448 |
// When the manager code calls core_plugin_manager::instance(), it'll get back the stub.
|
|
|
449 |
$stubavailableplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fullsubtype::class);
|
|
|
450 |
$stubavailableplugininfo->method('is_deprecated')->willReturn(false);
|
|
|
451 |
$stubavailableplugininfo->method('is_deleted')->willReturn(false);
|
|
|
452 |
$stubavailableplugininfo->component = "fullsubtype_example";
|
|
|
453 |
$stubdeprecatedplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fulldeprecatedsubtype::class);
|
|
|
454 |
$stubdeprecatedplugininfo->method('is_deprecated')->willReturn(true);
|
|
|
455 |
$stubdeprecatedplugininfo->method('is_deleted')->willReturn(false);
|
|
|
456 |
$stubdeprecatedplugininfo->component = "fulldeprecatedsubtype_test";
|
|
|
457 |
$stubdeletedplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fulldeletedsubtype::class);
|
|
|
458 |
$stubdeletedplugininfo->method('is_deprecated')->willReturn(false);
|
|
|
459 |
$stubdeletedplugininfo->method('is_deleted')->willReturn(true);
|
|
|
460 |
$stubdeletedplugininfo->component = "fulldeletedsubtype_demo";
|
|
|
461 |
$stubpluginman = $this->createStub(\core_plugin_manager::class);
|
|
|
462 |
$stubpluginman->method('get_plugin_info')
|
|
|
463 |
->will($this->returnValueMap([
|
|
|
464 |
['fullsubtype_example', $stubavailableplugininfo],
|
|
|
465 |
['fulldeprecatedsubtype_test', $stubdeprecatedplugininfo],
|
|
|
466 |
['fulldeletedsubtype_demo', $stubdeletedplugininfo],
|
|
|
467 |
]));
|
|
|
468 |
$pluginmanrc = new \ReflectionClass(\core_plugin_manager::class);
|
|
|
469 |
$pluginmanrc->setStaticPropertyValue('singletoninstance', $stubpluginman);
|
|
|
470 |
|
|
|
471 |
$task1 = new \fullsubtype_example\task\adhoc_test(); // Available plugin type.
|
|
|
472 |
$task2 = new \fulldeprecatedsubtype_test\task\adhoc_test(); // Deprecated plugin type.
|
|
|
473 |
$task3 = new \fulldeletedsubtype_demo\task\adhoc_test(); // Deleted plugin type.
|
|
|
474 |
|
|
|
475 |
$DB->delete_records('task_adhoc');
|
|
|
476 |
|
|
|
477 |
// Task from a non-deprecated plugin type can be queued.
|
|
|
478 |
$this->assertIsInt(manager::queue_adhoc_task($task1));
|
|
|
479 |
$now = time();
|
|
|
480 |
$classname = get_class($task1);
|
|
|
481 |
$taskfromqueue = manager::get_next_adhoc_task($now, true, $classname);
|
|
|
482 |
$this->assertNotNull($taskfromqueue);
|
|
|
483 |
$taskfromqueue->execute();
|
|
|
484 |
manager::adhoc_task_complete($taskfromqueue);
|
|
|
485 |
|
|
|
486 |
// Task from a deprecated plugin type cannot be queued.
|
|
|
487 |
$this->assertTrue(\core_plugin_manager::instance()->get_plugin_info('fulldeprecatedsubtype_test')->is_deprecated());
|
|
|
488 |
$this->assertFalse(manager::queue_adhoc_task($task2));
|
|
|
489 |
$classname = get_class($task2);
|
|
|
490 |
$this->assertNull(manager::get_next_adhoc_task($now, true, $classname));
|
|
|
491 |
|
|
|
492 |
// Task from a deleted plugin type cannot be queued.
|
|
|
493 |
$this->assertTrue(\core_plugin_manager::instance()->get_plugin_info('fulldeletedsubtype_demo')->is_deleted());
|
|
|
494 |
$this->assertFalse(manager::queue_adhoc_task($task3));
|
|
|
495 |
$classname = get_class($task3);
|
|
|
496 |
$this->assertNull(manager::get_next_adhoc_task($now, true, $classname));
|
|
|
497 |
}
|
|
|
498 |
|
|
|
499 |
/**
|
|
|
500 |
* Test verifying \core\task\manager can still return and run adhoc tasks queued prior to plugin deprecation.
|
|
|
501 |
*
|
|
|
502 |
* This only verifies that existing tasks can be fetched and run via:
|
|
|
503 |
* - {@see \core\task\manager::get_next_adhoc_task()}
|
|
|
504 |
*
|
|
|
505 |
* @return void
|
|
|
506 |
*/
|
|
|
507 |
public function test_run_existing_adhoc_task_deprecated_plugintype(): void {
|
|
|
508 |
$this->resetAfterTest();
|
|
|
509 |
global $DB, $CFG;
|
|
|
510 |
|
|
|
511 |
$fakepluginroot = $CFG->libdir . '/tests/fixtures/fakeplugins/fake/fullfeatured';
|
|
|
512 |
require_once($fakepluginroot . '/classes/plugininfo/fullsubtype.php');
|
|
|
513 |
require_once($fakepluginroot . '/fullsubtype/example/classes/task/adhoc_test.php');
|
|
|
514 |
|
|
|
515 |
// Inject stub plugininfo instances into a stub plugin manager, then inject that into the static cache via reflection.
|
|
|
516 |
// When the task code calls core_plugin_manager::instance(), it'll get back the stub.
|
|
|
517 |
$stubavailableplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fullsubtype::class);
|
|
|
518 |
$stubavailableplugininfo->method('is_deprecated')->willReturn(false);
|
|
|
519 |
$stubavailableplugininfo->component = "fullsubtype_example";
|
|
|
520 |
$stubpluginman = $this->createStub(\core_plugin_manager::class);
|
|
|
521 |
$stubpluginman
|
|
|
522 |
->method('get_plugin_info')
|
|
|
523 |
->will($this->returnValueMap([
|
|
|
524 |
['fullsubtype_example', $stubavailableplugininfo],
|
|
|
525 |
]));
|
|
|
526 |
$pluginmanrc = new \ReflectionClass(\core_plugin_manager::class);
|
|
|
527 |
$pluginmanrc->setStaticPropertyValue('singletoninstance', $stubpluginman);
|
|
|
528 |
|
|
|
529 |
$task1 = new \fullsubtype_example\task\adhoc_test(); // An available plugin.
|
|
|
530 |
|
|
|
531 |
$DB->delete_records('task_adhoc');
|
|
|
532 |
|
|
|
533 |
// Queue the task for the available plugin.
|
|
|
534 |
$this->assertIsInt(manager::queue_adhoc_task($task1));
|
|
|
535 |
$this->assertEquals(1, $DB->count_records('task_adhoc'));
|
|
|
536 |
|
|
|
537 |
// Now, deprecate the plugin type by redefining the stubs and reinjecting into the stub plugin manager.
|
|
|
538 |
$stubdeprecatedplugininfo = $this->createStub(\fake_fullfeatured\plugininfo\fullsubtype::class);
|
|
|
539 |
$stubdeprecatedplugininfo->method('is_deprecated')->willReturn(true);
|
|
|
540 |
$stubdeprecatedplugininfo->component = "fullsubtype_example";
|
|
|
541 |
$stubpluginman = $this->createStub(\core_plugin_manager::class);
|
|
|
542 |
$stubpluginman
|
|
|
543 |
->method('get_plugin_info')
|
|
|
544 |
->will($this->returnValueMap([
|
|
|
545 |
['fullsubtype_example', $stubdeprecatedplugininfo],
|
|
|
546 |
]));
|
|
|
547 |
$pluginmanrc->setStaticPropertyValue('singletoninstance', $stubpluginman);
|
|
|
548 |
|
|
|
549 |
// Assert prior-queued tasks can be fetched and run.
|
|
|
550 |
$this->assertTrue(\core_plugin_manager::instance()->get_plugin_info('fullsubtype_example')->is_deprecated());
|
|
|
551 |
$classname = get_class($task1);
|
|
|
552 |
$now = time();
|
|
|
553 |
$taskfromqueue = manager::get_next_adhoc_task($now, true, $classname);
|
|
|
554 |
$this->assertNotNull($taskfromqueue);
|
|
|
555 |
$taskfromqueue->execute();
|
|
|
556 |
manager::adhoc_task_complete($taskfromqueue);
|
|
|
557 |
}
|
1 |
efrain |
558 |
}
|