Permalink
Browse files

MDL-38538 question autosave: fix sequencecheck handling.

  • Loading branch information...
1 parent e3485c5 commit c7fbfe46f91e3bb0f71c7b224ef9571e48a5a90a @timhunt timhunt committed Apr 4, 2013
@@ -347,6 +347,21 @@ public function get_step($i) {
return $this->steps[$i];
}
+ /**
+ * Get the number of real steps in this attempt.
+ * This is put as a hidden field in the HTML, so that when we receive some
+ * data to process, then we can check that it came from the question
+ * in the state we are now it.
+ * @return int a number that summarises the current state of this question attempt.
+ */
+ public function get_sequence_check_count() {
+ $numrealsteps = $this->get_num_steps();
+ if ($this->has_autosaved_step()) {
+ $numrealsteps -= 1;
+ }
+ return $numrealsteps;
+ }
+
/**
* Get the number of steps in this attempt.
* For internal/test code use only.
@@ -623,7 +623,7 @@ public function validate_sequence_number($slot, $postdata = null) {
$qa->get_control_field_name('sequencecheck'), PARAM_INT, $postdata);
if (is_null($sequencecheck)) {
return false;
- } else if ($sequencecheck != $qa->get_num_steps()) {
+ } else if ($sequencecheck != $qa->get_sequence_check_count()) {
throw new question_out_of_sequence_exception($this->id, $slot, $postdata);
} else {
return true;
@@ -642,7 +642,7 @@ public function is_autosave_required($slot, $postdata = null) {
$qa->get_control_field_name('sequencecheck'), PARAM_INT, $postdata);
if (is_null($sequencecheck)) {
return false;
- } else if ($sequencecheck != $qa->get_num_steps()) {
+ } else if ($sequencecheck != $qa->get_sequence_check_count()) {
return false;
} else {
return true;
@@ -325,7 +325,7 @@ protected function formulation(question_attempt $qa, qbehaviour_renderer $behavi
$output .= html_writer::empty_tag('input', array(
'type' => 'hidden',
'name' => $qa->get_control_field_name('sequencecheck'),
- 'value' => $qa->get_num_steps()));
+ 'value' => $qa->get_sequence_check_count()));
$output .= $qtoutput->formulation_and_controls($qa, $options);
if ($options->clearwrong) {
$output .= $qtoutput->clear_wrong($qa);
@@ -693,7 +693,7 @@ protected function response_data_to_post($data) {
$prefix = $this->quba->get_field_prefix($this->slot);
$fulldata = array(
'slots' => $this->slot,
- $prefix . ':sequencecheck' => $this->get_question_attempt()->get_num_steps(),
+ $prefix . ':sequencecheck' => $this->get_question_attempt()->get_sequence_check_count(),
);
foreach ($data as $name => $value) {
$fulldata[$prefix . $name] = $value;
@@ -786,6 +786,16 @@ protected function check_output_contains_text_input($name, $value = null, $enabl
}
}
+ protected function check_output_contains_hidden_input($name, $value) {
+ $attributes = array(
+ 'type' => 'hidden',
+ 'name' => $this->quba->get_field_prefix($this->slot) . $name,
+ 'value' => $value,
+ );
+ $this->assertTag($this->get_tag_matcher('input', $attributes), $this->currentoutput,
+ 'Looking for a hidden input with attributes ' . html_writer::attributes($attributes) . ' in ' . $this->currentoutput);
+ }
+
protected function get_tag_matcher($tag, $attributes) {
return array(
'tag' => $tag,
@@ -65,6 +65,7 @@ public function test_autosave_then_display() {
// Now check how that is re-displayed.
$this->render();
$this->check_output_contains_text_input('answer', 'first response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
// Process an autosave.
$this->load_quba();
@@ -78,6 +79,7 @@ public function test_autosave_then_display() {
$this->load_quba();
$this->render();
$this->check_output_contains_text_input('answer', 'second response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
$this->delete_quba();
}
@@ -108,6 +110,7 @@ public function test_autosave_then_autosave_different_data() {
// Now check how that is re-displayed.
$this->render();
$this->check_output_contains_text_input('answer', 'first response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
// Process an autosave.
$this->load_quba();
@@ -121,6 +124,7 @@ public function test_autosave_then_autosave_different_data() {
$this->load_quba();
$this->render();
$this->check_output_contains_text_input('answer', 'second response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
// Process a second autosave.
$this->load_quba();
@@ -134,6 +138,7 @@ public function test_autosave_then_autosave_different_data() {
$this->load_quba();
$this->render();
$this->check_output_contains_text_input('answer', 'third response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
$this->delete_quba();
}
@@ -164,6 +169,7 @@ public function test_autosave_then_autosave_same_data() {
// Now check how that is re-displayed.
$this->render();
$this->check_output_contains_text_input('answer', 'first response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
// Process an autosave.
$this->load_quba();
@@ -177,6 +183,7 @@ public function test_autosave_then_autosave_same_data() {
$this->load_quba();
$this->render();
$this->check_output_contains_text_input('answer', 'second response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
$stepid = $this->quba->get_question_attempt($this->slot)->get_last_step()->get_id();
@@ -196,6 +203,7 @@ public function test_autosave_then_autosave_same_data() {
$this->load_quba();
$this->render();
$this->check_output_contains_text_input('answer', 'second response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
$this->delete_quba();
}
@@ -226,6 +234,7 @@ public function test_autosave_then_autosave_original_data() {
// Now check how that is re-displayed.
$this->render();
$this->check_output_contains_text_input('answer', 'first response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
// Process an autosave.
$this->load_quba();
@@ -239,6 +248,7 @@ public function test_autosave_then_autosave_original_data() {
$this->load_quba();
$this->render();
$this->check_output_contains_text_input('answer', 'second response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
// Process a second autosave saving the original response.
// This should remove the autosave step.
@@ -253,6 +263,7 @@ public function test_autosave_then_autosave_original_data() {
$this->load_quba();
$this->render();
$this->check_output_contains_text_input('answer', 'first response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
$this->delete_quba();
}
@@ -283,6 +294,7 @@ public function test_autosave_then_real_save() {
// Now check how that is re-displayed.
$this->render();
$this->check_output_contains_text_input('answer', 'first response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
// Process an autosave.
$this->load_quba();
@@ -296,6 +308,7 @@ public function test_autosave_then_real_save() {
$this->load_quba();
$this->render();
$this->check_output_contains_text_input('answer', 'second response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
// Now save for real a third response.
$this->process_submission(array('answer' => 'third response'));
@@ -308,6 +321,7 @@ public function test_autosave_then_real_save() {
// Now check how that is re-displayed.
$this->render();
$this->check_output_contains_text_input('answer', 'third response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 3);
}
public function test_autosave_then_real_save_same() {
@@ -336,6 +350,7 @@ public function test_autosave_then_real_save_same() {
// Now check how that is re-displayed.
$this->render();
$this->check_output_contains_text_input('answer', 'first response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
// Process an autosave.
$this->load_quba();
@@ -349,6 +364,7 @@ public function test_autosave_then_real_save_same() {
$this->load_quba();
$this->render();
$this->check_output_contains_text_input('answer', 'second response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
// Now save for real of the same response.
$this->process_submission(array('answer' => 'second response'));
@@ -361,6 +377,7 @@ public function test_autosave_then_real_save_same() {
// Now check how that is re-displayed.
$this->render();
$this->check_output_contains_text_input('answer', 'second response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 3);
}
public function test_autosave_then_submit() {
@@ -389,6 +406,7 @@ public function test_autosave_then_submit() {
// Now check how that is re-displayed.
$this->render();
$this->check_output_contains_text_input('answer', 'first response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
// Process an autosave.
$this->load_quba();
@@ -402,6 +420,7 @@ public function test_autosave_then_submit() {
$this->load_quba();
$this->render();
$this->check_output_contains_text_input('answer', 'second response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
// Now submit a third response.
$this->process_submission(array('answer' => 'third response'));
@@ -415,6 +434,7 @@ public function test_autosave_then_submit() {
// Now check how that is re-displayed.
$this->render();
$this->check_output_contains_text_input('answer', 'third response', false);
+ $this->check_output_contains_hidden_input(':sequencecheck', 4);
}
public function test_autosave_and_save_concurrently() {
@@ -485,6 +505,7 @@ public function test_autosave_and_save_concurrently() {
$this->check_step_count(2);
$this->render();
$this->check_output_contains_text_input('answer', 'real response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
$DB2->dispose();
}
@@ -556,6 +577,7 @@ public function test_concurrent_autosaves() {
$this->check_step_count(2);
$this->render();
$this->check_output_contains_text_input('answer', 'autosaved response 1');
+ $this->check_output_contains_hidden_input(':sequencecheck', 1);
$DB2->dispose();
}
@@ -586,11 +608,12 @@ public function test_autosave_with_wrong_seq_number_ignored() {
// Now check how that is re-displayed.
$this->render();
$this->check_output_contains_text_input('answer', 'first response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
- // Process an autosave with a sequence number 1 to small (so from the past).
+ // Process an autosave with a sequence number 1 too small (so from the past).
$this->load_quba();
$postdata = $this->response_data_to_post(array('answer' => 'obsolete response'));
- $postdata[$this->quba->get_field_prefix($this->slot) . ':sequencecheck'] = $this->get_question_attempt()->get_num_steps() - 1;
+ $postdata[$this->quba->get_field_prefix($this->slot) . ':sequencecheck'] = $this->get_question_attempt()->get_sequence_check_count() - 1;
$this->quba->process_all_autosaves(null, $postdata);
$this->check_current_state(question_state::$complete);
$this->check_current_mark(null);
@@ -601,6 +624,7 @@ public function test_autosave_with_wrong_seq_number_ignored() {
$this->load_quba();
$this->render();
$this->check_output_contains_text_input('answer', 'first response');
+ $this->check_output_contains_hidden_input(':sequencecheck', 2);
$this->delete_quba();
}

0 comments on commit c7fbfe4

Please sign in to comment.