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 |
namespace quiz_statistics\tests;
|
|
|
17 |
|
|
|
18 |
use quiz_statistics\task\recalculate;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* Test methods for statistics recalculations
|
|
|
22 |
*
|
|
|
23 |
* @package quiz_statistics
|
|
|
24 |
* @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
|
|
|
25 |
* @author Mark Johnson <mark.johnson@catalyst-eu.net>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
trait statistics_test_trait {
|
|
|
29 |
/**
|
|
|
30 |
* Return a user, and a quiz with 2 questions.
|
|
|
31 |
*
|
|
|
32 |
* @return array [$user, $quiz, $course]
|
|
|
33 |
*/
|
|
|
34 |
protected function create_test_data(): array {
|
|
|
35 |
$this->resetAfterTest(true);
|
|
|
36 |
$generator = $this->getDataGenerator();
|
|
|
37 |
$user = $generator->create_user();
|
|
|
38 |
$course = $generator->create_course();
|
|
|
39 |
$quiz = $this->create_test_quiz($course);
|
|
|
40 |
$this->add_two_regular_questions($generator->get_plugin_generator('core_question'), $quiz);
|
|
|
41 |
return [$user, $quiz, $course];
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Assert that a task is queued for a quiz.
|
|
|
46 |
*
|
|
|
47 |
* Check that the quizid stored in the task's custom data matches the provided quiz,
|
|
|
48 |
* and that the run time is in one hour from when the test is being run (within a small margin of error).
|
|
|
49 |
*
|
|
|
50 |
* @param recalculate $task
|
|
|
51 |
* @param \stdClass $quiz
|
|
|
52 |
* @return void
|
|
|
53 |
*/
|
|
|
54 |
protected function assert_task_is_queued_for_quiz(recalculate $task, \stdClass $quiz): void {
|
|
|
55 |
$data = $task->get_custom_data();
|
|
|
56 |
$this->assertEquals($quiz->id, $data->quizid);
|
|
|
57 |
$this->assertEqualsWithDelta(time() + HOURSECS, $task->get_next_run_time(), 1);
|
|
|
58 |
}
|
|
|
59 |
}
|