Merge branch 'MDL-65308_36' of git://github.com/stronk7/moodle into MOODLE_36_STABLE

This commit is contained in:
Adrian Greeve
2019-04-24 09:28:41 +08:00
3 changed files with 20 additions and 4 deletions
@@ -238,12 +238,12 @@ class restore_log_rule implements processable {
protected function build_regexp($expression, $tokens) {
// Replace to temp (and preg_quote() safe) placeholders
foreach ($tokens as $token) {
$expression = preg_replace('~' . preg_quote($token, '~') . '~', '#@@#@@#', $expression, 1);
$expression = preg_replace('~' . preg_quote($token, '~') . '~', '%@@%@@%', $expression, 1);
}
// quote the expression
$expression = preg_quote($expression, '~');
// Replace all the placeholders
$expression = preg_replace('~#@@#@@#~', '(.*)', $expression);
$expression = preg_replace('~%@@%@@%~', '(.*)', $expression);
return '~' . $expression . '~';
}
}
@@ -49,4 +49,20 @@ class backup_restore_log_rule_testcase extends basic_testcase {
// But the original log has been kept unmodified by the process() call.
$this->assertEquals($originallog, $log);
}
public function test_build_regexp() {
$original = 'Any (string) with [placeholders] like {this} and {this}. [end].';
$expectation = '~Any \(string\) with (.*) like (.*) and (.*)\. (.*)\.~';
$lr = new restore_log_rule('this', 'doesnt', 'matter', 'here');
$class = new ReflectionClass('restore_log_rule');
$method = $class->getMethod('extract_tokens');
$method->setAccessible(true);
$tokens = $method->invoke($lr, $original);
$method = $class->getMethod('build_regexp');
$method->setAccessible(true);
$this->assertSame($expectation, $method->invoke($lr, $original, $tokens));
}
}
+2 -2
View File
@@ -103,9 +103,9 @@ class lesson_page_type_shortanswer extends lesson_page {
$ignorecase = 'i';
}
} else {
$expectedanswer = str_replace('*', '#####', $expectedanswer);
$expectedanswer = str_replace('*', '%@@%@@%', $expectedanswer);
$expectedanswer = preg_quote($expectedanswer, '/');
$expectedanswer = str_replace('#####', '.*', $expectedanswer);
$expectedanswer = str_replace('%@@%@@%', '.*', $expectedanswer);
}
// see if user typed in any of the correct answers
if ((!$this->lesson->custom && $this->lesson->jumpto_is_correct($this->properties->id, $answer->jumpto)) or ($this->lesson->custom && $answer->score > 0) ) {