Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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...
commit f4537d56761990cb66a6accb0df8534c495febb1 1 parent 850aa18
Didier Raboud OdyX authored danpoltawski committed
5 question/type/shortanswer/question.php
@@ -102,6 +102,11 @@ public static function compare_string_with_wildcard($string, $pattern, $ignoreca
102 102 $regexp .= 'i';
103 103 }
104 104
  105 + if (function_exists('normalizer_normalize')) {
  106 + $regexp = normalizer_normalize($regexp, Normalizer::FORM_C);
  107 + $string = normalizer_normalize($string, Normalizer::FORM_C);
  108 + }
  109 +
105 110 return preg_match($regexp, trim($string));
106 111 }
107 112
12 question/type/shortanswer/tests/question_test.php
@@ -106,6 +106,18 @@ public function test_compare_string_with_wildcard() {
106 106 // See http://moodle.org/mod/forum/discuss.php?d=120557
107 107 $this->assertTrue((bool)qtype_shortanswer_question::compare_string_with_wildcard(
108 108 'ITÁLIE', 'Itálie', true));
  109 +
  110 + if (function_exists('normalizer_normalize')) {
  111 + // Test ambiguous unicode representations
  112 + $this->assertTrue((bool)qtype_shortanswer_question::compare_string_with_wildcard(
  113 + 'départ', 'DÉPART', true));
  114 + $this->assertFalse((bool)qtype_shortanswer_question::compare_string_with_wildcard(
  115 + 'départ', 'DÉPART', false));
  116 + $this->assertTrue((bool)qtype_shortanswer_question::compare_string_with_wildcard(
  117 + 'd'."\xC3\xA9".'part', 'd'."\x65\xCC\x81".'part', false));
  118 + $this->assertTrue((bool)qtype_shortanswer_question::compare_string_with_wildcard(
  119 + 'd'."\xC3\xA9".'part', 'D'."\x45\xCC\x81".'PART', true));
  120 + }
109 121 }
110 122
111 123 public function test_is_complete_response() {

0 comments on commit f4537d5

Please sign in to comment.
Something went wrong with that request. Please try again.