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_deleted 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_deleted_test extends \advanced_testcase {
38
    use \quiz_question_helper_test_trait;
39
    use statistics_test_trait;
40
 
41
    /**
42
     * Deleting an attempt 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_deletion(): void {
47
        [$user, $quiz] = $this->create_test_data();
48
        $this->attempt_quiz($quiz, $user);
49
        [, , $attempt] = $this->attempt_quiz($quiz, $user, 2);
50
        statistics_helper::run_pending_recalculation_tasks(true);
51
 
52
        $tasks = manager::get_adhoc_tasks(recalculate::class);
53
        $this->assertEmpty($tasks);
54
 
55
        quiz_delete_attempt($attempt->get_attemptid(), $quiz);
56
 
57
        $tasks = manager::get_adhoc_tasks(recalculate::class);
58
        $this->assertCount(1, $tasks);
59
        $task = reset($tasks);
60
        $this->assert_task_is_queued_for_quiz($task, $quiz);
61
    }
62
 
63
    /**
64
     * Deleting multiple attempts of the same quiz should only queue one instance of the task.
65
     *
66
     * @return void
67
     */
68
    public function test_queue_single_task_for_multiple_deletions(): void {
69
        [$user1, $quiz] = $this->create_test_data();
70
        $user2 = $this->getDataGenerator()->create_user();
71
        $this->attempt_quiz($quiz, $user1);
72
        [, , $attempt1] = $this->attempt_quiz($quiz, $user1, 2);
73
        $this->attempt_quiz($quiz, $user2);
74
        [, , $attempt2] = $this->attempt_quiz($quiz, $user2, 2);
75
        statistics_helper::run_pending_recalculation_tasks(true);
76
 
77
        $tasks = manager::get_adhoc_tasks(recalculate::class);
78
        $this->assertEmpty($tasks);
79
 
80
        quiz_delete_attempt($attempt1->get_attemptid(), $quiz);
81
        quiz_delete_attempt($attempt2->get_attemptid(), $quiz);
82
 
83
        $tasks = manager::get_adhoc_tasks(recalculate::class);
84
        $this->assertCount(1, $tasks);
85
        $task = reset($tasks);
86
        $this->assert_task_is_queued_for_quiz($task, $quiz);
87
    }
88
 
89
    /**
90
     * Deleting another attempt after processing the task should queue a new task.
91
     *
92
     * @return void
93
     */
94
    public function test_queue_new_task_after_processing(): void {
95
        [$user1, $quiz, $course] = $this->create_test_data();
96
        $user2 = $this->getDataGenerator()->create_user();
97
        $this->attempt_quiz($quiz, $user1);
98
        [, , $attempt1] = $this->attempt_quiz($quiz, $user1, 2);
99
        $this->attempt_quiz($quiz, $user2);
100
        [, , $attempt2] = $this->attempt_quiz($quiz, $user2, 2);
101
        statistics_helper::run_pending_recalculation_tasks(true);
102
 
103
        $tasks = manager::get_adhoc_tasks(recalculate::class);
104
        $this->assertEmpty($tasks);
105
 
106
        quiz_delete_attempt($attempt1->get_attemptid(), $quiz);
107
 
108
        $tasks = manager::get_adhoc_tasks(recalculate::class);
109
        $this->assertCount(1, $tasks);
110
 
111
        $this->expectOutputRegex("~Re-calculating statistics for quiz {$quiz->name} \({$quiz->id}\) " .
112
            "from course {$course->shortname} \({$course->id}\) with 3 attempts~");
113
        statistics_helper::run_pending_recalculation_tasks();
114
 
115
        $tasks = manager::get_adhoc_tasks(recalculate::class);
116
        $this->assertEmpty($tasks);
117
 
118
        quiz_delete_attempt($attempt2->get_attemptid(), $quiz);
119
 
120
        $tasks = manager::get_adhoc_tasks(recalculate::class);
121
        $this->assertCount(1, $tasks);
122
 
123
        $task = reset($tasks);
124
        $this->assert_task_is_queued_for_quiz($task, $quiz);
125
    }
126
 
127
    /**
128
     * Deleting attempts from different quizzes will queue a task for each.
129
     *
130
     * @return void
131
     */
132
    public function test_queue_separate_tasks_for_multiple_quizzes(): void {
133
        [$user1, $quiz1] = $this->create_test_data();
134
        [$user2, $quiz2] = $this->create_test_data();
135
        $this->attempt_quiz($quiz1, $user1);
136
        [, , $attempt1] = $this->attempt_quiz($quiz1, $user1, 2);
137
        $this->attempt_quiz($quiz2, $user2);
138
        [, , $attempt2] = $this->attempt_quiz($quiz2, $user2, 2);
139
        statistics_helper::run_pending_recalculation_tasks(true);
140
 
141
        $tasks = manager::get_adhoc_tasks(recalculate::class);
142
        $this->assertEmpty($tasks);
143
 
144
        quiz_delete_attempt($attempt1->get_attemptid(), $quiz1);
145
        quiz_delete_attempt($attempt2->get_attemptid(), $quiz2);
146
 
147
        $tasks = manager::get_adhoc_tasks(recalculate::class);
148
        $this->assertCount(2, $tasks);
149
        $task1 = array_shift($tasks);
150
        $this->assert_task_is_queued_for_quiz($task1, $quiz1);
151
        $task2 = array_shift($tasks);
152
        $this->assert_task_is_queued_for_quiz($task2, $quiz2);
153
    }
154
}