[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/tests/ -> random_question_loader_test.php (source)

   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   * Tests for the {@link core_question\bank\random_question_loader} class.
  19   *
  20   * @package   core_question
  21   * @copyright 2015 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  
  28  /**
  29   * Tests for the {@link core_question\bank\random_question_loader} class.
  30   *
  31   * @copyright  2015 The Open University
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class random_question_loader_testcase extends advanced_testcase {
  35  
  36      public function test_empty_category_gives_null() {
  37          $this->resetAfterTest();
  38          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  39  
  40          $cat = $generator->create_question_category();
  41          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  42  
  43          $this->assertNull($loader->get_next_question_id($cat->id, 0));
  44          $this->assertNull($loader->get_next_question_id($cat->id, 1));
  45      }
  46  
  47      public function test_unknown_category_behaves_like_empty() {
  48          // It is up the caller to make sure the category id is valid.
  49          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  50          $this->assertNull($loader->get_next_question_id(-1, 1));
  51      }
  52  
  53      public function test_descriptions_not_returned() {
  54          $this->resetAfterTest();
  55          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  56  
  57          $cat = $generator->create_question_category();
  58          $info = $generator->create_question('description', null, array('category' => $cat->id));
  59          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  60  
  61          $this->assertNull($loader->get_next_question_id($cat->id, 0));
  62      }
  63  
  64      public function test_hidden_questions_not_returned() {
  65          global $DB;
  66          $this->resetAfterTest();
  67          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  68  
  69          $cat = $generator->create_question_category();
  70          $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
  71          $DB->set_field('question', 'hidden', 1, array('id' => $question1->id));
  72          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  73  
  74          $this->assertNull($loader->get_next_question_id($cat->id, 0));
  75      }
  76  
  77      public function test_cloze_subquestions_not_returned() {
  78          $this->resetAfterTest();
  79          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  80  
  81          $cat = $generator->create_question_category();
  82          $question1 = $generator->create_question('multianswer', null, array('category' => $cat->id));
  83          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  84  
  85          $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 0));
  86          $this->assertNull($loader->get_next_question_id($cat->id, 0));
  87      }
  88  
  89      public function test_random_questions_not_returned() {
  90          $this->resetAfterTest();
  91          $this->setAdminUser();
  92          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  93  
  94          $cat = $generator->create_question_category();
  95          $course = $this->getDataGenerator()->create_course();
  96          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course));
  97          quiz_add_random_questions($quiz, 1, $cat->id, 1, false);
  98          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
  99  
 100          $this->assertNull($loader->get_next_question_id($cat->id, 0));
 101      }
 102  
 103      public function test_one_question_category_returns_that_q_then_null() {
 104          $this->resetAfterTest();
 105          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 106  
 107          $cat = $generator->create_question_category();
 108          $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
 109          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
 110  
 111          $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 1));
 112          $this->assertNull($loader->get_next_question_id($cat->id, 0));
 113      }
 114  
 115      public function test_two_question_category_returns_both_then_null() {
 116          $this->resetAfterTest();
 117          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 118  
 119          $cat = $generator->create_question_category();
 120          $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
 121          $question2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
 122          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
 123  
 124          $questionids = array();
 125          $questionids[] = $loader->get_next_question_id($cat->id, 0);
 126          $questionids[] = $loader->get_next_question_id($cat->id, 0);
 127          sort($questionids);
 128          $this->assertEquals(array($question1->id, $question2->id), $questionids);
 129  
 130          $this->assertNull($loader->get_next_question_id($cat->id, 1));
 131      }
 132  
 133      public function test_nested_categories() {
 134          $this->resetAfterTest();
 135          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 136  
 137          $cat1 = $generator->create_question_category();
 138          $cat2 = $generator->create_question_category(array('parent' => $cat1->id));
 139          $question1 = $generator->create_question('shortanswer', null, array('category' => $cat1->id));
 140          $question2 = $generator->create_question('shortanswer', null, array('category' => $cat2->id));
 141          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
 142  
 143          $this->assertEquals($question2->id, $loader->get_next_question_id($cat2->id, 1));
 144          $this->assertEquals($question1->id, $loader->get_next_question_id($cat1->id, 1));
 145  
 146          $this->assertNull($loader->get_next_question_id($cat1->id, 0));
 147      }
 148  
 149      public function test_used_question_not_returned_until_later() {
 150          $this->resetAfterTest();
 151          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 152  
 153          $cat = $generator->create_question_category();
 154          $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
 155          $question2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
 156          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()),
 157                  array($question2->id => 2));
 158  
 159          $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 0));
 160          $this->assertNull($loader->get_next_question_id($cat->id, 0));
 161      }
 162  
 163      public function test_previously_used_question_not_returned_until_later() {
 164          $this->resetAfterTest();
 165          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 166  
 167          $cat = $generator->create_question_category();
 168          $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
 169          $question2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
 170          $quba = question_engine::make_questions_usage_by_activity('test', context_system::instance());
 171          $quba->set_preferred_behaviour('deferredfeedback');
 172          $question = question_bank::load_question($question2->id);
 173          $quba->add_question($question);
 174          $quba->add_question($question);
 175          $quba->start_all_questions();
 176          question_engine::save_questions_usage_by_activity($quba);
 177  
 178          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array($quba->get_id())));
 179  
 180          $this->assertEquals($question1->id, $loader->get_next_question_id($cat->id, 0));
 181          $this->assertEquals($question2->id, $loader->get_next_question_id($cat->id, 0));
 182          $this->assertNull($loader->get_next_question_id($cat->id, 0));
 183      }
 184  
 185      public function test_empty_category_does_not_have_question_available() {
 186          $this->resetAfterTest();
 187          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 188  
 189          $cat = $generator->create_question_category();
 190          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
 191  
 192          $this->assertFalse($loader->is_question_available($cat->id, 0, 1));
 193          $this->assertFalse($loader->is_question_available($cat->id, 1, 1));
 194      }
 195  
 196      public function test_descriptions_not_available() {
 197          $this->resetAfterTest();
 198          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 199  
 200          $cat = $generator->create_question_category();
 201          $info = $generator->create_question('description', null, array('category' => $cat->id));
 202          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
 203  
 204          $this->assertFalse($loader->is_question_available($cat->id, 0, $info->id));
 205          $this->assertFalse($loader->is_question_available($cat->id, 1, $info->id));
 206      }
 207  
 208      public function test_existing_question_is_available_but_then_marked_used() {
 209          $this->resetAfterTest();
 210          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 211  
 212          $cat = $generator->create_question_category();
 213          $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
 214          $loader = new \core_question\bank\random_question_loader(new qubaid_list(array()));
 215  
 216          $this->assertTrue($loader->is_question_available($cat->id, 0, $question1->id));
 217          $this->assertFalse($loader->is_question_available($cat->id, 0, $question1->id));
 218  
 219          $this->assertFalse($loader->is_question_available($cat->id, 0, -1));
 220      }
 221  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1