[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/tests/ -> questionlib_test.php (source)

   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 (some of) ../questionlib.php.
  19   *
  20   * @package    core_question
  21   * @category   phpunit
  22   * @copyright  2006 The Open University
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  require_once($CFG->libdir . '/questionlib.php');
  31  require_once($CFG->dirroot . '/tag/lib.php');
  32  
  33  // Get the necessary files to perform backup and restore.
  34  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  35  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  36  
  37  /**
  38   * Unit tests for (some of) ../questionlib.php.
  39   *
  40   * @copyright  2006 The Open University
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class core_questionlib_testcase extends advanced_testcase {
  44  
  45      /**
  46       * Test set up.
  47       *
  48       * This is executed before running any test in this file.
  49       */
  50      public function setUp() {
  51          $this->resetAfterTest();
  52      }
  53  
  54      /**
  55       * Tidy up open files that may be left open.
  56       */
  57      protected function tearDown() {
  58          gc_collect_cycles();
  59      }
  60  
  61      public function test_question_reorder_qtypes() {
  62          $this->assertEquals(
  63              array(0 => 't2', 1 => 't1', 2 => 't3'),
  64              question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', +1));
  65          $this->assertEquals(
  66              array(0 => 't1', 1 => 't2', 2 => 't3'),
  67              question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't1', -1));
  68          $this->assertEquals(
  69              array(0 => 't2', 1 => 't1', 2 => 't3'),
  70              question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't2', -1));
  71          $this->assertEquals(
  72              array(0 => 't1', 1 => 't2', 2 => 't3'),
  73              question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 't3', +1));
  74          $this->assertEquals(
  75              array(0 => 't1', 1 => 't2', 2 => 't3'),
  76              question_reorder_qtypes(array('t1' => '', 't2' => '', 't3' => ''), 'missing', +1));
  77      }
  78  
  79      public function test_match_grade_options() {
  80          $gradeoptions = question_bank::fraction_options_full();
  81  
  82          $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'error'));
  83          $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'error'));
  84          $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'error'));
  85          $this->assertFalse(match_grade_options($gradeoptions, 0.3333, 'error'));
  86  
  87          $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.3333333, 'nearest'));
  88          $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.333333, 'nearest'));
  89          $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33333, 'nearest'));
  90          $this->assertEquals(0.3333333, match_grade_options($gradeoptions, 0.33, 'nearest'));
  91  
  92          $this->assertEquals(-0.1428571, match_grade_options($gradeoptions, -0.15, 'nearest'));
  93      }
  94  
  95      /**
  96       * This function tests that the functions responsible for moving questions to
  97       * different contexts also updates the tag instances associated with the questions.
  98       */
  99      public function test_altering_tag_instance_context() {
 100          global $CFG, $DB;
 101  
 102          // Set to admin user.
 103          $this->setAdminUser();
 104  
 105          // Create two course categories - we are going to delete one of these later and will expect
 106          // all the questions belonging to the course in the deleted category to be moved.
 107          $coursecat1 = $this->getDataGenerator()->create_category();
 108          $coursecat2 = $this->getDataGenerator()->create_category();
 109  
 110          // Create a couple of categories and questions.
 111          $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
 112          $questioncat1 = $questiongenerator->create_question_category(array('contextid' =>
 113              context_coursecat::instance($coursecat1->id)->id));
 114          $questioncat2 = $questiongenerator->create_question_category(array('contextid' =>
 115              context_coursecat::instance($coursecat2->id)->id));
 116          $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id));
 117          $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat1->id));
 118          $question3 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id));
 119          $question4 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat2->id));
 120  
 121          // Now lets tag these questions.
 122          tag_set('question', $question1->id, array('tag 1', 'tag 2'), 'core_question', $questioncat1->contextid);
 123          tag_set('question', $question2->id, array('tag 3', 'tag 4'), 'core_question', $questioncat1->contextid);
 124          tag_set('question', $question3->id, array('tag 5', 'tag 6'), 'core_question', $questioncat2->contextid);
 125          tag_set('question', $question4->id, array('tag 7', 'tag 8'), 'core_question', $questioncat2->contextid);
 126  
 127          // Test moving the questions to another category.
 128          question_move_questions_to_category(array($question1->id, $question2->id), $questioncat2->id);
 129  
 130          // Test that all tag_instances belong to one context.
 131          $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
 132              'contextid' => $questioncat2->contextid)));
 133  
 134          // Test moving them back.
 135          question_move_questions_to_category(array($question1->id, $question2->id), $questioncat1->id);
 136  
 137          // Test that all tag_instances are now reset to how they were initially.
 138          $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
 139              'contextid' => $questioncat1->contextid)));
 140          $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
 141              'contextid' => $questioncat2->contextid)));
 142  
 143          // Now test moving a whole question category to another context.
 144          question_move_category_to_context($questioncat1->id, $questioncat1->contextid, $questioncat2->contextid);
 145  
 146          // Test that all tag_instances belong to one context.
 147          $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
 148              'contextid' => $questioncat2->contextid)));
 149  
 150          // Now test moving them back.
 151          question_move_category_to_context($questioncat1->id, $questioncat2->contextid,
 152              context_coursecat::instance($coursecat1->id)->id);
 153  
 154          // Test that all tag_instances are now reset to how they were initially.
 155          $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
 156              'contextid' => $questioncat1->contextid)));
 157          $this->assertEquals(4, $DB->count_records('tag_instance', array('component' => 'core_question',
 158              'contextid' => $questioncat2->contextid)));
 159  
 160          // Now we want to test deleting the course category and moving the questions to another category.
 161          question_delete_course_category($coursecat1, $coursecat2, false);
 162  
 163          // Test that all tag_instances belong to one context.
 164          $this->assertEquals(8, $DB->count_records('tag_instance', array('component' => 'core_question',
 165              'contextid' => $questioncat2->contextid)));
 166  
 167          // Create a course.
 168          $course = $this->getDataGenerator()->create_course();
 169  
 170          // Create some question categories and questions in this course.
 171          $questioncat = $questiongenerator->create_question_category(array('contextid' =>
 172              context_course::instance($course->id)->id));
 173          $question1 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id));
 174          $question2 = $questiongenerator->create_question('shortanswer', null, array('category' => $questioncat->id));
 175  
 176          // Add some tags to these questions.
 177          tag_set('question', $question1->id, array('tag 1', 'tag 2'), 'core_question', $questioncat->contextid);
 178          tag_set('question', $question2->id, array('tag 1', 'tag 2'), 'core_question', $questioncat->contextid);
 179  
 180          // Create a course that we are going to restore the other course to.
 181          $course2 = $this->getDataGenerator()->create_course();
 182  
 183          // Create backup file and save it to the backup location.
 184          $bc = new backup_controller(backup::TYPE_1COURSE, $course->id, backup::FORMAT_MOODLE,
 185              backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
 186          $bc->execute_plan();
 187          $results = $bc->get_results();
 188          $file = $results['backup_destination'];
 189          $fp = get_file_packer();
 190          $filepath = $CFG->dataroot . '/temp/backup/test-restore-course';
 191          $file->extract_to_pathname($fp, $filepath);
 192          $bc->destroy();
 193          unset($bc);
 194  
 195          // Now restore the course.
 196          $rc = new restore_controller('test-restore-course', $course2->id, backup::INTERACTIVE_NO,
 197              backup::MODE_GENERAL, 2, backup::TARGET_NEW_COURSE);
 198          $rc->execute_precheck();
 199          $rc->execute_plan();
 200  
 201          // Get the created question category.
 202          $restoredcategory = $DB->get_record('question_categories', array('contextid' => context_course::instance($course2->id)->id),
 203              '*', MUST_EXIST);
 204  
 205          // Check that there are two questions in the restored to course's context.
 206          $this->assertEquals(2, $DB->count_records('question', array('category' => $restoredcategory->id)));
 207      }
 208  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1