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\courseformat;
18
 
19
use core_courseformat\local\overview\overviewfactory;
20
 
21
/**
22
 * Tests for Assignment overview integration.
23
 *
24
 * @covers \mod_assign\course\overview
25
 * @package    mod_assign
26
 * @category   test
27
 * @copyright  2025 Ferran Recio <ferran@moodle.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
final class overview_test extends \advanced_testcase {
31
    #[\Override]
32
    public static function setUpBeforeClass(): void {
33
        global $CFG;
34
        require_once($CFG->dirroot . '/mod/assign/locallib.php');
35
        require_once($CFG->dirroot . '/mod/assign/tests/fixtures/testable_assign.php');
36
        parent::setUpBeforeClass();
37
    }
38
 
39
    /**
40
     * Test get_actions_overview method.
41
     *
42
     * @covers ::get_actions_overview
43
     */
44
    public function test_get_actions_overview(): void {
45
        $this->resetAfterTest();
46
        $course = $this->getDataGenerator()->create_course();
47
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
48
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
49
 
50
        // Setup the assignment.
51
        $activity = $this->getDataGenerator()->create_module(
52
            'assign',
53
            ['course' => $course->id],
54
        );
55
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
56
        $assign = new \mod_assign_testable_assign($cm->context, $cm, $course);
57
 
58
        // Check for 0 submissions.
59
        $this->setUser($teacher);
60
        $item = overviewfactory::create($cm)->get_actions_overview();
61
        $this->assertEquals(get_string('actions'), $item->get_name());
62
        $this->assertEquals(get_string('gradeverb'), $item->get_value());
63
        $this->assertEquals(0, $item->get_alert_count());
64
        $this->assertEquals(get_string('numberofsubmissionsneedgrading', 'assign'), $item->get_alert_label());
65
 
66
        // Simulate an assignment submission.
67
        $this->setUser($student);
68
        $submission = $assign->get_user_submission($student->id, true);
69
        $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
70
        $assign->testable_update_submission($submission, $student->id, true, false);
71
        $data = new \stdClass();
72
        $data->onlinetext_editor = [
73
            'itemid' => file_get_unused_draft_itemid(),
74
            'text' => 'Submission text',
75
            'format' => FORMAT_MOODLE,
76
        ];
77
        $plugin = $assign->get_submission_plugin_by_type('onlinetext');
78
        $plugin->save($submission, $data);
79
 
80
        // Check for 1 ungraded submission.
81
        $this->setUser($teacher);
82
        $item = overviewfactory::create($cm)->get_actions_overview();
83
        $this->assertEquals(get_string('actions'), $item->get_name());
84
        $this->assertEquals(get_string('gradeverb'), $item->get_value());
85
        $this->assertEquals(1, $item->get_alert_count());
86
        $this->assertEquals(get_string('numberofsubmissionsneedgrading', 'assign'), $item->get_alert_label());
87
 
88
        // Check students cannot access submissions.
89
        $this->setUser($student);
90
        $item = overviewfactory::create($cm)->get_actions_overview();
91
        $this->assertNull($item);
92
    }
93
 
94
    /**
95
     * Test get_due_date_overview method.
96
     *
97
     * @covers ::get_due_date_overview
98
     * @dataProvider get_due_date_overview_provider
99
     * @param int|null $timeincrement null if no due date, or due date increment.
100
     */
101
    public function test_get_due_date_overview(
102
        int|null $timeincrement,
103
    ): void {
104
        $this->resetAfterTest();
105
        $course = $this->getDataGenerator()->create_course();
106
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
107
 
108
        if ($timeincrement === null) {
109
            $expectedtime = null;
110
        } else {
111
            $expectedtime = $this->mock_clock_with_frozen()->time() + $timeincrement;
112
        }
113
 
114
        $activity = $this->getDataGenerator()->create_module(
115
            'assign',
116
            [
117
                'course' => $course->id,
118
                'assignsubmission_onlinetext_enabled' => 1,
119
                'duedate' => !empty($expectedtime) ? $expectedtime : 0,
120
            ],
121
        );
122
 
123
        $this->setUser($teacher);
124
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
125
 
126
        $item = overviewfactory::create($cm)->get_due_date_overview();
127
        $this->assertEquals(get_string('duedate', 'assign'), $item->get_name());
128
        $this->assertEquals($expectedtime, $item->get_value());
129
    }
130
 
131
    /**
132
     * Provider for get_due_date_overview.
133
     *
134
     * @return array
135
     */
136
    public static function get_due_date_overview_provider(): array {
137
        return [
138
            'no_due' => [
139
                'timeincrement' => null,
140
            ],
141
            'past_due' => [
142
                'timeincrement' => -1 * (4 * DAYSECS),
143
            ],
144
            'future_due' => [
145
                'timeincrement' => (4 * DAYSECS),
146
            ],
147
        ];
148
    }
149
 
150
    /**
151
     * Test get_extra_submissions_overview method.
152
     *
153
     * @covers ::get_extra_submissions_overview
154
     */
155
    public function test_get_extra_submissions_overview(): void {
156
        $this->resetAfterTest();
157
        $course = $this->getDataGenerator()->create_course();
158
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
159
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
160
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student2');
161
 
162
        // Setup the assignment.
163
        $activity = $this->getDataGenerator()->create_module(
164
            'assign',
165
            [
166
                'course' => $course->id,
167
                'assignsubmission_onlinetext_enabled' => 1,
168
            ],
169
        );
170
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
171
        $assign = new \mod_assign_testable_assign($cm->context, $cm, $course);
172
 
173
        // Check teacher has 0 submissions.
174
        $this->setUser($teacher);
175
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
176
        $overview = overviewfactory::create($cm);
177
        $reflection = new \ReflectionClass($overview);
178
        $method = $reflection->getMethod('get_extra_submissions_overview');
179
        $method->setAccessible(true);
180
        $item = $method->invoke($overview);
181
        $this->assertEquals(get_string('submissions', 'assign'), $item->get_name());
182
        $this->assertEquals(0, $item->get_value());
183
 
184
        // Simulate an assignment submission.
185
        $this->setUser($student);
186
        $submission = $assign->get_user_submission($student->id, true);
187
        $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
188
        $assign->testable_update_submission($submission, $student->id, true, false);
189
        $data = new \stdClass();
190
        $data->onlinetext_editor = [
191
            'itemid' => file_get_unused_draft_itemid(),
192
            'text' => 'Submission text',
193
            'format' => FORMAT_MOODLE,
194
        ];
195
        $plugin = $assign->get_submission_plugin_by_type('onlinetext');
196
        $plugin->save($submission, $data);
197
 
198
        // Check students cannot access submissions.
199
        $this->setUser($student);
200
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
201
        $overview = overviewfactory::create($cm);
202
        $reflection = new \ReflectionClass($overview);
203
        $method = $reflection->getMethod('get_extra_submissions_overview');
204
        $method->setAccessible(true);
205
        $item = $method->invoke($overview);
206
        $this->assertNull($item);
207
 
208
        // Check teacher has 1 submissions.
209
        $this->setUser($teacher);
210
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
211
        $overview = overviewfactory::create($cm);
212
        $reflection = new \ReflectionClass($overview);
213
        $method = $reflection->getMethod('get_extra_submissions_overview');
214
        $method->setAccessible(true);
215
        $item = $method->invoke($overview);
216
        $this->assertEquals(get_string('submissions', 'assign'), $item->get_name());
217
        $this->assertEquals(1, $item->get_value());
218
    }
219
 
220
    /**
221
     * Test get_extra_submission_status_overview method.
222
     *
223
     * @covers ::get_extra_submission_status_overview
224
     */
225
    public function test_get_extra_submission_status_overview(): void {
226
        $this->resetAfterTest();
227
        $course = $this->getDataGenerator()->create_course();
228
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
229
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
230
 
231
        // Setup the assignment.
232
        $activity = $this->getDataGenerator()->create_module(
233
            'assign',
234
            [
235
                'course' => $course->id,
236
                'assignsubmission_onlinetext_enabled' => 1,
237
            ],
238
        );
239
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
240
        $assign = new \mod_assign_testable_assign($cm->context, $cm, $course);
241
 
242
        // Check teacher does not has submission status.
243
        $this->setUser($teacher);
244
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
245
        $overview = overviewfactory::create($cm);
246
        $reflection = new \ReflectionClass($overview);
247
        $method = $reflection->getMethod('get_extra_submission_status_overview');
248
        $method->setAccessible(true);
249
        $item = $method->invoke($overview);
250
        $this->assertNull($item);
251
 
252
        // Admin does not have submission status.
253
        $this->setAdminUser();
254
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
255
        $overview = overviewfactory::create($cm);
256
        $reflection = new \ReflectionClass($overview);
257
        $method = $reflection->getMethod('get_extra_submission_status_overview');
258
        $method->setAccessible(true);
259
        $item = $method->invoke($overview);
260
        $this->assertNull($item);
261
 
262
        // Check student see the new status.
263
        $this->setUser($student);
264
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
265
        $overview = overviewfactory::create($cm);
266
        $reflection = new \ReflectionClass($overview);
267
        $method = $reflection->getMethod('get_extra_submission_status_overview');
268
        $method->setAccessible(true);
269
        $item = $method->invoke($overview);
270
        $this->assertEquals(get_string('submissionstatus', 'assign'), $item->get_name());
271
        $this->assertEquals(get_string('submissionstatus_new', 'assign'), $item->get_value());
272
 
273
        // Simulate an assignment submission.
274
        $this->setUser($student);
275
        $submission = $assign->get_user_submission($student->id, true);
276
        $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
277
        $assign->testable_update_submission($submission, $student->id, true, false);
278
        $data = new \stdClass();
279
        $data->onlinetext_editor = [
280
            'itemid' => file_get_unused_draft_itemid(),
281
            'text' => 'Submission text',
282
            'format' => FORMAT_MOODLE,
283
        ];
284
        $plugin = $assign->get_submission_plugin_by_type('onlinetext');
285
        $plugin->save($submission, $data);
286
 
287
        // Check student see the new status.
288
        $this->setUser($student);
289
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
290
        $overview = overviewfactory::create($cm);
291
        $reflection = new \ReflectionClass($overview);
292
        $method = $reflection->getMethod('get_extra_submission_status_overview');
293
        $method->setAccessible(true);
294
        $item = $method->invoke($overview);
295
        $this->assertEquals(get_string('submissionstatus', 'assign'), $item->get_name());
296
        $this->assertEquals(get_string('submissionstatus_submitted', 'assign'), $item->get_value());
297
    }
298
 
299
    /**
300
     * Test get_extra_submission_status_overview method in group submissions.
301
     *
302
     * @covers ::get_extra_submission_status_overview
303
     */
304
    public function test_get_extra_submission_status_overview_groups(): void {
305
        $this->resetAfterTest();
306
        $course = $this->getDataGenerator()->create_course();
307
        $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher');
308
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
309
        $student2 = $this->getDataGenerator()->create_and_enrol($course, 'student');
310
 
311
        $group = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
312
        groups_add_member($group, $student);
313
        groups_add_member($group, $student2);
314
 
315
        // Setup the assignment.
316
        $activity = $this->getDataGenerator()->create_module(
317
            'assign',
318
            [
319
                'course' => $course->id,
320
                'assignsubmission_onlinetext_enabled' => 1,
321
                'teamsubmission' => 1,
322
            ],
323
        );
324
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
325
 
326
        $assign = new \mod_assign_testable_assign($cm->context, $cm, $course);
327
 
328
        // Check teacher does not has submission status.
329
        $this->setUser($teacher);
330
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
331
        $overview = overviewfactory::create($cm);
332
        $reflection = new \ReflectionClass($overview);
333
        $method = $reflection->getMethod('get_extra_submission_status_overview');
334
        $method->setAccessible(true);
335
        $item = $method->invoke($overview);
336
        $this->assertNull($item);
337
 
338
        // Admin does not have submission status.
339
        $this->setAdminUser();
340
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
341
        $overview = overviewfactory::create($cm);
342
        $reflection = new \ReflectionClass($overview);
343
        $method = $reflection->getMethod('get_extra_submission_status_overview');
344
        $method->setAccessible(true);
345
        $item = $method->invoke($overview);
346
        $this->assertNull($item);
347
 
348
        // Check student see the new status.
349
        $this->setUser($student);
350
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
351
        $overview = overviewfactory::create($cm);
352
        $reflection = new \ReflectionClass($overview);
353
        $method = $reflection->getMethod('get_extra_submission_status_overview');
354
        $method->setAccessible(true);
355
        $item = $method->invoke($overview);
356
        $this->assertEquals(get_string('submissionstatus', 'assign'), $item->get_name());
357
        $this->assertEquals(get_string('submissionstatus_new', 'assign'), $item->get_value());
358
 
359
        // Simulate an assignment submission.
360
        $this->setUser($student2);
361
        $submission = $assign->get_group_submission($student2->id, $group->id, true);
362
        $submission->status = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
363
        $assign->testable_update_submission($submission, $student2->id, true, false);
364
        $data = new \stdClass();
365
        $data->onlinetext_editor = [
366
            'itemid' => file_get_unused_draft_itemid(),
367
            'text' => 'Submission text',
368
            'format' => FORMAT_MOODLE,
369
        ];
370
        $plugin = $assign->get_submission_plugin_by_type('onlinetext');
371
        $plugin->save($submission, $data);
372
 
373
        // Check student see the new status.
374
        $this->setUser($student);
375
        $cm = get_fast_modinfo($course)->get_cm($activity->cmid);
376
        $overview = overviewfactory::create($cm);
377
        $reflection = new \ReflectionClass($overview);
378
        $method = $reflection->getMethod('get_extra_submission_status_overview');
379
        $method->setAccessible(true);
380
        $item = $method->invoke($overview);
381
        $this->assertEquals(get_string('submissionstatus', 'assign'), $item->get_name());
382
        $this->assertEquals(get_string('submissionstatus_submitted', 'assign'), $item->get_value());
383
    }
384
}