Skip to content

Commit

Permalink
MDL-31065 qtype shortanswer: refactor the unit tests to use a proper …
Browse files Browse the repository at this point in the history
…helper.

Conflicts:

	question/behaviour/adaptive/simpletest/testwalkthrough.php
  • Loading branch information
timhunt committed Jan 20, 2012
1 parent 1aff7bb commit 2a040c2
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 51 deletions.
8 changes: 4 additions & 4 deletions question/behaviour/adaptive/simpletest/testwalkthrough.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public function test_adaptive_multichoice2() {
public function test_adaptive_shortanswer_partially_right() { public function test_adaptive_shortanswer_partially_right() {


// Create a short answer question // Create a short answer question
$sa = test_question_maker::make_a_shortanswer_question(); $sa = test_question_maker::make_question('shortanswer');
$this->start_attempt_at_question($sa, 'adaptive'); $this->start_attempt_at_question($sa, 'adaptive');


// Check the initial state. // Check the initial state.
Expand Down Expand Up @@ -289,7 +289,7 @@ public function test_adaptive_shortanswer_partially_right() {
public function test_adaptive_shortanswer_wrong_right_wrong() { public function test_adaptive_shortanswer_wrong_right_wrong() {


// Create a short answer question // Create a short answer question
$sa = test_question_maker::make_a_shortanswer_question(); $sa = test_question_maker::make_question('shortanswer');
$this->start_attempt_at_question($sa, 'adaptive'); $this->start_attempt_at_question($sa, 'adaptive');


// Check the initial state. // Check the initial state.
Expand Down Expand Up @@ -368,7 +368,7 @@ public function test_adaptive_shortanswer_wrong_right_wrong() {
public function test_adaptive_shortanswer_invalid_after_complete() { public function test_adaptive_shortanswer_invalid_after_complete() {


// Create a short answer question // Create a short answer question
$sa = test_question_maker::make_a_shortanswer_question(); $sa = test_question_maker::make_question('shortanswer');
$this->start_attempt_at_question($sa, 'adaptive'); $this->start_attempt_at_question($sa, 'adaptive');


// Check the initial state. // Check the initial state.
Expand Down Expand Up @@ -446,7 +446,7 @@ public function test_adaptive_shortanswer_invalid_after_complete() {
public function test_adaptive_shortanswer_try_to_submit_blank() { public function test_adaptive_shortanswer_try_to_submit_blank() {


// Create a short answer question with correct answer true. // Create a short answer question with correct answer true.
$sa = test_question_maker::make_a_shortanswer_question(); $sa = test_question_maker::make_question('shortanswer');
$this->start_attempt_at_question($sa, 'adaptive'); $this->start_attempt_at_question($sa, 'adaptive');


// Check the initial state. // Check the initial state.
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function test_immediatecbm_feedback_multichoice_try_to_submit_blank() {
public function test_immediatecbm_feedback_shortanswer_try_to_submit_no_certainty() { public function test_immediatecbm_feedback_shortanswer_try_to_submit_no_certainty() {


// Create a short answer question with correct answer true. // Create a short answer question with correct answer true.
$sa = test_question_maker::make_a_shortanswer_question(); $sa = test_question_maker::make_question('shortanswer');
$this->start_attempt_at_question($sa, 'immediatecbm'); $this->start_attempt_at_question($sa, 'immediatecbm');


// Check the initial state. // Check the initial state.
Expand Down
6 changes: 3 additions & 3 deletions question/behaviour/interactive/simpletest/testwalkthrough.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function test_interactive_finish_when_try_again_showing() {
public function test_interactive_shortanswer_try_to_submit_blank() { public function test_interactive_shortanswer_try_to_submit_blank() {


// Create a short answer question. // Create a short answer question.
$sa = test_question_maker::make_a_shortanswer_question(); $sa = test_question_maker::make_question('shortanswer');
$sa->hints = array( $sa->hints = array(
new question_hint(0, 'This is the first hint.', FORMAT_HTML), new question_hint(0, 'This is the first hint.', FORMAT_HTML),
new question_hint(0, 'This is the second hint.', FORMAT_HTML), new question_hint(0, 'This is the second hint.', FORMAT_HTML),
Expand Down Expand Up @@ -420,7 +420,7 @@ public function test_interactive_feedback_multichoice_multiple_reset() {


public function test_interactive_regrade_changing_num_tries_leaving_open() { public function test_interactive_regrade_changing_num_tries_leaving_open() {
// Create a multichoice multiple question. // Create a multichoice multiple question.
$q = test_question_maker::make_a_shortanswer_question(); $q = test_question_maker::make_question('shortanswer');
$q->hints = array( $q->hints = array(
new question_hint_with_parts(0, 'This is the first hint.', FORMAT_HTML, true, true), new question_hint_with_parts(0, 'This is the first hint.', FORMAT_HTML, true, true),
new question_hint_with_parts(0, 'This is the second hint.', FORMAT_HTML, true, true), new question_hint_with_parts(0, 'This is the second hint.', FORMAT_HTML, true, true),
Expand Down Expand Up @@ -453,7 +453,7 @@ public function test_interactive_regrade_changing_num_tries_leaving_open() {


public function test_interactive_regrade_changing_num_tries_finished() { public function test_interactive_regrade_changing_num_tries_finished() {
// Create a multichoice multiple question. // Create a multichoice multiple question.
$q = test_question_maker::make_a_shortanswer_question(); $q = test_question_maker::make_question('shortanswer');
$q->hints = array( $q->hints = array(
new question_hint_with_parts(0, 'This is the first hint.', FORMAT_HTML, true, true), new question_hint_with_parts(0, 'This is the first hint.', FORMAT_HTML, true, true),
new question_hint_with_parts(0, 'This is the second hint.', FORMAT_HTML, true, true), new question_hint_with_parts(0, 'This is the second hint.', FORMAT_HTML, true, true),
Expand Down
23 changes: 0 additions & 23 deletions question/engine/simpletest/helpers.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -340,29 +340,6 @@ public static function make_a_matching_question() {
return $match; return $match;
} }


/**
* Makes a shortanswer question with correct ansewer 'frog', partially
* correct answer 'toad' and defaultmark 1.
* @return qtype_shortanswer_question
*/
public static function make_a_shortanswer_question() {
question_bank::load_question_definition_classes('shortanswer');
$sa = new qtype_shortanswer_question();
self::initialise_a_question($sa);
$sa->name = 'Short answer question';
$sa->questiontext = 'Name an amphibian: __________';
$sa->generalfeedback = 'Generalfeedback: frog or toad would have been OK.';
$sa->usecase = false;
$sa->answers = array(
13 => new question_answer(13, 'frog', 1.0, 'Frog is a very good answer.', FORMAT_HTML),
14 => new question_answer(14, 'toad', 0.8, 'Toad is an OK good answer.', FORMAT_HTML),
15 => new question_answer(15, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
);
$sa->qtype = question_bank::get_qtype('shortanswer');

return $sa;
}

/** /**
* Makes a truefalse question with correct ansewer true, defaultmark 1. * Makes a truefalse question with correct ansewer true, defaultmark 1.
* @return qtype_essay_question * @return qtype_essay_question
Expand Down
133 changes: 133 additions & 0 deletions question/type/shortanswer/simpletest/helper.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Test helpers for the shortanswer question type.
*
* @package qtype_shortanswer
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/


defined('MOODLE_INTERNAL') || die();


/**
* Test helper class for the shortanswer question type.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_shortanswer_test_helper extends question_test_helper {
public function get_test_questions() {
return array('frogtoad', 'frogonly');
}

/**
* Makes a shortanswer question with correct ansewer 'frog', partially
* correct answer 'toad' and defaultmark 1. This question also has a
* '*' match anything answer.
* @return qtype_shortanswer_question
*/
public function make_shortanswer_question_frogtoad() {
question_bank::load_question_definition_classes('shortanswer');
$sa = new qtype_shortanswer_question();
test_question_maker::initialise_a_question($sa);
$sa->name = 'Short answer question';
$sa->questiontext = 'Name an amphibian: __________';
$sa->generalfeedback = 'Generalfeedback: frog or toad would have been OK.';
$sa->usecase = false;
$sa->answers = array(
13 => new question_answer(13, 'frog', 1.0, 'Frog is a very good answer.', FORMAT_HTML),
14 => new question_answer(14, 'toad', 0.8, 'Toad is an OK good answer.', FORMAT_HTML),
15 => new question_answer(15, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
);
$sa->qtype = question_bank::get_qtype('shortanswer');

return $sa;
}

/**
* Gets the question data for a shortanswer question with with correct
* ansewer 'frog', partially correct answer 'toad' and defaultmark 1.
* This question also has a '*' match anything answer.
* @return stdClass
*/
public function get_shortanswer_question_data_frogtoad() {
$qdata = new stdClass();
test_question_maker::initialise_question_data($qdata);

$qdata->qtype = 'shortanswer';
$qdata->name = 'Short answer question';
$qdata->questiontext = 'Name an amphibian: __________';
$qdata->generalfeedback = 'Generalfeedback: frog or toad would have been OK.';

$qdata->options = new stdClass();
$qdata->options->usecase = false;
$qdata->options->answers = array(
13 => new question_answer(13, 'frog', 1.0, 'Frog is a very good answer.', FORMAT_HTML),
14 => new question_answer(14, 'toad', 0.8, 'Toad is an OK good answer.', FORMAT_HTML),
15 => new question_answer(15, '*', 0.0, 'That is a bad answer.', FORMAT_HTML),
);

return $qdata;
}

/**
* Makes a shortanswer question with just the correct ansewer 'frog', and
* no other answer matching.
* @return qtype_shortanswer_question
*/
public function make_shortanswer_question_frogonly() {
question_bank::load_question_definition_classes('shortanswer');
$sa = new qtype_shortanswer_question();
test_question_maker::initialise_a_question($sa);
$sa->name = 'Short answer question';
$sa->questiontext = 'Name the best amphibian: __________';
$sa->generalfeedback = 'Generalfeedback: you should have said frog.';
$sa->usecase = false;
$sa->answers = array(
13 => new question_answer(13, 'frog', 1.0, 'Frog is right.', FORMAT_HTML),
);
$sa->qtype = question_bank::get_qtype('shortanswer');

return $sa;
}

/**
* Gets the question data for a shortanswer questionwith just the correct
* ansewer 'frog', and no other answer matching.
* @return stdClass
*/
public function get_shortanswer_question_data_frogonly() {
$qdata = new stdClass();
test_question_maker::initialise_question_data($qdata);

$qdata->qtype = 'shortanswer';
$qdata->name = 'Short answer question';
$qdata->questiontext = 'Name the best amphibian: __________';
$qdata->generalfeedback = 'Generalfeedback: you should have said frog.';

$qdata->options = new stdClass();
$qdata->options->usecase = false;
$qdata->options->answers = array(
13 => new question_answer(13, 'frog', 1.0, 'Frog is right.', FORMAT_HTML),
);

return $qdata;
}
}
16 changes: 8 additions & 8 deletions question/type/shortanswer/simpletest/testquestion.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@


defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();


require_once($CFG->dirroot . '/question/type/shortanswer/question.php');
require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php'); require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');
require_once($CFG->dirroot . '/question/type/shortanswer/question.php');




/** /**
Expand Down Expand Up @@ -108,7 +108,7 @@ public function test_compare_string_with_wildcard() {
} }


public function test_is_complete_response() { public function test_is_complete_response() {
$question = test_question_maker::make_a_shortanswer_question(); $question = test_question_maker::make_question('shortanswer');


$this->assertFalse($question->is_complete_response(array())); $this->assertFalse($question->is_complete_response(array()));
$this->assertFalse($question->is_complete_response(array('answer' => ''))); $this->assertFalse($question->is_complete_response(array('answer' => '')));
Expand All @@ -118,7 +118,7 @@ public function test_is_complete_response() {
} }


public function test_is_gradable_response() { public function test_is_gradable_response() {
$question = test_question_maker::make_a_shortanswer_question(); $question = test_question_maker::make_question('shortanswer');


$this->assertFalse($question->is_gradable_response(array())); $this->assertFalse($question->is_gradable_response(array()));
$this->assertFalse($question->is_gradable_response(array('answer' => ''))); $this->assertFalse($question->is_gradable_response(array('answer' => '')));
Expand All @@ -128,7 +128,7 @@ public function test_is_gradable_response() {
} }


public function test_grading() { public function test_grading() {
$question = test_question_maker::make_a_shortanswer_question(); $question = test_question_maker::make_question('shortanswer');


$this->assertEqual(array(0, question_state::$gradedwrong), $this->assertEqual(array(0, question_state::$gradedwrong),
$question->grade_response(array('answer' => 'x'))); $question->grade_response(array('answer' => 'x')));
Expand All @@ -139,26 +139,26 @@ public function test_grading() {
} }


public function test_get_correct_response() { public function test_get_correct_response() {
$question = test_question_maker::make_a_shortanswer_question(); $question = test_question_maker::make_question('shortanswer');


$this->assertEqual(array('answer' => 'frog'), $this->assertEqual(array('answer' => 'frog'),
$question->get_correct_response()); $question->get_correct_response());
} }


public function test_get_question_summary() { public function test_get_question_summary() {
$sa = test_question_maker::make_a_shortanswer_question(); $sa = test_question_maker::make_question('shortanswer');
$qsummary = $sa->get_question_summary(); $qsummary = $sa->get_question_summary();
$this->assertEqual('Name an amphibian: __________', $qsummary); $this->assertEqual('Name an amphibian: __________', $qsummary);
} }


public function test_summarise_response() { public function test_summarise_response() {
$sa = test_question_maker::make_a_shortanswer_question(); $sa = test_question_maker::make_question('shortanswer');
$summary = $sa->summarise_response(array('answer' => 'dog')); $summary = $sa->summarise_response(array('answer' => 'dog'));
$this->assertEqual('dog', $summary); $this->assertEqual('dog', $summary);
} }


public function test_classify_response() { public function test_classify_response() {
$sa = test_question_maker::make_a_shortanswer_question(); $sa = test_question_maker::make_question('shortanswer');
$sa->start_attempt(new question_attempt_step(), 1); $sa->start_attempt(new question_attempt_step(), 1);


$this->assertEqual(array( $this->assertEqual(array(
Expand Down
21 changes: 10 additions & 11 deletions question/type/shortanswer/simpletest/testquestiontype.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();


require_once($CFG->dirroot . '/question/type/shortanswer/questiontype.php'); require_once($CFG->dirroot . '/question/type/shortanswer/questiontype.php');
require_once($CFG->dirroot . '/question/engine/simpletest/helpers.php');




/** /**
Expand All @@ -52,12 +53,7 @@ public function tearDown() {
} }


protected function get_test_question_data() { protected function get_test_question_data() {
$q = new stdClass(); return test_question_maker::get_question_data('shortanswer');
$q->id = 1;
$q->options->answers[1] = (object) array('answer' => 'frog', 'fraction' => 1);
$q->options->answers[2] = (object) array('answer' => '*', 'fraction' => 0.1);

return $q;
} }


public function test_name() { public function test_name() {
Expand All @@ -69,18 +65,21 @@ public function test_can_analyse_responses() {
} }


public function test_get_random_guess_score() { public function test_get_random_guess_score() {
$q = $this->get_test_question_data(); $q = test_question_maker::get_question_data('shortanswer');
$q->options->answers[15]->fraction = 0.1;
$this->assertEqual(0.1, $this->qtype->get_random_guess_score($q)); $this->assertEqual(0.1, $this->qtype->get_random_guess_score($q));
} }


public function test_get_possible_responses() { public function test_get_possible_responses() {
$q = $this->get_test_question_data(); $q = test_question_maker::get_question_data('shortanswer');


$this->assertEqual(array( $this->assertEqual(array(
$q->id => array( $q->id => array(
1 => new question_possible_response('frog', 1), 13 => new question_possible_response('frog', 1),
2 => new question_possible_response('*', 0.1), 14 => new question_possible_response('toad', 0.8),
null => question_possible_response::no_response()), 15 => new question_possible_response('*', 0),
null => question_possible_response::no_response()
),
), $this->qtype->get_possible_responses($q)); ), $this->qtype->get_possible_responses($q));
} }
} }
2 changes: 1 addition & 1 deletion question/type/truefalse/simpletest/testquestiontype.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();


require_once($CFG->dirroot . '/question/type//truefalse/questiontype.php'); require_once($CFG->dirroot . '/question/type/truefalse/questiontype.php');




/** /**
Expand Down

0 comments on commit 2a040c2

Please sign in to comment.