Proyectos de Subversion Moodle

Rev

| 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
namespace quiz_statistics;
17
 
18
defined('MOODLE_INTERNAL') || die();
19
 
20
global $CFG;
21
require_once($CFG->dirroot . '/mod/quiz/tests/quiz_question_helper_test_trait.php');
22
 
23
use core\task\manager;
24
use quiz_statistics\task\recalculate;
25
use quiz_statistics\tests\statistics_helper;
26
use quiz_statistics\tests\statistics_test_trait;
27
 
28
/**
29
 * Unit tests for attempt_submitted observer
30
 *
31
 * @package   quiz_statistics
32
 * @copyright 2023 onwards Catalyst IT EU {@link https://catalyst-eu.net}
33
 * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @covers    \quiz_statistics\hook_callbacks::quiz_attempt_submitted_or_deleted
36
 */
37
class quiz_attempt_submitted_test extends \advanced_testcase {
38
    use \quiz_question_helper_test_trait;
39
    use statistics_test_trait;
40
 
41
    /**
42
     * Attempting a quiz should queue the recalculation task for that quiz in 1 hour's time.
43
     *
44
     * @return void
45
     */
46
    public function test_queue_task_on_submission(): void {
47
        [$user, $quiz] = $this->create_test_data();
48
 
49
        $tasks = manager::get_adhoc_tasks(recalculate::class);
50
        $this->assertEmpty($tasks);
51
 
52
        $this->attempt_quiz($quiz, $user);
53
 
54
        $tasks = manager::get_adhoc_tasks(recalculate::class);
55
        $this->assertCount(1, $tasks);
56
        $task = reset($tasks);
57
        $this->assert_task_is_queued_for_quiz($task, $quiz);
58
    }
59
 
60
    /**
61
     * Attempting a quiz multiple times should only queue one instance of the task.
62
     *
63
     * @return void
64
     */
65
    public function test_queue_single_task_for_multiple_submissions(): void {
66
        [$user1, $quiz] = $this->create_test_data();
67
        $user2 = $this->getDataGenerator()->create_user();
68
 
69
        $tasks = manager::get_adhoc_tasks(recalculate::class);
70
        $this->assertEmpty($tasks);
71
 
72
        $this->attempt_quiz($quiz, $user1);
73
        $this->attempt_quiz($quiz, $user2);
74
 
75
        $tasks = manager::get_adhoc_tasks(recalculate::class);
76
        $this->assertCount(1, $tasks);
77
        $task = reset($tasks);
78
        $this->assert_task_is_queued_for_quiz($task, $quiz);
79
    }
80
 
81
    /**
82
     * Attempting the quiz again after processing the task should queue a new task.
83
     *
84
     * @return void
85
     */
86
    public function test_queue_new_task_after_processing(): void {
87
        [$user1, $quiz, $course] = $this->create_test_data();
88
        $user2 = $this->getDataGenerator()->create_user();
89
 
90
        $tasks = manager::get_adhoc_tasks(recalculate::class);
91
        $this->assertEmpty($tasks);
92
 
93
        $this->attempt_quiz($quiz, $user1);
94
 
95
        $tasks = manager::get_adhoc_tasks(recalculate::class);
96
        $this->assertCount(1, $tasks);
97
 
98
        $this->expectOutputRegex("~Re-calculating statistics for quiz {$quiz->name} \({$quiz->id}\) " .
99
            "from course {$course->shortname} \({$course->id}\) with 1 attempts~");
100
        statistics_helper::run_pending_recalculation_tasks();
101
 
102
        $tasks = manager::get_adhoc_tasks(recalculate::class);
103
        $this->assertEmpty($tasks);
104
 
105
        $this->attempt_quiz($quiz, $user2);
106
 
107
        $tasks = manager::get_adhoc_tasks(recalculate::class);
108
        $this->assertCount(1, $tasks);
109
 
110
        $task = reset($tasks);
111
        $this->assert_task_is_queued_for_quiz($task, $quiz);
112
    }
113
 
114
    /**
115
     * Attempting different quizzes will queue a task for each.
116
     *
117
     * @return void
118
     */
119
    public function test_queue_separate_tasks_for_multiple_quizzes(): void {
120
        [$user1, $quiz1] = $this->create_test_data();
121
        [$user2, $quiz2] = $this->create_test_data();
122
 
123
        $tasks = manager::get_adhoc_tasks(recalculate::class);
124
        $this->assertEmpty($tasks);
125
 
126
        $this->attempt_quiz($quiz1, $user1);
127
        $this->attempt_quiz($quiz2, $user2);
128
 
129
        $tasks = manager::get_adhoc_tasks(recalculate::class);
130
        $this->assertCount(2, $tasks);
131
        $task1 = array_shift($tasks);
132
        $this->assert_task_is_queued_for_quiz($task1, $quiz1);
133
        $task2 = array_shift($tasks);
134
        $this->assert_task_is_queued_for_quiz($task2, $quiz2);
135
    }
136
}