Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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($CFG->dirroot . '/mod/assign/upgradelib.php');
  26  require_once (__DIR__ . '/fixtures/testable_assign.php');
  27  
  28  /**
  29   * Unit tests for (some of) mod/assign/locallib.php.
  30   *
  31   * @package    mod_assign
  32   * @category   test
  33   * @copyright  1999 onwards Martin Dougiamas  {@link http://moodle.com}
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class base_test extends \advanced_testcase {
  37  
  38      /** @const Default number of students to create */
  39      const DEFAULT_STUDENT_COUNT = 3;
  40      /** @const Default number of teachers to create */
  41      const DEFAULT_TEACHER_COUNT = 2;
  42      /** @const Default number of editing teachers to create */
  43      const DEFAULT_EDITING_TEACHER_COUNT = 2;
  44      /** @const Optional extra number of students to create */
  45      const EXTRA_STUDENT_COUNT = 40;
  46      /** @const Optional number of suspended students */
  47      const EXTRA_SUSPENDED_COUNT = 10;
  48      /** @const Optional extra number of teachers to create */
  49      const EXTRA_TEACHER_COUNT = 5;
  50      /** @const Optional extra number of editing teachers to create */
  51      const EXTRA_EDITING_TEACHER_COUNT = 5;
  52      /** @const Number of groups to create */
  53      const GROUP_COUNT = 6;
  54  
  55      /** @var \stdClass $course New course created to hold the assignments */
  56      protected $course = null;
  57  
  58      /** @var array $teachers List of DEFAULT_TEACHER_COUNT teachers in the course*/
  59      protected $teachers = null;
  60  
  61      /** @var array $editingteachers List of DEFAULT_EDITING_TEACHER_COUNT editing teachers in the course */
  62      protected $editingteachers = null;
  63  
  64      /** @var array $students List of DEFAULT_STUDENT_COUNT students in the course*/
  65      protected $students = null;
  66  
  67      /** @var array $extrateachers List of EXTRA_TEACHER_COUNT teachers in the course*/
  68      protected $extrateachers = null;
  69  
  70      /** @var array $extraeditingteachers List of EXTRA_EDITING_TEACHER_COUNT editing teachers in the course*/
  71      protected $extraeditingteachers = null;
  72  
  73      /** @var array $extrastudents List of EXTRA_STUDENT_COUNT students in the course*/
  74      protected $extrastudents = null;
  75  
  76      /** @var array $extrasuspendedstudents List of EXTRA_SUSPENDED_COUNT students in the course*/
  77      protected $extrasuspendedstudents = null;
  78  
  79      /** @var array $groups List of 10 groups in the course */
  80      protected $groups = null;
  81  
  82      /**
  83       * Setup function - we will create a course and add an assign instance to it.
  84       */
  85      protected function setUp(): void {
  86          global $DB;
  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      /*
 137       * For tests that make sense to use alot of data, create extra students/teachers.
 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  
 214      public function test_create_instance() {
 215          $this->assertNotEmpty($this->create_instance());
 216      }
 217  
 218  }
 219  
 220  class_alias('mod_assign_testable_assign', 'testable_assign');