Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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 mod_assign\external;
18
 
19
use mod_assign_test_generator;
20
use mod_assign_external;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
global $CFG;
25
 
26
require_once("$CFG->dirroot/mod/assign/tests/generator.php");
27
require_once("$CFG->dirroot/mod/assign/tests/fixtures/event_mod_assign_fixtures.php");
28
require_once("$CFG->dirroot/mod/assign/tests/externallib_advanced_testcase.php");
29
 
30
/**
31
 * Test the remove submission external function.
32
 *
33
 * @package    mod_assign
34
 * @category   test
35
 *
36
 * @copyright  2024 Daniel Ureña <durenadev@gmail.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 * @covers     \mod_assign\external\remove_submission
39
 */
40
final class remove_submission_test extends \mod_assign\externallib_advanced_testcase {
41
    // Use the generator helper.
42
    use mod_assign_test_generator;
43
 
44
    /**
45
     * Prepare course with users, teacher and assign.
46
     *
47
     * @return array Containing course, student1, student2, teacher assign and instance data
48
     */
49
    protected function prepare_course(): array {
50
 
51
        $course = $this->getDataGenerator()->create_course();
52
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
53
        $params['course'] = $course->id;
54
        $params['assignsubmission_onlinetext_enabled'] = 1;
55
        $instance = $generator->create_instance($params);
56
        $cm = get_coursemodule_from_instance('assign', $instance->id);
57
        $context = \context_module::instance($cm->id);
58
        $assign = new \mod_assign_testable_assign($context, $cm, $course);
59
 
60
        $teacher  = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
61
        $student1 = $this->getDataGenerator()->create_and_enrol($course, 'student');
62
        $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
63
 
64
        return [$course, $student1, $student2, $teacher, $assign, $instance];
65
    }
66
 
67
    /**
68
     * Test remove submission by WS with invalid assign id.
69
     * @covers ::execute
70
     */
71
    public function test_remove_submission_with_invalid_assign_id(): void {
72
        $this->resetAfterTest();
73
        [$course, $student1, $student2, $teacher, $assign, $instance] = $this->prepare_course();
74
        $this->expectException(\moodle_exception::class);
75
        remove_submission::execute($student1->id, 123);
76
    }
77
 
78
    /**
79
     * Test remove submission by WS with invalid user id.
80
     * @covers ::execute
81
     */
82
    public function test_remove_submission_with_invalid_user_id(): void {
83
        $this->resetAfterTest();
84
        [$course, $student1, $student2, $teacher, $assign, $instance] = $this->prepare_course();
85
        $this->setUser($student1);
86
        $result = remove_submission::execute(123, $assign->get_instance()->id);
87
        $this->assertFalse($result['status']);
88
        $this->assertEquals('submissionnotfoundtoremove', $result['warnings'][0]['warningcode']);
89
    }
90
 
91
    /**
92
     * Test teacher can't remove student submissions by WS.
93
     * @covers ::execute
94
     */
95
    public function test_teacher_remove_submissions(): void {
96
        $this->resetAfterTest();
97
        [$course, $student1, $student2, $teacher, $assign, $instance] = $this->prepare_course();
98
        $this->add_submission($student1, $assign);
99
        $this->add_submission($student2, $assign);
100
        $this->setUser($teacher);
101
 
102
        $result = remove_submission::execute($student1->id, $assign->get_instance()->id);
103
        $this->assertFalse($result['status']);
104
        $this->assertEquals('couldnotremovesubmission', $result['warnings'][0]['warningcode']);
105
 
106
        $result = remove_submission::execute($student2->id, $assign->get_instance()->id);
107
        $this->assertFalse($result['status']);
108
        $this->assertEquals('couldnotremovesubmission', $result['warnings'][0]['warningcode']);
109
    }
110
 
111
    /**
112
     * Test teacher can remove student submissions by WS if they have added capability.
113
     * @covers ::execute
114
     */
115
    public function test_teacher_editothersubmission_remove_submissions(): void {
116
        global $DB;
117
        $this->resetAfterTest();
118
        [$course, $student1, $student2, $teacher, $assign, $instance] = $this->prepare_course();
119
        $this->add_submission($student1, $assign);
120
        $this->add_submission($student2, $assign);
121
 
122
        $capability = 'mod/assign:editothersubmission';
123
        $roleteacher = $DB->get_record('role', ['shortname' => 'teacher']);
124
        assign_capability($capability, CAP_ALLOW, $roleteacher->id, $assign->get_context()->id);
125
        role_assign($roleteacher->id, $teacher->id, $assign->get_context()->id);
126
        accesslib_clear_all_caches_for_unit_testing();
127
 
128
        $this->setUser($teacher);
129
 
130
        $result = remove_submission::execute($student1->id, $assign->get_instance()->id);
131
        $this->assertTrue($result['status']);
132
        $this->assertEmpty($result['warnings']);
133
 
134
        $result = remove_submission::execute($student2->id, $assign->get_instance()->id);
135
        $this->assertTrue($result['status']);
136
        $this->assertEmpty($result['warnings']);
137
    }
138
 
139
    /**
140
     * Test user can't remove their own non-existent submission.
141
     * @covers ::execute
142
     */
143
    public function test_remove_own_notexists_submission(): void {
144
        $this->resetAfterTest();
145
        [$course, $student1, $student2, $teacher, $assign, $instance] = $this->prepare_course();
146
 
147
        // Remove own submission when user has no submission to remove.
148
        $this->setUser($student1);
149
        $result = remove_submission::execute($student1->id, $assign->get_instance()->id);
150
        $this->assertFalse($result['status']);
151
        $this->assertEquals('submissionnotfoundtoremove', $result['warnings'][0]['warningcode']);
152
    }
153
 
154
    /**
155
     * Test user can remove their own existing submission.
156
     * @covers ::execute
157
     */
158
    public function test_remove_own_submission(): void {
159
        global $DB;
160
        $this->resetAfterTest();
161
        [$course, $student1, $student2, $teacher, $assign, $instance] = $this->prepare_course();
162
 
163
        // Remove own submission.
164
        $this->add_submission($student2, $assign);
165
        $this->setUser($student2);
166
 
167
        $result = remove_submission::execute($student2->id, $assign->get_instance()->id);
168
        $this->assertTrue($result['status']);
169
        $this->assertEmpty($result['warnings']);
170
 
171
        // Make sure submission was removed.
172
        $submission      = $assign->get_user_submission($student2->id, 0);
173
        $submissionquery = $DB->get_record('assign_submission', ['id' => $submission->id]);
174
        $this->assertEquals(ASSIGN_SUBMISSION_STATUS_NEW, $submissionquery->status);
175
 
176
        // Try to remove after removed.
177
        $result = remove_submission::execute($student2->id, $assign->get_instance()->id);
178
        $this->assertFalse($result['status']);
179
        $this->assertEquals('submissionnotfoundtoremove', $result['warnings'][0]['warningcode']);
180
    }
181
 
182
    /**
183
     * Test user can remove their own reopened submission.
184
     * @covers ::execute
185
     */
186
    public function test_remove_own_submission_reopened(): void {
187
        global $DB, $USER;
188
        $this->resetAfterTest();
189
        [$course, $student1, $student2, $teacher, $assign, $instance] = $this->prepare_course();
190
 
191
        // Create submission and reopen.
192
        $this->add_submission($student2, $assign);
193
        // Grade and reopen.
194
        $this->setUser($teacher);
195
        $feedbackpluginparams = [];
196
        $feedbackpluginparams['files_filemanager'] = file_get_unused_draft_itemid();
197
        $feedbackeditorparams = ['text' => 'Yeeha!', 'format' => 1];
198
        $feedbackpluginparams['assignfeedbackcomments_editor'] = $feedbackeditorparams;
199
        mod_assign_external::save_grade(
200
            $instance->id,
201
            $student1->id,
202
            50.0,
203
            -1,
204
            false,
205
            'released',
206
            false,
207
            $feedbackpluginparams
208
        );
209
        $USER->ignoresesskey = true;
210
        $assign->testable_process_add_attempt($student1->id);
211
 
212
        // Create submission.
213
        $this->add_submission($student2, $assign);
214
 
215
        // Remove own submission.
216
        $this->setUser($student2);
217
        $result = remove_submission::execute($student2->id, $assign->get_instance()->id);
218
        $this->assertTrue($result['status']);
219
        $this->assertEmpty($result['warnings']);
220
 
221
        // Make sure submission was removed.
222
        $submission      = $assign->get_user_submission($student2->id, 0);
223
        $submissionquery = $DB->get_record('assign_submission', ['id' => $submission->id]);
224
        $this->assertEquals(ASSIGN_SUBMISSION_STATUS_NEW, $submissionquery->status);
225
    }
226
}