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   * This file contains tests for the question_bank class.
  19   *
  20   * @package    moodlecore
  21   * @subpackage questionbank
  22   * @copyright  2011 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once (__DIR__ . '/../lib.php');
  31  
  32  
  33  /**
  34   *Unit tests for the {@link question_bank} class.
  35   *
  36   * @copyright  2011 The Open University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class question_bank_test extends advanced_testcase {
  40  
  41      public function test_sort_qtype_array() {
  42          $config = new stdClass();
  43          $config->multichoice_sortorder = '1';
  44          $config->calculated_sortorder = '2';
  45          $qtypes = array(
  46              'frog' => 'toad',
  47              'calculated' => 'newt',
  48              'multichoice' => 'eft',
  49          );
  50          $this->assertEquals(question_bank::sort_qtype_array($qtypes, $config), array(
  51              'multichoice' => 'eft',
  52              'calculated' => 'newt',
  53              'frog' => 'toad',
  54          ));
  55      }
  56  
  57      public function test_fraction_options() {
  58          $fractions = question_bank::fraction_options();
  59          $this->assertSame(get_string('none'), reset($fractions));
  60          $this->assertSame('0.0', key($fractions));
  61          $this->assertSame('5%', end($fractions));
  62          $this->assertSame('0.05', key($fractions));
  63          array_shift($fractions);
  64          array_pop($fractions);
  65          array_pop($fractions);
  66          $this->assertSame('100%', reset($fractions));
  67          $this->assertSame('1.0', key($fractions));
  68          $this->assertSame('11.11111%', end($fractions));
  69          $this->assertSame('0.1111111', key($fractions));
  70      }
  71  
  72      public function test_fraction_options_full() {
  73          $fractions = question_bank::fraction_options_full();
  74          $this->assertSame(get_string('none'), reset($fractions));
  75          $this->assertSame('0.0', key($fractions));
  76          $this->assertSame('-100%', end($fractions));
  77          $this->assertSame('-1.0', key($fractions));
  78          array_shift($fractions);
  79          array_pop($fractions);
  80          array_pop($fractions);
  81          $this->assertSame('100%', reset($fractions));
  82          $this->assertSame('1.0', key($fractions));
  83          $this->assertSame('-83.33333%', end($fractions));
  84          $this->assertSame('-0.8333333', key($fractions));
  85      }
  86  
  87      public function test_get_questions_from_categories_with_usage_counts() {
  88          $this->resetAfterTest();
  89          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  90  
  91          $cat = $generator->create_question_category();
  92          $questiondata1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  93          $questiondata2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  94          $questiondata3 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  95  
  96          $quba = question_engine::make_questions_usage_by_activity('test', context_system::instance());
  97          $quba->set_preferred_behaviour('deferredfeedback');
  98          $question1 = question_bank::load_question($questiondata1->id);
  99          $question3 = question_bank::load_question($questiondata3->id);
 100          $quba->add_question($question1);
 101          $quba->add_question($question1);
 102          $quba->add_question($question3);
 103          $quba->start_all_questions();
 104          question_engine::save_questions_usage_by_activity($quba);
 105  
 106          $this->assertEquals(array(
 107                  $questiondata2->id => 0,
 108                  $questiondata3->id => 1,
 109                  $questiondata1->id => 2,
 110          ), question_bank::get_finder()->get_questions_from_categories_with_usage_counts(
 111                  array($cat->id), new qubaid_list(array($quba->get_id()))));
 112      }
 113  
 114      public function test_load_many_for_cache() {
 115          $this->resetAfterTest();
 116          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 117          $cat = $generator->create_question_category();
 118          $q1 = $generator->create_question('shortanswer', null, ['category' => $cat->id]);
 119  
 120          $qs = question_finder::get_instance()->load_many_for_cache([$q1->id]);
 121          $this->assertArrayHasKey($q1->id, $qs);
 122      }
 123  
 124      public function test_load_many_for_cache_missing_id() {
 125          // Try to load a non-existent question.
 126          $this->expectException('dml_missing_record_exception');
 127          question_finder::get_instance()->load_many_for_cache([-1]);
 128      }
 129  }