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.

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

   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 mod_quiz;
  18  
  19  use mod_quiz\question\bank\qbank_helper;
  20  use quiz;
  21  
  22  /**
  23   * Class mod_quiz_tags_testcase
  24   * Class for tests related to usage of question tags in quizzes.
  25   *
  26   * @package    mod_quiz
  27   * @category   test
  28   * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class tags_test extends \advanced_testcase {
  32      public function test_restore_random_question_by_tag() {
  33          global $CFG, $USER, $DB;
  34  
  35          require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  36          require_once($CFG->dirroot . '/mod/quiz/locallib.php');
  37  
  38          $this->resetAfterTest();
  39          $this->setAdminUser();
  40  
  41          $backupid = 'abc';
  42          $backuppath = make_backup_temp_directory($backupid);
  43          get_file_packer('application/vnd.moodle.backup')->extract_to_pathname(
  44                  __DIR__ . "/fixtures/random_by_tag_quiz.mbz", $backuppath);
  45  
  46          // Do the restore to new course with default settings.
  47          $categoryid = $DB->get_field_sql("SELECT MIN(id) FROM {course_categories}");
  48          $newcourseid = \restore_dbops::create_new_course('Test fullname', 'Test shortname', $categoryid);
  49          $rc = new \restore_controller($backupid, $newcourseid, \backup::INTERACTIVE_NO, \backup::MODE_GENERAL, $USER->id,
  50                  \backup::TARGET_NEW_COURSE);
  51  
  52          $this->assertTrue($rc->execute_precheck());
  53          $rc->execute_plan();
  54          $rc->destroy();
  55  
  56          // Get the information about the resulting course and check that it is set up correctly.
  57          $modinfo = get_fast_modinfo($newcourseid);
  58          $quiz = array_values($modinfo->get_instances_of('quiz'))[0];
  59          $quizobj = quiz::create($quiz->instance);
  60          $structure = structure::create_for_quiz($quizobj);
  61  
  62          // Are the correct slots returned?
  63          $slots = $structure->get_slots();
  64          $this->assertCount(1, $slots);
  65  
  66          $quizobj->preload_questions();
  67          $quizobj->load_questions();
  68          $questions = $quizobj->get_questions();
  69  
  70          $this->assertCount(1, $questions);
  71  
  72          $question = array_values($questions)[0];
  73  
  74          $tag1 = \core_tag_tag::get_by_name(0, 't1', 'id, name');
  75          $this->assertNotFalse($tag1);
  76  
  77          $tag2 = \core_tag_tag::get_by_name(0, 't2', 'id, name');
  78          $this->assertNotFalse($tag2);
  79  
  80          $tag3 = \core_tag_tag::get_by_name(0, 't3', 'id, name');
  81          $this->assertNotFalse($tag3);
  82  
  83          $slottags = quiz_retrieve_slot_tags($question->slotid);
  84          $this->assertEqualsCanonicalizing(
  85                  [
  86                      ['tagid' => $tag2->id, 'tagname' => $tag2->name]
  87                  ],
  88                  array_map(function($tag) {
  89                      return ['tagid' => $tag->tagid, 'tagname' => $tag->tagname];
  90                  }, $slottags)
  91          );
  92  
  93          $defaultcategory = question_get_default_category(\context_course::instance($newcourseid)->id);
  94          $this->assertEquals($defaultcategory->id, $question->randomfromcategory);
  95          $this->assertEquals(0, $question->randomincludingsubcategories);
  96      }
  97  }