MDL-62358 Question: Improve number function
authorHuong Nguyen <huongnv13@gmail.com>
Fri, 11 May 2018 04:05:05 +0000 (11:05 +0700)
committerHuong Nguyen <huongnv13@gmail.com>
Thu, 14 Jun 2018 12:43:55 +0000 (19:43 +0700)
Support all 'question numbers' that might be needed

question/engine/renderer.php
question/engine/tests/helpers.php
question/engine/tests/questionengine_test.php

index 8d0b4f6..17246d5 100644 (file)
@@ -156,15 +156,15 @@ class core_question_renderer extends plugin_renderer_base {
      * @return HTML fragment.
      */
     protected function number($number) {
+        if (trim($number) === '') {
+            return '';
+        }
         $numbertext = '';
-        if (is_numeric($number)) {
+        if (trim($number) === 'i') {
+            $numbertext = get_string('information', 'question');
+        } else {
             $numbertext = get_string('questionx', 'question',
                     html_writer::tag('span', $number, array('class' => 'qno')));
-        } else if ($number == 'i') {
-            $numbertext = get_string('information', 'question');
-        }
-        if (!$numbertext) {
-            return '';
         }
         return html_writer::tag('h3', $numbertext, array('class' => 'no'));
     }
index 0614f60..6877d45 100644 (file)
@@ -1243,3 +1243,22 @@ class question_test_recordset extends moodle_recordset {
         $this->records = null;
     }
 }
+
+/**
+ * Helper class for tests that help to test core_question_renderer.
+ *
+ * @copyright  2018 Huong Nguyen <huongnv13@gmail.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class testable_core_question_renderer extends core_question_renderer {
+
+    /**
+     * Test the private number function.
+     *
+     * @param null|string $number
+     * @return HTML
+     */
+    public function number($number) {
+        return parent::number($number);
+    }
+}
index 9e800b1..db1f72e 100644 (file)
@@ -131,4 +131,20 @@ class question_engine_test extends advanced_testcase {
     public function test_is_manual_grade_in_range_ungraded() {
         $this->assertTrue(question_engine::is_manual_grade_in_range(1, 2));
     }
+
+    public function test_render_question_number() {
+        global $PAGE;
+        $renderer = new testable_core_question_renderer($PAGE, 'core_question');
+
+        // Test with number is i character.
+        $this->assertEquals('<h3 class="no">Information</h3>', $renderer->number('i'));
+        // Test with number is empty string.
+        $this->assertEquals('', $renderer->number(''));
+        // Test with number is 0.
+        $this->assertEquals('<h3 class="no">Question <span class="qno">0</span></h3>', $renderer->number(0));
+        // Test with number is numeric.
+        $this->assertEquals('<h3 class="no">Question <span class="qno">1</span></h3>', $renderer->number(1));
+        // Test with number is string.
+        $this->assertEquals('<h3 class="no">Question <span class="qno">1 of 2</span></h3>', $renderer->number('1 of 2'));
+    }
 }