Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
 
1441 ariadna 19
use core\url;
20
 
1 efrain 21
/**
22
 * Test class for adhoc tasks.
23
 *
24
 * @package core
25
 * @category test
26
 * @copyright 2013 Damyon Wiese
27
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers \core\task\manager
29
 */
30
final class adhoc_task_test extends \advanced_testcase {
31
    public static function setUpBeforeClass(): void {
32
        parent::setUpBeforeClass();
33
 
34
        require_once(__DIR__ . '/../fixtures/task_fixtures.php');
35
    }
36
 
37
    /**
38
     * Test getting name of task that implements it's own get_name method
39
     *
40
     * @covers \core\task\adhoc_task
41
     */
42
    public function test_get_name(): void {
43
        $task = new \core\task\adhoc_test_task();
44
        $this->assertEquals('Test adhoc class', $task->get_name());
45
    }
46
 
47
    /**
48
     * Test getting name of task that uses the default implementation of get_name
49
     *
50
     * @covers \core\task\adhoc_task
51
     */
52
    public function test_get_name_default(): void {
53
        $task = new \mod_fake\task\adhoc_component_task();
54
        $this->assertEquals('Adhoc component task', $task->get_name());
55
    }
56
 
57
    /**
58
     * Test basic adhoc task execution.
59
     */
60
    public function test_get_next_adhoc_task_now(): void {
61
        $this->resetAfterTest(true);
62
 
1441 ariadna 63
        $clock = $this->mock_clock_with_frozen();
64
 
1 efrain 65
        // Create an adhoc task.
66
        $task = new adhoc_test_task();
67
 
68
        // Queue it.
69
        manager::queue_adhoc_task($task);
70
 
71
        // Get it from the scheduler.
1441 ariadna 72
        $task = manager::get_next_adhoc_task($clock->time());
1 efrain 73
        $this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
74
        $task->execute();
75
        manager::adhoc_task_complete($task);
76
    }
77
 
78
    /**
79
     * Test basic adhoc task execution.
80
     */
81
    public function test_get_next_adhoc_task_class(): void {
82
        $this->resetAfterTest(true);
83
 
1441 ariadna 84
        $clock = $this->mock_clock_with_frozen();
85
 
1 efrain 86
        // Create an adhoc task.
87
        $task = new \core\task\adhoc_test_task();
88
 
89
        // Queue it.
90
        manager::queue_adhoc_task($task);
91
 
92
        $classname = get_class($task);
93
 
94
        // The task will not be returned.
1441 ariadna 95
        $this->assertNull(manager::get_next_adhoc_task($clock->time(), true, "{$classname}notexists"));
1 efrain 96
 
97
        // Get it from the scheduler.
1441 ariadna 98
        $task = manager::get_next_adhoc_task($clock->time(), true, $classname);
1 efrain 99
        $this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
100
        $task->execute();
101
        manager::adhoc_task_complete($task);
102
    }
103
 
104
    /**
105
     * Test adhoc task failure retry backoff.
106
     */
107
    public function test_get_next_adhoc_task_fail_retry(): void {
108
        $this->resetAfterTest(true);
109
 
1441 ariadna 110
        $clock = $this->mock_clock_with_frozen();
111
 
1 efrain 112
        // Create an adhoc task.
113
        $task = new adhoc_test_task();
114
        manager::queue_adhoc_task($task);
115
 
1441 ariadna 116
        $now = $clock->time();
1 efrain 117
 
118
        // Get it from the scheduler, execute it, and mark it as failed.
119
        $task = manager::get_next_adhoc_task($now);
120
        $taskid = $task->get_id();
121
        $task->execute();
122
        manager::adhoc_task_failed($task);
123
 
124
        // The task will not be returned immediately.
125
        $this->assertNull(manager::get_next_adhoc_task($now));
126
 
127
        // Should get the adhoc task (retry after delay). Fail it again.
1441 ariadna 128
        $clock->bump(120);
129
        $task = manager::get_next_adhoc_task($clock->time());
1 efrain 130
        $this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
131
        $this->assertEquals($taskid, $task->get_id());
132
        $task->execute();
133
        manager::adhoc_task_failed($task);
134
 
135
        // Should get the adhoc task immediately.
136
        $task = manager::get_adhoc_task($taskid);
137
        $this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
138
        $this->assertEquals($taskid, $task->get_id());
139
        $task->execute();
140
        manager::adhoc_task_complete($task);
141
 
142
        // Should not get any task.
143
        $this->assertNull(manager::get_next_adhoc_task($now));
144
    }
145
 
146
    /**
147
     * Test that failed tasks eventually hit the maximum delay.
148
     *
149
     * @covers \core\task\adhoc_task
150
     */
151
    public function test_get_next_adhoc_task_maximum_fail_delay(): void {
152
        $this->resetAfterTest(true);
153
 
1441 ariadna 154
        $clock = $this->mock_clock_with_frozen();
155
        $now = $clock->time();
1 efrain 156
 
157
        // Create an adhoc task.
158
        $task = new adhoc_test_task();
159
        $attemptsavailable = $task->get_attempts_available();
160
        manager::queue_adhoc_task($task);
161
 
162
        // Exhaust all attempts available.
163
        for ($x = 0; $x < $attemptsavailable; $x++) {
164
            $delay = $task->get_fail_delay() * 2;
165
            $task = manager::get_next_adhoc_task($now + $delay);
166
            $task->execute();
167
            manager::adhoc_task_failed($task);
168
        }
169
        // Check that the fail delay is now set to 24 hours (the maximum amount of times).
170
        $this->assertEquals(DAYSECS, $task->get_fail_delay());
171
    }
172
 
173
    /**
174
     * Test adhoc task failure retry backoff.
175
     */
176
    public function test_adhoc_task_with_retry_flag(): void {
177
        global $DB;
178
        $this->resetAfterTest();
179
 
1441 ariadna 180
        $clock = $this->mock_clock_with_frozen();
181
 
182
        $now = $clock->time();
1 efrain 183
        // Create a normal adhoc task.
184
        $task = new adhoc_test_task();
185
        $taskid1 = manager::queue_adhoc_task(task: $task);
186
 
187
        // This is a normal task, so it should have limited attempts.
188
        $attemptsavailable = $DB->get_field(
189
            table: 'task_adhoc',
190
            return: 'attemptsavailable',
191
            conditions: ['id' => $taskid1]
192
        );
193
        $this->assertEquals(expected: 12, actual: $attemptsavailable);
194
 
195
        // Get the task from the scheduler, execute it, and mark it as failed.
196
        $task = manager::get_next_adhoc_task(timestart: $now);
197
        $taskid1 = $task->get_id();
198
        $task->execute();
199
        manager::adhoc_task_failed(task: $task);
200
 
201
        // Now that the task has failed, there should be one less attempt available.
202
        $attemptsavailable = $DB->get_field(
203
            table: 'task_adhoc',
204
            return: 'attemptsavailable',
205
            conditions: ['id' => $taskid1]
206
        );
207
        $this->assertEquals(expected: 12 - 1, actual: $attemptsavailable);
208
 
209
        // Create a no-retry adhoc task.
1441 ariadna 210
        $now = $clock->time();
1 efrain 211
        $task = new no_retry_adhoc_task();
212
        $taskid2 = manager::queue_adhoc_task(task: $task);
213
 
214
        // This is no-retry task, so it should have only 1 attempt available.
215
        $attemptsavailable = $DB->get_field(
216
            table: 'task_adhoc',
217
            return: 'attemptsavailable',
218
            conditions: ['id' => $taskid2]
219
        );
220
        $this->assertEquals(
221
            expected: 1,
222
            actual: $attemptsavailable,
223
        );
224
 
225
        // Get the task from the scheduler, execute it, and mark it as failed.
226
        $task = manager::get_next_adhoc_task(timestart: $now);
227
        $taskid2 = $task->get_id();
228
        $task->execute();
229
        manager::adhoc_task_failed(task: $task);
230
 
231
        // This is no-retry task, the remaining available attempts should be reduced to 0.
232
        $attemptsavailable = $DB->get_field(
233
            table: 'task_adhoc',
234
            return: 'attemptsavailable',
235
            conditions: ['id' => $taskid2]
236
        );
237
        $this->assertEquals(
238
            expected: 0,
239
            actual: $attemptsavailable,
240
        );
241
 
242
        // There will be two records in the task_adhoc table, one for each task.
243
        $this->assertEquals(
244
            expected: 2,
245
            actual: $DB->count_records(table: 'task_adhoc')
246
        );
247
        // But get_next_adhoc_task() should return only the allowed re-try task.
248
        // The no-retry task should not be returned because it has no remaining attempts.
249
        do {
250
            $task = manager::get_next_adhoc_task(timestart: $now + 86400);
251
            if ($task) {
252
                manager::adhoc_task_failed(task: $task);
253
                $this->assertEquals(
254
                    expected: $taskid1,
255
                    actual: $task->get_id(),
256
                );
257
            }
258
        } while ($task);
259
 
260
        // Mark the normal task as complete.
261
        $task = manager::get_adhoc_task(taskid: $taskid1);
262
        manager::adhoc_task_complete($task);
263
 
264
        // There will be one record in the task_adhoc table.
265
        $this->assertEquals(
266
            expected: 1,
267
            actual: $DB->count_records(table: 'task_adhoc')
268
        );
269
 
270
        // But get_next_adhoc_task() should return nothing.
271
        $this->assertNull(manager::get_next_adhoc_task(timestart: $now + 86400));
272
    }
273
 
274
    /**
275
     * Test adhoc task failure cleanup.
276
     */
277
    public function test_adhoc_task_clean_up(): void {
278
        global $DB, $CFG;
279
        $this->resetAfterTest();
280
 
1441 ariadna 281
        $clock = $this->mock_clock_with_frozen();
282
 
1 efrain 283
        // Create two no-retry adhoc tasks.
284
        $task1 = new no_retry_adhoc_task();
285
        $taskid1 = manager::queue_adhoc_task(task: $task1);
286
        $task2 = new no_retry_adhoc_task();
287
        $taskid2 = manager::queue_adhoc_task(task: $task2);
288
 
289
        // Get the tasks and mark it as failed.
290
        $task = manager::get_adhoc_task($taskid1);
291
        manager::adhoc_task_failed(task: $task);
292
        $task = manager::get_adhoc_task($taskid2);
293
        manager::adhoc_task_failed(task: $task);
294
 
295
        // These are no-retry tasks, the remaining available attempts should be reduced to 0.
296
        $this->assertEquals(
297
            expected: 0,
298
            actual: $DB->get_field(
299
                table: 'task_adhoc',
300
                return: 'attemptsavailable',
301
                conditions: ['id' => $taskid1],
302
            ),
303
        );
304
        $this->assertEquals(
305
            expected: 0,
306
            actual: $DB->get_field(
307
                table: 'task_adhoc',
308
                return: 'attemptsavailable',
309
                conditions: ['id' => $taskid2],
310
            ),
311
        );
312
 
313
        // There will be two records in the task_adhoc table.
314
        $this->assertEquals(
315
            expected: 2,
316
            actual: $DB->count_records(table: 'task_adhoc'),
317
        );
318
 
319
        // Clean up failed adhoc tasks. This will clean nothing because the tasks are not old enough.
320
        manager::clean_failed_adhoc_tasks();
321
 
322
        // There will be two records in the task_adhoc table.
323
        $this->assertEquals(
324
            expected: 2,
325
            actual: $DB->count_records(table: 'task_adhoc'),
326
        );
327
 
328
        // Update the time of the task2 to be older more than 2 days.
329
        $DB->set_field(
330
            table: 'task_adhoc',
331
            newfield: 'firststartingtime',
1441 ariadna 332
            newvalue: $clock->time() - (DAYSECS * 2) - 10, // Plus 10 seconds to make sure it is older than 2 days.
1 efrain 333
            conditions: ['id' => $taskid2],
334
        );
335
 
336
        // Clean up failed adhoc tasks. This will clean nothing because the tasks are not old enough.
337
        manager::clean_failed_adhoc_tasks();
338
 
339
        // There will be two records in the task_adhoc table.
340
        $this->assertEquals(
341
            expected: 2,
342
            actual: $DB->count_records(table: 'task_adhoc'),
343
        );
344
 
345
        // Update the time of the task1 to be older than the cleanup time.
346
        $DB->set_field(
347
            table: 'task_adhoc',
348
            newfield: 'firststartingtime',
349
            // Plus 10 seconds to make sure it is older than the retention time.
1441 ariadna 350
            newvalue: $clock->time() - $CFG->task_adhoc_failed_retention - 10,
1 efrain 351
            conditions: ['id' => $taskid1],
352
        );
353
 
354
        // Clean up failed adhoc tasks. task1 should be cleaned now.
355
        manager::clean_failed_adhoc_tasks();
356
 
357
        // There will be one record in the task_adhoc table.
358
        $this->assertEquals(
359
            expected: 1,
360
            actual: $DB->count_records(table: 'task_adhoc'),
361
        );
362
 
363
        // Update the duration of the Failed ad hoc task retention period to one day.
364
        $CFG->task_adhoc_failed_retention = DAYSECS;
365
 
366
        // Clean up failed adhoc tasks. task2 should be cleaned now.
367
        manager::clean_failed_adhoc_tasks();
368
 
369
        // The task_adhoc table should be empty now.
370
        $this->assertEquals(
371
            expected: 0,
372
            actual: $DB->count_records(table: 'task_adhoc'),
373
        );
374
    }
375
 
376
    /**
377
     * Test adhoc task failure will retain the time information.
378
     */
379
    public function test_adhoc_task_failed_will_retain_time_info(): void {
380
        global $DB;
381
        $this->resetAfterTest();
382
 
1441 ariadna 383
        $clock = $this->mock_clock_with_frozen();
384
        $now = $clock->time();
385
 
1 efrain 386
        // Create an adhoc task.
387
        $task = new adhoc_test_task();
388
        // Queue it.
389
        $taskid = manager::queue_adhoc_task(task: $task);
390
 
391
        // Update the timecreated of the task to be older.
392
        $DB->set_field(
393
            table: 'task_adhoc',
394
            newfield: 'timecreated',
1441 ariadna 395
            newvalue: $clock->time() - DAYSECS,
1 efrain 396
            conditions: ['id' => $taskid],
397
        );
398
 
399
        // Get the timecreated value before marking the task as failed.
400
        $timecreatedbefore = $DB->get_field(
401
            table: 'task_adhoc',
402
            return: 'timecreated',
403
            conditions: ['id' => $taskid],
404
        );
405
 
406
        // Get the task from the scheduler.
407
        $task = manager::get_next_adhoc_task(timestart: $now);
408
        // Execute the task.
409
        $task->execute();
410
        // Mark the task as failed.
411
        manager::adhoc_task_failed(task: $task);
412
 
413
        // Get the timecreated value after marking the task as failed.
414
        $timecreatedafter = $DB->get_field(
415
            table: 'task_adhoc',
416
            return: 'timecreated',
417
            conditions: ['id' => $taskid],
418
        );
419
 
420
        // The timecreated values should be the same.
421
        $this->assertEquals($timecreatedbefore, $timecreatedafter);
422
    }
423
 
424
    /**
425
     * Test future adhoc task execution.
426
     */
427
    public function test_get_next_adhoc_task_future(): void {
428
        $this->resetAfterTest(true);
429
 
1441 ariadna 430
        $clock = $this->mock_clock_with_frozen();
431
        $now = $clock->time();
432
 
1 efrain 433
        // Create an adhoc task in future.
434
        $task = new adhoc_test_task();
435
        $task->set_next_run_time($now + 1000);
436
        manager::queue_adhoc_task($task);
437
 
438
        // Fetching the next task should not return anything.
439
        $this->assertNull(manager::get_next_adhoc_task($now));
440
 
441
        // Fetching in the future should return the task.
442
        $task = manager::get_next_adhoc_task($now + 1020);
443
        $this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
444
        $task->execute();
445
        manager::adhoc_task_complete($task);
446
    }
447
 
448
    /**
449
     * Test queueing an adhoc task belonging to a component, where we set the task component accordingly
450
     */
451
    public function test_queue_adhoc_task_for_component(): void {
452
        $this->resetAfterTest();
453
 
454
        $task = new \mod_forum\task\send_user_digests();
455
        $task->set_component('mod_test');
456
 
457
        manager::queue_adhoc_task($task);
458
        $this->assertDebuggingNotCalled();
459
    }
460
 
461
    /**
462
     * Test queueing an adhoc task belonging to a component, where we do not set the task component
463
     */
464
    public function test_queue_task_for_component_without_set_component(): void {
465
        $this->resetAfterTest();
466
 
467
        $task = new \mod_forum\task\send_user_digests();
468
 
469
        manager::queue_adhoc_task($task);
470
        $this->assertDebuggingNotCalled();
471
 
472
        // Assert the missing component was set.
473
        $this->assertEquals('mod_forum', $task->get_component());
474
    }
475
 
476
    /**
477
     * Test queueing an adhoc task belonging to an invalid component, where we do not set the task component
478
     */
479
    public function test_queue_task_for_invalid_component_without_set_component(): void {
480
        $this->resetAfterTest();
481
 
482
        $task = new \mod_fake\task\adhoc_component_task();
483
 
484
        manager::queue_adhoc_task($task);
1441 ariadna 485
        $this->assertdebuggingcalledcount(
486
            2,
487
            array_fill(0, 2, 'Component not set and the class namespace does not match a valid component (mod_fake).'),
488
        );
1 efrain 489
    }
490
 
491
    /**
492
     * Test empty set of adhoc tasks
493
     */
494
    public function test_get_adhoc_tasks_empty_set(): void {
495
        $this->resetAfterTest(true);
496
 
497
        $this->assertEquals([], manager::get_adhoc_tasks('\\core\\task\\adhoc_test_task'));
498
    }
499
 
500
    /**
501
     * Test correct set of adhoc tasks is returned for class.
502
     */
503
    public function test_get_adhoc_tasks_result_set(): void {
504
        $this->resetAfterTest(true);
505
 
506
        for ($i = 0; $i < 3; $i++) {
507
            $task = new adhoc_test_task();
508
            manager::queue_adhoc_task($task);
509
        }
510
 
511
        for ($i = 0; $i < 3; $i++) {
512
            $task = new adhoc_test2_task();
513
            manager::queue_adhoc_task($task);
514
        }
515
 
516
        $adhoctests = manager::get_adhoc_tasks('\\core\\task\\adhoc_test_task');
517
        $adhoctest2s = manager::get_adhoc_tasks('\\core\\task\\adhoc_test2_task');
518
 
519
        $this->assertCount(3, $adhoctests);
520
        $this->assertCount(3, $adhoctest2s);
521
 
522
        foreach ($adhoctests as $task) {
523
            $this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
524
        }
525
 
526
        foreach ($adhoctest2s as $task) {
527
            $this->assertInstanceOf('\\core\\task\\adhoc_test2_task', $task);
528
        }
529
    }
530
 
531
    /**
532
     * Ensure that the reschedule_or_queue_adhoc_task function will schedule a new task if no tasks exist.
533
     */
534
    public function test_reschedule_or_queue_adhoc_task_no_existing(): void {
535
        $this->resetAfterTest(true);
536
 
537
        // Schedule adhoc task.
538
        $task = new adhoc_test_task();
539
        $task->set_custom_data(['courseid' => 10]);
540
        manager::reschedule_or_queue_adhoc_task($task);
541
        $this->assertEquals(1, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
542
    }
543
 
544
    /**
545
     * Ensure that the reschedule_or_queue_adhoc_task function will schedule a new task if a task for the same user does
546
     * not exist.
547
     */
548
    public function test_reschedule_or_queue_adhoc_task_different_user(): void {
549
        $this->resetAfterTest(true);
550
        $user = \core_user::get_user_by_username('admin');
551
 
552
        // Schedule adhoc task.
553
        $task = new adhoc_test_task();
554
        $task->set_custom_data(['courseid' => 10]);
555
        manager::reschedule_or_queue_adhoc_task($task);
556
 
557
        // Schedule adhoc task for a different user.
558
        $task = new adhoc_test_task();
559
        $task->set_custom_data(['courseid' => 10]);
560
        $task->set_userid($user->id);
561
        manager::reschedule_or_queue_adhoc_task($task);
562
 
563
        $this->assertEquals(2, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
564
    }
565
 
566
    /**
567
     * Ensure that the reschedule_or_queue_adhoc_task function will schedule a new task if a task with different custom
568
     * data exists.
569
     */
570
    public function test_reschedule_or_queue_adhoc_task_different_data(): void {
571
        $this->resetAfterTest(true);
572
 
573
        // Schedule adhoc task.
574
        $task = new adhoc_test_task();
575
        $task->set_custom_data(['courseid' => 10]);
576
        manager::reschedule_or_queue_adhoc_task($task);
577
 
578
        // Schedule adhoc task for a different user.
579
        $task = new adhoc_test_task();
580
        $task->set_custom_data(['courseid' => 11]);
581
        manager::reschedule_or_queue_adhoc_task($task);
582
 
583
        $this->assertEquals(2, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
584
    }
585
 
586
    /**
587
     * Ensure that the reschedule_or_queue_adhoc_task function will not make any change for matching data if no time was
588
     * specified.
589
     */
590
    public function test_reschedule_or_queue_adhoc_task_match_no_change(): void {
591
        $this->resetAfterTest(true);
592
 
1441 ariadna 593
        $clock = $this->mock_clock_with_frozen();
594
 
1 efrain 595
        // Schedule adhoc task.
596
        $task = new adhoc_test_task();
597
        $task->set_custom_data(['courseid' => 10]);
1441 ariadna 598
        $task->set_next_run_time($clock->time() + DAYSECS);
1 efrain 599
        manager::reschedule_or_queue_adhoc_task($task);
600
 
601
        $before = manager::get_adhoc_tasks('core\task\adhoc_test_task');
602
 
603
        // Schedule the task again but do not specify a time.
604
        $task = new adhoc_test_task();
605
        $task->set_custom_data(['courseid' => 10]);
606
        manager::reschedule_or_queue_adhoc_task($task);
607
 
608
        $this->assertEquals(1, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
609
        $this->assertEquals($before, manager::get_adhoc_tasks('core\task\adhoc_test_task'));
610
    }
611
 
612
    /**
613
     * Ensure that the reschedule_or_queue_adhoc_task function will update the run time if there are planned changes.
614
     */
615
    public function test_reschedule_or_queue_adhoc_task_match_update_runtime(): void {
616
        $this->resetAfterTest(true);
617
 
1441 ariadna 618
        $clock = $this->mock_clock_with_frozen();
619
 
620
        $initialruntime = $clock->time() + DAYSECS;
621
        $newruntime = $clock->time() + WEEKSECS;
622
 
1 efrain 623
        // Schedule adhoc task.
624
        $task = new adhoc_test_task();
625
        $task->set_custom_data(['courseid' => 10]);
626
        $task->set_next_run_time($initialruntime);
627
        manager::reschedule_or_queue_adhoc_task($task);
628
 
629
        $before = manager::get_adhoc_tasks('core\task\adhoc_test_task');
630
 
631
        // Schedule the task again.
632
        $task = new adhoc_test_task();
633
        $task->set_custom_data(['courseid' => 10]);
634
        $task->set_next_run_time($newruntime);
635
        manager::reschedule_or_queue_adhoc_task($task);
636
 
637
        $tasks = manager::get_adhoc_tasks('core\task\adhoc_test_task');
638
        $this->assertEquals(1, count($tasks));
639
        $this->assertNotEquals($before, $tasks);
640
        $firsttask = reset($tasks);
641
        $this->assertEquals($newruntime, $firsttask->get_next_run_time());
642
    }
643
 
644
    /**
645
     * Test queue_adhoc_task "if not scheduled".
646
     */
647
    public function test_queue_adhoc_task_if_not_scheduled(): void {
648
        $this->resetAfterTest(true);
649
        $user = \core_user::get_user_by_username('admin');
650
 
651
        // Schedule adhoc task.
652
        $task = new adhoc_test_task();
653
        $task->set_custom_data(['courseid' => 10]);
654
        $this->assertNotEmpty(manager::queue_adhoc_task($task, true));
655
        $this->assertEquals(1, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
656
 
657
        // Schedule adhoc task with a user.
658
        $task = new adhoc_test_task();
659
        $task->set_custom_data(['courseid' => 10]);
660
        $task->set_userid($user->id);
661
        $this->assertNotEmpty(manager::queue_adhoc_task($task, true));
662
        $this->assertEquals(2, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
663
 
664
        // Schedule same adhoc task with different custom data.
665
        $task = new adhoc_test_task();
666
        $task->set_custom_data(['courseid' => 1]);
667
        $this->assertNotEmpty(manager::queue_adhoc_task($task, true));
668
        $this->assertEquals(3, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
669
 
670
        // Schedule same adhoc task with same custom data.
671
        $task = new adhoc_test_task();
672
        $task->set_custom_data(['courseid' => 1]);
673
        $this->assertEmpty(manager::queue_adhoc_task($task, true));
674
        $this->assertEquals(3, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
675
 
676
        // Schedule same adhoc task with same custom data and a user.
677
        $task = new adhoc_test_task();
678
        $task->set_custom_data(['courseid' => 1]);
679
        $task->set_userid($user->id);
680
        $this->assertNotEmpty(manager::queue_adhoc_task($task, true));
681
        $this->assertEquals(4, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
682
 
683
        // Schedule same adhoc task without custom data.
684
        // Note: This task was created earlier.
685
        $task = new adhoc_test_task();
686
        $this->assertNotEmpty(manager::queue_adhoc_task($task, true));
687
        $this->assertEquals(5, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
688
 
689
        // Schedule same adhoc task without custom data (again).
690
        $task5 = new adhoc_test_task();
691
        $this->assertEmpty(manager::queue_adhoc_task($task5, true));
692
        $this->assertEquals(5, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
693
 
694
        // Schedule same adhoc task without custom data but with a userid.
695
        $task6 = new adhoc_test_task();
696
        $user = \core_user::get_user_by_username('admin');
697
        $task6->set_userid($user->id);
698
        $this->assertNotEmpty(manager::queue_adhoc_task($task6, true));
699
        $this->assertEquals(6, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
700
 
701
        // Schedule same adhoc task again without custom data but with a userid.
702
        $task6 = new adhoc_test_task();
703
        $user = \core_user::get_user_by_username('admin');
704
        $task6->set_userid($user->id);
705
        $this->assertEmpty(manager::queue_adhoc_task($task6, true));
706
        $this->assertEquals(6, count(manager::get_adhoc_tasks('core\task\adhoc_test_task')));
707
    }
708
 
709
    /**
710
     * Test that when no userid is specified, it returns empty from the DB
711
     * too.
712
     * @covers \core\task\adhoc_task
713
     */
714
    public function test_adhoc_task_user_empty(): void {
715
        $this->resetAfterTest(true);
716
 
1441 ariadna 717
        $clock = $this->mock_clock_with_frozen();
718
 
1 efrain 719
        // Create an adhoc task in future.
720
        $task = new adhoc_test_task();
721
        manager::queue_adhoc_task($task);
722
 
723
        // Get it back from the scheduler.
1441 ariadna 724
        $now = $clock->time();
1 efrain 725
        $task = manager::get_next_adhoc_task($now);
726
        manager::adhoc_task_complete($task);
727
 
728
        $this->assertEmpty($task->get_userid());
729
    }
730
 
731
    /**
732
     * Test that when a userid is specified, that userid is subsequently
733
     * returned.
734
     *
735
     * @covers \core\task\adhoc_task
736
     */
737
    public function test_adhoc_task_user_set(): void {
738
        $this->resetAfterTest(true);
739
 
740
        // Create an adhoc task in future.
741
        $task = new adhoc_test_task();
742
        $user = \core_user::get_user_by_username('admin');
743
        $task->set_userid($user->id);
744
        manager::queue_adhoc_task($task);
745
 
746
        // Get it back from the scheduler.
1441 ariadna 747
        $clock = $this->mock_clock_with_frozen();
748
        $now = $clock->time();
1 efrain 749
        $task = manager::get_next_adhoc_task($now);
750
        manager::adhoc_task_complete($task);
751
 
752
        $this->assertEquals($user->id, $task->get_userid());
753
    }
754
 
755
    /**
756
     * Test adhoc task with the first starting time.
757
     */
758
    public function test_adhoc_task_get_first_starting_time(): void {
759
        global $DB;
760
        $this->resetAfterTest(true);
761
 
1441 ariadna 762
        $clock = $this->mock_clock_with_frozen();
763
        $now = $clock->time();
1 efrain 764
 
765
        // Create an adhoc task.
766
        $task = new adhoc_test_task();
767
        // Queue it.
768
        $taskid = manager::queue_adhoc_task(task: $task);
769
 
770
        // Get the firststartingtime value.
771
        $firststartingtime = $DB->get_field(
772
            table: 'task_adhoc',
773
            return: 'firststartingtime',
774
            conditions: ['id' => $taskid],
775
        );
776
        $this->assertNull($firststartingtime);
777
 
778
        // This will make sure that the task will be started after the $now value.
1441 ariadna 779
        $clock->bump(5);
1 efrain 780
 
781
        // Get the task from the scheduler.
782
        $task = manager::get_next_adhoc_task(timestart: $now);
783
        // Mark the task as starting.
784
        manager::adhoc_task_starting($task);
785
        // Execute the task.
786
        $task->execute();
787
        // Mark the task as failed.
788
        manager::adhoc_task_failed(task: $task);
789
 
790
        // Get the firststartingtime value.
791
        $origintimestarted = $DB->get_field(
792
            table: 'task_adhoc',
793
            return: 'firststartingtime',
794
            conditions: ['id' => $taskid],
795
        );
796
        $this->assertNotNull($origintimestarted);
797
        $this->assertGreaterThan($now, $origintimestarted);
798
 
1441 ariadna 799
        // Time travel 24 hours into the future.
800
        $clock->bump(DAYSECS * 3);
801
        $now = $clock->time();
1 efrain 802
        // Get the task from the scheduler.
1441 ariadna 803
        $task = manager::get_next_adhoc_task(timestart: $now);
1 efrain 804
        // Mark the task as starting.
805
        manager::adhoc_task_starting($task);
806
        // Execute the task.
807
        $task->execute();
808
        // Mark the task as failed.
809
        manager::adhoc_task_failed(task: $task);
810
 
811
        // Get the firststartingtime value.
812
        $firststartingtime = $DB->get_field(
813
            table: 'task_adhoc',
814
            return: 'firststartingtime',
815
            conditions: ['id' => $taskid],
816
        );
817
 
818
        // The firststartingtime value should not be changed.
819
        $this->assertEquals($origintimestarted, $firststartingtime);
820
    }
821
 
822
    /**
823
     * Test get_concurrency_limit() method to return 0 by default.
824
     *
825
     * @covers \core\task\adhoc_task
826
     */
827
    public function test_get_concurrency_limit(): void {
828
        $this->resetAfterTest(true);
829
        $task = new adhoc_test_task();
830
        $concurrencylimit = $task->get_concurrency_limit();
831
        $this->assertEquals(0, $concurrencylimit);
832
    }
833
 
834
    /**
835
     * Test get_concurrency_limit() method to return a default value set in config.
836
     * @covers \core\task\adhoc_task
837
     */
838
    public function test_get_concurrency_limit_default(): void {
839
        $this->resetAfterTest(true);
840
        set_config('task_concurrency_limit_default', 10);
841
        $task = new adhoc_test_task();
842
        $concurrencylimit = $task->get_concurrency_limit();
843
        $this->assertEquals(10, $concurrencylimit);
844
    }
845
 
846
    /**
847
     * Test get_concurrency_limit() method to return a value for specific task class.
848
     * @covers \core\task\adhoc_task
849
     */
850
    public function test_get_concurrency_limit_for_task(): void {
851
        global $CFG;
852
        $this->resetAfterTest(true);
853
        set_config('task_concurrency_limit_default', 10);
854
        $CFG->task_concurrency_limit = ['core\task\adhoc_test_task' => 5];
855
        $task = new adhoc_test_task();
856
        $concurrencylimit = $task->get_concurrency_limit();
857
        $this->assertEquals(5, $concurrencylimit);
858
    }
859
 
860
    /**
861
     * Test adhoc task sorting.
862
     */
863
    public function test_get_next_adhoc_task_sorting(): void {
864
        $this->resetAfterTest(true);
865
 
1441 ariadna 866
        $clock = $this->mock_clock_with_frozen();
867
 
1 efrain 868
        // Create adhoc tasks.
869
        $task1 = new adhoc_test_task();
870
        $task1->set_next_run_time(1510000000);
871
        $task1->set_custom_data_as_string('Task 1');
872
        manager::queue_adhoc_task($task1);
873
 
874
        $task2 = new adhoc_test_task();
875
        $task2->set_next_run_time(1520000000);
876
        $task2->set_custom_data_as_string('Task 2');
877
        manager::queue_adhoc_task($task2);
878
 
879
        $task3 = new adhoc_test_task();
880
        $task3->set_next_run_time(1520000000);
881
        $task3->set_custom_data_as_string('Task 3');
882
        manager::queue_adhoc_task($task3);
883
 
884
        // Shuffle tasks.
885
        $task1->set_next_run_time(1540000000);
886
        manager::reschedule_or_queue_adhoc_task($task1);
887
 
888
        $task3->set_next_run_time(1530000000);
889
        manager::reschedule_or_queue_adhoc_task($task3);
890
 
891
        $task2->set_next_run_time(1530000000);
892
        manager::reschedule_or_queue_adhoc_task($task2);
893
 
894
        // Confirm, that tasks are sorted by nextruntime and then by id (ascending).
1441 ariadna 895
        $task = manager::get_next_adhoc_task($clock->time());
1 efrain 896
        $this->assertEquals('Task 2', $task->get_custom_data_as_string());
897
        manager::adhoc_task_complete($task);
898
 
1441 ariadna 899
        $task = manager::get_next_adhoc_task($clock->time());
1 efrain 900
        $this->assertEquals('Task 3', $task->get_custom_data_as_string());
901
        manager::adhoc_task_complete($task);
902
 
1441 ariadna 903
        $task = manager::get_next_adhoc_task($clock->time());
1 efrain 904
        $this->assertEquals('Task 1', $task->get_custom_data_as_string());
905
        manager::adhoc_task_complete($task);
906
    }
907
 
908
    /**
909
     * Test adhoc task run from CLI.
910
     */
911
    public function test_run_adhoc_from_cli(): void {
912
        $this->resetAfterTest(true);
913
 
914
        $taskid = 1;
915
 
916
        if (!manager::is_runnable()) {
917
            $this->markTestSkipped("Cannot run tasks");
918
        }
919
 
920
        ob_start();
921
        manager::run_adhoc_from_cli($taskid);
922
        $output = ob_get_contents();
923
        ob_end_clean();
924
 
925
        $this->assertMatchesRegularExpression(
926
            sprintf('!admin/cli/adhoc_task.php\W+--id=%d\W+--force!', $taskid),
927
            $output
928
        );
929
    }
930
 
931
    /**
932
     * Test adhoc class run from CLI.
933
     */
934
    public function test_run_all_adhoc_from_cli(): void {
935
        $this->resetAfterTest(true);
936
 
937
        $classname = 'fake';
938
 
939
        if (!manager::is_runnable()) {
940
            $this->markTestSkipped("Cannot run tasks");
941
        }
942
 
943
        ob_start();
944
        manager::run_all_adhoc_from_cli(false, $classname);
945
        $output = ob_get_contents();
946
        ob_end_clean();
947
 
948
        $this->assertMatchesRegularExpression(
949
            sprintf('!admin/cli/adhoc_task.php\W+--classname=%s\W+--force!', $classname),
950
            $output
951
        );
952
    }
953
 
954
    /**
955
     * Test send messages when adhoc task reaches the max fail delay time.
956
     *
1441 ariadna 957
     * @covers \core\task\failed_task_callbacks::send_failed_task_max_delay_message
1 efrain 958
     */
959
    public function test_adhoc_message_max_fail_delay(): void {
960
        $this->resetAfterTest();
961
        $this->setAdminUser();
962
 
1441 ariadna 963
        $clock = $this->mock_clock_with_frozen();
964
 
1 efrain 965
        // Redirect messages.
966
        $messagesink = $this->redirectMessages();
967
 
968
        // Create an adhoc task.
969
        $task = new adhoc_test_task();
970
        manager::queue_adhoc_task($task);
971
 
1441 ariadna 972
        $now = $clock->time();
1 efrain 973
 
974
        // Get it from the scheduler, execute it, and mark it as failed.
975
        $task = manager::get_next_adhoc_task($now);
976
        $taskid = $task->get_id();
977
        $task->execute();
978
 
979
        // Catch the message. The task has not reach the max time delay yet.
980
        manager::adhoc_task_failed($task);
981
        $messages = $messagesink->get_messages();
982
        $this->assertCount(0, $messages);
983
 
984
        // Should get the adhoc task immediately.
985
        $task = manager::get_adhoc_task($taskid);
986
        $task->set_fail_delay(86400);
987
        $this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
988
        $this->assertEquals($taskid, $task->get_id());
989
        $task->execute();
990
 
991
        // Catch the message.
992
        manager::adhoc_task_failed($task);
993
        $messages = $messagesink->get_messages();
994
        $this->assertCount(1, $messages);
995
 
996
        // Get the task and execute it second time.
997
        $task = manager::get_adhoc_task($taskid);
998
        // Set the fail delay to 12 hours.
999
        $task->set_fail_delay(43200);
1000
        $task->execute();
1001
        manager::adhoc_task_failed($task);
1002
 
1003
        // Catch the message.
1004
        $messages = $messagesink->get_messages();
1005
        $this->assertCount(2, $messages);
1006
 
1007
        // Get the task and execute it third time.
1008
        $task = manager::get_adhoc_task($taskid);
1009
        // Set the fail delay to 48 hours.
1010
        $task->set_fail_delay(172800);
1011
        $task->execute();
1012
        manager::adhoc_task_failed($task);
1013
 
1014
        // Catch the message.
1015
        $messages = $messagesink->get_messages();
1016
        $this->assertCount(3, $messages);
1017
 
1018
        // Check first message information.
1019
        $this->assertStringContainsString('Task failed: Test adhoc class', $messages[0]->subject);
1020
        $this->assertEquals('failedtaskmaxdelay', $messages[0]->eventtype);
1021
        $this->assertEquals('-10', $messages[0]->useridfrom);
1022
        $this->assertEquals('2', $messages[0]->useridto);
1441 ariadna 1023
        $this->assertEquals('Task logs', $messages[0]->contexturlname);
1024
        $this->assertEquals(
1025
            (new url('/admin/tasklogs.php', ['filter' => get_class($task)]))->out(false),
1026
            $messages[0]->contexturl,
1027
        );
1 efrain 1028
 
1029
        // Close sink.
1030
        $messagesink->close();
1031
    }
1032
}