MDL-47494 ddwtos: Merge pull request #4 from jamiepratt/wip_unittestconversions

NOBUG changed unit tests to use PHPUnit
This commit is contained in:
Tim Hunt
2012-06-20 04:21:55 -07:00
4 changed files with 67 additions and 59 deletions
@@ -19,35 +19,37 @@
*
* @package qtype
* @subpackage ddwtos
* @copyright 2010 The Open University
* @copyright 2012 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/simpletest/helpers.php');
require_once($CFG->dirroot . '/question/type/ddwtos/simpletest/helper.php');
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
require_once($CFG->dirroot . '/question/type/ddwtos/tests/helper.php');
/**
* Unit tests for the matching question definition class.
*
* @copyright 2009 The Open University
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @group qtype_ddwtos
*/
class qtype_ddwtos_question_test extends UnitTestCase {
class qtype_ddwtos_question_test extends basic_testcase {
public function test_get_question_summary() {
$dd = test_question_maker::make_question('ddwtos');
$this->assertEqual('The [[1]] brown [[2]] jumped over the [[3]] dog.; ' .
$this->assertEquals('The [[1]] brown [[2]] jumped over the [[3]] dog.; ' .
'[[1]] -> {quick / slow}; [[2]] -> {fox / dog}; [[3]] -> {lazy / assiduous}',
$dd->get_question_summary());
}
public function test_get_question_summary_maths() {
$dd = test_question_maker::make_question('ddwtos', 'maths');
$this->assertEqual('Fill in the operators to make this equation work: ' .
$this->assertEquals('Fill in the operators to make this equation work: ' .
'7 [[1]] 11 [[2]] 13 [[1]] 17 [[2]] 19 = 3; [[1]] -> {+ / - / * / /}',
$dd->get_question_summary());
}
@@ -57,7 +59,7 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd->shufflechoices = false;
$dd->start_attempt(new question_attempt_step(), 1);
$this->assertEqual('{quick} {fox} {lazy}',
$this->assertEquals('{quick} {fox} {lazy}',
$dd->summarise_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
}
@@ -66,18 +68,18 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd->shufflechoices = false;
$dd->start_attempt(new question_attempt_step(), 1);
$this->assertEqual('{+} {-} {+} {-}',
$this->assertEquals('{+} {-} {+} {-}',
$dd->summarise_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
}
public function test_get_random_guess_score() {
$dd = test_question_maker::make_question('ddwtos');
$this->assertEqual(0.5, $dd->get_random_guess_score());
$this->assertEquals(0.5, $dd->get_random_guess_score());
}
public function test_get_random_guess_score_maths() {
$dd = test_question_maker::make_question('ddwtos', 'maths');
$this->assertEqual(0.25, $dd->get_random_guess_score());
$this->assertEquals(0.25, $dd->get_random_guess_score());
}
public function test_get_right_choice_for() {
@@ -85,8 +87,8 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd->shufflechoices = false;
$dd->start_attempt(new question_attempt_step(), 1);
$this->assertEqual(1, $dd->get_right_choice_for(1));
$this->assertEqual(1, $dd->get_right_choice_for(2));
$this->assertEquals(1, $dd->get_right_choice_for(1));
$this->assertEquals(1, $dd->get_right_choice_for(2));
}
public function test_get_right_choice_for_maths() {
@@ -94,8 +96,8 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd->shufflechoices = false;
$dd->start_attempt(new question_attempt_step(), 1);
$this->assertEqual(1, $dd->get_right_choice_for(1));
$this->assertEqual(2, $dd->get_right_choice_for(2));
$this->assertEquals(1, $dd->get_right_choice_for(1));
$this->assertEquals(2, $dd->get_right_choice_for(2));
}
public function test_clear_wrong_from_response() {
@@ -104,7 +106,7 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd->start_attempt(new question_attempt_step(), 1);
$initialresponse = array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1');
$this->assertEqual(array('p1' => '1', 'p2' => '0', 'p3' => '1', 'p4' => '0'),
$this->assertEquals(array('p1' => '1', 'p2' => '0', 'p3' => '1', 'p4' => '0'),
$dd->clear_wrong_from_response($initialresponse));
}
@@ -113,9 +115,9 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd->shufflechoices = false;
$dd->start_attempt(new question_attempt_step(), 1);
$this->assertEqual(array(2, 3),
$this->assertEquals(array(2, 3),
$dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '2')));
$this->assertEqual(array(3, 3),
$this->assertEquals(array(3, 3),
$dd->get_num_parts_right(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
}
@@ -124,7 +126,7 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd->shufflechoices = false;
$dd->start_attempt(new question_attempt_step(), 1);
$this->assertEqual(array(2, 4),
$this->assertEquals(array(2, 4),
$dd->get_num_parts_right(array(
'p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
}
@@ -133,7 +135,7 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd = test_question_maker::make_question('ddwtos');
$dd->start_attempt(new question_attempt_step(), 1);
$this->assertEqual(array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT),
$this->assertEquals(array('p1' => PARAM_INT, 'p2' => PARAM_INT, 'p3' => PARAM_INT),
$dd->get_expected_data());
}
@@ -142,7 +144,7 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd->shufflechoices = false;
$dd->start_attempt(new question_attempt_step(), 1);
$this->assertEqual(array('p1' => '1', 'p2' => '1', 'p3' => '1'),
$this->assertEquals(array('p1' => '1', 'p2' => '1', 'p3' => '1'),
$dd->get_correct_response());
}
@@ -151,7 +153,7 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd->shufflechoices = false;
$dd->start_attempt(new question_attempt_step(), 1);
$this->assertEqual(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
$this->assertEquals(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2'),
$dd->get_correct_response());
}
@@ -210,11 +212,11 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd->shufflechoices = false;
$dd->start_attempt(new question_attempt_step(), 1);
$this->assertEqual(array(1, question_state::$gradedright),
$this->assertEquals(array(1, question_state::$gradedright),
$dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1')));
$this->assertEqual(array(1/3, question_state::$gradedpartial),
$this->assertEquals(array(1/3, question_state::$gradedpartial),
$dd->grade_response(array('p1' => '1')));
$this->assertEqual(array(0, question_state::$gradedwrong),
$this->assertEquals(array(0, question_state::$gradedwrong),
$dd->grade_response(array('p1' => '2', 'p2' => '2', 'p3' => '2')));
}
@@ -223,11 +225,11 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd->shufflechoices = false;
$dd->start_attempt(new question_attempt_step(), 1);
$this->assertEqual(array(1, question_state::$gradedright),
$this->assertEquals(array(1, question_state::$gradedright),
$dd->grade_response(array('p1' => '1', 'p2' => '2', 'p3' => '1', 'p4' => '2')));
$this->assertEqual(array(0.5, question_state::$gradedpartial),
$this->assertEquals(array(0.5, question_state::$gradedpartial),
$dd->grade_response(array('p1' => '1', 'p2' => '1', 'p3' => '1', 'p4' => '1')));
$this->assertEqual(array(0, question_state::$gradedwrong),
$this->assertEquals(array(0, question_state::$gradedwrong),
$dd->grade_response(array('p1' => '0', 'p2' => '1', 'p3' => '2', 'p4' => '1')));
}
@@ -236,12 +238,12 @@ class qtype_ddwtos_question_test extends UnitTestCase {
$dd->shufflechoices = false;
$dd->start_attempt(new question_attempt_step(), 1);
$this->assertEqual(array(
$this->assertEquals(array(
1 => new question_classified_response(1, 'quick', 1/3),
2 => new question_classified_response(2, 'dog', 0),
3 => new question_classified_response(1, 'lazy', 1/3),
), $dd->classify_response(array('p1' => '1', 'p2' => '2', 'p3' => '1')));
$this->assertEqual(array(
$this->assertEquals(array(
1 => question_classified_response::no_response(),
2 => new question_classified_response(1, 'fox', 1/3),
3 => new question_classified_response(2, 'assiduous', 0),
@@ -19,37 +19,39 @@
*
* @package qtype
* @subpackage ddwtos
* @copyright 2010 The Open University
* @copyright 2012 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/simpletest/helpers.php');
require_once($CFG->dirroot . '/question/type/ddwtos/simpletest/helper.php');
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
require_once($CFG->dirroot . '/question/type/ddwtos/tests/helper.php');
/**
* Unit tests for the drag-and-drop words into sentences question definition class.
*
* @copyright 2010 The Open University
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @group qtype_ddwtos
*/
class qtype_ddwtos_test extends UnitTestCase {
class qtype_ddwtos_test extends question_testcase {
/** @var qtype_ddwtos instance of the question type class to test. */
protected $qtype;
public function setUp() {
protected function setUp() {
$this->qtype = question_bank::get_qtype('ddwtos');;
}
public function tearDown() {
protected function tearDown() {
$this->qtype = null;
}
public function assert_same_xml($expectedxml, $xml) {
$this->assertEqual(str_replace("\r\n", "\n", $expectedxml),
$this->assertEquals(str_replace("\r\n", "\n", $expectedxml),
str_replace("\r\n", "\n", $xml));
}
@@ -83,6 +85,7 @@ class qtype_ddwtos_test extends UnitTestCase {
$dd->generalfeedback = 'This sentence uses each letter of the alphabet.';
$dd->qtype = 'ddwtos';
$dd->options = new stdClass();
$dd->options->shuffleanswers = true;
test_question_maker::set_standard_combined_feedback_fields($dd->options);
@@ -106,7 +109,7 @@ class qtype_ddwtos_test extends UnitTestCase {
}
public function test_name() {
$this->assertEqual($this->qtype->name(), 'ddwtos');
$this->assertEquals($this->qtype->name(), 'ddwtos');
}
public function test_can_analyse_responses() {
@@ -122,18 +125,18 @@ class qtype_ddwtos_test extends UnitTestCase {
$q = $this->qtype->make_question($qdata);
$this->assertEqual($expected, $q);
$this->assertEquals($expected, $q);
}
public function test_get_random_guess_score() {
$q = $this->get_test_question_data();
$this->assertWithinMargin(0.5, $this->qtype->get_random_guess_score($q), 0.0000001);
$this->assertEquals(0.5, $this->qtype->get_random_guess_score($q), '', 0.0000001);
}
public function test_get_possible_responses() {
$q = $this->get_test_question_data();
$this->assertEqual(array(
$this->assertEquals(array(
1 => array(
1 => new question_possible_response('quick', 1/3),
2 => new question_possible_response('slow', 0),
@@ -236,8 +239,8 @@ class qtype_ddwtos_test extends UnitTestCase {
$expectedq->hintshownumcorrect = array(true, true);
$expectedq->hintclearwrong = array(false, true);
$this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
$this->assertEqual($expectedq->hint, $q->hint);
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
$this->assertEquals($expectedq->hint, $q->hint);
}
public function test_xml_import_legacy() {
@@ -428,9 +431,9 @@ class qtype_ddwtos_test extends UnitTestCase {
$expectedq->hintshownumcorrect = array(true, true);
$expectedq->hintclearwrong = array(false, true);
$this->assert(new CheckSpecifiedFieldsExpectation($expectedq), $q);
$this->assertEqual($expectedq->choices, $q->choices);
$this->assertEqual($expectedq->hint, $q->hint);
$this->assert(new question_check_specified_fields_expectation($expectedq), $q);
$this->assertEquals($expectedq->choices, $q->choices);
$this->assertEquals($expectedq->hint, $q->hint);
}
public function test_xml_export() {
@@ -448,6 +451,7 @@ class qtype_ddwtos_test extends UnitTestCase {
$qdata->penalty = 0.3333333;
$qdata->hidden = 0;
$qdata->options = new stdClass();
$qdata->options->shuffleanswers = 1;
$qdata->options->correctfeedback = '<p>Your answer is correct.</p>';
$qdata->options->correctfeedbackformat = FORMAT_MOODLE;
@@ -19,22 +19,24 @@
*
* @package qtype
* @subpackage ddwtos
* @copyright 2010 The Open University
* @copyright 2012 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/simpletest/helpers.php');
require_once($CFG->dirroot . '/question/type/ddwtos/simpletest/helper.php');
require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
require_once($CFG->dirroot . '/question/type/ddwtos/tests/helper.php');
/**
* Unit tests for the drag-and-drop words into sentences question type.
*
* @copyright 2010 The Open University
* @copyright 2012 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @group qtype_ddwtos
*/
class qtype_ddwtos_walkthrough_test extends qbehaviour_walkthrough_test_base {
@@ -50,7 +52,7 @@ class qtype_ddwtos_walkthrough_test extends qbehaviour_walkthrough_test_base {
}
$expectedattrs['class'] = $class;
return new ContainsTagWithAttributes('span', $expectedattrs);
return new question_contains_tag_with_attributes('span', $expectedattrs);
}
public function test_interactive_behaviour() {
@@ -117,7 +119,7 @@ class qtype_ddwtos_walkthrough_test extends qbehaviour_walkthrough_test_base {
$this->get_contains_submit_button_expectation(false),
$this->get_contains_try_again_button_expectation(true),
$this->get_does_not_contain_correctness_expectation(),
new PatternExpectation('/' .
new question_pattern_expectation('/' .
preg_quote(get_string('notcomplete', 'qbehaviour_interactive')) . '/'),
$this->get_contains_hint_expectation('This is the first hint'));
@@ -375,7 +377,7 @@ class qtype_ddwtos_walkthrough_test extends qbehaviour_walkthrough_test_base {
// Check the initial state.
$this->check_current_state(question_state::$todo);
$this->check_current_mark(null);
$this->assertEqual('interactivecountback',
$this->assertEquals('interactivecountback',
$this->quba->get_question_attempt($this->slot)->get_behaviour_name());
$this->check_current_output(
$this->get_contains_drop_box_expectation('1', 1, false),
@@ -406,7 +408,7 @@ class qtype_ddwtos_walkthrough_test extends qbehaviour_walkthrough_test_base {
$this->get_contains_submit_button_expectation(false),
$this->get_contains_try_again_button_expectation(true),
$this->get_does_not_contain_correctness_expectation(),
new PatternExpectation('/' .
new question_pattern_expectation('/' .
preg_quote(get_string('notcomplete', 'qbehaviour_interactive')) . '/'),
$this->get_contains_hint_expectation('This is the first hint'),
$this->get_contains_num_parts_correct(2),
@@ -420,7 +422,7 @@ class qtype_ddwtos_walkthrough_test extends qbehaviour_walkthrough_test_base {
// Check that extract responses will return the reset data.
$prefix = $this->quba->get_field_prefix($this->slot);
$this->assertEqual(array('p1' => '1', 'p2' => '1'),
$this->assertEquals(array('p1' => '1', 'p2' => '1'),
$this->quba->extract_responses($this->slot,
array($prefix . 'p1' => '1', $prefix . 'p2' => '1', '-tryagain' => 1)));
@@ -460,7 +462,7 @@ class qtype_ddwtos_walkthrough_test extends qbehaviour_walkthrough_test_base {
$this->get_contains_submit_button_expectation(false),
$this->get_contains_try_again_button_expectation(true),
$this->get_does_not_contain_correctness_expectation(),
new PatternExpectation('/' .
new question_pattern_expectation('/' .
preg_quote(get_string('notcomplete', 'qbehaviour_interactive')) . '/'),
$this->get_contains_hint_expectation('This is the second hint'),
$this->get_contains_num_parts_correct(2),
@@ -746,7 +748,7 @@ class qtype_ddwtos_walkthrough_test extends qbehaviour_walkthrough_test_base {
// Verify.
$this->displayoptions->rightanswer = question_display_options::VISIBLE;
$this->assertEqual('{quick} {fox} {lazy}', $dd->get_right_answer_summary());
$this->assertEquals('{quick} {fox} {lazy}', $dd->get_right_answer_summary());
$this->check_current_state(question_state::$gradedright);
$this->check_current_mark(3);
$this->check_current_output(
@@ -754,7 +756,7 @@ class qtype_ddwtos_walkthrough_test extends qbehaviour_walkthrough_test_base {
$this->get_contains_drop_box_expectation('2', 2, true, 'correct'),
$this->get_contains_drop_box_expectation('3', 3, true, 'correct'),
$this->get_contains_correct_expectation(),
new PatternExpectation('/' .
new question_pattern_expectation('/' .
preg_quote('The [quick] brown [fox] jumped over the [lazy] dog.') . '/'));
}
}