public function test_question_saving_sumwithvariants() { $this->resetAfterTest(true); $this->setAdminUser(); $questiondata = test_question_maker::get_question_data('calculatedsimple', 'sumwithvariants'); $formdata = test_question_maker::get_question_form_data('calculatedsimple', 'sumwithvariants'); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(array()); $formdata->category = "{$cat->id},{$cat->contextid}"; qtype_calculatedsimple_edit_form::mock_submit((array) $formdata); $form = qtype_calculatedsimple_test_helper::get_question_editing_form($cat, $questiondata); $this->assertTrue($form->is_validated()); $fromform = $form->get_data(); $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); $actualquestiondata = end($actualquestionsdata); foreach ($questiondata as $property => $value) { if (!in_array($property, array('id', 'version', 'timemodified', 'timecreated', 'options'))) { $this->assertAttributeEquals($value, $property, $actualquestiondata); } } foreach ($questiondata->options as $optionname => $value) { if ($optionname != 'answers') { $this->assertAttributeEquals($value, $optionname, $actualquestiondata->options); } } foreach ($questiondata->options->answers as $answer) { $actualanswer = array_shift($actualquestiondata->options->answers); foreach ($answer as $ansproperty => $ansvalue) { if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) { $this->assertAttributeEquals($ansvalue, $ansproperty, $actualanswer); } } } $datasetloader = new qtype_calculated_dataset_loader($actualquestiondata->id); $this->assertEquals(10, $datasetloader->get_number_of_items()); for ($itemno = 1; $itemno <= 10; $itemno++) { $item = $datasetloader->get_values($itemno); $this->assertEquals($formdata->number[($itemno - 1) * 2 + 2], $item['a']); $this->assertEquals($formdata->number[($itemno - 1) * 2 + 1], $item['b']); } }
public function test_question_saving_true() { $this->resetAfterTest(true); $this->setAdminUser(); $questiondata = test_question_maker::get_question_data('truefalse'); $formdata = test_question_maker::get_question_form_data('truefalse'); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(array()); $formdata->category = "{$cat->id},{$cat->contextid}"; qtype_truefalse_edit_form::mock_submit((array) $formdata); $form = qtype_truefalse_test_helper::get_question_editing_form($cat, $questiondata); $this->assertTrue($form->is_validated()); $fromform = $form->get_data(); $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); $actualquestiondata = end($actualquestionsdata); foreach ($questiondata as $property => $value) { if (!in_array($property, array('options'))) { $this->assertAttributeEquals($value, $property, $actualquestiondata); } } foreach ($questiondata->options as $optionname => $value) { if (!in_array($optionname, array('trueanswer', 'falseanswer', 'answers'))) { $this->assertAttributeEquals($value, $optionname, $actualquestiondata->options); } } $answerindexes = array(); foreach ($questiondata->options->answers as $ansindex => $answer) { $actualanswer = array_shift($actualquestiondata->options->answers); foreach ($answer as $ansproperty => $ansvalue) { // This question does not use 'answerformat', will ignore it. if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) { $this->assertAttributeEquals($ansvalue, $ansproperty, $actualanswer); } } $answerindexes[$answer->answer] = $ansindex; } $this->assertEquals($questiondata->options->trueanswer, $answerindexes['True']); $this->assertEquals($questiondata->options->falseanswer, $answerindexes['False']); }
public function test_question_saving() { $this->resetAfterTest(true); $this->setAdminUser(); $questiondata = test_question_maker::get_question_data('linkerdesc'); $formdata = test_question_maker::get_question_form_data('linkerdesc'); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(array()); $formdata->category = "{$cat->id},{$cat->contextid}"; qtype_linkerdesc_edit_form::mock_submit((array) $formdata); $form = qtype_linkerdesc_test_helper::get_question_editing_form($cat, $questiondata); $this->assertTrue($form->is_validated()); $fromform = $form->get_data(); $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); $actualquestiondata = end($actualquestionsdata); foreach ($questiondata as $property => $value) { if (!in_array($property, array('id', 'version', 'timemodified', 'timecreated'))) { $this->assertAttributeEquals($value, $property, $actualquestiondata); } } }
public function test_export_multianswer() { $qdata = test_question_maker::get_question_data('multianswer', 'twosubq'); $exporter = new qformat_xml(); $xml = $exporter->writequestion($qdata); $expectedxml = '<!-- question: 0 --> <question type="cloze"> <name> <text>Simple multianswer</text> </name> <questiontext format="html"> <text><![CDATA[Complete this opening line of verse: "The {1:SHORTANSWER:Dog#Wrong, silly!~=Owl#Well done!~*#Wrong answer} and the {1:MULTICHOICE:Bow-wow#You seem to have a dog obsessions!~Wiggly worm#Now you are just being ridiculous!~=Pussy-cat#Well done!} went to sea".]]></text> </questiontext> <generalfeedback format="html"> <text><![CDATA[General feedback: It\'s from "The Owl and the Pussy-cat" by Lear: "The owl and the pussycat went to sea]]></text> </generalfeedback> <penalty>0.3333333</penalty> <hidden>0</hidden> <hint format="html"> <text>Hint 1</text> </hint> <hint format="html"> <text>Hint 2</text> </hint> </question> '; $this->assert_same_xml($expectedxml, $xml); }
public function test_get_possible_responses_no_star() { $q = test_question_maker::get_question_data('calculated'); unset($q->options->answers[17]); $this->assertEqual(array( $q->id => array( 13 => new question_possible_response( $this->get_possible_response('{a} + {b}', 0.001, 'nominal'), 1), 14 => new question_possible_response( $this->get_possible_response('{a} - {b}', 0.001, 'nominal'), 0), 0 => new question_possible_response( get_string('didnotmatchanyanswer', 'question'), 0), null => question_possible_response::no_response() ), ), $this->qtype->get_possible_responses($q)); }
public function test_export_multianswer_withdollars() { $qdata = test_question_maker::get_question_data('multianswer', 'dollarsigns'); $exporter = new qformat_xml(); $xml = $exporter->writequestion($qdata); $expectedxml = '<!-- question: 0 --> <question type="cloze"> <name> <text>Multianswer with $s</text> </name> <questiontext format="html"> <text>Which is the right order? {1:MULTICHOICE:=y,y,$3~$3,y,y}</text> </questiontext> <generalfeedback format="html"> <text></text> </generalfeedback> <penalty>0.3333333</penalty> <hidden>0</hidden> </question> '; $this->assert_same_xml($expectedxml, $xml); }
public function test_get_random_guess_score() { $q = test_question_maker::get_question_data('multianswer', 'twosubq'); $this->assertEquals(0.1666667, $this->qtype->get_random_guess_score($q), '', 0.0000001); }
public function test_question_saving_twosubq() { $this->resetAfterTest(true); $this->setAdminUser(); $questiondata = test_question_maker::get_question_data('multianswer'); $formdata = test_question_maker::get_question_form_data('multianswer'); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(array()); $formdata->category = "{$cat->id},{$cat->contextid}"; qtype_multianswer_edit_form::mock_submit((array) $formdata); $form = qtype_multianswer_test_helper::get_question_editing_form($cat, $questiondata); $this->assertTrue($form->is_validated()); $fromform = $form->get_data(); $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); $actualquestiondata = end($actualquestionsdata); foreach ($questiondata as $property => $value) { if (!in_array($property, array('id', 'version', 'timemodified', 'timecreated', 'options', 'hints', 'stamp'))) { $this->assertAttributeEquals($value, $property, $actualquestiondata); } } foreach ($questiondata->options as $optionname => $value) { if ($optionname != 'questions') { $this->assertAttributeEquals($value, $optionname, $actualquestiondata->options); } } foreach ($questiondata->hints as $hint) { $actualhint = array_shift($actualquestiondata->hints); foreach ($hint as $property => $value) { if (!in_array($property, array('id', 'questionid', 'options'))) { $this->assertAttributeEquals($value, $property, $actualhint); } } } $this->assertObjectHasAttribute('questions', $actualquestiondata->options); $subqpropstoignore = array('id', 'category', 'parent', 'contextid', 'question', 'options', 'stamp', 'version', 'timemodified', 'timecreated'); foreach ($questiondata->options->questions as $subqno => $subq) { $actualsubq = $actualquestiondata->options->questions[$subqno]; foreach ($subq as $subqproperty => $subqvalue) { if (!in_array($subqproperty, $subqpropstoignore)) { $this->assertAttributeEquals($subqvalue, $subqproperty, $actualsubq); } } foreach ($subq->options as $optionname => $value) { if (!in_array($optionname, array('answers'))) { $this->assertAttributeEquals($value, $optionname, $actualsubq->options); } } foreach ($subq->options->answers as $answer) { $actualanswer = array_shift($actualsubq->options->answers); foreach ($answer as $ansproperty => $ansvalue) { // These questions do not use 'answerformat', will ignore it. if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) { $this->assertAttributeEquals($ansvalue, $ansproperty, $actualanswer); } } } } }
public function test_get_possible_responses_no_star() { $q = test_question_maker::get_question_data('shortanswer', 'frogonly'); $this->assertEquals(array( $q->id => array( 13 => new question_possible_response('frog', 1), 0 => new question_possible_response(get_string('didnotmatchanyanswer', 'question'), 0), null => question_possible_response::no_response() ), ), $this->qtype->get_possible_responses($q)); }
public function test_xml_export() { $qdata = test_question_maker::get_question_data('stack', 'test0'); $exporter = new qformat_xml(); $xml = $exporter->writequestion($qdata); $expectedxml = '<!-- question: 0 --> <question type="stack"> <name> <text>test-0</text> </name> <questiontext format="html"> <text>What is $1+1$? [[input:ans1]] [[validation:ans1]]</text> </questiontext> <generalfeedback format="html"> <text></text> </generalfeedback> <defaultgrade>1</defaultgrade> <penalty>0.3333333</penalty> <hidden>0</hidden> <questionvariables> <text></text> </questionvariables> <specificfeedback format="html"> <text>[[feedback:firsttree]]</text> </specificfeedback> <questionnote> <text></text> </questionnote> <questionsimplify>1</questionsimplify> <assumepositive>0</assumepositive> <prtcorrect format="html"> <text><![CDATA[<p>Correct answer, well done.</p>]]></text> </prtcorrect> <prtpartiallycorrect format="html"> <text><![CDATA[<p>Your answer is partially correct.</p>]]></text> </prtpartiallycorrect> <prtincorrect format="html"> <text><![CDATA[<p>Incorrect answer.</p>]]></text> </prtincorrect> <multiplicationsign>dot</multiplicationsign> <sqrtsign>1</sqrtsign> <complexno>i</complexno> <inversetrig>cos-1</inversetrig> <matrixparens>[</matrixparens> <variantsselectionseed></variantsselectionseed> <input> <name>ans1</name> <type>algebraic</type> <tans>2</tans> <boxsize>5</boxsize> <strictsyntax>1</strictsyntax> <insertstars>0</insertstars> <syntaxhint></syntaxhint> <forbidwords></forbidwords> <allowwords></allowwords> <forbidfloat>1</forbidfloat> <requirelowestterms>0</requirelowestterms> <checkanswertype>0</checkanswertype> <mustverify>1</mustverify> <showvalidation>1</showvalidation> <options></options> </input> <prt> <name>firsttree</name> <value>1</value> <autosimplify>1</autosimplify> <feedbackvariables> <text></text> </feedbackvariables> <node> <name>0</name> <answertest>EqualComAss</answertest> <sans>ans1</sans> <tans>2</tans> <testoptions></testoptions> <quiet>0</quiet> <truescoremode>=</truescoremode> <truescore>1</truescore> <truepenalty>0</truepenalty> <truenextnode>-1</truenextnode> <trueanswernote>firsttree-1-T</trueanswernote> <truefeedback format="html"> <text></text> </truefeedback> <falsescoremode>=</falsescoremode> <falsescore>0</falsescore> <falsepenalty>0</falsepenalty> <falsenextnode>-1</falsenextnode> <falseanswernote>firsttree-1-F</falseanswernote> <falsefeedback format="html"> <text></text> </falsefeedback> </node> </prt> <deployedseed>12345</deployedseed> <qtest> <testcase>1</testcase> <testinput> <name>ans1</name> <value>2</value> </testinput> <expected> <name>firsttree</name> <expectedscore>1</expectedscore> <expectedpenalty>0</expectedpenalty> <expectedanswernote>firsttree-1-T</expectedanswernote> </expected> </qtest> </question> '; $this->assert_same_xml($expectedxml, $xml); }
public function test_get_random_guess_score() { $q = test_question_maker::get_question_data('multianswer', 'twosubq'); $this->assertWithinMargin(0.1666667, $this->qtype->get_random_guess_score($q), 1.0E-7); }
public function test_question_saving_trims_answers() { $this->resetAfterTest(true); $this->setAdminUser(); $questiondata = test_question_maker::get_question_data('shortanswer'); $formdata = test_question_maker::get_question_form_data('shortanswer'); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(array()); $formdata->category = "{$cat->id},{$cat->contextid}"; $formdata->answer[0] = ' frog '; qtype_shortanswer_edit_form::mock_submit((array) $formdata); $form = qtype_shortanswer_test_helper::get_question_editing_form($cat, $questiondata); $this->assertTrue($form->is_validated()); $fromform = $form->get_data(); $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); $actualquestiondata = end($actualquestionsdata); $firstsavedanswer = reset($questiondata->options->answers); $this->assertEquals('frog', $firstsavedanswer->answer); }
public function test_question_saving_foursubq() { $this->resetAfterTest(true); $this->setAdminUser(); $questiondata = test_question_maker::get_question_data('match'); $formdata = test_question_maker::get_question_form_data('match'); $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); $cat = $generator->create_question_category(array()); $formdata->category = "{$cat->id},{$cat->contextid}"; qtype_match_edit_form::mock_submit((array) $formdata); $form = qtype_match_test_helper::get_question_editing_form($cat, $questiondata); $this->assertTrue($form->is_validated()); $fromform = $form->get_data(); $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); $actualquestiondata = end($actualquestionsdata); foreach ($questiondata as $property => $value) { if (!in_array($property, array('id', 'version', 'timemodified', 'timecreated', 'options', 'stamp'))) { $this->assertAttributeEquals($value, $property, $actualquestiondata); } } foreach ($questiondata->options as $optionname => $value) { if ($optionname != 'subquestions') { $this->assertAttributeEquals($value, $optionname, $actualquestiondata->options); } } $this->assertObjectHasAttribute('subquestions', $actualquestiondata->options); $subqpropstoignore = array('id'); foreach ($questiondata->options->subquestions as $subq) { $actualsubq = array_shift($actualquestiondata->options->subquestions); foreach ($subq as $subqproperty => $subqvalue) { if (!in_array($subqproperty, $subqpropstoignore)) { $this->assertAttributeEquals($subqvalue, $subqproperty, $actualsubq); } } } }