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