Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/question/engine/simpletest/testquestionattempt.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00030 defined('MOODLE_INTERNAL') || die();
00031 
00032 require_once(dirname(__FILE__) . '/../lib.php');
00033 require_once(dirname(__FILE__) . '/helpers.php');
00034 
00035 
00044 class question_attempt_test extends UnitTestCase {
00045     private $question;
00046     private $usageid;
00047     private $qa;
00048 
00049     public function setUp() {
00050         $this->question = test_question_maker::make_question('description');
00051         $this->question->defaultmark = 3;
00052         $this->usageid = 13;
00053         $this->qa = new question_attempt($this->question, $this->usageid);
00054     }
00055 
00056     public function tearDown() {
00057         $this->question = null;
00058         $this->useageid = null;
00059         $this->qa = null;
00060     }
00061 
00062     public function test_constructor_sets_maxmark() {
00063         $qa = new question_attempt($this->question, $this->usageid);
00064         $this->assertIdentical($this->question, $qa->get_question());
00065         $this->assertEqual(3, $qa->get_max_mark());
00066     }
00067 
00068     public function test_maxmark_beats_default_mark() {
00069         $qa = new question_attempt($this->question, $this->usageid, null, 2);
00070         $this->assertEqual(2, $qa->get_max_mark());
00071     }
00072 
00073     public function test_get_set_slot() {
00074         $this->qa->set_slot(7);
00075         $this->assertEqual(7, $this->qa->get_slot());
00076     }
00077 
00078     public function test_fagged_initially_false() {
00079         $this->assertEqual(false, $this->qa->is_flagged());
00080     }
00081 
00082     public function test_set_is_flagged() {
00083         $this->qa->set_flagged(true);
00084         $this->assertEqual(true, $this->qa->is_flagged());
00085     }
00086 
00087     public function test_get_qt_field_name() {
00088         $name = $this->qa->get_qt_field_name('test');
00089         $this->assertPattern('/^' . preg_quote($this->qa->get_field_prefix()) . '/', $name);
00090         $this->assertPattern('/_test$/', $name);
00091     }
00092 
00093     public function test_get_behaviour_field_name() {
00094         $name = $this->qa->get_behaviour_field_name('test');
00095         $this->assertPattern('/^' . preg_quote($this->qa->get_field_prefix()) . '/', $name);
00096         $this->assertPattern('/_-test$/', $name);
00097     }
00098 
00099     public function test_get_field_prefix() {
00100         $this->qa->set_slot(7);
00101         $name = $this->qa->get_field_prefix();
00102         $this->assertPattern('/' . preg_quote($this->usageid) . '/', $name);
00103         $this->assertPattern('/' . preg_quote($this->qa->get_slot()) . '/', $name);
00104     }
00105 
00106     public function test_get_submitted_var_not_present_var_returns_null() {
00107         $this->assertNull($this->qa->get_submitted_var(
00108                 'reallyunlikelyvariablename', PARAM_BOOL));
00109     }
00110 
00111     public function test_get_submitted_var_param_mark_not_present() {
00112         $this->assertNull($this->qa->get_submitted_var(
00113                 'name', question_attempt::PARAM_MARK, array()));
00114     }
00115 
00116     public function test_get_submitted_var_param_mark_blank() {
00117         $this->assertIdentical('', $this->qa->get_submitted_var(
00118                 'name', question_attempt::PARAM_MARK, array('name' => '')));
00119     }
00120 
00121     public function test_get_submitted_var_param_mark_number() {
00122         $this->assertIdentical(123.0, $this->qa->get_submitted_var(
00123                 'name', question_attempt::PARAM_MARK, array('name' => '123')));
00124     }
00125 
00126     public function test_get_submitted_var_param_mark_invalid() {
00127         $this->assertIdentical(0.0, $this->qa->get_submitted_var(
00128                 'name', question_attempt::PARAM_MARK, array('name' => 'frog')));
00129     }
00130 }
00131 
00132 
00139 class question_attempt_with_steps_test extends UnitTestCase {
00140     private $question;
00141     private $qa;
00142 
00143     public function setUp() {
00144         $this->question = test_question_maker::make_question('description');
00145         $this->qa = new testable_question_attempt($this->question, 0, null, 2);
00146         for ($i = 0; $i < 3; $i++) {
00147             $step = new question_attempt_step(array('i' => $i));
00148             $this->qa->add_step($step);
00149         }
00150     }
00151 
00152     public function tearDown() {
00153         $this->qa = null;
00154     }
00155 
00156     public function test_get_step_before_start() {
00157         $this->expectException();
00158         $step = $this->qa->get_step(-1);
00159     }
00160 
00161     public function test_get_step_at_start() {
00162         $step = $this->qa->get_step(0);
00163         $this->assertEqual(0, $step->get_qt_var('i'));
00164     }
00165 
00166     public function test_get_step_at_end() {
00167         $step = $this->qa->get_step(2);
00168         $this->assertEqual(2, $step->get_qt_var('i'));
00169     }
00170 
00171     public function test_get_step_past_end() {
00172         $this->expectException();
00173         $step = $this->qa->get_step(3);
00174     }
00175 
00176     public function test_get_num_steps() {
00177         $this->assertEqual(3, $this->qa->get_num_steps());
00178     }
00179 
00180     public function test_get_last_step() {
00181         $step = $this->qa->get_last_step();
00182         $this->assertEqual(2, $step->get_qt_var('i'));
00183     }
00184 
00185     public function test_get_last_qt_var_there1() {
00186         $this->assertEqual(2, $this->qa->get_last_qt_var('i'));
00187     }
00188 
00189     public function test_get_last_qt_var_there2() {
00190         $this->qa->get_step(0)->set_qt_var('_x', 'a value');
00191         $this->assertEqual('a value', $this->qa->get_last_qt_var('_x'));
00192     }
00193 
00194     public function test_get_last_qt_var_missing() {
00195         $this->assertNull($this->qa->get_last_qt_var('notthere'));
00196     }
00197 
00198     public function test_get_last_qt_var_missing_default() {
00199         $this->assertEqual('default', $this->qa->get_last_qt_var('notthere', 'default'));
00200     }
00201 
00202     public function test_get_last_behaviour_var_missing() {
00203         $this->assertNull($this->qa->get_last_qt_var('notthere'));
00204     }
00205 
00206     public function test_get_last_behaviour_var_there() {
00207         $this->qa->get_step(1)->set_behaviour_var('_x', 'a value');
00208         $this->assertEqual('a value', '' . $this->qa->get_last_behaviour_var('_x'));
00209     }
00210 
00211     public function test_get_state_gets_state_of_last() {
00212         $this->qa->get_step(2)->set_state(question_state::$gradedright);
00213         $this->qa->get_step(1)->set_state(question_state::$gradedwrong);
00214         $this->assertEqual(question_state::$gradedright, $this->qa->get_state());
00215     }
00216 
00217     public function test_get_mark_gets_mark_of_last() {
00218         $this->assertEqual(2, $this->qa->get_max_mark());
00219         $this->qa->get_step(2)->set_fraction(0.5);
00220         $this->qa->get_step(1)->set_fraction(0.1);
00221         $this->assertEqual(1, $this->qa->get_mark());
00222     }
00223 
00224     public function test_get_fraction_gets_fraction_of_last() {
00225         $this->qa->get_step(2)->set_fraction(0.5);
00226         $this->qa->get_step(1)->set_fraction(0.1);
00227         $this->assertEqual(0.5, $this->qa->get_fraction());
00228     }
00229 
00230     public function test_get_fraction_returns_null_if_none() {
00231         $this->assertNull($this->qa->get_fraction());
00232     }
00233 
00234     public function test_format_mark() {
00235         $this->qa->get_step(2)->set_fraction(0.5);
00236         $this->assertEqual('1.00', $this->qa->format_mark(2));
00237     }
00238 
00239     public function test_format_max_mark() {
00240         $this->assertEqual('2.0000000', $this->qa->format_max_mark(7));
00241     }
00242 
00243     public function test_get_min_fraction() {
00244         $this->qa->set_min_fraction(-1);
00245         $this->assertEqual(-1, $this->qa->get_min_fraction(0));
00246     }
00247 
00248     public function test_cannot_get_min_fraction_before_start() {
00249         $qa = new question_attempt($this->question, 0);
00250         $this->expectException();
00251         $qa->get_min_fraction();
00252     }
00253 }
00254 
00255 
00262 class question_attempt_db_test extends data_loading_method_test_base {
00263     public function test_load() {
00264         $records = new test_recordset(array(
00265             array('questionattemptid', 'contextid', 'questionusageid', 'slot',
00266                                    'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'flagged',
00267                                                                                        'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
00268                                                                                                                'attemptstepid', 'sequencenumber', 'state', 'fraction',
00269                                                                                                                                                 'timecreated', 'userid', 'name', 'value'),
00270             array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 1, 0, 'todo',              null, 1256233700, 1,       null, null),
00271             array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 2, 1, 'complete',          null, 1256233705, 1,   'answer',  '1'),
00272             array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0000000, 0.0000000, 1, '', '', '', 1256233790, 3, 2, 'complete',          null, 1256233710, 1,   'answer',  '0'),
00273             array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 4, 3, 'complete',          null, 1256233715, 1,   'answer',  '1'),
00274             array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 5, 4, 'gradedright',  1.0000000, 1256233720, 1,  '-finish',  '1'),
00275             array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 6, 5, 'mangrpartial', 0.5000000, 1256233790, 1, '-comment', 'Not good enough!'),
00276             array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 6, 5, 'mangrpartial', 0.5000000, 1256233790, 1,    '-mark',  '1'),
00277             array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 6, 5, 'mangrpartial', 0.5000000, 1256233790, 1, '-maxmark',  '2'),
00278         ));
00279 
00280         $question = test_question_maker::make_question('truefalse', 'true');
00281         $question->id = -1;
00282 
00283         question_bank::start_unit_test();
00284         question_bank::load_test_question_data($question);
00285         $qa = question_attempt::load_from_records($records, 1, new question_usage_null_observer(), 'deferredfeedback');
00286         question_bank::end_unit_test();
00287 
00288         $this->assertEqual($question->questiontext, $qa->get_question()->questiontext);
00289 
00290         $this->assertEqual(6, $qa->get_num_steps());
00291 
00292         $step = $qa->get_step(0);
00293         $this->assertEqual(question_state::$todo, $step->get_state());
00294         $this->assertNull($step->get_fraction());
00295         $this->assertEqual(1256233700, $step->get_timecreated());
00296         $this->assertEqual(1, $step->get_user_id());
00297         $this->assertEqual(array(), $step->get_all_data());
00298 
00299         $step = $qa->get_step(1);
00300         $this->assertEqual(question_state::$complete, $step->get_state());
00301         $this->assertNull($step->get_fraction());
00302         $this->assertEqual(1256233705, $step->get_timecreated());
00303         $this->assertEqual(1, $step->get_user_id());
00304         $this->assertEqual(array('answer' => '1'), $step->get_all_data());
00305 
00306         $step = $qa->get_step(2);
00307         $this->assertEqual(question_state::$complete, $step->get_state());
00308         $this->assertNull($step->get_fraction());
00309         $this->assertEqual(1256233710, $step->get_timecreated());
00310         $this->assertEqual(1, $step->get_user_id());
00311         $this->assertEqual(array('answer' => '0'), $step->get_all_data());
00312 
00313         $step = $qa->get_step(3);
00314         $this->assertEqual(question_state::$complete, $step->get_state());
00315         $this->assertNull($step->get_fraction());
00316         $this->assertEqual(1256233715, $step->get_timecreated());
00317         $this->assertEqual(1, $step->get_user_id());
00318         $this->assertEqual(array('answer' => '1'), $step->get_all_data());
00319 
00320         $step = $qa->get_step(4);
00321         $this->assertEqual(question_state::$gradedright, $step->get_state());
00322         $this->assertEqual(1, $step->get_fraction());
00323         $this->assertEqual(1256233720, $step->get_timecreated());
00324         $this->assertEqual(1, $step->get_user_id());
00325         $this->assertEqual(array('-finish' => '1'), $step->get_all_data());
00326 
00327         $step = $qa->get_step(5);
00328         $this->assertEqual(question_state::$mangrpartial, $step->get_state());
00329         $this->assertEqual(0.5, $step->get_fraction());
00330         $this->assertEqual(1256233790, $step->get_timecreated());
00331         $this->assertEqual(1, $step->get_user_id());
00332         $this->assertEqual(array('-comment' => 'Not good enough!', '-mark' => '1', '-maxmark' => '2'),
00333                 $step->get_all_data());
00334     }
00335 
00336     public function test_load_missing_question() {
00337         $records = new test_recordset(array(
00338             array('questionattemptid', 'contextid', 'questionusageid', 'slot',
00339                                    'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'flagged',
00340                                                                                        'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
00341                                                                                                                'attemptstepid', 'sequencenumber', 'state', 'fraction',
00342                                                                                                                                                 'timecreated', 'userid', 'name', 'value'),
00343             array(1, 123, 1, 1, 'deferredfeedback', -1, 1, 2.0000000, 0.0000000, 0, '', '', '', 1256233790, 1, 0, 'todo',              null, 1256233700, 1,       null, null),
00344         ));
00345 
00346         question_bank::start_unit_test();
00347         $qa = question_attempt::load_from_records($records, 1, new question_usage_null_observer(), 'deferredfeedback');
00348         question_bank::end_unit_test();
00349 
00350         $missingq = question_bank::get_qtype('missingtype')->make_deleted_instance(-1, 2);
00351         $this->assertEqual($missingq, $qa->get_question());
00352 
00353         $this->assertEqual(1, $qa->get_num_steps());
00354 
00355         $step = $qa->get_step(0);
00356         $this->assertEqual(question_state::$todo, $step->get_state());
00357         $this->assertNull($step->get_fraction());
00358         $this->assertEqual(1256233700, $step->get_timecreated());
00359         $this->assertEqual(1, $step->get_user_id());
00360         $this->assertEqual(array(), $step->get_all_data());
00361     }
00362 }
 All Data Structures Namespaces Files Functions Variables Enumerations