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