.
/**
* This file contains overall tests of multianswer questions.
*
* @package qtype
* @subpackage multianswer
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
/**
* Unit tests for the multianswer question type.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_multianswer_walkthrough_test extends qbehaviour_walkthrough_test_base {
protected function get_contains_subq_status(question_state $state) {
return new question_pattern_expectation('~' .
preg_quote($state->default_string(true), '~') . '
~');
}
public function test_deferred_feedback() {
// Create a multianswer question.
$q = test_question_maker::make_question('multianswer', 'fourmc');
$this->start_attempt_at_question($q, 'deferredfeedback', 4);
// Check the initial state.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_does_not_contain_validation_error_expectation());
// Save in incomplete answer.
$this->process_submission(array('sub1_answer' => '1', 'sub2_answer' => '',
'sub3_answer' => '', 'sub4_answer' => ''));
// Verify.
$this->check_current_state(question_state::$invalid);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_contains_validation_error_expectation());
// Save a partially correct answer.
$this->process_submission(array('sub1_answer' => '1', 'sub2_answer' => '1',
'sub3_answer' => '1', 'sub4_answer' => '1'));
// Verify.
$this->check_current_state(question_state::$complete);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_does_not_contain_validation_error_expectation());
// Now submit all and finish.
$this->finish();
// Verify.
$this->check_current_state(question_state::$gradedpartial);
$this->check_current_mark(2);
$this->check_current_output(
$this->get_contains_mark_summary(2),
$this->get_contains_partcorrect_expectation(),
$this->get_does_not_contain_validation_error_expectation());
}
public function test_deferred_feedback_numericalzero_not_answered() {
// Tests the situation found in MDL-35370.
// Create a multianswer question with one numerical subquestion, right answer zero.
$q = test_question_maker::make_question('multianswer', 'numericalzero');
$this->start_attempt_at_question($q, 'deferredfeedback', 1);
// Check the initial state.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_does_not_contain_validation_error_expectation());
// Now submit all and finish.
$this->finish();
// Verify.
$this->check_current_state(question_state::$gaveup);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
new question_pattern_expectation('~]* class="[^"]*incorrect[^"]*" [^>]*/>~'),
$this->get_contains_subq_status(question_state::$gaveup),
$this->get_does_not_contain_validation_error_expectation());
}
public function test_deferred_feedback_numericalzero_0_answer() {
// Tests the situation found in MDL-35370.
// Create a multianswer question with one numerical subquestion, right answer zero.
$q = test_question_maker::make_question('multianswer', 'numericalzero');
$this->start_attempt_at_question($q, 'deferredfeedback', 1);
// Check the initial state.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_does_not_contain_validation_error_expectation());
// Save a the correct answer.
$this->process_submission(array('sub1_answer' => '0'));
// Verify.
$this->check_current_state(question_state::$complete);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_does_not_contain_validation_error_expectation());
// Now submit all and finish.
$this->finish();
// Verify.
$this->check_current_state(question_state::$gradedright);
$this->check_current_mark(1);
$this->check_current_output(
$this->get_contains_mark_summary(1),
$this->get_contains_correct_expectation(),
$this->get_contains_subq_status(question_state::$gradedright),
$this->get_does_not_contain_validation_error_expectation());
}
public function test_deferred_feedback_numericalzero_0_wrong() {
// Tests the situation found in MDL-35370.
// Create a multianswer question with one numerical subquestion, right answer zero.
$q = test_question_maker::make_question('multianswer', 'numericalzero');
$this->start_attempt_at_question($q, 'deferredfeedback', 1);
// Check the initial state.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_does_not_contain_validation_error_expectation());
// Save a the correct answer.
$this->process_submission(array('sub1_answer' => '42'));
// Verify.
$this->check_current_state(question_state::$complete);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_does_not_contain_validation_error_expectation());
// Now submit all and finish.
$this->finish();
// Verify.
$this->check_current_state(question_state::$gradedwrong);
$this->check_current_mark(0);
$this->check_current_output(
$this->get_contains_mark_summary(0),
$this->get_contains_incorrect_expectation(),
$this->get_contains_subq_status(question_state::$gradedwrong),
$this->get_does_not_contain_validation_error_expectation());
}
public function test_interactive_feedback() {
// Create a multianswer question.
$q = test_question_maker::make_question('multianswer', 'fourmc');
$q->hints = array(
new question_hint_with_parts(11, 'This is the first hint.', FORMAT_HTML, false, true),
new question_hint_with_parts(12, 'This is the second hint.', FORMAT_HTML, true, true),
);
$choices = array('0' => 'California', '1' => 'Arizona');
$this->start_attempt_at_question($q, 'interactive', 4);
// Check the initial state.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->assertEquals('interactivecountback',
$this->quba->get_question_attempt($this->slot)->get_behaviour_name());
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_contains_submit_button_expectation(true),
$this->get_does_not_contain_validation_error_expectation(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_tries_remaining_expectation(3),
$this->get_does_not_contain_num_parts_correct(),
$this->get_no_hint_visible_expectation());
$this->check_output_contains_selectoptions(
$this->get_contains_select_expectation('sub1_answer', $choices, null, true),
$this->get_contains_select_expectation('sub2_answer', $choices, null, true),
$this->get_contains_select_expectation('sub3_answer', $choices, null, true),
$this->get_contains_select_expectation('sub4_answer', $choices, null, true));
// Submit a completely wrong response.
$this->process_submission(array('sub1_answer' => '1', 'sub2_answer' => '0',
'sub3_answer' => '1', 'sub4_answer' => '0', '-submit' => 1));
// Verify.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_does_not_contain_num_parts_correct(),
$this->get_contains_hidden_expectation(
$this->quba->get_field_prefix($this->slot) . 'sub1_answer', ''),
$this->get_contains_hidden_expectation(
$this->quba->get_field_prefix($this->slot) . 'sub2_answer', ''),
$this->get_contains_hidden_expectation(
$this->quba->get_field_prefix($this->slot) . 'sub3_answer', ''),
$this->get_contains_hidden_expectation(
$this->quba->get_field_prefix($this->slot) . 'sub4_answer', ''),
$this->get_does_not_contain_submit_button_expectation(),
$this->get_contains_try_again_button_expectation(true),
$this->get_does_not_contain_correctness_expectation(),
$this->get_contains_hint_expectation('This is the first hint.'));
$this->check_output_contains_selectoptions(
$this->get_contains_select_expectation('sub1_answer', $choices, 1, false),
$this->get_contains_select_expectation('sub2_answer', $choices, 0, false),
$this->get_contains_select_expectation('sub3_answer', $choices, 1, false),
$this->get_contains_select_expectation('sub4_answer', $choices, 0, false));
// Check that, if we review in this state, the try again button is disabled.
$displayoptions = new question_display_options();
$displayoptions->readonly = true;
$html = $this->quba->render_question($this->slot, $displayoptions);
$this->assert($this->get_contains_try_again_button_expectation(false), $html);
// Try again.
$this->process_submission(array('sub1_answer' => '',
'sub2_answer' => '', 'sub3_answer' => '',
'sub4_answer' => '', '-tryagain' => 1));
// Verify.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_submit_button_expectation(true),
$this->get_does_not_contain_feedback_expectation(),
$this->get_tries_remaining_expectation(2),
$this->get_no_hint_visible_expectation());
$this->check_output_contains_selectoptions(
$this->get_contains_select_expectation('sub1_answer', $choices, null, true),
$this->get_contains_select_expectation('sub2_answer', $choices, null, true),
$this->get_contains_select_expectation('sub3_answer', $choices, null, true),
$this->get_contains_select_expectation('sub4_answer', $choices, null, true));
// Submit a partially wrong response.
$this->process_submission(array('sub1_answer' => '1', 'sub2_answer' => '1',
'sub3_answer' => '1', 'sub4_answer' => '1', '-submit' => 1));
// Verify.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_num_parts_correct(2),
$this->get_contains_hidden_expectation(
$this->quba->get_field_prefix($this->slot) . 'sub1_answer', ''),
$this->get_contains_hidden_expectation(
$this->quba->get_field_prefix($this->slot) . 'sub2_answer', '1'),
$this->get_contains_hidden_expectation(
$this->quba->get_field_prefix($this->slot) . 'sub3_answer', ''),
$this->get_contains_hidden_expectation(
$this->quba->get_field_prefix($this->slot) . 'sub4_answer', '1'),
$this->get_does_not_contain_submit_button_expectation(),
$this->get_contains_hint_expectation('This is the second hint.'));
$this->check_output_contains_selectoptions(
$this->get_contains_select_expectation('sub1_answer', $choices, 1, false),
$this->get_contains_select_expectation('sub2_answer', $choices, 1, false),
$this->get_contains_select_expectation('sub3_answer', $choices, 1, false),
$this->get_contains_select_expectation('sub4_answer', $choices, 1, false));
// Try again.
$this->process_submission(array('sub1_answer' => '',
'sub2_answer' => '1', 'sub3_answer' => '',
'sub4_answer' => '1', '-tryagain' => 1));
// Verify.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_submit_button_expectation(true),
$this->get_does_not_contain_feedback_expectation(),
$this->get_tries_remaining_expectation(1),
$this->get_no_hint_visible_expectation());
$this->check_output_contains_selectoptions(
$this->get_contains_select_expectation('sub1_answer', $choices, '', true),
$this->get_contains_select_expectation('sub2_answer', $choices, '1', true),
$this->get_contains_select_expectation('sub3_answer', $choices, '', true),
$this->get_contains_select_expectation('sub4_answer', $choices, '1', true));
}
public function test_interactive_partial_response_does_not_reveal_answer() {
// Create a multianswer question.
$q = test_question_maker::make_question('multianswer', 'fourmc');
$q->hints = array(
new question_hint_with_parts(11, 'This is the first hint.', FORMAT_HTML, false, true),
new question_hint_with_parts(12, 'This is the second hint.', FORMAT_HTML, true, true),
);
$choices = array('0' => 'California', '1' => 'Arizona');
$this->start_attempt_at_question($q, 'interactive', 4);
// Check the initial state.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->assertEquals('interactivecountback',
$this->quba->get_question_attempt($this->slot)->get_behaviour_name());
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_contains_submit_button_expectation(true),
$this->get_does_not_contain_validation_error_expectation(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_tries_remaining_expectation(3),
$this->get_does_not_contain_num_parts_correct(),
$this->get_no_hint_visible_expectation());
$this->check_output_contains_selectoptions(
$this->get_contains_select_expectation('sub1_answer', $choices, null, true),
$this->get_contains_select_expectation('sub2_answer', $choices, null, true),
$this->get_contains_select_expectation('sub3_answer', $choices, null, true),
$this->get_contains_select_expectation('sub4_answer', $choices, null, true));
// Submit an incomplete response response.
$this->process_submission(array('sub1_answer' => '1', 'sub2_answer' => '1', '-submit' => 1));
// Verify.
$this->check_current_state(question_state::$invalid);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_does_not_contain_num_parts_correct(),
$this->get_contains_validation_error_expectation(),
$this->get_contains_submit_button_expectation(true),
$this->get_does_not_contain_try_again_button_expectation(),
$this->get_does_not_contain_correctness_expectation(),
$this->get_no_hint_visible_expectation());
$this->check_output_contains_selectoptions(
$this->get_contains_select_expectation('sub1_answer', $choices, 1, true),
$this->get_contains_select_expectation('sub2_answer', $choices, 1, true),
$this->get_contains_select_expectation('sub3_answer', $choices, null, true),
$this->get_contains_select_expectation('sub4_answer', $choices, null, true));
$this->render();
$a = array('mark' => '0.00', 'max' => '1.00');
$this->assertNotRegExp('~' . preg_quote(get_string('markoutofmax', 'question', $a), '~') . '~',
$this->currentoutput);
$a['mark'] = '1.00';
$this->assertNotRegExp('~' . preg_quote(get_string('markoutofmax', 'question', $a), '~') . '~',
$this->currentoutput);
}
public function test_interactivecountback_feedback() {
// Create a multianswer question.
$q = test_question_maker::make_question('multianswer', 'fourmc');
$q->hints = array(
new question_hint_with_parts(11, 'This is the first hint.', FORMAT_HTML, true, true),
new question_hint_with_parts(12, 'This is the second hint.', FORMAT_HTML, true, true),
);
$choices = array('0' => 'California', '1' => 'Arizona');
$this->start_attempt_at_question($q, 'interactive', 12);
// Check the initial state.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->assertEquals('interactivecountback',
$this->quba->get_question_attempt($this->slot)->get_behaviour_name());
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_contains_submit_button_expectation(true),
$this->get_does_not_contain_validation_error_expectation(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_tries_remaining_expectation(3),
$this->get_no_hint_visible_expectation());
$this->check_output_contains_selectoptions(
$this->get_contains_select_expectation('sub1_answer', $choices, null, true),
$this->get_contains_select_expectation('sub2_answer', $choices, null, true),
$this->get_contains_select_expectation('sub3_answer', $choices, null, true),
$this->get_contains_select_expectation('sub4_answer', $choices, null, true));
// Submit an answer with two right, and two wrong.
$this->process_submission(array('sub1_answer' => '1', 'sub2_answer' => '1',
'sub3_answer' => '1', 'sub4_answer' => '1', '-submit' => 1));
// Verify.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_does_not_contain_submit_button_expectation(),
$this->get_contains_try_again_button_expectation(true),
$this->get_does_not_contain_correctness_expectation(),
new question_pattern_expectation('/Tries remaining: 2/'),
$this->get_contains_hint_expectation('This is the first hint.'));
$this->check_output_contains_selectoptions(
$this->get_contains_select_expectation('sub1_answer', $choices, 1, false),
$this->get_contains_select_expectation('sub2_answer', $choices, 1, false),
$this->get_contains_select_expectation('sub3_answer', $choices, 1, false),
$this->get_contains_select_expectation('sub4_answer', $choices, 1, false));
// Check that extract responses will return the reset data.
$prefix = $this->quba->get_field_prefix($this->slot);
$this->assertEquals(array('sub1_answer' => 1),
$this->quba->extract_responses($this->slot, array($prefix . 'sub1_answer' => 1)));
// Do try again.
$this->process_submission(array('sub1_answer' => '',
'sub2_answer' => '1', 'sub3_answer' => '',
'sub4_answer' => '1', '-tryagain' => 1));
// Verify.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_submit_button_expectation(true),
$this->get_does_not_contain_feedback_expectation(),
$this->get_tries_remaining_expectation(2),
$this->get_no_hint_visible_expectation());
$this->check_output_contains_selectoptions(
$this->get_contains_select_expectation('sub1_answer', $choices, '', true),
$this->get_contains_select_expectation('sub2_answer', $choices, '1', true),
$this->get_contains_select_expectation('sub3_answer', $choices, '', true),
$this->get_contains_select_expectation('sub4_answer', $choices, '1', true));
// Submit the right answer.
$this->process_submission(array('sub1_answer' => '0', 'sub2_answer' => '1',
'sub3_answer' => '0', 'sub4_answer' => '1', '-submit' => 1));
// Verify.
$this->check_current_state(question_state::$gradedright);
$this->check_current_mark(10);
$this->check_current_output(
$this->get_does_not_contain_submit_button_expectation(),
$this->get_does_not_contain_try_again_button_expectation(),
$this->get_contains_correct_expectation(),
new question_no_pattern_expectation('/class="control\b[^"]*\bpartiallycorrect"/'));
$this->check_output_contains_selectoptions(
$this->get_contains_select_expectation('sub1_answer', $choices, '0', false),
$this->get_contains_select_expectation('sub2_answer', $choices, '1', false),
$this->get_contains_select_expectation('sub3_answer', $choices, '0', false),
$this->get_contains_select_expectation('sub4_answer', $choices, '1', false));
}
public function test_deferred_feedback_multiple() {
// Create a multianswer question.
$q = test_question_maker::make_question('multianswer', 'multiple');
$this->start_attempt_at_question($q, 'deferredfeedback', 2);
// Check the initial state.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_does_not_contain_validation_error_expectation());
// Save in incomplete answer.
$this->process_submission(array('sub1_choice0' => '1', 'sub1_choice1' => '1',
'sub1_choice2' => '', 'sub1_choice3' => '',
'sub1_choice4' => '', 'sub1_choice5' => '1',
));
// Verify.
$this->check_current_state(question_state::$invalid);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_contains_validation_error_expectation());
// Save a partially correct answer.
$this->process_submission(array('sub1_choice0' => '1', 'sub1_choice1' => '',
'sub1_choice2' => '', 'sub1_choice3' => '',
'sub1_choice4' => '1', 'sub1_choice5' => '1',
'sub2_choice0' => '', 'sub2_choice1' => '',
'sub2_choice2' => '', 'sub2_choice3' => '',
'sub2_choice4' => '1',
));
// Verify.
$this->check_current_state(question_state::$complete);
$this->check_current_mark(null);
$this->check_current_output(
$this->get_contains_marked_out_of_summary(),
$this->get_does_not_contain_feedback_expectation(),
$this->get_does_not_contain_validation_error_expectation());
// Now submit all and finish.
$this->finish();
// Verify.
$this->check_current_state(question_state::$gradedpartial);
$this->check_current_mark(1.5);
$this->check_current_output(
$this->get_contains_mark_summary(1.5),
$this->get_contains_partcorrect_expectation(),
$this->get_does_not_contain_validation_error_expectation());
}
}