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_quiz;
  18  
  19  /**
  20   * PHPUnit data generator testcase
  21   *
  22   * @package    mod_quiz
  23   * @category   phpunit
  24   * @copyright  2012 Matt Petro
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   * @covers \mod_quiz_generator
  27   */
  28  class generator_test extends \advanced_testcase {
  29      public function test_generator() {
  30          global $DB, $SITE;
  31  
  32          $this->resetAfterTest(true);
  33  
  34          $this->assertEquals(0, $DB->count_records('quiz'));
  35  
  36          /** @var \mod_quiz_generator $generator */
  37          $generator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
  38          $this->assertInstanceOf('mod_quiz_generator', $generator);
  39          $this->assertEquals('quiz', $generator->get_modulename());
  40  
  41          $generator->create_instance(array('course'=>$SITE->id));
  42          $generator->create_instance(array('course'=>$SITE->id));
  43          $createtime = time();
  44          $quiz = $generator->create_instance(array('course' => $SITE->id, 'timecreated' => 0));
  45          $this->assertEquals(3, $DB->count_records('quiz'));
  46  
  47          $cm = get_coursemodule_from_instance('quiz', $quiz->id);
  48          $this->assertEquals($quiz->id, $cm->instance);
  49          $this->assertEquals('quiz', $cm->modname);
  50          $this->assertEquals($SITE->id, $cm->course);
  51  
  52          $context = \context_module::instance($cm->id);
  53          $this->assertEquals($quiz->cmid, $context->instanceid);
  54  
  55          $this->assertEqualsWithDelta($createtime,
  56                  $DB->get_field('quiz', 'timecreated', ['id' => $cm->instance]), 2);
  57      }
  58  
  59      public function test_generating_a_user_override() {
  60          $this->resetAfterTest(true);
  61  
  62          $generator = $this->getDataGenerator();
  63          $course = $generator->create_course();
  64          $user = $generator->create_user();
  65          $quiz = $generator->create_module('quiz', ['course' => $course->id]);
  66          $generator->enrol_user($user->id, $course->id, 'student');
  67  
  68          /** @var \mod_quiz_generator $quizgenerator */
  69          $quizgenerator = $generator->get_plugin_generator('mod_quiz');
  70          $quizgenerator->create_override([
  71              'quiz' => $quiz->id,
  72              'userid' => $user->id,
  73              'timeclose' => strtotime('2022-10-20'),
  74          ]);
  75  
  76          // Check the corresponding calendar event now exists.
  77          $events = calendar_get_events(strtotime('2022-01-01'),
  78                  strtotime('2022-12-31'), $user->id, false, $course->id);
  79          $this->assertCount(1, $events);
  80          $event = reset($events);
  81          $this->assertEquals($user->id, $event->userid);
  82          $this->assertEquals(0, $event->groupid);
  83          $this->assertEquals(0, $event->courseid);
  84          $this->assertEquals('quiz', $event->modulename);
  85          $this->assertEquals($quiz->id, $event->instance);
  86          $this->assertEquals('close', $event->eventtype);
  87          $this->assertEquals(strtotime('2022-10-20'), $event->timestart);
  88      }
  89  
  90      public function test_generating_a_group_override() {
  91          $this->resetAfterTest(true);
  92  
  93          $generator = $this->getDataGenerator();
  94          $course = $generator->create_course();
  95          $quiz = $generator->create_module('quiz', ['course' => $course->id]);
  96          $group = $generator->create_group(['courseid' => $course->id]);
  97  
  98          /** @var \mod_quiz_generator $quizgenerator */
  99          $quizgenerator = $generator->get_plugin_generator('mod_quiz');
 100          $quizgenerator->create_override([
 101              'quiz' => $quiz->id,
 102              'groupid' => $group->id,
 103              'timeclose' => strtotime('2022-10-20'),
 104          ]);
 105  
 106          // Check the corresponding calendar event now exists.
 107          $events = calendar_get_events(strtotime('2022-01-01'),
 108                  strtotime('2022-12-31'), false, $group->id, $course->id);
 109          $this->assertCount(1, $events);
 110          $event = reset($events);
 111          $this->assertEquals(0, $event->userid);
 112          $this->assertEquals($group->id, $event->groupid);
 113          $this->assertEquals($course->id, $event->courseid);
 114          $this->assertEquals('quiz', $event->modulename);
 115          $this->assertEquals($quiz->id, $event->instance);
 116          $this->assertEquals('close', $event->eventtype);
 117          $this->assertEquals(strtotime('2022-10-20'), $event->timestart);
 118      }
 119  }