[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/type/match/tests/ -> questiontype_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   * Unit tests for the matching question definition class.
  19   *
  20   * @package   qtype_match
  21   * @copyright 2009 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  30  require_once($CFG->dirroot . '/question/type/match/questiontype.php');
  31  require_once($CFG->dirroot . '/question/type/edit_question_form.php');
  32  require_once($CFG->dirroot . '/question/type/match/edit_match_form.php');
  33  
  34  
  35  /**
  36   * Unit tests for the matching question definition class.
  37   *
  38   * @copyright 2009 The Open University
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class qtype_match_test extends advanced_testcase {
  42      /** @var qtype_match instance of the question type class to test. */
  43      protected $qtype;
  44  
  45      protected function setUp() {
  46          $this->qtype = new qtype_match();
  47      }
  48  
  49      protected function tearDown() {
  50          $this->qtype = null;
  51      }
  52  
  53      protected function get_test_question_data() {
  54          global $USER;
  55          $q = new stdClass();
  56          $q->id = 0;
  57          $q->name = 'Matching question';
  58          $q->category = 0;
  59          $q->contextid = 0;
  60          $q->parent = 0;
  61          $q->questiontext = 'Classify the animals.';
  62          $q->questiontextformat = FORMAT_HTML;
  63          $q->generalfeedback = 'General feedback.';
  64          $q->generalfeedbackformat = FORMAT_HTML;
  65          $q->defaultmark = 1;
  66          $q->penalty = 0.3333333;
  67          $q->length = 1;
  68          $q->stamp = make_unique_id_code();
  69          $q->version = make_unique_id_code();
  70          $q->hidden = 0;
  71          $q->timecreated = time();
  72          $q->timemodified = time();
  73          $q->createdby = $USER->id;
  74          $q->modifiedby = $USER->id;
  75  
  76          $q->options = new stdClass();
  77          $q->options->shuffleanswers = false;
  78          test_question_maker::set_standard_combined_feedback_fields($q->options);
  79  
  80          $q->options->subquestions = array(
  81              14 => (object) array(
  82                  'id' => 14,
  83                  'questiontext' => 'frog',
  84                  'questiontextformat' => FORMAT_HTML,
  85                  'answertext' => 'amphibian'),
  86              15 => (object) array(
  87                  'id' => 15,
  88                  'questiontext' => 'cat',
  89                  'questiontextformat' => FORMAT_HTML,
  90                  'answertext' => 'mammal'),
  91              16 => (object) array(
  92                  'id' => 16,
  93                  'questiontext' => 'newt',
  94                  'questiontextformat' => FORMAT_HTML,
  95                  'answertext' => 'amphibian'),
  96              17 => (object) array(
  97                  'id' => 17,
  98                  'questiontext' => '',
  99                  'questiontextformat' => FORMAT_HTML,
 100                  'answertext' => 'insect'),
 101          );
 102  
 103          return $q;
 104      }
 105  
 106      public function test_name() {
 107          $this->assertEquals($this->qtype->name(), 'match');
 108      }
 109  
 110      public function test_can_analyse_responses() {
 111          $this->assertTrue($this->qtype->can_analyse_responses());
 112      }
 113  
 114      public function test_get_random_guess_score() {
 115          $q = $this->get_test_question_data();
 116          $this->assertEquals(0.3333333, $this->qtype->get_random_guess_score($q), '', 0.0000001);
 117      }
 118  
 119      public function test_get_possible_responses() {
 120          $q = $this->get_test_question_data();
 121  
 122          $this->assertEquals(array(
 123              14 => array(
 124                  14 => new question_possible_response('frog: amphibian', 1/3),
 125                  15 => new question_possible_response('frog: mammal', 0),
 126                  17 => new question_possible_response('frog: insect', 0),
 127                  null => question_possible_response::no_response()),
 128              15 => array(
 129                  14 => new question_possible_response('cat: amphibian', 0),
 130                  15 => new question_possible_response('cat: mammal', 1/3),
 131                  17 => new question_possible_response('cat: insect', 0),
 132                  null => question_possible_response::no_response()),
 133              16 => array(
 134                  14 => new question_possible_response('newt: amphibian', 1/3),
 135                  15 => new question_possible_response('newt: mammal', 0),
 136                  17 => new question_possible_response('newt: insect', 0),
 137                  null => question_possible_response::no_response()),
 138          ), $this->qtype->get_possible_responses($q));
 139      }
 140  
 141  
 142      public function test_question_saving_foursubq() {
 143          $this->resetAfterTest(true);
 144          $this->setAdminUser();
 145  
 146          $questiondata = test_question_maker::get_question_data('match');
 147          $formdata = test_question_maker::get_question_form_data('match');
 148  
 149          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 150          $cat = $generator->create_question_category(array());
 151  
 152          $formdata->category = "{$cat->id},{$cat->contextid}";
 153  
 154          qtype_match_edit_form::mock_submit((array)$formdata);
 155  
 156          $form = qtype_match_test_helper::get_question_editing_form($cat, $questiondata);
 157          $this->assertTrue($form->is_validated());
 158  
 159          $fromform = $form->get_data();
 160  
 161          $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
 162          $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
 163          $actualquestiondata = end($actualquestionsdata);
 164  
 165          foreach ($questiondata as $property => $value) {
 166              if (!in_array($property, array('id', 'version', 'timemodified', 'timecreated', 'options', 'stamp'))) {
 167                  $this->assertAttributeEquals($value, $property, $actualquestiondata);
 168              }
 169          }
 170  
 171          foreach ($questiondata->options as $optionname => $value) {
 172              if ($optionname != 'subquestions') {
 173                  $this->assertAttributeEquals($value, $optionname, $actualquestiondata->options);
 174              }
 175          }
 176  
 177          $this->assertObjectHasAttribute('subquestions', $actualquestiondata->options);
 178  
 179          $subqpropstoignore = array('id');
 180          foreach ($questiondata->options->subquestions as $subq) {
 181              $actualsubq = array_shift($actualquestiondata->options->subquestions);
 182              foreach ($subq as $subqproperty => $subqvalue) {
 183                  if (!in_array($subqproperty, $subqpropstoignore)) {
 184                      $this->assertAttributeEquals($subqvalue, $subqproperty, $actualsubq);
 185                  }
 186              }
 187          }
 188      }
 189  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1