/mod/assign/tests/base_test.php

https://github.com/markn86/moodle · PHP · 224 lines · 131 code · 36 blank · 57 comment · 12 complexity · 3deea20a153e336edc35a9d2d60c8566 MD5 · raw file

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