Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [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_get_questions_from_categories_with_usage_counts() {
  87          $this->resetAfterTest();
  88          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  89  
  90          $cat = $generator->create_question_category();
  91          $questiondata1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  92          $questiondata2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  93          $questiondata3 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  94  
  95          $quba = question_engine::make_questions_usage_by_activity('test', \context_system::instance());
  96          $quba->set_preferred_behaviour('deferredfeedback');
  97          $question1 = question_bank::load_question($questiondata1->id);
  98          $question3 = question_bank::load_question($questiondata3->id);
  99          $quba->add_question($question1);
 100          $quba->add_question($question1);
 101          $quba->add_question($question3);
 102          $quba->start_all_questions();
 103          question_engine::save_questions_usage_by_activity($quba);
 104  
 105          $this->assertEquals(array(
 106                  $questiondata2->id => 0,
 107                  $questiondata3->id => 1,
 108                  $questiondata1->id => 2,
 109          ), question_bank::get_finder()->get_questions_from_categories_with_usage_counts(
 110                  array($cat->id), new qubaid_list(array($quba->get_id()))));
 111      }
 112  
 113      public function test_load_many_for_cache() {
 114          $this->resetAfterTest();
 115          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 116          $cat = $generator->create_question_category();
 117          $q1 = $generator->create_question('shortanswer', null, ['category' => $cat->id]);
 118  
 119          $qs = question_finder::get_instance()->load_many_for_cache([$q1->id]);
 120          $this->assertArrayHasKey($q1->id, $qs);
 121      }
 122  
 123      public function test_load_many_for_cache_missing_id() {
 124          // Try to load a non-existent question.
 125          $this->expectException(\dml_missing_record_exception::class);
 126          question_finder::get_instance()->load_many_for_cache([-1]);
 127      }
 128  
 129      /**
 130       * Test get_questions_from_categories.
 131       *
 132       * @covers \question_finder::get_questions_from_categories
 133       *
 134       * @return void
 135       */
 136      public function test_get_questions_from_categories(): void {
 137          $this->resetAfterTest();
 138  
 139          /** @var core_question_generator $questiongenerator */
 140          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 141  
 142          // Create three questions in a question bank category, each with three versions.
 143          // The first question has all three versions in status ready.
 144          $cat = $questiongenerator->create_question_category();
 145          $q1v1 = $questiongenerator->create_question('truefalse', null, ['name' => 'Q1V1', 'category' => $cat->id]);
 146          $q1v2 = $questiongenerator->update_question($q1v1, null, ['name' => 'Q1V2']);
 147          $q1v3 = $questiongenerator->update_question($q1v2, null, ['name' => 'Q1V3']);
 148          // The second question has the first version in status draft, the second version in status ready,
 149          // and third version in status draft.
 150          $q2v1 = $questiongenerator->create_question('numerical', null, ['name' => 'Q2V2', 'category' => $cat->id,
 151              'status' => question_version_status::QUESTION_STATUS_DRAFT, ]);
 152          $q2v2 = $questiongenerator->update_question($q2v1, null, ['name' => 'Q2V2',
 153              'status' => question_version_status::QUESTION_STATUS_READY, ]);
 154          $q2v3 = $questiongenerator->update_question($q2v2, null,
 155              ['name' => 'Q2V3', 'status' => question_version_status::QUESTION_STATUS_DRAFT]);
 156          // The third question has all three version in status draft.
 157          $q3v1 = $questiongenerator->create_question('shortanswer', null, ['name' => 'Q3V1', 'category' => $cat->id,
 158              'status' => question_version_status::QUESTION_STATUS_DRAFT, ]);
 159          $q3v2 = $questiongenerator->update_question($q3v1, null, ['name' => 'Q3V2',
 160              'status' => question_version_status::QUESTION_STATUS_DRAFT, ]);
 161          $q3v3 = $questiongenerator->update_question($q3v2, null, ['name' => 'Q3V3',
 162              'status' => question_version_status::QUESTION_STATUS_DRAFT]);
 163  
 164          // Test the returned array of questions in that category is the desired one with version three of the first
 165          // question, version two of the second question, and the third question omitted completely since there are
 166          // only draft versions.
 167          $this->assertEquals([$q1v3->id => $q1v3->id, $q2v2->id => $q2v2->id],
 168              question_bank::get_finder()->get_questions_from_categories([$cat->id], ""));
 169      }
 170  }