Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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 Embedded answer (Cloze) question importer.
  19   *
  20   * @package   qformat_multianswer
  21   * @copyright 2012 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->libdir . '/questionlib.php');
  30  require_once($CFG->dirroot . '/question/format.php');
  31  require_once($CFG->dirroot . '/question/format/multianswer/format.php');
  32  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  33  
  34  
  35  /**
  36   * Unit tests for the Embedded answer (Cloze) question importer.
  37   *
  38   * @copyright 2012 The Open University
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class qformat_multianswer_test extends question_testcase {
  42  
  43      public function test_import() {
  44          $lines = file(__DIR__ . '/fixtures/questions.multianswer.txt');
  45  
  46          $importer = new qformat_multianswer();
  47          $qs = $importer->readquestions($lines);
  48  
  49          $expectedq = (object) array(
  50              'name' => 'Match the following cities with the correct state:
  51  * San Francisco: {#1}
  52  * ...',
  53              'questiontext' => 'Match the following cities with the correct state:
  54  * San Francisco: {#1}
  55  * Tucson: {#2}
  56  * Los Angeles: {#3}
  57  * Phoenix: {#4}
  58  The capital of France is {#5}.
  59  ',
  60              'questiontextformat' => FORMAT_MOODLE,
  61              'generalfeedback' => '',
  62              'generalfeedbackformat' => FORMAT_MOODLE,
  63              'qtype' => 'multianswer',
  64              'defaultmark' => 5,
  65              'penalty' => 0.3333333,
  66              'length' => 1,
  67          );
  68  
  69          $this->assertEquals(1, count($qs));
  70          $this->assert(new question_check_specified_fields_expectation($expectedq), $qs[0]);
  71  
  72          $this->assertEquals('multichoice', $qs[0]->options->questions[1]->qtype);
  73          $this->assertEquals('multichoice', $qs[0]->options->questions[2]->qtype);
  74          $this->assertEquals('multichoice', $qs[0]->options->questions[3]->qtype);
  75          $this->assertEquals('multichoice', $qs[0]->options->questions[4]->qtype);
  76          $this->assertEquals('shortanswer', $qs[0]->options->questions[5]->qtype);
  77      }
  78  
  79      public function test_read_brokencloze_1() {
  80          $lines = file(__DIR__ . '/fixtures/broken_multianswer_1.txt');
  81          $importer = new qformat_multianswer();
  82  
  83          // The importer echoes some errors, so we need to capture and check that.
  84          ob_start();
  85          $questions = $importer->readquestions($lines);
  86          $output = ob_get_contents();
  87          ob_end_clean();
  88  
  89          // Check that there were some expected errors.
  90          $this->assertStringContainsString('Error importing question', $output);
  91          $this->assertStringContainsString('Invalid embedded answers (Cloze) question', $output);
  92          $this->assertStringContainsString('This type of question requires at least 2 choices', $output);
  93  
  94          // No question  have been imported.
  95          $this->assertCount(0, $questions);
  96      }
  97  
  98      public function test_read_brokencloze_2() {
  99          $lines = file(__DIR__ . '/fixtures/broken_multianswer_2.txt');
 100          $importer = new qformat_multianswer();
 101  
 102          // The importer echoes some errors, so we need to capture and check that.
 103          ob_start();
 104          $questions = $importer->readquestions($lines);
 105          $output = ob_get_contents();
 106          ob_end_clean();
 107  
 108          // Check that there were some expected errors.
 109          $this->assertStringContainsString('Error importing question', $output);
 110          $this->assertStringContainsString('Invalid embedded answers (Cloze) question', $output);
 111          $this->assertStringContainsString('One of the answers should have a score of 100% so it is possible to get full marks for this question.',
 112                  $output);
 113  
 114          // No question  have been imported.
 115          $this->assertCount(0, $questions);
 116      }
 117  
 118      public function test_read_brokencloze_3() {
 119          $lines = file(__DIR__ . '/fixtures/broken_multianswer_3.txt');
 120          $importer = new qformat_multianswer();
 121  
 122          // The importer echoes some errors, so we need to capture and check that.
 123          ob_start();
 124          $questions = $importer->readquestions($lines);
 125          $output = ob_get_contents();
 126          ob_end_clean();
 127  
 128          // Check that there were some expected errors.
 129          $this->assertStringContainsString('Error importing question', $output);
 130          $this->assertStringContainsString('Invalid embedded answers (Cloze) question', $output);
 131          $this->assertStringContainsString('The answer must be a number, for example -1.234 or 3e8, or \'*\'.', $output);
 132  
 133          // No question  have been imported.
 134          $this->assertCount(0, $questions);
 135      }
 136  
 137      public function test_read_brokencloze_4() {
 138          $lines = file(__DIR__ . '/fixtures/broken_multianswer_4.txt');
 139          $importer = new qformat_multianswer();
 140  
 141          // The importer echoes some errors, so we need to capture and check that.
 142          ob_start();
 143          $questions = $importer->readquestions($lines);
 144          $output = ob_get_contents();
 145          ob_end_clean();
 146  
 147          // Check that there were some expected errors.
 148          $this->assertStringContainsString('Error importing question', $output);
 149          $this->assertStringContainsString('Invalid embedded answers (Cloze) question', $output);
 150          $this->assertStringContainsString('The question text must include at least one embedded answer.', $output);
 151  
 152          // No question  have been imported.
 153          $this->assertCount(0, $questions);
 154      }
 155  }