MDL-47963 quiz editing: fix failing unit tests
authorTim Hunt <T.J.Hunt@open.ac.uk>
Fri, 31 Oct 2014 17:18:46 +0000 (17:18 +0000)
committerDan Poltawski <dan@moodle.com>
Fri, 31 Oct 2014 17:22:38 +0000 (17:22 +0000)
The unit tests were initialising the structure class in a different way
from real use. That was clearly silly, so I got rid of create_for,
leaving just the real create_for_quiz.

mod/quiz/classes/structure.php
mod/quiz/tests/repaginate_test.php
mod/quiz/tests/structure_test.php

index ad2bb5e..d1f477b 100644 (file)
@@ -70,25 +70,15 @@ class structure {
         return new self();
     }
 
-    /**
-     * Create an instance of this class representing the structure of a given quiz.
-     * @param \stdClass $quiz the quiz settings.
-     * @return structure
-     */
-    public static function create_for($quiz) {
-        $structure = self::create();
-        $structure->populate_structure($quiz);
-        return $structure;
-    }
-
     /**
      * Create an instance of this class representing the structure of a given quiz.
      * @param \quiz $quizobj the quiz.
      * @return structure
      */
     public static function create_for_quiz($quizobj) {
-        $structure = self::create_for($quizobj->get_quiz());
+        $structure = self::create();
         $structure->quizobj = $quizobj;
+        $structure->populate_structure($quizobj->get_quiz());
         return $structure;
     }
 
index 06cda7d..f82c20c 100644 (file)
@@ -90,6 +90,7 @@ class mod_quiz_repaginate_test extends advanced_testcase {
 
         $quiz = $quizgenerator->create_instance(array(
                 'course' => $SITE->id, 'questionsperpage' => 0, 'grade' => 100.0, 'sumgrades' => 2));
+        $cm = get_coursemodule_from_instance('quiz', $quiz->id, $SITE->id);
 
         // Create five questions.
         $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
@@ -109,7 +110,8 @@ class mod_quiz_repaginate_test extends advanced_testcase {
         quiz_add_quiz_question($match->id, $quiz);
 
         // Return the quiz object.
-        return \mod_quiz\structure::create_for($quiz);
+        $quizobj = new quiz($quiz, $cm, $SITE);
+        return \mod_quiz\structure::create_for_quiz($quizobj);
     }
 
     /**
index 0a1d84c..c9117c8 100644 (file)
@@ -282,6 +282,7 @@ class mod_quiz_structure_testcase extends advanced_testcase {
         // Setup a quiz with 1 standard and 1 random question.
         $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
         $quiz = $quizgenerator->create_instance(array('course' => $SITE->id, 'questionsperpage' => 3, 'grade' => 100.0));
+        $cm = get_coursemodule_from_instance('quiz', $quiz->id, $SITE->id);
 
         $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
         $cat = $questiongenerator->create_question_category();
@@ -293,7 +294,8 @@ class mod_quiz_structure_testcase extends advanced_testcase {
         // Get the random question.
         $randomq = $DB->get_record('question', array('qtype' => 'random'));
 
-        $structure = \mod_quiz\structure::create_for($quiz);
+        $quizobj = new quiz($quiz, $cm, $SITE);
+        $structure = \mod_quiz\structure::create_for_quiz($quizobj);
 
         // Check that the setup looks right.
         $this->assertEquals(2, $structure->get_question_count());
@@ -303,7 +305,7 @@ class mod_quiz_structure_testcase extends advanced_testcase {
         // Remove the standard question.
         $structure->remove_slot($quiz, 1);
 
-        $alteredstructure = \mod_quiz\structure::create_for($quiz);
+        $alteredstructure = \mod_quiz\structure::create_for_quiz($quizobj);
 
         // Check the new ordering, and that the slot number was updated.
         $this->assertEquals(1, $alteredstructure->get_question_count());
@@ -314,7 +316,7 @@ class mod_quiz_structure_testcase extends advanced_testcase {
 
         // Remove the random question.
         $structure->remove_slot($quiz, 1);
-        $alteredstructure = \mod_quiz\structure::create_for($quiz);
+        $alteredstructure = \mod_quiz\structure::create_for_quiz($quizobj);
 
         // Check that new ordering.
         $this->assertEquals(0, $alteredstructure->get_question_count());