|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once(dirname(__FILE__) . '/../lib.php'); 00030 00031 00038 class qubaid_condition_test extends UnitTestCaseUsingDatabase { 00039 00040 public function setUp() { 00041 parent::setUp(); 00042 $this->switch_to_test_db(); 00043 } 00044 00045 public function tearDown() { 00046 $this->revert_to_real_db(); 00047 parent::tearDown(); 00048 } 00049 00050 protected function check_typical_question_attempts_query( 00051 qubaid_condition $qubaids, $expectedsql, $expectedparams) { 00052 $sql = "SELECT qa.id, qa.maxmark 00053 FROM {$qubaids->from_question_attempts('qa')} 00054 WHERE {$qubaids->where()} AND qa.slot = :slot"; 00055 $this->assertEqual($expectedsql, $sql); 00056 00057 $params = $qubaids->from_where_params(); 00058 $params['slot'] = 1; 00059 $this->assertEqual($expectedparams, $params); 00060 } 00061 00062 protected function check_typical_in_query(qubaid_condition $qubaids, 00063 $expectedsql, $expectedparams) { 00064 $sql = "SELECT qa.id, qa.maxmark 00065 FROM {question_attempts} qa 00066 WHERE qa.questionusageid {$qubaids->usage_id_in()}"; 00067 $this->assertEqual($expectedsql, $sql); 00068 00069 $this->assertEqual($expectedparams, $qubaids->usage_id_in_params()); 00070 } 00071 00072 public function test_qubaid_list_one_join() { 00073 $qubaids = new qubaid_list(array(1)); 00074 $this->check_typical_question_attempts_query($qubaids, 00075 "SELECT qa.id, qa.maxmark 00076 FROM {question_attempts} qa 00077 WHERE qa.questionusageid = :qubaid1 AND qa.slot = :slot", 00078 array('qubaid1' => 1, 'slot' => 1)); 00079 } 00080 00081 public function test_qubaid_list_several_join() { 00082 $qubaids = new qubaid_list(array(1, 3, 7)); 00083 $this->check_typical_question_attempts_query($qubaids, 00084 "SELECT qa.id, qa.maxmark 00085 FROM {question_attempts} qa 00086 WHERE qa.questionusageid IN (:qubaid2,:qubaid3,:qubaid4) AND qa.slot = :slot", 00087 array('qubaid2' => 1, 'qubaid3' => 3, 'qubaid4' => 7, 'slot' => 1)); 00088 } 00089 00090 public function test_qubaid_join() { 00091 $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid', 'ot.id = 1'); 00092 00093 $this->check_typical_question_attempts_query($qubaids, 00094 "SELECT qa.id, qa.maxmark 00095 FROM {other_table} ot 00096 JOIN {question_attempts} qa ON qa.questionusageid = ot.usageid 00097 WHERE ot.id = 1 AND qa.slot = :slot", array('slot' => 1)); 00098 } 00099 00100 public function test_qubaid_join_no_where_join() { 00101 $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid'); 00102 00103 $this->check_typical_question_attempts_query($qubaids, 00104 "SELECT qa.id, qa.maxmark 00105 FROM {other_table} ot 00106 JOIN {question_attempts} qa ON qa.questionusageid = ot.usageid 00107 WHERE 1 = 1 AND qa.slot = :slot", array('slot' => 1)); 00108 } 00109 00110 public function test_qubaid_list_one_in() { 00111 global $CFG; 00112 $qubaids = new qubaid_list(array(1)); 00113 $this->check_typical_in_query($qubaids, 00114 "SELECT qa.id, qa.maxmark 00115 FROM {question_attempts} qa 00116 WHERE qa.questionusageid = :qubaid5", array('qubaid5' => 1)); 00117 } 00118 00119 public function test_qubaid_list_several_in() { 00120 global $CFG; 00121 $qubaids = new qubaid_list(array(1, 2, 3)); 00122 $this->check_typical_in_query($qubaids, 00123 "SELECT qa.id, qa.maxmark 00124 FROM {question_attempts} qa 00125 WHERE qa.questionusageid IN (:qubaid6,:qubaid7,:qubaid8)", 00126 array('qubaid6' => 1, 'qubaid7' => 2, 'qubaid8' => 3)); 00127 } 00128 00129 public function test_qubaid_join_in() { 00130 global $CFG; 00131 $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid', 'ot.id = 1'); 00132 00133 $this->check_typical_in_query($qubaids, 00134 "SELECT qa.id, qa.maxmark 00135 FROM {question_attempts} qa 00136 WHERE qa.questionusageid IN (SELECT ot.usageid FROM {other_table} ot WHERE ot.id = 1)", 00137 array()); 00138 } 00139 00140 public function test_qubaid_join_no_where_in() { 00141 global $CFG; 00142 $qubaids = new qubaid_join("{other_table} ot", 'ot.usageid'); 00143 00144 $this->check_typical_in_query($qubaids, 00145 "SELECT qa.id, qa.maxmark 00146 FROM {question_attempts} qa 00147 WHERE qa.questionusageid IN (SELECT ot.usageid FROM {other_table} ot WHERE 1 = 1)", 00148 array()); 00149 } 00150 }