Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 
17
namespace mod_assign;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
23
require_once($CFG->dirroot . '/mod/assign/externallib.php');
24
require_once(__DIR__ . '/fixtures/testable_assign.php');
25
 
26
/**
27
 * Base class for unit tests for external functions in mod_assign.
28
 *
29
 * @package    mod_assign
30
 * @author     Andrew Madden <andrewmadden@catalyst-au.net>
31
 * @copyright  2021 Catalyst IT
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 */
34
abstract class externallib_advanced_testcase extends \externallib_advanced_testcase {
35
 
36
    /**
37
     * Create a submission for testing the get_submission_status function.
38
     * @param  bool $submitforgrading whether to submit for grading the submission
39
     * @param  array $params Optional params to use for creating assignment instance.
40
     * @return array an array containing all the required data for testing
41
     */
42
    protected function create_submission_for_testing_status(bool $submitforgrading = false, array $params = []): array {
43
        global $DB;
44
 
45
        // Create a course and assignment and users.
46
        $course = self::getDataGenerator()->create_course(['groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1]);
47
 
48
        $group1 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
49
        $group2 = $this->getDataGenerator()->create_group(['courseid' => $course->id]);
50
 
51
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
52
        $params = array_merge([
53
            'course' => $course->id,
54
            'assignsubmission_file_maxfiles' => 1,
55
            'assignsubmission_file_maxsizebytes' => 1024 * 1024,
56
            'assignsubmission_onlinetext_enabled' => 1,
57
            'assignsubmission_file_enabled' => 1,
58
            'submissiondrafts' => 1,
59
            'assignfeedback_file_enabled' => 1,
60
            'assignfeedback_comments_enabled' => 1,
1441 ariadna 61
            'maxattempts' => ASSIGN_UNLIMITED_ATTEMPTS,
1 efrain 62
            'attemptreopenmethod' => ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
63
            'sendnotifications' => 0
64
        ], $params);
65
 
66
        set_config('submissionreceipts', 0, 'assign');
67
 
68
        $instance = $generator->create_instance($params);
69
        $cm = get_coursemodule_from_instance('assign', $instance->id);
70
        $context = \context_module::instance($cm->id);
71
 
72
        $assign = new \mod_assign_testable_assign($context, $cm, $course);
73
 
74
        $student1 = self::getDataGenerator()->create_user();
75
        $student2 = self::getDataGenerator()->create_user();
76
        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
77
        $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
78
        $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);
79
        $teacher = self::getDataGenerator()->create_user();
80
        $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
81
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
82
 
83
        $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $student1->id]);
84
        $this->getDataGenerator()->create_group_member(['groupid' => $group1->id, 'userid' => $teacher->id]);
85
        $this->getDataGenerator()->create_group_member(['groupid' => $group2->id, 'userid' => $student2->id]);
86
        $this->getDataGenerator()->create_group_member(['groupid' => $group2->id, 'userid' => $teacher->id]);
87
 
88
        $this->setUser($student1);
89
 
90
        // Create a student1 with an online text submission.
91
        // Simulate a submission.
92
        $assign->get_user_submission($student1->id, true);
93
 
94
        $data = new \stdClass();
95
        $data->onlinetext_editor = [
96
            'itemid' => file_get_unused_draft_itemid(),
97
            'text' => 'Submission text with a <a href="@@PLUGINFILE@@/intro.txt">link</a>',
98
            'format' => FORMAT_MOODLE,
99
        ];
100
 
101
        $draftidfile = file_get_unused_draft_itemid();
102
        $usercontext = \context_user::instance($student1->id);
103
        $filerecord = [
104
            'contextid' => $usercontext->id,
105
            'component' => 'user',
106
            'filearea'  => 'draft',
107
            'itemid'    => $draftidfile,
108
            'filepath'  => '/',
109
            'filename'  => 't.txt',
110
        ];
111
        $fs = get_file_storage();
112
        $fs->create_file_from_string($filerecord, 'text contents');
113
 
114
        $data->files_filemanager = $draftidfile;
115
 
116
        $notices = [];
117
        $assign->save_submission($data, $notices);
118
 
119
        if ($submitforgrading) {
120
            // Now, submit the draft for grading.
121
            $notices = [];
122
 
123
            $data = new \stdClass;
124
            $data->userid = $student1->id;
125
            $assign->submit_for_grading($data, $notices);
126
        }
127
 
128
        return [$assign, $instance, $student1, $student2, $teacher, $group1, $group2];
129
    }
130
 
131
    /**
132
     * Create a course, assignment module instance, student and teacher and enrol them in
133
     * the course.
134
     *
135
     * @param array $params parameters to be provided to the assignment module creation
136
     * @return array containing the course, assignment module, student and teacher
137
     */
138
    protected function create_assign_with_student_and_teacher(array $params = []): array {
139
        global $DB;
140
 
141
        $course = $this->getDataGenerator()->create_course();
142
        $params = array_merge([
143
            'course' => $course->id,
144
            'name' => 'assignment',
145
            'intro' => 'assignment intro text',
146
        ], $params);
147
 
148
        // Create a course and assignment and users.
149
        $assign = $this->getDataGenerator()->create_module('assign', $params);
150
 
151
        $cm = get_coursemodule_from_instance('assign', $assign->id);
152
        $context = \context_module::instance($cm->id);
153
 
154
        $student = $this->getDataGenerator()->create_user();
155
        $studentrole = $DB->get_record('role', ['shortname' => 'student']);
156
        $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
157
        $teacher = $this->getDataGenerator()->create_user();
158
        $teacherrole = $DB->get_record('role', ['shortname' => 'teacher']);
159
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
160
 
161
        assign_capability('mod/assign:view', CAP_ALLOW, $teacherrole->id, $context->id, true);
162
        assign_capability('mod/assign:viewgrades', CAP_ALLOW, $teacherrole->id, $context->id, true);
163
        assign_capability('mod/assign:grade', CAP_ALLOW, $teacherrole->id, $context->id, true);
164
        accesslib_clear_all_caches_for_unit_testing();
165
 
166
        return [
167
            'course' => $course,
168
            'assign' => $assign,
169
            'student' => $student,
170
            'teacher' => $teacher,
171
        ];
172
    }
173
}