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]

   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   * Data generators tests
  19   *
  20   * @package    moodlecore
  21   * @subpackage questionengine
  22   * @copyright  2013 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  
  29  /**
  30   * Test data generator
  31   *
  32   * @copyright  2013 The Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class core_question_generator_testcase extends advanced_testcase {
  36      public function test_create() {
  37          global $DB;
  38  
  39          $this->resetAfterTest();
  40          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  41  
  42          $count = $DB->count_records('question_categories');
  43  
  44          $cat = $generator->create_question_category();
  45          $count += $count ? 1 : 2; // Calling $generator->create_question_category() for the first time
  46                                    // creates a Top category as well.
  47          $this->assertEquals($count, $DB->count_records('question_categories'));
  48  
  49          $cat = $generator->create_question_category(array(
  50                  'name' => 'My category', 'sortorder' => 1));
  51          $this->assertSame('My category', $cat->name);
  52          $this->assertSame(1, $cat->sortorder);
  53      }
  54  
  55      public function test_idnumbers_in_categories_and_questions() {
  56          $this->resetAfterTest();
  57          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  58          list($category, $course, $qcat, $questions) = $generator->setup_course_and_questions();
  59          // Check default idnumbers in question_category and questions.
  60          $this->assertNull($qcat->idnumber);
  61          $this->assertNull($questions[0]->idnumber);
  62          $this->assertNull($questions[1]->idnumber);
  63          // Check created idnumbers.
  64          $qcat1 = $generator->create_question_category(array(
  65                  'name' => 'My category', 'sortorder' => 1, 'idnumber' => 'myqcat'));
  66          $this->assertSame('myqcat', $qcat1->idnumber);
  67          $quest1 = $generator->update_question($questions[0], null, ['idnumber' => 'myquest']);
  68          $this->assertSame('myquest', $quest1->idnumber);
  69          $quest3 = $generator->create_question('shortanswer', null,
  70                  ['name' => 'sa1', 'category' => $qcat1->id, 'idnumber' => 'myquest_3']);
  71          $this->assertSame('myquest_3', $quest3->idnumber);
  72          // Check idnumbers of questions moved. Note have to use load_question_data or we only get to see old cached data.
  73          question_move_questions_to_category([$quest1->id], $qcat1->id);
  74          $this->assertSame('myquest', question_bank::load_question_data($quest1->id)->idnumber);
  75          // Can only change idnumber of quest2 once quest1 has been moved to another category.
  76          $quest2 = $generator->update_question($questions[1], null, ['idnumber' => 'myquest']);
  77          question_move_questions_to_category([$quest2->id], $qcat1->id);
  78          $this->assertSame('myquest_4', question_bank::load_question_data($quest2->id)->idnumber);
  79          // Check can add an idnumber of 0.
  80          $quest4 = $generator->create_question('shortanswer', null, ['name' => 'sa1', 'category' => $qcat1->id, 'idnumber' => '0']);
  81          $this->assertSame('0', $quest4->idnumber);
  82      }
  83  }