2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Base class for unit tests for mod_assign.
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
30 require_once($CFG->dirroot
. '/mod/assign/locallib.php');
31 require_once($CFG->dirroot
. '/mod/assign/upgradelib.php');
32 require_once(__DIR__
. '/fixtures/testable_assign.php');
35 * Unit tests for (some of) mod/assign/locallib.php.
37 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class mod_assign_base_testcase
extends advanced_testcase
{
42 /** @const Default number of students to create */
43 const DEFAULT_STUDENT_COUNT
= 3;
44 /** @const Default number of teachers to create */
45 const DEFAULT_TEACHER_COUNT
= 2;
46 /** @const Default number of editing teachers to create */
47 const DEFAULT_EDITING_TEACHER_COUNT
= 2;
48 /** @const Optional extra number of students to create */
49 const EXTRA_STUDENT_COUNT
= 40;
50 /** @const Optional number of suspended students */
51 const EXTRA_SUSPENDED_COUNT
= 10;
52 /** @const Optional extra number of teachers to create */
53 const EXTRA_TEACHER_COUNT
= 5;
54 /** @const Optional extra number of editing teachers to create */
55 const EXTRA_EDITING_TEACHER_COUNT
= 5;
56 /** @const Number of groups to create */
57 const GROUP_COUNT
= 6;
59 /** @var stdClass $course New course created to hold the assignments */
60 protected $course = null;
62 /** @var array $teachers List of DEFAULT_TEACHER_COUNT teachers in the course*/
63 protected $teachers = null;
65 /** @var array $editingteachers List of DEFAULT_EDITING_TEACHER_COUNT editing teachers in the course */
66 protected $editingteachers = null;
68 /** @var array $students List of DEFAULT_STUDENT_COUNT students in the course*/
69 protected $students = null;
71 /** @var array $extrateachers List of EXTRA_TEACHER_COUNT teachers in the course*/
72 protected $extrateachers = null;
74 /** @var array $extraeditingteachers List of EXTRA_EDITING_TEACHER_COUNT editing teachers in the course*/
75 protected $extraeditingteachers = null;
77 /** @var array $extrastudents List of EXTRA_STUDENT_COUNT students in the course*/
78 protected $extrastudents = null;
80 /** @var array $extrasuspendedstudents List of EXTRA_SUSPENDED_COUNT students in the course*/
81 protected $extrasuspendedstudents = null;
83 /** @var array $groups List of 10 groups in the course */
84 protected $groups = null;
87 * Setup function - we will create a course and add an assign instance to it.
89 protected function setUp() {
92 $this->resetAfterTest(true);
94 $this->course
= $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
95 $this->teachers
= array();
96 for ($i = 0; $i < self
::DEFAULT_TEACHER_COUNT
; $i++
) {
97 array_push($this->teachers
, $this->getDataGenerator()->create_user());
100 $this->editingteachers
= array();
101 for ($i = 0; $i < self
::DEFAULT_EDITING_TEACHER_COUNT
; $i++
) {
102 array_push($this->editingteachers
, $this->getDataGenerator()->create_user());
105 $this->students
= array();
106 for ($i = 0; $i < self
::DEFAULT_STUDENT_COUNT
; $i++
) {
107 array_push($this->students
, $this->getDataGenerator()->create_user());
110 $this->groups
= array();
111 for ($i = 0; $i < self
::GROUP_COUNT
; $i++
) {
112 array_push($this->groups
, $this->getDataGenerator()->create_group(array('courseid'=>$this->course
->id
)));
115 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
116 foreach ($this->teachers
as $i => $teacher) {
117 $this->getDataGenerator()->enrol_user($teacher->id
,
120 groups_add_member($this->groups
[$i % self
::GROUP_COUNT
], $teacher);
123 $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
124 foreach ($this->editingteachers
as $i => $editingteacher) {
125 $this->getDataGenerator()->enrol_user($editingteacher->id
,
127 $editingteacherrole->id
);
128 groups_add_member($this->groups
[$i % self
::GROUP_COUNT
], $editingteacher);
131 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
132 foreach ($this->students
as $i => $student) {
133 $this->getDataGenerator()->enrol_user($student->id
,
136 groups_add_member($this->groups
[$i % self
::GROUP_COUNT
], $student);
141 * For tests that make sense to use alot of data, create extra students/teachers.
143 protected function create_extra_users() {
145 $this->extrateachers
= array();
146 for ($i = 0; $i < self
::EXTRA_TEACHER_COUNT
; $i++
) {
147 array_push($this->extrateachers
, $this->getDataGenerator()->create_user());
150 $this->extraeditingteachers
= array();
151 for ($i = 0; $i < self
::EXTRA_EDITING_TEACHER_COUNT
; $i++
) {
152 array_push($this->extraeditingteachers
, $this->getDataGenerator()->create_user());
155 $this->extrastudents
= array();
156 for ($i = 0; $i < self
::EXTRA_STUDENT_COUNT
; $i++
) {
157 array_push($this->extrastudents
, $this->getDataGenerator()->create_user());
160 $this->extrasuspendedstudents
= array();
161 for ($i = 0; $i < self
::EXTRA_SUSPENDED_COUNT
; $i++
) {
162 array_push($this->extrasuspendedstudents
, $this->getDataGenerator()->create_user());
165 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
166 foreach ($this->extrateachers
as $i => $teacher) {
167 $this->getDataGenerator()->enrol_user($teacher->id
,
170 groups_add_member($this->groups
[$i % self
::GROUP_COUNT
], $teacher);
173 $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
174 foreach ($this->extraeditingteachers
as $i => $editingteacher) {
175 $this->getDataGenerator()->enrol_user($editingteacher->id
,
177 $editingteacherrole->id
);
178 groups_add_member($this->groups
[$i % self
::GROUP_COUNT
], $editingteacher);
181 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
182 foreach ($this->extrastudents
as $i => $student) {
183 $this->getDataGenerator()->enrol_user($student->id
,
186 if ($i < (self
::EXTRA_STUDENT_COUNT
/ 2)) {
187 groups_add_member($this->groups
[$i % self
::GROUP_COUNT
], $student);
191 foreach ($this->extrasuspendedstudents
as $i => $suspendedstudent) {
192 $this->getDataGenerator()->enrol_user($suspendedstudent->id
,
194 $studentrole->id
, 'manual', 0, 0, ENROL_USER_SUSPENDED
);
195 if ($i < (self
::EXTRA_SUSPENDED_COUNT
/ 2)) {
196 groups_add_member($this->groups
[$i % self
::GROUP_COUNT
], $suspendedstudent);
202 * Convenience function to create a testable instance of an assignment.
204 * @param array $params Array of parameters to pass to the generator
205 * @return testable_assign Testable wrapper around the assign class.
207 protected function create_instance($params=array()) {
208 $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
209 if (!isset($params['course'])) {
210 $params['course'] = $this->course
->id
;
212 $instance = $generator->create_instance($params);
213 $cm = get_coursemodule_from_instance('assign', $instance->id
);
214 $context = context_module
::instance($cm->id
);
215 return new mod_assign_testable_assign($context, $cm, $this->course
);
218 public function test_create_instance() {
219 $this->assertNotEmpty($this->create_instance());
224 class_alias('mod_assign_testable_assign', 'testable_assign');