Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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 core_question;
  18  
  19  use core_question\local\bank\question_version_status;
  20  use qubaid_list;
  21  use question_bank;
  22  use question_engine;
  23  use question_finder;
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once (__DIR__ . '/../lib.php');
  29  
  30  /**
  31   * Unit tests for the {@see question_bank} class.
  32   *
  33   * @package    core_question
  34   * @category   test
  35   * @copyright  2011 The Open University
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class questionbank_test extends \advanced_testcase {
  39  
  40      public function test_sort_qtype_array() {
  41          $config = new \stdClass();
  42          $config->multichoice_sortorder = '1';
  43          $config->calculated_sortorder = '2';
  44          $qtypes = array(
  45              'frog' => 'toad',
  46              'calculated' => 'newt',
  47              'multichoice' => 'eft',
  48          );
  49          $this->assertEquals(question_bank::sort_qtype_array($qtypes, $config), array(
  50              'multichoice' => 'eft',
  51              'calculated' => 'newt',
  52              'frog' => 'toad',
  53          ));
  54      }
  55  
  56      public function test_fraction_options() {
  57          $fractions = question_bank::fraction_options();
  58          $this->assertSame(get_string('none'), reset($fractions));
  59          $this->assertSame('0.0', key($fractions));
  60          $this->assertSame('5%', end($fractions));
  61          $this->assertSame('0.05', key($fractions));
  62          array_shift($fractions);
  63          array_pop($fractions);
  64          array_pop($fractions);
  65          $this->assertSame('100%', reset($fractions));
  66          $this->assertSame('1.0', key($fractions));
  67          $this->assertSame('11.11111%', end($fractions));
  68          $this->assertSame('0.1111111', key($fractions));
  69      }
  70  
  71      public function test_fraction_options_full() {
  72          $fractions = question_bank::fraction_options_full();
  73          $this->assertSame(get_string('none'), reset($fractions));
  74          $this->assertSame('0.0', key($fractions));
  75          $this->assertSame('-100%', end($fractions));
  76          $this->assertSame('-1.0', key($fractions));
  77          array_shift($fractions);
  78          array_pop($fractions);
  79          array_pop($fractions);
  80          $this->assertSame('100%', reset($fractions));
  81          $this->assertSame('1.0', key($fractions));
  82          $this->assertSame('-83.33333%', end($fractions));
  83          $this->assertSame('-0.8333333', key($fractions));
  84      }
  85  
  86      public function test_load_many_for_cache() {
  87          $this->resetAfterTest();
  88          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  89          $cat = $generator->create_question_category();
  90          $q1 = $generator->create_question('shortanswer', null, ['category' => $cat->id]);
  91  
  92          $qs = question_finder::get_instance()->load_many_for_cache([$q1->id]);
  93          $this->assertArrayHasKey($q1->id, $qs);
  94      }
  95  
  96      public function test_load_many_for_cache_missing_id() {
  97          // Try to load a non-existent question.
  98          $this->expectException(\dml_missing_record_exception::class);
  99          question_finder::get_instance()->load_many_for_cache([-1]);
 100      }
 101  
 102      /**
 103       * Test get_questions_from_categories.
 104       *
 105       * @covers \question_finder::get_questions_from_categories
 106       *
 107       * @return void
 108       */
 109      public function test_get_questions_from_categories(): void {
 110          $this->resetAfterTest();
 111  
 112          /** @var core_question_generator $questiongenerator */
 113          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 114  
 115          // Create three questions in a question bank category, each with three versions.
 116          // The first question has all three versions in status ready.
 117          $cat = $questiongenerator->create_question_category();
 118          $q1v1 = $questiongenerator->create_question('truefalse', null, ['name' => 'Q1V1', 'category' => $cat->id]);
 119          $q1v2 = $questiongenerator->update_question($q1v1, null, ['name' => 'Q1V2']);
 120          $q1v3 = $questiongenerator->update_question($q1v2, null, ['name' => 'Q1V3']);
 121          // The second question has the first version in status draft, the second version in status ready,
 122          // and third version in status draft.
 123          $q2v1 = $questiongenerator->create_question('numerical', null, ['name' => 'Q2V2', 'category' => $cat->id,
 124              'status' => question_version_status::QUESTION_STATUS_DRAFT, ]);
 125          $q2v2 = $questiongenerator->update_question($q2v1, null, ['name' => 'Q2V2',
 126              'status' => question_version_status::QUESTION_STATUS_READY, ]);
 127          $q2v3 = $questiongenerator->update_question($q2v2, null,
 128              ['name' => 'Q2V3', 'status' => question_version_status::QUESTION_STATUS_DRAFT]);
 129          // The third question has all three version in status draft.
 130          $q3v1 = $questiongenerator->create_question('shortanswer', null, ['name' => 'Q3V1', 'category' => $cat->id,
 131              'status' => question_version_status::QUESTION_STATUS_DRAFT, ]);
 132          $q3v2 = $questiongenerator->update_question($q3v1, null, ['name' => 'Q3V2',
 133              'status' => question_version_status::QUESTION_STATUS_DRAFT, ]);
 134          $q3v3 = $questiongenerator->update_question($q3v2, null, ['name' => 'Q3V3',
 135              'status' => question_version_status::QUESTION_STATUS_DRAFT]);
 136  
 137          // Test the returned array of questions in that category is the desired one with version three of the first
 138          // question, version two of the second question, and the third question omitted completely since there are
 139          // only draft versions.
 140          $this->assertEquals([$q1v3->id => $q1v3->id, $q2v2->id => $q2v2->id],
 141              question_bank::get_finder()->get_questions_from_categories([$cat->id], ""));
 142      }
 143  }