Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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   * Unit tests for export/import description (info) for question category in the Moodle XML format.
  18   *
  19   * @package    qformat_aiken
  20   * @copyright  2018 Jean-Michel Vedrine
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  global $CFG;
  27  require_once($CFG->libdir . '/questionlib.php');
  28  require_once($CFG->dirroot . '/question/format.php');
  29  require_once($CFG->dirroot . '/question/format/aiken/format.php');
  30  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  31  require_once($CFG->dirroot . '/question/editlib.php');
  32  
  33  /**
  34   * Unit tests for the Aiken question format export.
  35   *
  36   * @copyright  2018 Jean-Michel vedrine)
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class qformat_aiken_export_test extends advanced_testcase {
  40      /**
  41       * Assert that 2 strings are the same, ignoring ends of line.
  42       * We need to override this function because we don't want any output
  43       * @param   string    $expectedtext The expected string.
  44       * @param   string    $text The actual string.
  45       */
  46      public function assert_same_aiken($expectedtext, $text) {
  47          $this->assertEquals(str_replace("\r\n", "\n", $expectedtext),
  48                  str_replace("\r\n", "\n", $text));
  49      }
  50  
  51      public function test_export_questions() {
  52          $this->resetAfterTest();
  53          $this->setAdminUser();
  54          // Create a new course category and and a new course in that.
  55          $category = $this->getDataGenerator()->create_category();
  56          $course = $this->getDataGenerator()->create_course(array('category' => $category->id));
  57          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  58          $context = context_coursecat::instance($category->id);
  59          $cat = $generator->create_question_category(array('contextid' => $context->id));
  60          $question1 = $generator->create_question('shortanswer', null,
  61                  array('category' => $cat->id));
  62          $question2 = $generator->create_question('essay', null,
  63                  array('category' => $cat->id));
  64          $question3 = $generator->create_question('numerical', null,
  65                  array('category' => $cat->id));
  66          $question4  = $generator->create_question('multichoice', 'one_of_four',
  67                  array('category' => $cat->id));
  68          $question4  = $generator->create_question('multichoice', 'two_of_four',
  69                  array('category' => $cat->id));
  70          $exporter = new qformat_aiken();
  71          $exporter->category = $cat;
  72          $exporter->setCourse($course);
  73          $expectedoutput = <<<EOT
  74  Which is the oddest number?
  75  A) One
  76  B) Two
  77  C) Three
  78  D) Four
  79  ANSWER: A
  80  
  81  EOT;
  82          $this->assert_same_aiken($expectedoutput, $exporter->exportprocess());
  83      }
  84  
  85      public function test_export_multiline_question() {
  86          $this->resetAfterTest();
  87          $this->setAdminUser();
  88          // Create a new course category and and a new course in that.
  89          $category = $this->getDataGenerator()->create_category();
  90          $course = $this->getDataGenerator()->create_course(array('category' => $category->id));
  91          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  92          $context = context_coursecat::instance($category->id);
  93          $cat = $generator->create_question_category(array('contextid' => $context->id));
  94          $question  = $generator->create_question('multichoice', 'one_of_four',
  95                  array('category' => $cat->id));
  96          $question->questiontext = <<<EOT
  97  <p>Which is the</p>
  98  <p>oddest number?</p>
  99  EOT;
 100          $exporter = new qformat_aiken();
 101          $exporter->category = $cat;
 102          $exporter->setCourse($course);
 103          $expectedoutput = <<<EOT
 104  Which is the oddest number?
 105  A) One
 106  B) Two
 107  C) Three
 108  D) Four
 109  ANSWER: A
 110  
 111  EOT;
 112          $this->assert_same_aiken($expectedoutput, $exporter->exportprocess());
 113      }
 114  }