Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  •    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 true-false question definition class.
      19   *
      20   * @package    qtype
      21   * @subpackage truefalse
      22   * @copyright  2007 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($CFG->dirroot . '/question/type/truefalse/questiontype.php');
      31  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
      32  require_once($CFG->dirroot . '/question/type/edit_question_form.php');
      33  require_once($CFG->dirroot . '/question/type/truefalse/edit_truefalse_form.php');
      34  
      35  /**
      36   * Unit tests for the true-false question definition class.
      37   *
      38   * @copyright  2007 The Open University
      39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      40   */
      41  class qtype_truefalse_test extends advanced_testcase {
      42      protected $qtype;
      43  
      44      protected function setUp() {
      45          $this->qtype = new qtype_truefalse();
      46      }
      47  
      48      protected function tearDown() {
      49          $this->qtype = null;
      50      }
      51  
      52      public function test_name() {
      53          $this->assertEquals($this->qtype->name(), 'truefalse');
      54      }
      55  
      56      public function test_can_analyse_responses() {
      57          $this->assertTrue($this->qtype->can_analyse_responses());
      58      }
      59  
      60      public function test_get_random_guess_score() {
      61          $this->assertEquals(0.5, $this->qtype->get_random_guess_score(null));
      62      }
      63  
      64      public function test_get_possible_responses() {
      65          $q = new stdClass();
      66          $q->id = 1;
      67          $q->options = new stdClass();
      68          $q->options->trueanswer = 1;
      69          $q->options->falseanswer = 2;
      70          $q->options->answers[1] = (object) array('fraction' => 1);
      71          $q->options->answers[2] = (object) array('fraction' => 0);
      72  
      73          $this->assertEquals(array(
      74              $q->id => array(
      75                  0 => new question_possible_response(get_string('false', 'qtype_truefalse'), 0),
      76                  1 => new question_possible_response(get_string('true', 'qtype_truefalse'), 1),
      77                  null => question_possible_response::no_response()),
      78          ), $this->qtype->get_possible_responses($q));
      79      }
      80  
      81      public function test_question_saving_true() {
      82          $this->resetAfterTest(true);
      83          $this->setAdminUser();
      84  
      85          $questiondata = test_question_maker::get_question_data('truefalse');
      86          $formdata = test_question_maker::get_question_form_data('truefalse');
      87  
      88          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
      89          $cat = $generator->create_question_category(array());
      90  
      91          $formdata->category = "{$cat->id},{$cat->contextid}";
      92          qtype_truefalse_edit_form::mock_submit((array)$formdata);
      93  
      94          $form = qtype_truefalse_test_helper::get_question_editing_form($cat, $questiondata);
      95  
      96          $this->assertTrue($form->is_validated());
      97  
      98          $fromform = $form->get_data();
      99  
     100          $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
     101          $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
     102          $actualquestiondata = end($actualquestionsdata);
     103  
     104          foreach ($questiondata as $property => $value) {
     105              if (!in_array($property, array('options'))) {
     106                  $this->assertAttributeEquals($value, $property, $actualquestiondata);
     107              }
     108          }
     109  
     110          foreach ($questiondata->options as $optionname => $value) {
     111              if (!in_array($optionname, array('trueanswer', 'falseanswer', 'answers'))) {
     112                  $this->assertAttributeEquals($value, $optionname, $actualquestiondata->options);
     113              }
     114          }
     115  
     116          $answerindexes = array();
     117          foreach ($questiondata->options->answers as $ansindex => $answer) {
     118              $actualanswer = array_shift($actualquestiondata->options->answers);
     119              foreach ($answer as $ansproperty => $ansvalue) {
     120                  // This question does not use 'answerformat', will ignore it.
     121                  if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
     122                      $this->assertAttributeEquals($ansvalue, $ansproperty, $actualanswer);
     123                  }
     124              }
     125              $answerindexes[$answer->answer] = $ansindex;
     126          }
     127  
     128          $this->assertEquals($questiondata->options->trueanswer, $answerindexes['True']);
     129          $this->assertEquals($questiondata->options->falseanswer, $answerindexes['False']);
     130      }
     131  }
    

    Search This Site: