Browse files

MDL-37157: Normalize short answers before comparison

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.
  • Loading branch information...
1 parent 2075186 commit 5a449045c220bc4581372456d04ca47b01b4489e @OdyX OdyX committed Dec 18, 2012
Showing with 17 additions and 0 deletions.
  1. +5 −0 question/type/shortanswer/question.php
  2. +12 −0 question/type/shortanswer/tests/question_test.php
View
5 question/type/shortanswer/question.php
@@ -102,6 +102,11 @@ public static function compare_string_with_wildcard($string, $pattern, $ignoreca
$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));
}
View
12 question/type/shortanswer/tests/question_test.php
@@ -106,6 +106,18 @@ public function test_compare_string_with_wildcard() {
// 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() {

0 comments on commit 5a44904

Please sign in to comment.