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 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 core_question;
  18  
  19  use qubaid_condition;
  20  use qubaid_join;
  21  use qubaid_list;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  require_once (__DIR__ . '/../lib.php');
  27  
  28  /**
  29   * Unit tests for qubaid_condition and subclasses.
  30   *
  31   * @package    core_question
  32   * @category   test
  33   * @copyright  2009 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class qubaid_condition_test extends \advanced_testcase {
  37  
  38      protected function normalize_sql($sql, $params) {
  39          $newparams = array();
  40          preg_match_all('/(?<!:):([a-z][a-z0-9_]*)/', $sql, $named_matches);
  41          foreach($named_matches[1] as $param) {
  42              if (array_key_exists($param, $params)) {
  43                  $newparams[] = $params[$param];
  44              }
  45          }
  46          $newsql = preg_replace('/(?<!:):[a-z][a-z0-9_]*/', '?', $sql);
  47          return array($newsql, $newparams);
  48      }
  49  
  50      protected function check_typical_question_attempts_query(
  51              qubaid_condition $qubaids, $expectedsql, $expectedparams) {
  52          $sql = "SELECT qa.id, qa.maxmark
  53              FROM {$qubaids->from_question_attempts('qa')}
  54              WHERE {$qubaids->where()} AND qa.slot = :slot";
  55          $params = $qubaids->from_where_params();
  56          $params['slot'] = 1;
  57  
  58          // NOTE: parameter names may change thanks to $DB->inorequaluniqueindex, normal comparison is very wrong!!
  59          list($sql, $params) = $this->normalize_sql($sql, $params);
  60          list($expectedsql, $expectedparams) = $this->normalize_sql($expectedsql, $expectedparams);
  61  
  62          $this->assertEquals($expectedsql, $sql);
  63          $this->assertEquals($expectedparams, $params);
  64      }
  65  
  66      protected function check_typical_in_query(qubaid_condition $qubaids,
  67              $expectedsql, $expectedparams) {
  68          $sql = "SELECT qa.id, qa.maxmark
  69              FROM {question_attempts} qa
  70              WHERE qa.questionusageid {$qubaids->usage_id_in()}";
  71  
  72          // NOTE: parameter names may change thanks to $DB->inorequaluniqueindex, normal comparison is very wrong!!
  73          list($sql, $params) = $this->normalize_sql($sql, $qubaids->usage_id_in_params());
  74          list($expectedsql, $expectedparams) = $this->normalize_sql($expectedsql, $expectedparams);
  75  
  76          $this->assertEquals($expectedsql, $sql);
  77          $this->assertEquals($expectedparams, $params);
  78      }
  79  
  80      public function test_qubaid_list_one_join() {
  81          $qubaids = new qubaid_list(array(1));
  82          $this->check_typical_question_attempts_query($qubaids,
  83                  "SELECT qa.id, qa.maxmark
  84              FROM {question_attempts} qa
  85              WHERE qa.questionusageid = :qubaid1 AND qa.slot = :slot",
  86              array('qubaid1' => 1, 'slot' => 1));
  87      }
  88  
  89      public function test_qubaid_list_several_join() {
  90          $qubaids = new qubaid_list(array(1, 3, 7));
  91          $this->check_typical_question_attempts_query($qubaids,
  92                  "SELECT qa.id, qa.maxmark
  93              FROM {question_attempts} qa
  94              WHERE qa.questionusageid IN (:qubaid2,:qubaid3,:qubaid4) AND qa.slot = :slot",
  95              array('qubaid2' => 1, 'qubaid3' => 3, 'qubaid4' => 7, 'slot' => 1));
  96      }
  97  
  98      public function test_qubaid_join() {
  99          $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid', 'ot.id = 1');
 100  
 101          $this->check_typical_question_attempts_query($qubaids,
 102                  "SELECT qa.id, qa.maxmark
 103              FROM {other_table} ot
 104                  JOIN {question_attempts} qa ON qa.questionusageid = ot.usageid
 105              WHERE ot.id = 1 AND qa.slot = :slot", array('slot' => 1));
 106      }
 107  
 108      public function test_qubaid_join_no_where_join() {
 109          $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid');
 110  
 111          $this->check_typical_question_attempts_query($qubaids,
 112                  "SELECT qa.id, qa.maxmark
 113              FROM {other_table} ot
 114                  JOIN {question_attempts} qa ON qa.questionusageid = ot.usageid
 115              WHERE 1 = 1 AND qa.slot = :slot", array('slot' => 1));
 116      }
 117  
 118      public function test_qubaid_list_one_in() {
 119          global $CFG;
 120          $qubaids = new qubaid_list(array(1));
 121          $this->check_typical_in_query($qubaids,
 122                  "SELECT qa.id, qa.maxmark
 123              FROM {question_attempts} qa
 124              WHERE qa.questionusageid = :qubaid5", array('qubaid5' => 1));
 125      }
 126  
 127      public function test_qubaid_list_several_in() {
 128          global $CFG;
 129          $qubaids = new qubaid_list(array(1, 2, 3));
 130          $this->check_typical_in_query($qubaids,
 131                  "SELECT qa.id, qa.maxmark
 132              FROM {question_attempts} qa
 133              WHERE qa.questionusageid IN (:qubaid6,:qubaid7,:qubaid8)",
 134                  array('qubaid6' => 1, 'qubaid7' => 2, 'qubaid8' => 3));
 135      }
 136  
 137      public function test_qubaid_join_in() {
 138          global $CFG;
 139          $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid', 'ot.id = 1');
 140  
 141          $this->check_typical_in_query($qubaids,
 142                  "SELECT qa.id, qa.maxmark
 143              FROM {question_attempts} qa
 144              WHERE qa.questionusageid IN (SELECT ot.usageid FROM {other_table} ot WHERE ot.id = 1)",
 145                  array());
 146      }
 147  
 148      public function test_qubaid_join_no_where_in() {
 149          global $CFG;
 150          $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid');
 151  
 152          $this->check_typical_in_query($qubaids,
 153                  "SELECT qa.id, qa.maxmark
 154              FROM {question_attempts} qa
 155              WHERE qa.questionusageid IN (SELECT ot.usageid FROM {other_table} ot WHERE 1 = 1)",
 156                  array());
 157      }
 158  }