[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/question/engine/tests/ -> questionusagebyactivity_test.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file contains tests for the question_usage_by_activity class.
  19   *
  20   * @package    moodlecore
  21   * @subpackage questionengine
  22   * @copyright  2009 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once(dirname(__FILE__) . '/../lib.php');
  31  require_once(dirname(__FILE__) . '/helpers.php');
  32  
  33  
  34  /**
  35   * Unit tests for the question_usage_by_activity class.
  36   *
  37   * @copyright  2009 The Open University
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class question_usage_by_activity_test extends advanced_testcase {
  41  
  42      public function test_set_get_preferred_model() {
  43          // Set up
  44          $quba = question_engine::make_questions_usage_by_activity('unit_test',
  45                  context_system::instance());
  46  
  47          // Exercise SUT and verify.
  48          $quba->set_preferred_behaviour('deferredfeedback');
  49          $this->assertEquals('deferredfeedback', $quba->get_preferred_behaviour());
  50      }
  51  
  52      public function test_set_get_id() {
  53          // Set up
  54          $quba = question_engine::make_questions_usage_by_activity('unit_test',
  55                  context_system::instance());
  56  
  57          // Exercise SUT and verify
  58          $quba->set_id_from_database(123);
  59          $this->assertEquals(123, $quba->get_id());
  60      }
  61  
  62      public function test_fake_id() {
  63          // Set up
  64          $quba = question_engine::make_questions_usage_by_activity('unit_test',
  65                  context_system::instance());
  66  
  67          // Exercise SUT and verify
  68          $this->assertNotEmpty($quba->get_id());
  69      }
  70  
  71      public function test_create_usage_and_add_question() {
  72          // Exercise SUT
  73          $context = context_system::instance();
  74          $quba = question_engine::make_questions_usage_by_activity('unit_test', $context);
  75          $quba->set_preferred_behaviour('deferredfeedback');
  76          $tf = test_question_maker::make_question('truefalse', 'true');
  77          $slot = $quba->add_question($tf);
  78  
  79          // Verify.
  80          $this->assertEquals($slot, 1);
  81          $this->assertEquals('unit_test', $quba->get_owning_component());
  82          $this->assertSame($context, $quba->get_owning_context());
  83          $this->assertEquals($quba->question_count(), 1);
  84          $this->assertEquals($quba->get_question_state($slot), question_state::$notstarted);
  85      }
  86  
  87      public function test_get_question() {
  88          // Set up.
  89          $quba = question_engine::make_questions_usage_by_activity('unit_test',
  90                  context_system::instance());
  91          $quba->set_preferred_behaviour('deferredfeedback');
  92          $tf = test_question_maker::make_question('truefalse', 'true');
  93          $slot = $quba->add_question($tf);
  94  
  95          // Exercise SUT and verify.
  96          $this->assertSame($tf, $quba->get_question($slot));
  97  
  98          $this->setExpectedException('moodle_exception');
  99          $quba->get_question($slot + 1);
 100      }
 101  
 102      public function test_extract_responses() {
 103          // Start a deferred feedback attempt with CBM and add the question to it.
 104          $tf = test_question_maker::make_question('truefalse', 'true');
 105          $quba = question_engine::make_questions_usage_by_activity('unit_test',
 106                  context_system::instance());
 107          $quba->set_preferred_behaviour('deferredcbm');
 108          $slot = $quba->add_question($tf);
 109          $quba->start_all_questions();
 110  
 111          // Prepare data to be submitted
 112          $prefix = $quba->get_field_prefix($slot);
 113          $answername = $prefix . 'answer';
 114          $certaintyname = $prefix . '-certainty';
 115          $getdata = array(
 116              $answername => 1,
 117              $certaintyname => 3,
 118              'irrelevant' => 'should be ignored',
 119          );
 120  
 121          // Exercise SUT
 122          $submitteddata = $quba->extract_responses($slot, $getdata);
 123  
 124          // Verify.
 125          $this->assertEquals(array('answer' => 1, '-certainty' => 3), $submitteddata);
 126      }
 127  
 128      public function test_access_out_of_sequence_throws_exception() {
 129          // Start a deferred feedback attempt with CBM and add the question to it.
 130          $tf = test_question_maker::make_question('truefalse', 'true');
 131          $quba = question_engine::make_questions_usage_by_activity('unit_test',
 132                  context_system::instance());
 133          $quba->set_preferred_behaviour('deferredcbm');
 134          $slot = $quba->add_question($tf);
 135          $quba->start_all_questions();
 136  
 137          // Prepare data to be submitted
 138          $prefix = $quba->get_field_prefix($slot);
 139          $answername = $prefix . 'answer';
 140          $certaintyname = $prefix . '-certainty';
 141          $postdata = array(
 142              $answername => 1,
 143              $certaintyname => 3,
 144              $prefix . ':sequencecheck' => 1,
 145              'irrelevant' => 'should be ignored',
 146          );
 147  
 148          // Exercise SUT - no exception yet.
 149          $quba->process_all_actions($slot, $postdata);
 150  
 151          $postdata = array(
 152              $answername => 1,
 153              $certaintyname => 3,
 154              $prefix . ':sequencecheck' => 3,
 155              'irrelevant' => 'should be ignored',
 156          );
 157  
 158          // Exercise SUT - now it should fail.
 159          $this->setExpectedException('question_out_of_sequence_exception');
 160          $quba->process_all_actions($slot, $postdata);
 161      }
 162  }
 163  
 164  /**
 165   * Unit tests for loading data into the {@link question_usage_by_activity} class.
 166   *
 167   * @copyright  2012 The Open University
 168   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 169   */
 170  class question_usage_db_test extends data_loading_method_test_base {
 171      public function test_load() {
 172          $scid = context_system::instance()->id;
 173          $records = new question_test_recordset(array(
 174          array('qubaid', 'contextid', 'component', 'preferredbehaviour',
 175                                                 'questionattemptid', 'questionusageid', 'slot',
 176                                                                'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'maxfraction', 'flagged',
 177                                                                                                               'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
 178                                                                                                                                       'attemptstepid', 'sequencenumber', 'state', 'fraction',
 179                                                                                                                                                                       'timecreated', 'userid', 'name', 'value'),
 180          array(1, $scid, 'unit_test', 'interactive', 1, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 1.0000000, 0, '', '', '', 1256233790, 1, 0, 'todo',             null, 1256233700, 1,       null, null),
 181          array(1, $scid, 'unit_test', 'interactive', 1, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 1.0000000, 0, '', '', '', 1256233790, 2, 1, 'todo',             null, 1256233705, 1,   'answer',  '1'),
 182          array(1, $scid, 'unit_test', 'interactive', 1, 1, 1, 'interactive', -1, 1, 2.0000000, 0.0000000, 1.0000000, 0, '', '', '', 1256233790, 5, 2, 'gradedright', 1.0000000, 1256233720, 1,  '-finish',  '1'),
 183          ));
 184  
 185          $question = test_question_maker::make_question('truefalse', 'true');
 186          $question->id = -1;
 187  
 188          question_bank::start_unit_test();
 189          question_bank::load_test_question_data($question);
 190          $quba = question_usage_by_activity::load_from_records($records, 1);
 191          question_bank::end_unit_test();
 192  
 193          $this->assertEquals('unit_test', $quba->get_owning_component());
 194          $this->assertEquals(1, $quba->get_id());
 195          $this->assertInstanceOf('question_engine_unit_of_work', $quba->get_observer());
 196          $this->assertEquals('interactive', $quba->get_preferred_behaviour());
 197  
 198          $qa = $quba->get_question_attempt(1);
 199  
 200          $this->assertEquals($question->questiontext, $qa->get_question()->questiontext);
 201  
 202          $this->assertEquals(3, $qa->get_num_steps());
 203  
 204          $step = $qa->get_step(0);
 205          $this->assertEquals(question_state::$todo, $step->get_state());
 206          $this->assertNull($step->get_fraction());
 207          $this->assertEquals(1256233700, $step->get_timecreated());
 208          $this->assertEquals(1, $step->get_user_id());
 209          $this->assertEquals(array(), $step->get_all_data());
 210  
 211          $step = $qa->get_step(1);
 212          $this->assertEquals(question_state::$todo, $step->get_state());
 213          $this->assertNull($step->get_fraction());
 214          $this->assertEquals(1256233705, $step->get_timecreated());
 215          $this->assertEquals(1, $step->get_user_id());
 216          $this->assertEquals(array('answer' => '1'), $step->get_all_data());
 217  
 218          $step = $qa->get_step(2);
 219          $this->assertEquals(question_state::$gradedright, $step->get_state());
 220          $this->assertEquals(1, $step->get_fraction());
 221          $this->assertEquals(1256233720, $step->get_timecreated());
 222          $this->assertEquals(1, $step->get_user_id());
 223          $this->assertEquals(array('-finish' => '1'), $step->get_all_data());
 224      }
 225  
 226      public function test_load_data_no_steps() {
 227          // The code had a bug where if one question_attempt had no steps,
 228          // load_from_records got stuck in an infinite loop. This test is to
 229          // verify that no longer happens.
 230          $scid = context_system::instance()->id;
 231          $records = new question_test_recordset(array(
 232          array('qubaid', 'contextid', 'component', 'preferredbehaviour',
 233                                                     'questionattemptid', 'questionusageid', 'slot',
 234                                                               'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'maxfraction', 'flagged',
 235                                                                                                              'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
 236                                                                                                                                                                                 'attemptstepid', 'sequencenumber', 'state', 'fraction',
 237                                                                                                                                                                                                           'timecreated', 'userid', 'name', 'value'),
 238          array(1, $scid, 'unit_test', 'interactive', 1, 1, 1, 'interactive', 0, 1, 1.0000000, 0.0000000, 1.0000000, 0, 'This question is missing. Unable to display anything.', '', '', 0, null, null, null, null, null, null, null, null),
 239          array(1, $scid, 'unit_test', 'interactive', 2, 1, 2, 'interactive', 0, 1, 1.0000000, 0.0000000, 1.0000000, 0, 'This question is missing. Unable to display anything.', '', '', 0, null, null, null, null, null, null, null, null),
 240          array(1, $scid, 'unit_test', 'interactive', 3, 1, 3, 'interactive', 0, 1, 1.0000000, 0.0000000, 1.0000000, 0, 'This question is missing. Unable to display anything.', '', '', 0, null, null, null, null, null, null, null, null),
 241          ));
 242  
 243          question_bank::start_unit_test();
 244          $quba = question_usage_by_activity::load_from_records($records, 1);
 245          question_bank::end_unit_test();
 246  
 247          $this->assertEquals('unit_test', $quba->get_owning_component());
 248          $this->assertEquals(1, $quba->get_id());
 249          $this->assertInstanceOf('question_engine_unit_of_work', $quba->get_observer());
 250          $this->assertEquals('interactive', $quba->get_preferred_behaviour());
 251  
 252          $this->assertEquals(array(1, 2, 3), $quba->get_slots());
 253  
 254          $qa = $quba->get_question_attempt(1);
 255          $this->assertEquals(0, $qa->get_num_steps());
 256      }
 257  
 258      public function test_load_data_no_qas() {
 259          // The code had a bug where if a question_usage had no question_attempts,
 260          // load_from_records got stuck in an infinite loop. This test is to
 261          // verify that no longer happens.
 262          $scid = context_system::instance()->id;
 263          $records = new question_test_recordset(array(
 264          array('qubaid', 'contextid', 'component', 'preferredbehaviour',
 265                                                     'questionattemptid', 'questionusageid', 'slot',
 266                                                                          'behaviour', 'questionid', 'variant', 'maxmark', 'minfraction', 'maxfraction', 'flagged',
 267                                                                                                                 'questionsummary', 'rightanswer', 'responsesummary', 'timemodified',
 268                                                                                                                                           'attemptstepid', 'sequencenumber', 'state', 'fraction',
 269                                                                                                                                                                     'timecreated', 'userid', 'name', 'value'),
 270          array(1, $scid, 'unit_test', 'interactive', null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null),
 271          ));
 272  
 273          question_bank::start_unit_test();
 274          $quba = question_usage_by_activity::load_from_records($records, 1);
 275          question_bank::end_unit_test();
 276  
 277          $this->assertEquals('unit_test', $quba->get_owning_component());
 278          $this->assertEquals(1, $quba->get_id());
 279          $this->assertInstanceOf('question_engine_unit_of_work', $quba->get_observer());
 280          $this->assertEquals('interactive', $quba->get_preferred_behaviour());
 281  
 282          $this->assertEquals(array(), $quba->get_slots());
 283      }
 284  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1