[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/question/engine/tests/ -> datalib_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 parts of {@link question_engine_data_mapper}.
  19   *
  20   * @package   core_question
  21   * @category  test
  22   * @copyright 2014 The Open University
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once (__DIR__ . '/../lib.php');
  31  require_once (__DIR__ . '/helpers.php');
  32  
  33  
  34  /**
  35   * Unit tests for parts of {@link question_engine_data_mapper}.
  36   *
  37   * Note that many of the methods used when attempting questions, like
  38   * load_questions_usage_by_activity, insert_question_*, delete_steps are
  39   * tested elsewhere, e.g. by {@link question_usage_autosave_test}. We do not
  40   * re-test them here.
  41   *
  42   * @copyright 2014 The Open University
  43   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class question_engine_data_mapper_testcase extends qbehaviour_walkthrough_test_base {
  46  
  47      /**
  48       * We create two usages, each with two questions, a short-answer marked
  49       * out of 5, and and essay marked out of 10. We just start these attempts.
  50       *
  51       * Then we change the max mark for the short-answer question in one of the
  52       * usages to 20, using a qubaid_list, and verify.
  53       *
  54       * Then we change the max mark for the essay question in the other
  55       * usage to 2, using a qubaid_join, and verify.
  56       */
  57      public function test_set_max_mark_in_attempts() {
  58  
  59          // Set up some things the tests will need.
  60          $this->resetAfterTest();
  61          $dm = new question_engine_data_mapper();
  62  
  63          // Create the questions.
  64          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  65          $cat = $generator->create_question_category();
  66          $sa = $generator->create_question('shortanswer', null,
  67                  array('category' => $cat->id));
  68          $essay = $generator->create_question('essay', null,
  69                  array('category' => $cat->id));
  70  
  71          // Create the first usage.
  72          $q = question_bank::load_question($sa->id);
  73          $this->start_attempt_at_question($q, 'interactive', 5);
  74  
  75          $q = question_bank::load_question($essay->id);
  76          $this->start_attempt_at_question($q, 'interactive', 10);
  77  
  78          $this->finish();
  79          $this->save_quba();
  80          $usage1id = $this->quba->get_id();
  81  
  82          // Create the second usage.
  83          $this->quba = question_engine::make_questions_usage_by_activity('unit_test',
  84                  context_system::instance());
  85  
  86          $q = question_bank::load_question($sa->id);
  87          $this->start_attempt_at_question($q, 'interactive', 5);
  88          $this->process_submission(array('answer' => 'fish'));
  89  
  90          $q = question_bank::load_question($essay->id);
  91          $this->start_attempt_at_question($q, 'interactive', 10);
  92  
  93          $this->finish();
  94          $this->save_quba();
  95          $usage2id = $this->quba->get_id();
  96  
  97          // Test set_max_mark_in_attempts with a qubaid_list.
  98          $usagestoupdate = new qubaid_list(array($usage1id));
  99          $dm->set_max_mark_in_attempts($usagestoupdate, 1, 20.0);
 100          $quba1 = question_engine::load_questions_usage_by_activity($usage1id);
 101          $quba2 = question_engine::load_questions_usage_by_activity($usage2id);
 102          $this->assertEquals(20, $quba1->get_question_max_mark(1));
 103          $this->assertEquals(10, $quba1->get_question_max_mark(2));
 104          $this->assertEquals( 5, $quba2->get_question_max_mark(1));
 105          $this->assertEquals(10, $quba2->get_question_max_mark(2));
 106  
 107          // Test set_max_mark_in_attempts with a qubaid_join.
 108          $usagestoupdate = new qubaid_join('{question_usages} qu', 'qu.id',
 109                  'qu.id = :usageid', array('usageid' => $usage2id));
 110          $dm->set_max_mark_in_attempts($usagestoupdate, 2, 2.0);
 111          $quba1 = question_engine::load_questions_usage_by_activity($usage1id);
 112          $quba2 = question_engine::load_questions_usage_by_activity($usage2id);
 113          $this->assertEquals(20, $quba1->get_question_max_mark(1));
 114          $this->assertEquals(10, $quba1->get_question_max_mark(2));
 115          $this->assertEquals( 5, $quba2->get_question_max_mark(1));
 116          $this->assertEquals( 2, $quba2->get_question_max_mark(2));
 117  
 118          // Test the nothing to do case.
 119          $usagestoupdate = new qubaid_join('{question_usages} qu', 'qu.id',
 120                  'qu.id = :usageid', array('usageid' => -1));
 121          $dm->set_max_mark_in_attempts($usagestoupdate, 2, 2.0);
 122          $quba1 = question_engine::load_questions_usage_by_activity($usage1id);
 123          $quba2 = question_engine::load_questions_usage_by_activity($usage2id);
 124          $this->assertEquals(20, $quba1->get_question_max_mark(1));
 125          $this->assertEquals(10, $quba1->get_question_max_mark(2));
 126          $this->assertEquals( 5, $quba2->get_question_max_mark(1));
 127          $this->assertEquals( 2, $quba2->get_question_max_mark(2));
 128      }
 129  
 130      public function test_load_used_variants() {
 131          $this->resetAfterTest();
 132          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 133  
 134          $cat = $generator->create_question_category();
 135          $questiondata1 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
 136          $questiondata2 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
 137          $questiondata3 = $generator->create_question('shortanswer', null, array('category' => $cat->id));
 138  
 139          $quba = question_engine::make_questions_usage_by_activity('test', context_system::instance());
 140          $quba->set_preferred_behaviour('deferredfeedback');
 141          $question1 = question_bank::load_question($questiondata1->id);
 142          $question3 = question_bank::load_question($questiondata3->id);
 143          $quba->add_question($question1);
 144          $quba->add_question($question1);
 145          $quba->add_question($question3);
 146          $quba->start_all_questions();
 147          question_engine::save_questions_usage_by_activity($quba);
 148  
 149          $this->assertEquals(array(
 150                      $questiondata1->id => array(1 => 2),
 151                      $questiondata2->id => array(),
 152                      $questiondata3->id => array(1 => 1),
 153                  ), question_engine::load_used_variants(
 154                      array($questiondata1->id, $questiondata2->id, $questiondata3->id),
 155                      new qubaid_list(array($quba->get_id()))));
 156      }
 157  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1