MDL-37157: Normalize short answers before comparison
authorDidier Raboud <didier.raboud@liip.ch>
Tue, 18 Dec 2012 13:32:15 +0000 (14:32 +0100)
committerDidier Raboud <didier.raboud@liip.ch>
Tue, 18 Dec 2012 17:44:15 +0000 (18:44 +0100)
This ensures that ambiguous Unicode representations can be compared sanely.
Also add some tests to ensure it keeps working.

Thanks to Tim Hunt for the syntax check.

question/type/shortanswer/question.php
question/type/shortanswer/tests/question_test.php

index 5e7626b..d46f4e0 100644 (file)
@@ -102,6 +102,11 @@ class qtype_shortanswer_question extends question_graded_by_strategy
             $regexp .= 'i';
         }
 
+        if (function_exists('normalizer_normalize')) {
+            $regexp = normalizer_normalize($regexp, Normalizer::FORM_C);
+            $string = normalizer_normalize($string, Normalizer::FORM_C);
+        }
+
         return preg_match($regexp, trim($string));
     }
 
index 839712c..d287ff4 100644 (file)
@@ -106,6 +106,18 @@ class qtype_shortanswer_question_test extends advanced_testcase {
         // See http://moodle.org/mod/forum/discuss.php?d=120557
         $this->assertTrue((bool)qtype_shortanswer_question::compare_string_with_wildcard(
                 'ITÁLIE', 'Itálie', true));
+
+        if (function_exists('normalizer_normalize')) {
+            // Test ambiguous unicode representations
+            $this->assertTrue((bool)qtype_shortanswer_question::compare_string_with_wildcard(
+                    'départ', 'DÉPART', true));
+            $this->assertFalse((bool)qtype_shortanswer_question::compare_string_with_wildcard(
+                    'départ', 'DÉPART', false));
+            $this->assertTrue((bool)qtype_shortanswer_question::compare_string_with_wildcard(
+                    'd'."\xC3\xA9".'part', 'd'."\x65\xCC\x81".'part', false));
+            $this->assertTrue((bool)qtype_shortanswer_question::compare_string_with_wildcard(
+                    'd'."\xC3\xA9".'part', 'D'."\x45\xCC\x81".'PART', true));
+        }
     }
 
     public function test_is_complete_response() {