Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
use mod_assign_testable_assign;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
require_once($CFG->dirroot . '/mod/assign/locallib.php');
25
require_once(__DIR__ . '/fixtures/testable_assign.php');
26
 
27
/**
28
 * Unit tests for (some of) mod/assign/locallib.php.
29
 *
30
 * @package    mod_assign
31
 * @category   test
32
 * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
1441 ariadna 35
final class base_test extends \advanced_testcase {
1 efrain 36
 
37
    /** @var Default number of students to create */
38
    const DEFAULT_STUDENT_COUNT = 3;
39
    /** @var Default number of teachers to create */
40
    const DEFAULT_TEACHER_COUNT = 2;
41
    /** @var Default number of editing teachers to create */
42
    const DEFAULT_EDITING_TEACHER_COUNT = 2;
43
    /** @var Optional extra number of students to create */
44
    const EXTRA_STUDENT_COUNT = 40;
45
    /** @var Optional number of suspended students */
46
    const EXTRA_SUSPENDED_COUNT = 10;
47
    /** @var Optional extra number of teachers to create */
48
    const EXTRA_TEACHER_COUNT = 5;
49
    /** @var Optional extra number of editing teachers to create */
50
    const EXTRA_EDITING_TEACHER_COUNT = 5;
51
    /** @var Number of groups to create */
52
    const GROUP_COUNT = 6;
53
 
54
    /** @var \stdClass $course New course created to hold the assignments */
55
    protected $course = null;
56
 
57
    /** @var array $teachers List of DEFAULT_TEACHER_COUNT teachers in the course*/
58
    protected $teachers = null;
59
 
60
    /** @var array $editingteachers List of DEFAULT_EDITING_TEACHER_COUNT editing teachers in the course */
61
    protected $editingteachers = null;
62
 
63
    /** @var array $students List of DEFAULT_STUDENT_COUNT students in the course*/
64
    protected $students = null;
65
 
66
    /** @var array $extrateachers List of EXTRA_TEACHER_COUNT teachers in the course*/
67
    protected $extrateachers = null;
68
 
69
    /** @var array $extraeditingteachers List of EXTRA_EDITING_TEACHER_COUNT editing teachers in the course*/
70
    protected $extraeditingteachers = null;
71
 
72
    /** @var array $extrastudents List of EXTRA_STUDENT_COUNT students in the course*/
73
    protected $extrastudents = null;
74
 
75
    /** @var array $extrasuspendedstudents List of EXTRA_SUSPENDED_COUNT students in the course*/
76
    protected $extrasuspendedstudents = null;
77
 
78
    /** @var array $groups List of 10 groups in the course */
79
    protected $groups = null;
80
 
81
    /**
82
     * Setup function - we will create a course and add an assign instance to it.
83
     */
84
    protected function setUp(): void {
85
        global $DB;
1441 ariadna 86
        parent::setUp();
1 efrain 87
 
88
        $this->resetAfterTest(true);
89
 
90
        $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
91
        $this->teachers = array();
92
        for ($i = 0; $i < self::DEFAULT_TEACHER_COUNT; $i++) {
93
            array_push($this->teachers, $this->getDataGenerator()->create_user());
94
        }
95
 
96
        $this->editingteachers = array();
97
        for ($i = 0; $i < self::DEFAULT_EDITING_TEACHER_COUNT; $i++) {
98
            array_push($this->editingteachers, $this->getDataGenerator()->create_user());
99
        }
100
 
101
        $this->students = array();
102
        for ($i = 0; $i < self::DEFAULT_STUDENT_COUNT; $i++) {
103
            array_push($this->students, $this->getDataGenerator()->create_user());
104
        }
105
 
106
        $this->groups = array();
107
        for ($i = 0; $i < self::GROUP_COUNT; $i++) {
108
            array_push($this->groups, $this->getDataGenerator()->create_group(array('courseid'=>$this->course->id)));
109
        }
110
 
111
        $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
112
        foreach ($this->teachers as $i => $teacher) {
113
            $this->getDataGenerator()->enrol_user($teacher->id,
114
                                                  $this->course->id,
115
                                                  $teacherrole->id);
116
            groups_add_member($this->groups[$i % self::GROUP_COUNT], $teacher);
117
        }
118
 
119
        $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
120
        foreach ($this->editingteachers as $i => $editingteacher) {
121
            $this->getDataGenerator()->enrol_user($editingteacher->id,
122
                                                  $this->course->id,
123
                                                  $editingteacherrole->id);
124
            groups_add_member($this->groups[$i % self::GROUP_COUNT], $editingteacher);
125
        }
126
 
127
        $studentrole = $DB->get_record('role', array('shortname'=>'student'));
128
        foreach ($this->students as $i => $student) {
129
            $this->getDataGenerator()->enrol_user($student->id,
130
                                                  $this->course->id,
131
                                                  $studentrole->id);
132
            groups_add_member($this->groups[$i % self::GROUP_COUNT], $student);
133
        }
134
    }
135
 
136
    /*
1441 ariadna 137
     * For tests that make sense to use a lot of data, create extra students/teachers.
1 efrain 138
     */
139
    protected function create_extra_users() {
140
        global $DB;
141
        $this->extrateachers = array();
142
        for ($i = 0; $i < self::EXTRA_TEACHER_COUNT; $i++) {
143
            array_push($this->extrateachers, $this->getDataGenerator()->create_user());
144
        }
145
 
146
        $this->extraeditingteachers = array();
147
        for ($i = 0; $i < self::EXTRA_EDITING_TEACHER_COUNT; $i++) {
148
            array_push($this->extraeditingteachers, $this->getDataGenerator()->create_user());
149
        }
150
 
151
        $this->extrastudents = array();
152
        for ($i = 0; $i < self::EXTRA_STUDENT_COUNT; $i++) {
153
            array_push($this->extrastudents, $this->getDataGenerator()->create_user());
154
        }
155
 
156
        $this->extrasuspendedstudents = array();
157
        for ($i = 0; $i < self::EXTRA_SUSPENDED_COUNT; $i++) {
158
            array_push($this->extrasuspendedstudents, $this->getDataGenerator()->create_user());
159
        }
160
 
161
        $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
162
        foreach ($this->extrateachers as $i => $teacher) {
163
            $this->getDataGenerator()->enrol_user($teacher->id,
164
                                                  $this->course->id,
165
                                                  $teacherrole->id);
166
            groups_add_member($this->groups[$i % self::GROUP_COUNT], $teacher);
167
        }
168
 
169
        $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
170
        foreach ($this->extraeditingteachers as $i => $editingteacher) {
171
            $this->getDataGenerator()->enrol_user($editingteacher->id,
172
                                                  $this->course->id,
173
                                                  $editingteacherrole->id);
174
            groups_add_member($this->groups[$i % self::GROUP_COUNT], $editingteacher);
175
        }
176
 
177
        $studentrole = $DB->get_record('role', array('shortname'=>'student'));
178
        foreach ($this->extrastudents as $i => $student) {
179
            $this->getDataGenerator()->enrol_user($student->id,
180
                                                  $this->course->id,
181
                                                  $studentrole->id);
182
            if ($i < (self::EXTRA_STUDENT_COUNT / 2)) {
183
                groups_add_member($this->groups[$i % self::GROUP_COUNT], $student);
184
            }
185
        }
186
 
187
        foreach ($this->extrasuspendedstudents as $i => $suspendedstudent) {
188
            $this->getDataGenerator()->enrol_user($suspendedstudent->id,
189
                                                  $this->course->id,
190
                                                  $studentrole->id, 'manual', 0, 0, ENROL_USER_SUSPENDED);
191
            if ($i < (self::EXTRA_SUSPENDED_COUNT / 2)) {
192
                groups_add_member($this->groups[$i % self::GROUP_COUNT], $suspendedstudent);
193
            }
194
        }
195
    }
196
 
197
    /**
198
     * Convenience function to create a testable instance of an assignment.
199
     *
200
     * @param array $params Array of parameters to pass to the generator
201
     * @return testable_assign Testable wrapper around the assign class.
202
     */
203
    protected function create_instance($params=array()) {
204
        $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
205
        if (!isset($params['course'])) {
206
            $params['course'] = $this->course->id;
207
        }
208
        $instance = $generator->create_instance($params);
209
        $cm = get_coursemodule_from_instance('assign', $instance->id);
210
        $context = \context_module::instance($cm->id);
211
        return new mod_assign_testable_assign($context, $cm, $this->course);
212
    }
213
 
11 efrain 214
    public function test_create_instance(): void {
1 efrain 215
        $this->assertNotEmpty($this->create_instance());
216
    }
217
 
218
}
219
 
220
class_alias('mod_assign_testable_assign', 'testable_assign');