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 400] [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  /**
  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(array(
  49                  'name' => 'My category', 'sortorder' => 1));
  50          $this->assertSame('My category', $cat->name);
  51          $this->assertSame(1, $cat->sortorder);
  52      }
  53  
  54      public function test_idnumbers_in_categories_and_questions() {
  55          $this->resetAfterTest();
  56          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  57          list($category, $course, $qcat, $questions) = $generator->setup_course_and_questions();
  58          // Check default idnumbers in question_category and questions.
  59          $this->assertNull($qcat->idnumber);
  60          $this->assertNull($questions[0]->idnumber);
  61          $this->assertNull($questions[1]->idnumber);
  62          // Check created idnumbers.
  63          $qcat1 = $generator->create_question_category(array(
  64                  'name' => 'My category', 'sortorder' => 1, 'idnumber' => 'myqcat'));
  65          $this->assertSame('myqcat', $qcat1->idnumber);
  66          $quest1 = $generator->update_question($questions[0], null, ['idnumber' => 'myquest']);
  67          $this->assertSame('myquest', $quest1->idnumber);
  68          $quest3 = $generator->create_question('shortanswer', null,
  69                  ['name' => 'sa1', 'category' => $qcat1->id, 'idnumber' => 'myquest_3']);
  70          $this->assertSame('myquest_3', $quest3->idnumber);
  71          // Check idnumbers of questions moved. Note have to use load_question_data or we only get to see old cached data.
  72          question_move_questions_to_category([$quest1->id], $qcat1->id);
  73          $this->assertSame('myquest', \question_bank::load_question_data($quest1->id)->idnumber);
  74          // Can only change idnumber of quest2 once quest1 has been moved to another category.
  75          $quest2 = $generator->update_question($questions[1], null, ['idnumber' => 'myquest']);
  76          question_move_questions_to_category([$quest2->id], $qcat1->id);
  77          $this->assertSame('myquest_4', \question_bank::load_question_data($quest2->id)->idnumber);
  78          // Check can add an idnumber of 0.
  79          $quest4 = $generator->create_question('shortanswer', null, ['name' => 'sa1', 'category' => $qcat1->id, 'idnumber' => '0']);
  80          $this->assertSame('0', $quest4->idnumber);
  81      }
  82  }