MDL-66980 question bank: fix load_many_for_cache missing record check
authorTim Hunt <T.J.Hunt@open.ac.uk>
Mon, 21 Oct 2019 10:18:12 +0000 (11:18 +0100)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Mon, 21 Oct 2019 10:18:12 +0000 (11:18 +0100)
question/engine/bank.php
question/engine/tests/questionbank_test.php

index 61b01a7..c239388 100644 (file)
@@ -610,7 +610,7 @@ class question_finder implements cache_data_source {
                                             WHERE q.id ' . $idcondition, $params);
 
         foreach ($questionids as $id) {
-            if (!array_key_exists($id, $questionids)) {
+            if (!array_key_exists($id, $questiondata)) {
                 throw new dml_missing_record_exception('question', '', array('id' => $id));
             }
             get_question_options($questiondata[$id]);
index 8735079..c0b4cb2 100644 (file)
@@ -110,4 +110,20 @@ class question_bank_test extends advanced_testcase {
         ), question_bank::get_finder()->get_questions_from_categories_with_usage_counts(
                 array($cat->id), new qubaid_list(array($quba->get_id()))));
     }
+
+    public function test_load_many_for_cache() {
+        $this->resetAfterTest();
+        $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
+        $cat = $generator->create_question_category();
+        $q1 = $generator->create_question('shortanswer', null, ['category' => $cat->id]);
+
+        $qs = question_finder::get_instance()->load_many_for_cache([$q1->id]);
+        $this->assertArrayHasKey($q1->id, $qs);
+    }
+
+    public function test_load_many_for_cache_missing_id() {
+        // Try to load a non-existent question.
+        $this->expectException('dml_missing_record_exception');
+        question_finder::get_instance()->load_many_for_cache([-1]);
+    }
 }