Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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_lesson;
  18  
  19  /**
  20   * Genarator tests class for mod_lesson.
  21   *
  22   * @package    mod_lesson
  23   * @category   test
  24   * @copyright  2013 Marina Glancy
  25   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class generator_test extends \advanced_testcase {
  28  
  29      public function test_create_instance() {
  30          global $DB;
  31          $this->resetAfterTest();
  32          $this->setAdminUser();
  33  
  34          $course = $this->getDataGenerator()->create_course();
  35  
  36          $this->assertFalse($DB->record_exists('lesson', array('course' => $course->id)));
  37          $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
  38          $records = $DB->get_records('lesson', array('course' => $course->id), 'id');
  39          $this->assertEquals(1, count($records));
  40          $this->assertTrue(array_key_exists($lesson->id, $records));
  41  
  42          $params = array('course' => $course->id, 'name' => 'Another lesson');
  43          $lesson = $this->getDataGenerator()->create_module('lesson', $params);
  44          $records = $DB->get_records('lesson', array('course' => $course->id), 'id');
  45          $this->assertEquals(2, count($records));
  46          $this->assertEquals('Another lesson', $records[$lesson->id]->name);
  47      }
  48  
  49      public function test_create_content() {
  50          global $DB;
  51          $this->resetAfterTest();
  52          $this->setAdminUser();
  53  
  54          $course = $this->getDataGenerator()->create_course();
  55          $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
  56          $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
  57  
  58          $page1 = $lessongenerator->create_content($lesson);
  59          $page2 = $lessongenerator->create_content($lesson, array('title' => 'Custom title'));
  60          $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
  61          $this->assertEquals(2, count($records));
  62          $this->assertEquals($page1->id, $records[$page1->id]->id);
  63          $this->assertEquals($page2->id, $records[$page2->id]->id);
  64          $this->assertEquals('Custom title', $records[$page2->id]->title);
  65      }
  66  
  67      /**
  68       * This tests the true/false question generator.
  69       */
  70      public function test_create_question_truefalse() {
  71          global $DB;
  72          $this->resetAfterTest();
  73          $this->setAdminUser();
  74  
  75          $course = $this->getDataGenerator()->create_course();
  76          $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
  77          $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
  78  
  79          $page1 = $lessongenerator->create_question_truefalse($lesson);
  80          $page2 = $lessongenerator->create_question_truefalse($lesson, array('title' => 'Custom title'));
  81          $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
  82          $p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
  83          $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
  84          $this->assertCount(2, $records);
  85          $this->assertCount(2, $p1answers); // True/false only supports 2 answer records.
  86          $this->assertCount(2, $p2answers);
  87          $this->assertEquals($page1->id, $records[$page1->id]->id);
  88          $this->assertEquals($page2->id, $records[$page2->id]->id);
  89          $this->assertEquals($page2->title, $records[$page2->id]->title);
  90      }
  91  
  92      /**
  93       * This tests the multichoice question generator.
  94       */
  95      public function test_create_question_multichoice() {
  96          global $DB;
  97          $this->resetAfterTest();
  98          $this->setAdminUser();
  99  
 100          $course = $this->getDataGenerator()->create_course();
 101          $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
 102          $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
 103  
 104          $page1 = $lessongenerator->create_question_multichoice($lesson);
 105          $page2 = $lessongenerator->create_question_multichoice($lesson, array('title' => 'Custom title'));
 106          $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
 107          $p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
 108          $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
 109          $this->assertCount(2, $records);
 110          $this->assertCount(2, $p1answers); // Multichoice requires at least 2 records.
 111          $this->assertCount(2, $p2answers);
 112          $this->assertEquals($page1->id, $records[$page1->id]->id);
 113          $this->assertEquals($page2->id, $records[$page2->id]->id);
 114          $this->assertEquals($page2->title, $records[$page2->id]->title);
 115      }
 116  
 117      /**
 118       * This tests the essay question generator.
 119       */
 120      public function test_create_question_essay() {
 121          global $DB;
 122          $this->resetAfterTest();
 123          $this->setAdminUser();
 124  
 125          $course = $this->getDataGenerator()->create_course();
 126          $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
 127          $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
 128  
 129          $page1 = $lessongenerator->create_question_essay($lesson);
 130          $page2 = $lessongenerator->create_question_essay($lesson, array('title' => 'Custom title'));
 131          $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
 132          $p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
 133          $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
 134          $this->assertCount(2, $records);
 135          $this->assertCount(1, $p1answers); // Essay creates a single (empty) answer record.
 136          $this->assertCount(1, $p2answers);
 137          $this->assertEquals($page1->id, $records[$page1->id]->id);
 138          $this->assertEquals($page2->id, $records[$page2->id]->id);
 139          $this->assertEquals($page2->title, $records[$page2->id]->title);
 140      }
 141  
 142      /**
 143       * This tests the matching question generator.
 144       */
 145      public function test_create_question_matching() {
 146          global $DB;
 147          $this->resetAfterTest();
 148          $this->setAdminUser();
 149  
 150          $course = $this->getDataGenerator()->create_course();
 151          $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
 152          $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
 153  
 154          $page1 = $lessongenerator->create_question_matching($lesson);
 155          $page2 = $lessongenerator->create_question_matching($lesson, array('title' => 'Custom title'));
 156          $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
 157          $p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
 158          $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
 159          $this->assertCount(2, $records);
 160          $this->assertCount(4, $p1answers); // Matching creates two extra records plus 1 for each answer value.
 161          $this->assertCount(4, $p2answers);
 162          $this->assertEquals($page1->id, $records[$page1->id]->id);
 163          $this->assertEquals($page2->id, $records[$page2->id]->id);
 164          $this->assertEquals($page2->title, $records[$page2->id]->title);
 165      }
 166  
 167      /**
 168       * This tests the numeric question generator.
 169       */
 170      public function test_create_question_numeric() {
 171          global $DB;
 172          $this->resetAfterTest();
 173          $this->setAdminUser();
 174  
 175          $course = $this->getDataGenerator()->create_course();
 176          $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
 177          $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
 178  
 179          $page1 = $lessongenerator->create_question_numeric($lesson);
 180          $page2 = $lessongenerator->create_question_numeric($lesson, array('title' => 'Custom title'));
 181          $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
 182          $p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
 183          $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
 184          $this->assertCount(2, $records);
 185          $this->assertCount(1, $p1answers); // Numeric only requires 1 answer.
 186          $this->assertCount(1, $p2answers);
 187          $this->assertEquals($page1->id, $records[$page1->id]->id);
 188          $this->assertEquals($page2->id, $records[$page2->id]->id);
 189          $this->assertEquals($page2->title, $records[$page2->id]->title);
 190      }
 191  
 192      /**
 193       * This tests the shortanswer question generator.
 194       */
 195      public function test_create_question_shortanswer() {
 196          global $DB;
 197          $this->resetAfterTest();
 198          $this->setAdminUser();
 199  
 200          $course = $this->getDataGenerator()->create_course();
 201          $lesson = $this->getDataGenerator()->create_module('lesson', array('course' => $course));
 202          $lessongenerator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
 203  
 204          $page1 = $lessongenerator->create_question_shortanswer($lesson);
 205          $page2 = $lessongenerator->create_question_shortanswer($lesson, array('title' => 'Custom title'));
 206          $records = $DB->get_records('lesson_pages', array('lessonid' => $lesson->id), 'id');
 207          $p1answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page1->id), 'id');
 208          $p2answers = $DB->get_records('lesson_answers', array('lessonid' => $lesson->id, 'pageid' => $page2->id), 'id');
 209          $this->assertCount(2, $records);
 210          $this->assertCount(1, $p1answers); // Shortanswer only requires 1 answer.
 211          $this->assertCount(1, $p2answers);
 212          $this->assertEquals($page1->id, $records[$page1->id]->id);
 213          $this->assertEquals($page2->id, $records[$page2->id]->id);
 214          $this->assertEquals($page2->title, $records[$page2->id]->title);
 215      }
 216  }