Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 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 (__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   */
  35  class base_test extends \advanced_testcase {
  36  
  37      /** @const Default number of students to create */
  38      const DEFAULT_STUDENT_COUNT = 3;
  39      /** @const Default number of teachers to create */
  40      const DEFAULT_TEACHER_COUNT = 2;
  41      /** @const Default number of editing teachers to create */
  42      const DEFAULT_EDITING_TEACHER_COUNT = 2;
  43      /** @const Optional extra number of students to create */
  44      const EXTRA_STUDENT_COUNT = 40;
  45      /** @const Optional number of suspended students */
  46      const EXTRA_SUSPENDED_COUNT = 10;
  47      /** @const Optional extra number of teachers to create */
  48      const EXTRA_TEACHER_COUNT = 5;
  49      /** @const Optional extra number of editing teachers to create */
  50      const EXTRA_EDITING_TEACHER_COUNT = 5;
  51      /** @const 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;
  86  
  87          $this->resetAfterTest(true);
  88  
  89          $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
  90          $this->teachers = array();
  91          for ($i = 0; $i < self::DEFAULT_TEACHER_COUNT; $i++) {
  92              array_push($this->teachers, $this->getDataGenerator()->create_user());
  93          }
  94  
  95          $this->editingteachers = array();
  96          for ($i = 0; $i < self::DEFAULT_EDITING_TEACHER_COUNT; $i++) {
  97              array_push($this->editingteachers, $this->getDataGenerator()->create_user());
  98          }
  99  
 100          $this->students = array();
 101          for ($i = 0; $i < self::DEFAULT_STUDENT_COUNT; $i++) {
 102              array_push($this->students, $this->getDataGenerator()->create_user());
 103          }
 104  
 105          $this->groups = array();
 106          for ($i = 0; $i < self::GROUP_COUNT; $i++) {
 107              array_push($this->groups, $this->getDataGenerator()->create_group(array('courseid'=>$this->course->id)));
 108          }
 109  
 110          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 111          foreach ($this->teachers as $i => $teacher) {
 112              $this->getDataGenerator()->enrol_user($teacher->id,
 113                                                    $this->course->id,
 114                                                    $teacherrole->id);
 115              groups_add_member($this->groups[$i % self::GROUP_COUNT], $teacher);
 116          }
 117  
 118          $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
 119          foreach ($this->editingteachers as $i => $editingteacher) {
 120              $this->getDataGenerator()->enrol_user($editingteacher->id,
 121                                                    $this->course->id,
 122                                                    $editingteacherrole->id);
 123              groups_add_member($this->groups[$i % self::GROUP_COUNT], $editingteacher);
 124          }
 125  
 126          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 127          foreach ($this->students as $i => $student) {
 128              $this->getDataGenerator()->enrol_user($student->id,
 129                                                    $this->course->id,
 130                                                    $studentrole->id);
 131              groups_add_member($this->groups[$i % self::GROUP_COUNT], $student);
 132          }
 133      }
 134  
 135      /*
 136       * For tests that make sense to use alot of data, create extra students/teachers.
 137       */
 138      protected function create_extra_users() {
 139          global $DB;
 140          $this->extrateachers = array();
 141          for ($i = 0; $i < self::EXTRA_TEACHER_COUNT; $i++) {
 142              array_push($this->extrateachers, $this->getDataGenerator()->create_user());
 143          }
 144  
 145          $this->extraeditingteachers = array();
 146          for ($i = 0; $i < self::EXTRA_EDITING_TEACHER_COUNT; $i++) {
 147              array_push($this->extraeditingteachers, $this->getDataGenerator()->create_user());
 148          }
 149  
 150          $this->extrastudents = array();
 151          for ($i = 0; $i < self::EXTRA_STUDENT_COUNT; $i++) {
 152              array_push($this->extrastudents, $this->getDataGenerator()->create_user());
 153          }
 154  
 155          $this->extrasuspendedstudents = array();
 156          for ($i = 0; $i < self::EXTRA_SUSPENDED_COUNT; $i++) {
 157              array_push($this->extrasuspendedstudents, $this->getDataGenerator()->create_user());
 158          }
 159  
 160          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 161          foreach ($this->extrateachers as $i => $teacher) {
 162              $this->getDataGenerator()->enrol_user($teacher->id,
 163                                                    $this->course->id,
 164                                                    $teacherrole->id);
 165              groups_add_member($this->groups[$i % self::GROUP_COUNT], $teacher);
 166          }
 167  
 168          $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
 169          foreach ($this->extraeditingteachers as $i => $editingteacher) {
 170              $this->getDataGenerator()->enrol_user($editingteacher->id,
 171                                                    $this->course->id,
 172                                                    $editingteacherrole->id);
 173              groups_add_member($this->groups[$i % self::GROUP_COUNT], $editingteacher);
 174          }
 175  
 176          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 177          foreach ($this->extrastudents as $i => $student) {
 178              $this->getDataGenerator()->enrol_user($student->id,
 179                                                    $this->course->id,
 180                                                    $studentrole->id);
 181              if ($i < (self::EXTRA_STUDENT_COUNT / 2)) {
 182                  groups_add_member($this->groups[$i % self::GROUP_COUNT], $student);
 183              }
 184          }
 185  
 186          foreach ($this->extrasuspendedstudents as $i => $suspendedstudent) {
 187              $this->getDataGenerator()->enrol_user($suspendedstudent->id,
 188                                                    $this->course->id,
 189                                                    $studentrole->id, 'manual', 0, 0, ENROL_USER_SUSPENDED);
 190              if ($i < (self::EXTRA_SUSPENDED_COUNT / 2)) {
 191                  groups_add_member($this->groups[$i % self::GROUP_COUNT], $suspendedstudent);
 192              }
 193          }
 194      }
 195  
 196      /**
 197       * Convenience function to create a testable instance of an assignment.
 198       *
 199       * @param array $params Array of parameters to pass to the generator
 200       * @return testable_assign Testable wrapper around the assign class.
 201       */
 202      protected function create_instance($params=array()) {
 203          $generator = $this->getDataGenerator()->get_plugin_generator('mod_assign');
 204          if (!isset($params['course'])) {
 205              $params['course'] = $this->course->id;
 206          }
 207          $instance = $generator->create_instance($params);
 208          $cm = get_coursemodule_from_instance('assign', $instance->id);
 209          $context = \context_module::instance($cm->id);
 210          return new mod_assign_testable_assign($context, $cm, $this->course);
 211      }
 212  
 213      public function test_create_instance() {
 214          $this->assertNotEmpty($this->create_instance());
 215      }
 216  
 217  }
 218  
 219  class_alias('mod_assign_testable_assign', 'testable_assign');