Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 311 and 401]

   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  namespace qtype_calculatedsimple;
  18  
  19  use qtype_calculated_dataset_loader;
  20  use qtype_calculatedsimple;
  21  use qtype_calculatedsimple_edit_form;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  require_once($CFG->dirroot . '/question/type/calculatedsimple/questiontype.php');
  27  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  28  require_once($CFG->dirroot . '/question/type/edit_question_form.php');
  29  require_once($CFG->dirroot . '/question/type/calculatedsimple/edit_calculatedsimple_form.php');
  30  
  31  
  32  /**
  33   * Unit tests for the calculatedsimple question type class.
  34   *
  35   * @package    qtype_calculatedsimple
  36   * @copyright  2007 The Open University
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   *
  39   * @covers \question_type
  40   * @covers \qtype_calculatedsimple
  41   * @covers \question_wizard_form
  42   * @covers \question_edit_form
  43   * @covers \qtype_calculated_edit_form
  44   * @covers \qtype_calculatedsimple_edit_form
  45   *
  46   */
  47  class question_type_test extends \advanced_testcase {
  48      protected $qtype;
  49  
  50      protected function setUp(): void {
  51          $this->qtype = new qtype_calculatedsimple();
  52      }
  53  
  54      protected function tearDown(): void {
  55          $this->qtype = null;
  56      }
  57  
  58      public function test_name() {
  59          $this->assertEquals($this->qtype->name(), 'calculatedsimple');
  60      }
  61  
  62      public function test_can_analyse_responses() {
  63          $this->assertTrue($this->qtype->can_analyse_responses());
  64      }
  65  
  66  
  67      public function test_question_saving_sumwithvariants() {
  68          $this->resetAfterTest(true);
  69          $this->setAdminUser();
  70  
  71          $questiondata = \test_question_maker::get_question_data('calculatedsimple', 'sumwithvariants');
  72          $formdata = \test_question_maker::get_question_form_data('calculatedsimple', 'sumwithvariants');
  73  
  74          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  75          $cat = $generator->create_question_category(array());
  76  
  77          $formdata->category = "{$cat->id},{$cat->contextid}";
  78          qtype_calculatedsimple_edit_form::mock_submit((array)$formdata);
  79  
  80          $form = \qtype_calculatedsimple_test_helper::get_question_editing_form($cat, $questiondata);
  81  
  82          $this->assertTrue($form->is_validated());
  83  
  84          $fromform = $form->get_data();
  85  
  86          $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
  87          $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
  88          $actualquestiondata = end($actualquestionsdata);
  89  
  90          foreach ($questiondata as $property => $value) {
  91              if (!in_array($property, array('id', 'timemodified', 'timecreated', 'options', 'idnumber'))) {
  92                  $this->assertEquals($value, $actualquestiondata->$property);
  93              }
  94          }
  95  
  96          foreach ($questiondata->options as $optionname => $value) {
  97              if ($optionname != 'answers') {
  98                  $this->assertEquals($value, $actualquestiondata->options->$optionname);
  99              }
 100          }
 101  
 102          foreach ($questiondata->options->answers as $answer) {
 103              $actualanswer = array_shift($actualquestiondata->options->answers);
 104              foreach ($answer as $ansproperty => $ansvalue) {
 105                  if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
 106                      $this->assertEquals($ansvalue, $actualanswer->$ansproperty);
 107                  }
 108              }
 109          }
 110  
 111          $datasetloader = new qtype_calculated_dataset_loader($actualquestiondata->id);
 112  
 113          $this->assertEquals(10, $datasetloader->get_number_of_items());
 114  
 115          for ($itemno = 1; $itemno <= 10; $itemno++) {
 116              $item = $datasetloader->get_values($itemno);
 117              $this->assertEquals((float)$formdata->number[($itemno -1)*2 + 2], $item['a']);
 118              $this->assertEquals((float)$formdata->number[($itemno -1)*2 + 1], $item['b']);
 119          }
 120      }
 121  }