]> git.mjollnir.org Git - moodle.git/commitdiff
quiz unittests: MDL-17295 start writing unit tests for some of mod/quiz/locallib...
authortjhunt <tjhunt>
Sun, 23 Nov 2008 12:46:51 +0000 (12:46 +0000)
committertjhunt <tjhunt>
Sun, 23 Nov 2008 12:46:51 +0000 (12:46 +0000)
mod/quiz/locallib.php
mod/quiz/simpletest/testlocallib.php [new file with mode: 0644]

index 4703777094f7113bb26d24bab872b327614eb966..fe95c005c6b9e0f2344e4e13efeb111b6c875528 100644 (file)
@@ -209,11 +209,11 @@ function quiz_delete_attempt($attempt, $quiz) {
 /**
  * Returns a comma separated list of question ids for the current page
  *
- * @return string         Comma separated list of question ids
- * @param string $layout  The string representing the quiz layout. Each page is represented as a
- *                        comma separated list of question ids and 0 indicating page breaks.
- *                        So 5,2,0,3,0 means questions 5 and 2 on page 1 and question 3 on page 2
- * @param integer $page   The number of the current page.
+ * @param string $layout the string representing the quiz layout. Each page is represented as a
+ *      comma separated list of question ids and 0 indicating page breaks.
+ *      So 5,2,0,3,0 means questions 5 and 2 on page 1 and question 3 on page 2
+ * @param integer $page the number of the current page.
+ * @return string comma separated list of question ids
  */
 function quiz_questions_on_page($layout, $page) {
     $pages = explode(',0', $layout);
@@ -223,51 +223,46 @@ function quiz_questions_on_page($layout, $page) {
 /**
  * Returns a comma separated list of question ids for the quiz
  *
- * @return string         Comma separated list of question ids, without page breaks
- * @param string $layout  The string representing the quiz layout. Each page is represented as a
- *                        comma separated list of question ids and 0 indicating page breaks.
- *                        So 5,2,0,3,0 means questions 5 and 2 on page 1 and question 3 on page 2
+ * @param string $layout The string representing the quiz layout. Each page is
+ *      represented as a comma separated list of question ids and 0 indicating
+ *      page breaks. So 5,2,0,3,0 means questions 5 and 2 on page 1 and question
+ *      3 on page 2
+ * @return string comma separated list of question ids, without page breaks.
  */
 function quiz_questions_in_quiz($layout) {
-    if($layout){
-        $layout=str_replace(',0', '', $layout);
-        //remove also the zero in the beginning, if any
-        if(isset($layout[0]) && $layout[0]=="0" &&
-                isset($layout[1]) && $layout[1]==","){
-            $layout=substr($layout,2);
-        }
-    }
+    $layout = preg_replace('/,(0+,)+/', ',', $layout); // Remove page breaks from the middle.
+    $layout = preg_replace('/^0+,/', '', $layout); // And from the start.
+    $layout = preg_replace('/(^|,)0+$/', '', $layout); // And from the end.
     return $layout;
 }
 
 /**
- * Returns the number of pages in the quiz layout
+ * Returns the number of pages in a quiz layout
  *
- * @return integer         Comma separated list of question ids
- * @param string $layout  The string representing the quiz layout.
+ * @param string $layout The string representing the quiz layout. Always ends in ,0
+ * @return integer The number of pages in the quiz.
  */
 function quiz_number_of_pages($layout) {
-    $count=0;
-    if($layout){
+    $count = 0;
+    if ($layout !== '') {
         //if the first page is empty, include it, too
-        if (strcmp($layout[0],"0")===0){
+        if (strcmp($layout[0], '0') === 0) {
             $count++;
         }
-        $count+=substr_count($layout, ',0');
+        $count += substr_count($layout, ',0');
     }
     return $count;
 }
 /**
  * Returns the number of questions in the quiz layout
  *
- * @return integer         Comma separated list of question ids
- * @param string $layout  The string representing the quiz layout.
+ * @param string $layout the string representing the quiz layout.
+ * @return integer The number of questions in the quiz.
  */
 function quiz_number_of_questions_in_quiz($layout) {
-    //TODO: clean layout before counting
-    $layout=quiz_questions_in_quiz($layout);
-    $count=substr_count($layout, ',');
-    if($count>0){
+    $layout = quiz_questions_in_quiz($layout);
+    $count = substr_count($layout, ',');
+    if ($layout !== '') {
         $count++;
     }
     return $count;
@@ -276,9 +271,9 @@ function quiz_number_of_questions_in_quiz($layout) {
 /**
  * Returns the first question number for the current quiz page
  *
- * @return integer  The number of the first question
  * @param string $quizlayout The string representing the layout for the whole quiz
  * @param string $pagelayout The string representing the layout for the current page
+ * @return integer the number of the first question
  */
 function quiz_first_questionnumber($quizlayout, $pagelayout) {
     // this works by finding all the questions from the quizlayout that
@@ -298,10 +293,10 @@ function quiz_first_questionnumber($quizlayout, $pagelayout) {
 /**
  * Re-paginates the quiz layout
  *
- * @return string         The new layout string
  * @param string $layout  The string representing the quiz layout.
  * @param integer $perpage The number of questions per page
  * @param boolean $shuffle Should the questions be reordered randomly?
+ * @return string the new layout string
  */
 function quiz_repaginate($layout, $perpage, $shuffle=false) {
     $layout = str_replace(',0', '', $layout); // remove existing page breaks
diff --git a/mod/quiz/simpletest/testlocallib.php b/mod/quiz/simpletest/testlocallib.php
new file mode 100644 (file)
index 0000000..833d5d6
--- /dev/null
@@ -0,0 +1,52 @@
+<?php
+/**
+ * Unit tests for (some of) mod/quiz/locallib.php.
+ *
+ * @author T.J.Hunt@open.ac.uk
+ * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
+ * @package quiz
+ */
+
+if (!defined('MOODLE_INTERNAL')) {
+    die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page.
+}
+
+require_once($CFG->dirroot . '/mod/quiz/locallib.php');
+
+class quiz_locallib_test extends MoodleUnitTestCase {
+    function test_quiz_questions_in_quiz() {
+        $this->assertEqual(quiz_questions_in_quiz(''), '');
+        $this->assertEqual(quiz_questions_in_quiz('0'), '');
+        $this->assertEqual(quiz_questions_in_quiz('0,0'), '');
+        $this->assertEqual(quiz_questions_in_quiz('0,0,0'), '');
+        $this->assertEqual(quiz_questions_in_quiz('1'), '1');
+        $this->assertEqual(quiz_questions_in_quiz('1,2'), '1,2');
+        $this->assertEqual(quiz_questions_in_quiz('1,0,2'), '1,2');
+        $this->assertEqual(quiz_questions_in_quiz('0,1,0,0,2,0'), '1,2');
+    }
+
+    function test_quiz_number_of_pages() {
+        $this->assertEqual(quiz_number_of_pages('0'), 1);
+        $this->assertEqual(quiz_number_of_pages('0,0'), 2);
+        $this->assertEqual(quiz_number_of_pages('0,0,0'), 3);
+        $this->assertEqual(quiz_number_of_pages('1,0'), 1);
+        $this->assertEqual(quiz_number_of_pages('1,2,0'), 1);
+        $this->assertEqual(quiz_number_of_pages('1,0,2,0'), 2);
+        $this->assertEqual(quiz_number_of_pages('1,2,3,0'), 1);
+        $this->assertEqual(quiz_number_of_pages('1,2,3,0'), 1);
+        $this->assertEqual(quiz_number_of_pages('0,1,0,0,2,0'), 4);
+    }
+
+    function test_quiz_number_of_questions_in_quiz() {
+        $this->assertEqual(quiz_number_of_questions_in_quiz('0'), 0);
+        $this->assertEqual(quiz_number_of_questions_in_quiz('0,0'), 0);
+        $this->assertEqual(quiz_number_of_questions_in_quiz('0,0,0'), 0);
+        $this->assertEqual(quiz_number_of_questions_in_quiz('1,0'), 1);
+        $this->assertEqual(quiz_number_of_questions_in_quiz('1,2,0'), 2);
+        $this->assertEqual(quiz_number_of_questions_in_quiz('1,0,2,0'), 2);
+        $this->assertEqual(quiz_number_of_questions_in_quiz('1,2,3,0'), 3);
+        $this->assertEqual(quiz_number_of_questions_in_quiz('1,2,3,0'), 3);
+        $this->assertEqual(quiz_number_of_questions_in_quiz('0,1,0,0,2,0'), 2);
+    }
+}
+?>
\ No newline at end of file