diff --git a/question/type/random/questiontype.php b/question/type/random/questiontype.php index 2a4955f16e9..7834cbccbd0 100644 --- a/question/type/random/questiontype.php +++ b/question/type/random/questiontype.php @@ -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. * diff --git a/question/type/random/tests/questiontype_test.php b/question/type/random/tests/questiontype_test.php index fae010dfff9..8d846147cf6 100644 --- a/question/type/random/tests/questiontype_test.php +++ b/question/type/random/tests/questiontype_test.php @@ -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));