MDL-40778 clear cache for random questions between tests
authorJamie Pratt <me@jamiep.org>
Sat, 20 Jul 2013 06:21:35 +0000 (13:21 +0700)
committerJamie Pratt <me@jamiep.org>
Mon, 22 Jul 2013 04:12:21 +0000 (11:12 +0700)
question/type/random/questiontype.php
question/type/random/tests/questiontype_test.php

index 2a4955f..7834cbc 100644 (file)
@@ -176,6 +176,15 @@ class qtype_random extends question_type {
         return $DB->update_record('question', $updateobject);
     }
 
+    /**
+     * During unit tests we need to be able to reset all caches so that each new test starts in a known state.
+     * Intended for use only for testing. This is a stop gap until we start using the MUC caching api here.
+     * You need to call this before every test that loads one or more random questions.
+     */
+    public function clear_caches_before_testing() {
+        $this->availablequestionsbycategory = array();
+    }
+
     /**
      * Get all the usable questions from a particular question category.
      *
index fae010d..8d84614 100644 (file)
@@ -65,6 +65,8 @@ class qtype_random_test extends advanced_testcase {
 
     public function test_question_creation() {
         $this->resetAfterTest();
+        question_bank::get_qtype('random')->clear_caches_before_testing();
+
         $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
         $cat = $generator->create_question_category();
         $question1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
@@ -72,6 +74,12 @@ class qtype_random_test extends advanced_testcase {
 
         $randomquestion = $generator->create_question('random', null, array('category' => $cat->id));
 
+        $expectedids = array($question1->id, $question2->id);
+        $actualids = question_bank::get_qtype('random')->get_available_questions_from_category($cat->id, 0);
+        sort($expectedids);
+        sort($actualids);
+        $this->assertEquals($expectedids, $actualids);
+
         $q = question_bank::load_question($randomquestion->id);
 
         $this->assertContains($q->id, array($question1->id, $question2->id));