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 310 and 401] [Versions 39 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  /**
  18   * Unit tests for the Moodle Aiken format.
  19   *
  20   * @package    qformat_aiken
  21   * @copyright  2018 Eric Merrill (eric.a.merrill@gmail.com)
  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/aiken/format.php');
  32  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  33  
  34  
  35  /**
  36   * Unit tests for the matching question definition class.
  37   *
  38   * @copyright  2018 Eric Merrill (eric.a.merrill@gmail.com)
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class aikenformat_test extends question_testcase {
  42      public function test_readquestions() {
  43          global $CFG;
  44  
  45          $lines = file($CFG->dirroot.'/question/format/aiken/tests/fixtures/aiken_errors.txt');
  46          $importer = new qformat_aiken($lines);
  47  
  48          // The importer echos some errors, so we need to capture and check that.
  49          ob_start();
  50          $questions = $importer->readquestions($lines);
  51          $output = ob_get_contents();
  52          ob_end_clean();
  53  
  54          // Check that there were some expected errors.
  55          $this->assertStringContainsString('Error importing question A question with too few answers', $output);
  56          $this->assertStringContainsString('Question must have at least 2 answers on line 3', $output);
  57          $this->assertStringContainsString('Question not started on line 5', $output);
  58          $this->assertStringContainsString('Question not started on line 7', $output);
  59          $this->assertStringContainsString('Error importing question A question started but not finished', $output);
  60          $this->assertStringContainsString('Question not completed before next question start on line 18', $output);
  61  
  62          // There are three expected questions.
  63          $this->assertCount(3, $questions);
  64  
  65          $q1 = null;
  66          $q2 = null;
  67          $q3 = null;
  68          foreach ($questions as $question) {
  69              if ($question->name === 'A good question') {
  70                  $q1 = $question;
  71              } else if ($question->name === 'A second good question') {
  72                  $q2 = $question;
  73              } else if ($question->name === 'A third good question with HTML chars such as > < &') {
  74                  $q3 = $question;
  75              }
  76          }
  77  
  78          // Check the first good question.
  79          $this->assertCount(2, $q1->answer);
  80          $this->assertEquals(1, $q1->fraction[0]);
  81          $this->assertEquals('Correct', $q1->answer[0]['text']);
  82          $this->assertEquals('Incorrect', $q1->answer[1]['text']);
  83  
  84          // Check the second good question.
  85          $this->assertCount(2, $q2->answer);
  86          $this->assertEquals(1, $q2->fraction[1]);
  87          $this->assertEquals('Incorrect (No space)', $q2->answer[0]['text']);
  88          $this->assertEquals('Correct (No space)', $q2->answer[1]['text']);
  89  
  90          // Check the third good question that has anwsers with special HTML chars such as <, >, and &.
  91          $this->assertCount(2, $q3->answer);
  92          $this->assertEquals(1, $q3->fraction[0]);
  93          $this->assertEquals('Correct (&lt; &gt; &amp;)', $q3->answer[0]['text']);
  94          $this->assertEquals('Incorrect (&lt; &gt; &amp;)', $q3->answer[1]['text']);
  95      }
  96  }