From: tjhunt Date: Wed, 10 Oct 2007 15:26:45 +0000 (+0000) Subject: MDL-11703 - escaped *'s in shortanswer answers were not matching properly. Merged... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=3270033ab8d8ae14dce63811bd57f2eb16156317;p=moodle.git MDL-11703 - escaped *'s in shortanswer answers were not matching properly. Merged from MOODLE_18_STABLE. --- diff --git a/question/type/shortanswer/questiontype.php b/question/type/shortanswer/questiontype.php index 5637a30271..ffa1b40a51 100644 --- a/question/type/shortanswer/questiontype.php +++ b/question/type/shortanswer/questiontype.php @@ -212,9 +212,12 @@ class question_shortanswer_qtype extends default_questiontype { // Break the string on non-escaped asterisks. $bits = preg_split('/(?dirroot . '/question/type/questiontype.php'); + +class question_shortanswer_qtype_test extends UnitTestCase { + var $qtype; + + function setUp() { + $this->qtype = new question_shortanswer_qtype(); + } + + function tearDown() { + $this->qtype = null; + } + + function test_name() { + $this->assertEqual($this->qtype->name(), 'shortanswer'); + } + + function test_compare_string_with_wildcard() { + // Test case sensitive literal matches. + $this->assertTrue($this->qtype->compare_string_with_wildcard('Frog', 'Frog', false)); + $this->assertFalse($this->qtype->compare_string_with_wildcard('Frog', 'frog', false)); + $this->assertTrue($this->qtype->compare_string_with_wildcard(' Frog ', 'Frog', false)); + $this->assertFalse($this->qtype->compare_string_with_wildcard('Frogs', 'Frog', false)); + + // Test case insensitive literal matches. + $this->assertTrue($this->qtype->compare_string_with_wildcard('Frog', 'frog', true)); + $this->assertTrue($this->qtype->compare_string_with_wildcard(' FROG ', 'Frog', true)); + $this->assertFalse($this->qtype->compare_string_with_wildcard('Frogs', 'Frog', true)); + + // Test case sensitive wildcard matches. + $this->assertTrue($this->qtype->compare_string_with_wildcard('Frog', 'F*og', false)); + $this->assertTrue($this->qtype->compare_string_with_wildcard('Fog', 'F*og', false)); + $this->assertTrue($this->qtype->compare_string_with_wildcard(' Fat dog ', 'F*og', false)); + $this->assertFalse($this->qtype->compare_string_with_wildcard('Frogs', 'F*og', false)); + $this->assertFalse($this->qtype->compare_string_with_wildcard('Fg', 'F*og', false)); + $this->assertFalse($this->qtype->compare_string_with_wildcard('frog', 'F*og', false)); + $this->assertFalse($this->qtype->compare_string_with_wildcard(' fat dog ', 'F*og', false)); + + // Test case insensitive wildcard matches. + $this->assertTrue($this->qtype->compare_string_with_wildcard('Frog', 'F*og', true)); + $this->assertTrue($this->qtype->compare_string_with_wildcard('Fog', 'F*og', true)); + $this->assertTrue($this->qtype->compare_string_with_wildcard(' Fat dog ', 'F*og', true)); + $this->assertFalse($this->qtype->compare_string_with_wildcard('Frogs', 'F*og', true)); + $this->assertFalse($this->qtype->compare_string_with_wildcard('Fg', 'F*og', true)); + $this->assertTrue($this->qtype->compare_string_with_wildcard('frog', 'F*og', true)); + $this->assertTrue($this->qtype->compare_string_with_wildcard(' fat dog ', 'F*og', true)); + + // Test match using regexp special chars. + $this->assertTrue($this->qtype->compare_string_with_wildcard(' * ', '\*', false)); + $this->assertTrue($this->qtype->compare_string_with_wildcard('*', '\*', false)); + $this->assertTrue($this->qtype->compare_string_with_wildcard('Frog*toad', 'Frog\*toad', false)); + $this->assertfalse($this->qtype->compare_string_with_wildcard('a', '[a-z]', false)); + $this->assertTrue($this->qtype->compare_string_with_wildcard('[a-z]', '[a-z]', false)); + $this->assertTrue($this->qtype->compare_string_with_wildcard('\{}/', '\{}/', true)); + } +} + +?>