Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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  /**
  18   * Base class for unit tests for mod_assign.
  19   *
  20   * @package    mod_assign
  21   * @category   phpunit
  22   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  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');
  33  
  34  /**
  35   * Unit tests for (some of) mod/assign/locallib.php.
  36   *
  37   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class mod_assign_base_testcase extends advanced_testcase {
  41  
  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;
  58  
  59      /** @var stdClass $course New course created to hold the assignments */
  60      protected $course = null;
  61  
  62      /** @var array $teachers List of DEFAULT_TEACHER_COUNT teachers in the course*/
  63      protected $teachers = null;
  64  
  65      /** @var array $editingteachers List of DEFAULT_EDITING_TEACHER_COUNT editing teachers in the course */
  66      protected $editingteachers = null;
  67  
  68      /** @var array $students List of DEFAULT_STUDENT_COUNT students in the course*/
  69      protected $students = null;
  70  
  71      /** @var array $extrateachers List of EXTRA_TEACHER_COUNT teachers in the course*/
  72      protected $extrateachers = null;
  73  
  74      /** @var array $extraeditingteachers List of EXTRA_EDITING_TEACHER_COUNT editing teachers in the course*/
  75      protected $extraeditingteachers = null;
  76  
  77      /** @var array $extrastudents List of EXTRA_STUDENT_COUNT students in the course*/
  78      protected $extrastudents = null;
  79  
  80      /** @var array $extrasuspendedstudents List of EXTRA_SUSPENDED_COUNT students in the course*/
  81      protected $extrasuspendedstudents = null;
  82  
  83      /** @var array $groups List of 10 groups in the course */
  84      protected $groups = null;
  85  
  86      /**
  87       * Setup function - we will create a course and add an assign instance to it.
  88       */
  89      protected function setUp(): void {
  90          global $DB;
  91  
  92          $this->resetAfterTest(true);
  93  
  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());
  98          }
  99  
 100          $this->editingteachers = array();
 101          for ($i = 0; $i < self::DEFAULT_EDITING_TEACHER_COUNT; $i++) {
 102              array_push($this->editingteachers, $this->getDataGenerator()->create_user());
 103          }
 104  
 105          $this->students = array();
 106          for ($i = 0; $i < self::DEFAULT_STUDENT_COUNT; $i++) {
 107              array_push($this->students, $this->getDataGenerator()->create_user());
 108          }
 109  
 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)));
 113          }
 114  
 115          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 116          foreach ($this->teachers as $i => $teacher) {
 117              $this->getDataGenerator()->enrol_user($teacher->id,
 118                                                    $this->course->id,
 119                                                    $teacherrole->id);
 120              groups_add_member($this->groups[$i % self::GROUP_COUNT], $teacher);
 121          }
 122  
 123          $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
 124          foreach ($this->editingteachers as $i => $editingteacher) {
 125              $this->getDataGenerator()->enrol_user($editingteacher->id,
 126                                                    $this->course->id,
 127                                                    $editingteacherrole->id);
 128              groups_add_member($this->groups[$i % self::GROUP_COUNT], $editingteacher);
 129          }
 130  
 131          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 132          foreach ($this->students as $i => $student) {
 133              $this->getDataGenerator()->enrol_user($student->id,
 134                                                    $this->course->id,
 135                                                    $studentrole->id);
 136              groups_add_member($this->groups[$i % self::GROUP_COUNT], $student);
 137          }
 138      }
 139  
 140      /*
 141       * For tests that make sense to use alot of data, create extra students/teachers.
 142       */
 143      protected function create_extra_users() {
 144          global $DB;
 145          $this->extrateachers = array();
 146          for ($i = 0; $i < self::EXTRA_TEACHER_COUNT; $i++) {
 147              array_push($this->extrateachers, $this->getDataGenerator()->create_user());
 148          }
 149  
 150          $this->extraeditingteachers = array();
 151          for ($i = 0; $i < self::EXTRA_EDITING_TEACHER_COUNT; $i++) {
 152              array_push($this->extraeditingteachers, $this->getDataGenerator()->create_user());
 153          }
 154  
 155          $this->extrastudents = array();
 156          for ($i = 0; $i < self::EXTRA_STUDENT_COUNT; $i++) {
 157              array_push($this->extrastudents, $this->getDataGenerator()->create_user());
 158          }
 159  
 160          $this->extrasuspendedstudents = array();
 161          for ($i = 0; $i < self::EXTRA_SUSPENDED_COUNT; $i++) {
 162              array_push($this->extrasuspendedstudents, $this->getDataGenerator()->create_user());
 163          }
 164  
 165          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 166          foreach ($this->extrateachers as $i => $teacher) {
 167              $this->getDataGenerator()->enrol_user($teacher->id,
 168                                                    $this->course->id,
 169                                                    $teacherrole->id);
 170              groups_add_member($this->groups[$i % self::GROUP_COUNT], $teacher);
 171          }
 172  
 173          $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
 174          foreach ($this->extraeditingteachers as $i => $editingteacher) {
 175              $this->getDataGenerator()->enrol_user($editingteacher->id,
 176                                                    $this->course->id,
 177                                                    $editingteacherrole->id);
 178              groups_add_member($this->groups[$i % self::GROUP_COUNT], $editingteacher);
 179          }
 180  
 181          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 182          foreach ($this->extrastudents as $i => $student) {
 183              $this->getDataGenerator()->enrol_user($student->id,
 184                                                    $this->course->id,
 185                                                    $studentrole->id);
 186              if ($i < (self::EXTRA_STUDENT_COUNT / 2)) {
 187                  groups_add_member($this->groups[$i % self::GROUP_COUNT], $student);
 188              }
 189          }
 190  
 191          foreach ($this->extrasuspendedstudents as $i => $suspendedstudent) {
 192              $this->getDataGenerator()->enrol_user($suspendedstudent->id,
 193                                                    $this->course->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);
 197              }
 198          }
 199      }
 200  
 201      /**
 202       * Convenience function to create a testable instance of an assignment.
 203       *
 204       * @param array $params Array of parameters to pass to the generator
 205       * @return testable_assign Testable wrapper around the assign class.
 206       */
 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;
 211          }
 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);
 216      }
 217  
 218      public function test_create_instance() {
 219          $this->assertNotEmpty($this->create_instance());
 220      }
 221  
 222  }
 223  
 224  class_alias('mod_assign_testable_assign', 'testable_assign');