Skip to content

Commit

Permalink
MDL-37506 question code: incorrect preg_quote uses.
Browse files Browse the repository at this point in the history
For preg_quote to work reliably, you must pass the delimiter you are
using for your regular expression. In many places we were failing to do
that.
  • Loading branch information
timhunt committed Jan 18, 2013
1 parent a5ec499 commit 03cefcc
Show file tree
Hide file tree
Showing 25 changed files with 73 additions and 73 deletions.
14 changes: 7 additions & 7 deletions question/behaviour/adaptive/tests/walkthrough_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,35 +43,35 @@ class qbehaviour_adaptive_walkthrough_test extends qbehaviour_walkthrough_test_b
protected function get_contains_penalty_info_expectation($penalty) {
$penaltyinfo = get_string('gradingdetailspenalty', 'qbehaviour_adaptive',
format_float($penalty, $this->displayoptions->markdp));
return new question_pattern_expectation('/'.preg_quote($penaltyinfo).'/');
return new question_pattern_expectation('/'.preg_quote($penaltyinfo, '/').'/');
}

protected function get_does_not_contain_penalty_info_expectation() {
$penaltyinfo = get_string('gradingdetailspenalty', 'qbehaviour_adaptive', 'XXXXX');
$penaltypattern = '/'.str_replace('XXXXX', '\\w*', preg_quote($penaltyinfo)).'/';
$penaltypattern = '/'.str_replace('XXXXX', '\\w*', preg_quote($penaltyinfo, '/')).'/';
return new question_no_pattern_expectation($penaltypattern);
}

protected function get_contains_total_penalty_expectation($penalty) {
$penaltyinfo = get_string('gradingdetailspenaltytotal', 'qbehaviour_adaptive',
format_float($penalty, $this->displayoptions->markdp));
return new question_pattern_expectation('/'.preg_quote($penaltyinfo).'/');
return new question_pattern_expectation('/'.preg_quote($penaltyinfo, '/').'/');
}

protected function get_does_not_contain_total_penalty_expectation() {
$penaltyinfo = get_string('gradingdetailspenaltytotal', 'qbehaviour_adaptive', 'XXXXX');
$penaltypattern = '/'.str_replace('XXXXX', '\\w*', preg_quote($penaltyinfo)).'/';
$penaltypattern = '/'.str_replace('XXXXX', '\\w*', preg_quote($penaltyinfo, '/')).'/';
return new question_no_pattern_expectation($penaltypattern);
}

protected function get_contains_disregarded_info_expectation() {
$penaltyinfo = get_string('disregardedwithoutpenalty', 'qbehaviour_adaptive');
return new question_pattern_expectation('/'.preg_quote($penaltyinfo).'/');
return new question_pattern_expectation('/'.preg_quote($penaltyinfo, '/').'/');
}

protected function get_does_not_contain_disregarded_info_expectation() {
$penaltyinfo = get_string('disregardedwithoutpenalty', 'qbehaviour_adaptive');
return new question_no_pattern_expectation('/'.preg_quote($penaltyinfo).'/');
return new question_no_pattern_expectation('/'.preg_quote($penaltyinfo, '/').'/');
}

public function test_adaptive_multichoice() {
Expand Down Expand Up @@ -165,7 +165,7 @@ public function test_adaptive_multichoice() {
$this->check_current_mark(1);
$this->check_current_output(
$this->get_contains_mark_summary(1),
new question_pattern_expectation('/' . preg_quote('Not good enough!') . '/'));
new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));

// Now change the correct answer to the question, and regrade.
$mc->answers[13]->fraction = -0.33333333;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function test_multichoice() {
$this->check_current_mark(1);
$this->check_current_output(
$this->get_contains_mark_summary(1),
new question_pattern_expectation('/' . preg_quote('Not good enough!') . '/'));
new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));

// Now change the correct answer to the question, and regrade.
$mc->answers[13]->fraction = -0.33333333;
Expand Down
12 changes: 6 additions & 6 deletions question/behaviour/deferredcbm/tests/walkthrough_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function test_deferred_cbm_truefalse_high_certainty() {
$this->check_current_state(question_state::$mangrpartial);
$this->check_current_mark(1);
$this->check_current_output(new question_pattern_expectation('/' .
preg_quote('Not good enough!') . '/'));
preg_quote('Not good enough!', '/') . '/'));

// Now change the correct answer to the question, and regrade.
$tf->rightanswer = false;
Expand Down Expand Up @@ -178,7 +178,7 @@ public function test_deferred_cbm_truefalse_default_certainty() {
new question_pattern_expectation('/' . preg_quote(
get_string('assumingcertainty', 'qbehaviour_deferredcbm',
question_cbm::get_string(
$qa->get_last_behaviour_var('_assumedcertainty')))) . '/'));
$qa->get_last_behaviour_var('_assumedcertainty'))), '/') . '/'));
$this->assertEquals(get_string('true', 'qtype_truefalse'),
$this->quba->get_response_summary($this->slot));
}
Expand All @@ -204,9 +204,9 @@ public function test_deferredcbm_resume_multichoice_single() {
$this->get_contains_incorrect_expectation());
$this->assertEquals('A [' . question_cbm::get_string(question_cbm::HIGH) . ']',
$this->quba->get_right_answer_summary($this->slot));
$this->assertRegExp('/' . preg_quote($mc->questiontext) . '/',
$this->assertRegExp('/' . preg_quote($mc->questiontext, '/') . '/',
$this->quba->get_question_summary($this->slot));
$this->assertRegExp('/(B|C) \[' . preg_quote(question_cbm::get_string(2)) . '\]/',
$this->assertRegExp('/(B|C) \[' . preg_quote(question_cbm::get_string(2), '/') . '\]/',
$this->quba->get_response_summary($this->slot));

// Save the old attempt.
Expand All @@ -228,7 +228,7 @@ public function test_deferredcbm_resume_multichoice_single() {
$this->get_does_not_contain_correctness_expectation());
$this->assertEquals('A [' . question_cbm::get_string(question_cbm::HIGH) . ']',
$this->quba->get_right_answer_summary($this->slot));
$this->assertRegExp('/' . preg_quote($mc->questiontext) . '/',
$this->assertRegExp('/' . preg_quote($mc->questiontext, '/') . '/',
$this->quba->get_question_summary($this->slot));
$this->assertNull($this->quba->get_response_summary($this->slot));

Expand All @@ -243,7 +243,7 @@ public function test_deferredcbm_resume_multichoice_single() {
$this->get_contains_mc_radio_expectation($rightindex, false, true),
$this->get_contains_cbm_radio_expectation(3, false, true),
$this->get_contains_correct_expectation());
$this->assertRegExp('/(A) \[' . preg_quote(question_cbm::get_string(3)) . '\]/',
$this->assertRegExp('/(A) \[' . preg_quote(question_cbm::get_string(3), '/') . '\]/',
$this->quba->get_response_summary($this->slot));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function test_deferredfeedback_feedback_truefalse() {
$this->get_does_not_contain_feedback_expectation());
$this->assertEquals(get_string('true', 'qtype_truefalse'),
$this->quba->get_right_answer_summary($this->slot));
$this->assertRegExp('/' . preg_quote($tf->questiontext) . '/',
$this->assertRegExp('/' . preg_quote($tf->questiontext, '/') . '/',
$this->quba->get_question_summary($this->slot));
$this->assertNull($this->quba->get_response_summary($this->slot));

Expand Down Expand Up @@ -97,7 +97,7 @@ public function test_deferredfeedback_feedback_truefalse() {
$this->check_current_state(question_state::$mangrpartial);
$this->check_current_mark(1);
$this->check_current_output(
new question_pattern_expectation('/' . preg_quote('Not good enough!') . '/'));
new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));

// Now change the correct answer to the question, and regrade.
$tf->rightanswer = false;
Expand Down
6 changes: 3 additions & 3 deletions question/behaviour/immediatecbm/tests/walkthrough_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function test_immediatecbm_feedback_multichoice_right() {
$this->get_does_not_contain_feedback_expectation());
$this->assertEquals('A [' . question_cbm::get_string(question_cbm::HIGH) . ']',
$this->quba->get_right_answer_summary($this->slot));
$this->assertRegExp('/' . preg_quote($mc->questiontext) . '/',
$this->assertRegExp('/' . preg_quote($mc->questiontext, '/') . '/',
$this->quba->get_question_summary($this->slot));
$this->assertNull($this->quba->get_response_summary($this->slot));

Expand Down Expand Up @@ -116,7 +116,7 @@ public function test_immediatecbm_feedback_multichoice_right() {
$this->check_current_mark(0.5);
$this->check_current_output(
$this->get_contains_partcorrect_expectation(),
new question_pattern_expectation('/' . preg_quote('Not good enough!') . '/'));
new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));

// Now change the correct answer to the question, and regrade.
$mc->answers[13]->fraction = -0.33333333;
Expand Down Expand Up @@ -183,7 +183,7 @@ public function test_immediatecbm_feedback_multichoice_try_to_submit_blank() {
$this->check_current_mark(0.5);
$this->check_current_output(
$this->get_contains_partcorrect_expectation(),
new question_pattern_expectation('/' . preg_quote('Not good enough!') . '/'));
new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
}

public function test_immediatecbm_feedback_shortanswer_try_to_submit_no_certainty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function test_immediatefeedback_feedback_multichoice_right() {
$this->check_current_mark(0.5);
$this->check_current_output(
$this->get_contains_partcorrect_expectation(),
new question_pattern_expectation('/' . preg_quote('Not good enough!') . '/'));
new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));

// Now change the correct answer to the question, and regrade.
$mc->answers[13]->fraction = -0.33333333;
Expand Down Expand Up @@ -192,7 +192,7 @@ public function test_immediatefeedback_feedback_multichoice_try_to_submit_blank(
$this->check_current_mark(0.5);
$this->check_current_output(
$this->get_contains_partcorrect_expectation(),
new question_pattern_expectation('/' . preg_quote('Not good enough!') . '/'));
new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
}

public function test_immediatefeedback_feedback_multichoice_wrong_on_finish() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function test_informationitem_feedback_description() {
$this->check_current_state(question_state::$manfinished);
$this->check_current_mark(null);
$this->check_current_output(
new question_pattern_expectation('/' . preg_quote('Not good enough!') . '/'));
new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));

// Check that trying to process a manual comment with a grade causes an exception.
$this->setExpectedException('moodle_exception');
Expand Down
10 changes: 5 additions & 5 deletions question/behaviour/interactive/tests/walkthrough_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function test_interactive_feedback_multichoice_right() {
$this->get_contains_try_again_button_expectation(true),
$this->get_does_not_contain_correctness_expectation(),
new question_pattern_expectation('/' .
preg_quote(get_string('notcomplete', 'qbehaviour_interactive')) . '/'),
preg_quote(get_string('notcomplete', 'qbehaviour_interactive'), '/') . '/'),
$this->get_contains_hint_expectation('This is the first hint'));

// Check that, if we review in this state, the try again button is disabled.
Expand Down Expand Up @@ -165,7 +165,7 @@ public function test_interactive_feedback_multichoice_right() {
$this->check_current_output(
$this->get_contains_mark_summary(0.5),
$this->get_contains_partcorrect_expectation(),
new question_pattern_expectation('/' . preg_quote('Not good enough!') . '/'));
new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));

// Check regrading does not mess anything up.
$this->quba->regrade_all_questions();
Expand Down Expand Up @@ -224,7 +224,7 @@ public function test_interactive_finish_when_try_again_showing() {
$this->get_contains_try_again_button_expectation(true),
$this->get_does_not_contain_correctness_expectation(),
new question_pattern_expectation('/' .
preg_quote(get_string('notcomplete', 'qbehaviour_interactive')) . '/'),
preg_quote(get_string('notcomplete', 'qbehaviour_interactive'), '/') . '/'),
$this->get_contains_hint_expectation('This is the first hint'));

// Finish the attempt.
Expand Down Expand Up @@ -289,7 +289,7 @@ public function test_interactive_shortanswer_try_to_submit_blank() {
$this->get_does_not_contain_validation_error_expectation(),
$this->get_contains_try_again_button_expectation(true),
new question_pattern_expectation('/' .
preg_quote(get_string('notcomplete', 'qbehaviour_interactive')) . '/'),
preg_quote(get_string('notcomplete', 'qbehaviour_interactive'), '/') . '/'),
$this->get_contains_hint_expectation('This is the first hint'));
$this->assertEquals('newt',
$this->quba->get_response_summary($this->slot));
Expand Down Expand Up @@ -387,7 +387,7 @@ public function test_interactive_feedback_multichoice_multiple_reset() {
$this->get_contains_try_again_button_expectation(true),
$this->get_does_not_contain_correctness_expectation(),
new question_pattern_expectation('/' .
preg_quote(get_string('notcomplete', 'qbehaviour_interactive')) . '/'),
preg_quote(get_string('notcomplete', 'qbehaviour_interactive'), '/') . '/'),
$this->get_contains_hint_expectation('This is the first hint'),
$this->get_contains_num_parts_correct(1),
$this->get_contains_standard_incorrect_combined_feedback_expectation(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function test_interactive_feedback_match_reset() {
$this->get_contains_try_again_button_expectation(true),
$this->get_does_not_contain_correctness_expectation(),
new question_pattern_expectation('/' .
preg_quote(get_string('notcomplete', 'qbehaviour_interactive')) . '/'),
preg_quote(get_string('notcomplete', 'qbehaviour_interactive'), '/') . '/'),
$this->get_contains_hint_expectation('This is the first hint'),
$this->get_contains_num_parts_correct(2),
$this->get_contains_standard_partiallycorrect_combined_feedback_expectation(),
Expand Down
10 changes: 5 additions & 5 deletions question/behaviour/manualgraded/tests/walkthrough_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function test_manual_graded_essay() {
$this->check_current_state(question_state::$mangrright);
$this->check_current_mark(10);
$this->check_current_output(
new question_pattern_expectation('/' . preg_quote('Not good enough!') . '/'));
new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));

// Now change the max mark for the question and regrade.
$this->quba->regrade_question($this->slot, true, 1);
Expand Down Expand Up @@ -147,7 +147,7 @@ public function test_manual_graded_truefalse() {
$this->check_current_output(
$this->get_does_not_contain_correctness_expectation(),
$this->get_does_not_contain_specific_feedback_expectation(),
new question_pattern_expectation('/' . preg_quote('Not good enough!') . '/'));
new question_pattern_expectation('/' . preg_quote('Not good enough!', '/') . '/'));
}

public function test_manual_graded_ignore_repeat_sumbission() {
Expand Down Expand Up @@ -194,15 +194,15 @@ public function test_manual_graded_ignore_repeat_sumbission() {
$this->check_current_mark(null);
$this->check_current_output(
new question_pattern_expectation('/' .
preg_quote('I am not sure what grade to award.') . '/'));
preg_quote('I am not sure what grade to award.', '/') . '/'));

// Now grade it.
$this->manual_grade('Pretty good!', '9.00000');
$this->check_step_count($numsteps + 2);
$this->check_current_state(question_state::$mangrpartial);
$this->check_current_mark(9);
$this->check_current_output(
new question_pattern_expectation('/' . preg_quote('Pretty good!') . '/'));
new question_pattern_expectation('/' . preg_quote('Pretty good!', '/') . '/'));

// Process the same data again, and make sure it does not add a step.
$this->manual_grade('Pretty good!', '9.00000');
Expand All @@ -217,7 +217,7 @@ public function test_manual_graded_ignore_repeat_sumbission() {
$this->check_current_mark(null);
$this->check_current_output(
new question_pattern_expectation('/' .
preg_quote('Actually, I am not sure any more.') . '/'));
preg_quote('Actually, I am not sure any more.', '/') . '/'));

$qa = $this->quba->get_question_attempt($this->slot);
$this->assertEquals('Commented: Actually, I am not sure any more.',
Expand Down
4 changes: 2 additions & 2 deletions question/behaviour/missing/tests/missingbehaviour_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ public function test_render_missing() {
$this->assertEquals(array('-submit' => '1', 'choice0' => '1'), $step->get_all_data());

$output = $qa->render(new question_display_options(), '1');
$this->assertRegExp('/' . preg_quote($qa->get_question()->questiontext) . '/', $output);
$this->assertRegExp('/' . preg_quote($qa->get_question()->questiontext, '/') . '/', $output);
$this->assertRegExp('/' . preg_quote(
get_string('questionusedunknownmodel', 'qbehaviour_missing')) . '/', $output);
get_string('questionusedunknownmodel', 'qbehaviour_missing'), '/') . '/', $output);
$this->assertTag(array('tag'=>'div', 'attributes'=>array('class'=>'warning')), $output);
}
}
2 changes: 1 addition & 1 deletion question/engine/questionattempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ protected function get_all_submitted_qt_vars($postdata) {
$postdata = $_POST;
}

$pattern = '/^' . preg_quote($this->get_field_prefix()) . '[^-:]/';
$pattern = '/^' . preg_quote($this->get_field_prefix(), '/') . '[^-:]/';
$prefixlen = strlen($this->get_field_prefix());

$submitteddata = array();
Expand Down
2 changes: 1 addition & 1 deletion question/engine/questionattemptstep.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public function add_prefix($field) {
* null if the extre prefix was not present.
*/
public function remove_prefix($field) {
if (preg_match('~^(-?_?)' . preg_quote($this->extraprefix) . '(.*)$~', $field, $matches)) {
if (preg_match('~^(-?_?)' . preg_quote($this->extraprefix, '~') . '(.*)$~', $field, $matches)) {
return $matches[1] . $matches[2];
} else {
return null;
Expand Down
Loading

0 comments on commit 03cefcc

Please sign in to comment.