Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400]

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