mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-11703 - escaped *'s in shortanswer answers were not matching properly. Merged from MOODLE_18_STABLE.
This commit is contained in:
parent
6f9ce92696
commit
3270033ab8
2 changed files with 77 additions and 2 deletions
|
@ -212,9 +212,12 @@ class question_shortanswer_qtype extends default_questiontype {
|
||||||
// Break the string on non-escaped asterisks.
|
// Break the string on non-escaped asterisks.
|
||||||
$bits = preg_split('/(?<!\\\\)\*/', $pattern);
|
$bits = preg_split('/(?<!\\\\)\*/', $pattern);
|
||||||
// Escape regexp special characters in the bits.
|
// Escape regexp special characters in the bits.
|
||||||
$bits = array_map('preg_quote', $bits);
|
$excapedbits = array();
|
||||||
|
foreach ($bits as $bit) {
|
||||||
|
$excapedbits[] = preg_quote(str_replace('\*', '*', $bit));
|
||||||
|
}
|
||||||
// Put it back together to make the regexp.
|
// Put it back together to make the regexp.
|
||||||
$regexp = '|^' . implode('.*', $bits) . '$|u';
|
$regexp = '|^' . implode('.*', $excapedbits) . '$|u';
|
||||||
|
|
||||||
// Make the match insensitive if requested to.
|
// Make the match insensitive if requested to.
|
||||||
if ($ignorecase) {
|
if ($ignorecase) {
|
||||||
|
|
72
question/type/shortanswer/simpletest/testquestiontype.php
Normal file
72
question/type/shortanswer/simpletest/testquestiontype.php
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Unit tests for (some of) question/type/shortanswer/questiontype.php.
|
||||||
|
*
|
||||||
|
* @copyright © 2007 The Open University
|
||||||
|
* @author T.J.Hunt@open.ac.uk
|
||||||
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
||||||
|
* @package question
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!defined('MOODLE_INTERNAL')) {
|
||||||
|
die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
|
||||||
|
}
|
||||||
|
|
||||||
|
require_once($CFG->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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
Loading…
Add table
Add a link
Reference in a new issue