1 |
efrain |
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 |
* Test case for sqlsrv dml support.
|
|
|
19 |
*
|
|
|
20 |
* @package core
|
|
|
21 |
* @category test
|
|
|
22 |
* @copyright 2017 John Okely
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace core;
|
|
|
27 |
|
|
|
28 |
use sqlsrv_native_moodle_database;
|
|
|
29 |
|
|
|
30 |
defined('MOODLE_INTERNAL') || die();
|
|
|
31 |
|
|
|
32 |
global $CFG;
|
|
|
33 |
require_once($CFG->dirroot.'/lib/dml/sqlsrv_native_moodle_database.php');
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Test case for sqlsrv dml support.
|
|
|
37 |
*
|
|
|
38 |
* @package core
|
|
|
39 |
* @category test
|
|
|
40 |
* @copyright 2017 John Okely
|
|
|
41 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
42 |
*/
|
|
|
43 |
class sqlsrv_native_moodle_database_test extends \advanced_testcase {
|
|
|
44 |
|
|
|
45 |
public function setUp(): void {
|
|
|
46 |
parent::setUp();
|
|
|
47 |
$this->resetAfterTest();
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Dataprovider for test_add_no_lock_to_temp_tables
|
|
|
52 |
* @return array Data for test_add_no_lock_to_temp_tables
|
|
|
53 |
*/
|
|
|
54 |
public function add_no_lock_to_temp_tables_provider() {
|
|
|
55 |
return [
|
|
|
56 |
"Basic temp table, nothing following" => [
|
|
|
57 |
'input' => 'SELECT * FROM {table_temp}',
|
|
|
58 |
'expected' => 'SELECT * FROM {table_temp} WITH (NOLOCK)'
|
|
|
59 |
],
|
|
|
60 |
"Basic temp table, with capitalised alias" => [
|
|
|
61 |
'input' => 'SELECT * FROM {table_temp} MYTABLE',
|
|
|
62 |
'expected' => 'SELECT * FROM {table_temp} MYTABLE WITH (NOLOCK)'
|
|
|
63 |
],
|
|
|
64 |
"Temp table with alias, and another non-temp table" => [
|
|
|
65 |
'input' => 'SELECT * FROM {table_temp} x WHERE y in (SELECT y from {table2})',
|
|
|
66 |
'expected' => 'SELECT * FROM {table_temp} x WITH (NOLOCK) WHERE y in (SELECT y from {table2})'
|
|
|
67 |
],
|
|
|
68 |
"Temp table with reserve word following, no alias" => [
|
|
|
69 |
'input' => 'SELECT DISTINCT * FROM {table_temp} WHERE y in (SELECT y from {table2} nottemp)',
|
|
|
70 |
'expected' => 'SELECT DISTINCT * FROM {table_temp} WITH (NOLOCK) WHERE y in (SELECT y from {table2} nottemp)'
|
|
|
71 |
],
|
|
|
72 |
"Temp table with reserve word, lower case" => [
|
|
|
73 |
'input' => 'SELECT DISTINCT * FROM {table_temp} where y in (SELECT y from {table2} nottemp)',
|
|
|
74 |
'expected' => 'SELECT DISTINCT * FROM {table_temp} WITH (NOLOCK) where y in (SELECT y from {table2} nottemp)'
|
|
|
75 |
],
|
|
|
76 |
"Another reserve word test" => [
|
|
|
77 |
'input' => 'SELECT DISTINCT * FROM {table_temp} PIVOT y in (SELECT y from {table2} nottemp)',
|
|
|
78 |
'expected' => 'SELECT DISTINCT * FROM {table_temp} WITH (NOLOCK) PIVOT y in (SELECT y from {table2} nottemp)'
|
|
|
79 |
],
|
|
|
80 |
"Another reserve word test should fail" => [
|
|
|
81 |
'input' => 'SELECT DISTINCT * FROM {table_temp} PIVOT y in (SELECT y from {table2} nottemp)',
|
|
|
82 |
'expected' => 'SELECT DISTINCT * FROM {table_temp} WITH (NOLOCK) PIVOT y in (SELECT y from {table2} nottemp)'
|
|
|
83 |
],
|
|
|
84 |
"Temp table with an alias starting with a keyword" => [
|
|
|
85 |
'input' => 'SELECT * FROM {table_temp} asx',
|
|
|
86 |
'expected' => 'SELECT * FROM {table_temp} asx WITH (NOLOCK)'
|
|
|
87 |
],
|
|
|
88 |
"Keep alias with underscore" => [
|
|
|
89 |
'input' => 'SELECT * FROM {table_temp} alias_for_table',
|
|
|
90 |
'expected' => 'SELECT * FROM {table_temp} alias_for_table WITH (NOLOCK)'
|
|
|
91 |
],
|
|
|
92 |
"Alias with number" => [
|
|
|
93 |
'input' => 'SELECT * FROM {table_temp} a5 WHERE y',
|
|
|
94 |
'expected' => 'SELECT * FROM {table_temp} a5 WITH (NOLOCK) WHERE y'
|
|
|
95 |
],
|
|
|
96 |
"Alias with number and underscore" => [
|
|
|
97 |
'input' => 'SELECT * FROM {table_temp} a_5 WHERE y',
|
|
|
98 |
'expected' => 'SELECT * FROM {table_temp} a_5 WITH (NOLOCK) WHERE y'
|
|
|
99 |
],
|
|
|
100 |
"Temp table in subquery" => [
|
|
|
101 |
'input' => 'select * FROM (SELECT DISTINCT * FROM {table_temp})',
|
|
|
102 |
'expected' => 'select * FROM (SELECT DISTINCT * FROM {table_temp} WITH (NOLOCK))'
|
|
|
103 |
],
|
|
|
104 |
"Temp table in subquery, with following commands" => [
|
|
|
105 |
'input' => 'select * FROM (SELECT DISTINCT * FROM {table_temp} ) WHERE y',
|
|
|
106 |
'expected' => 'select * FROM (SELECT DISTINCT * FROM {table_temp} WITH (NOLOCK) ) WHERE y'
|
|
|
107 |
],
|
|
|
108 |
"Temp table in subquery, with alias" => [
|
|
|
109 |
'input' => 'select * FROM (SELECT DISTINCT * FROM {table_temp} x) WHERE y',
|
|
|
110 |
'expected' => 'select * FROM (SELECT DISTINCT * FROM {table_temp} x WITH (NOLOCK)) WHERE y'
|
|
|
111 |
],
|
|
|
112 |
];
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
/**
|
|
|
116 |
* Test add_no_lock_to_temp_tables
|
|
|
117 |
*
|
|
|
118 |
* @param string $input The input SQL query
|
|
|
119 |
* @param string $expected The expected resultant query
|
|
|
120 |
* @dataProvider add_no_lock_to_temp_tables_provider
|
|
|
121 |
*/
|
11 |
efrain |
122 |
public function test_add_no_lock_to_temp_tables($input, $expected): void {
|
1 |
efrain |
123 |
$sqlsrv = new sqlsrv_native_moodle_database();
|
|
|
124 |
|
|
|
125 |
$reflector = new \ReflectionObject($sqlsrv);
|
|
|
126 |
|
|
|
127 |
$method = $reflector->getMethod('add_no_lock_to_temp_tables');
|
|
|
128 |
|
|
|
129 |
$temptablesproperty = $reflector->getProperty('temptables');
|
|
|
130 |
$temptables = new temptables_tester();
|
|
|
131 |
|
|
|
132 |
$temptablesproperty->setValue($sqlsrv, $temptables);
|
|
|
133 |
|
|
|
134 |
$result = $method->invoke($sqlsrv, $input);
|
|
|
135 |
|
|
|
136 |
$temptablesproperty->setValue($sqlsrv, null);
|
|
|
137 |
$this->assertEquals($expected, $result);
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Data provider for test_has_query_order_by
|
|
|
142 |
*
|
|
|
143 |
* @return array data for test_has_query_order_by
|
|
|
144 |
*/
|
|
|
145 |
public function has_query_order_by_provider() {
|
|
|
146 |
// Fixtures taken from https://docs.moodle.org/en/ad-hoc_contributed_reports.
|
|
|
147 |
|
|
|
148 |
return [
|
|
|
149 |
'User with language => FALSE' => [
|
|
|
150 |
'sql' => <<<EOT
|
|
|
151 |
SELECT username, lang
|
|
|
152 |
FROM prefix_user
|
|
|
153 |
EOT
|
|
|
154 |
,
|
|
|
155 |
'expectedmainquery' => <<<EOT
|
|
|
156 |
SELECT username, lang
|
|
|
157 |
FROM prefix_user
|
|
|
158 |
EOT
|
|
|
159 |
,
|
|
|
160 |
'expectedresult' => false
|
|
|
161 |
],
|
|
|
162 |
'List Users with extra info (email) in current course => FALSE' => [
|
|
|
163 |
'sql' => <<<EOT
|
|
|
164 |
SELECT u.firstname, u.lastname, u.email
|
|
|
165 |
FROM prefix_role_assignments AS ra
|
|
|
166 |
JOIN prefix_context AS context ON context.id = ra.contextid AND context.contextlevel = 50
|
|
|
167 |
JOIN prefix_course AS c ON c.id = context.instanceid AND c.id = %%COURSEID%%
|
|
|
168 |
JOIN prefix_user AS u ON u.id = ra.userid
|
|
|
169 |
EOT
|
|
|
170 |
,
|
|
|
171 |
'expectedmainquery' => <<<EOT
|
|
|
172 |
SELECT u.firstname, u.lastname, u.email
|
|
|
173 |
FROM prefix_role_assignments AS ra
|
|
|
174 |
JOIN prefix_context AS context ON context.id = ra.contextid AND context.contextlevel = 50
|
|
|
175 |
JOIN prefix_course AS c ON c.id = context.instanceid AND c.id = %%COURSEID%%
|
|
|
176 |
JOIN prefix_user AS u ON u.id = ra.userid
|
|
|
177 |
EOT
|
|
|
178 |
,
|
|
|
179 |
'expectedresult' => false
|
|
|
180 |
],
|
|
|
181 |
'ROW_NUMBER() OVER (ORDER BY ...) => FALSE (https://github.com/jleyva/moodle-block_configurablereports/issues/120)' => [
|
|
|
182 |
'sql' => <<<EOT
|
|
|
183 |
SELECT COUNT(*) AS 'Users who have logged in today'
|
|
|
184 |
FROM (
|
|
|
185 |
SELECT ROW_NUMBER() OVER(ORDER BY lastaccess DESC) AS Row
|
|
|
186 |
FROM mdl_user
|
|
|
187 |
WHERE lastaccess > DATEDIFF(s, '1970-01-01 02:00:00', (SELECT Convert(DateTime, DATEDIFF(DAY, 0, GETDATE()))))
|
|
|
188 |
) AS Logins
|
|
|
189 |
EOT
|
|
|
190 |
,
|
|
|
191 |
'expectedmainquery' => <<<EOT
|
|
|
192 |
SELECT COUNT() AS 'Users who have logged in today'
|
|
|
193 |
FROM () AS Logins
|
|
|
194 |
EOT
|
|
|
195 |
,
|
|
|
196 |
'expectedresult' => false
|
|
|
197 |
],
|
|
|
198 |
'CONTRIB-7725 workaround) => TRUE' => [
|
|
|
199 |
'sql' => <<<EOT
|
|
|
200 |
SELECT COUNT(*) AS 'Users who have logged in today'
|
|
|
201 |
FROM (
|
|
|
202 |
SELECT ROW_NUMBER() OVER(ORDER BY lastaccess DESC) AS Row
|
|
|
203 |
FROM mdl_user
|
|
|
204 |
WHERE lastaccess > DATEDIFF(s, '1970-01-01 02:00:00', (SELECT Convert(DateTime, DATEDIFF(DAY, 0, GETDATE()))))
|
|
|
205 |
) AS Logins ORDER BY 1
|
|
|
206 |
EOT
|
|
|
207 |
,
|
|
|
208 |
'expectedmainquery' => <<<EOT
|
|
|
209 |
SELECT COUNT() AS 'Users who have logged in today'
|
|
|
210 |
FROM () AS Logins ORDER BY 1
|
|
|
211 |
EOT
|
|
|
212 |
,
|
|
|
213 |
'expectedresult' => true
|
|
|
214 |
],
|
|
|
215 |
'Enrolment count in each Course => TRUE' => [
|
|
|
216 |
'sql' => <<<EOT
|
|
|
217 |
SELECT c.fullname, COUNT(ue.id) AS Enroled
|
|
|
218 |
FROM prefix_course AS c
|
|
|
219 |
JOIN prefix_enrol AS en ON en.courseid = c.id
|
|
|
220 |
JOIN prefix_user_enrolments AS ue ON ue.enrolid = en.id
|
|
|
221 |
GROUP BY c.id
|
|
|
222 |
ORDER BY c.fullname
|
|
|
223 |
EOT
|
|
|
224 |
,
|
|
|
225 |
'expectedmainquery' => <<<EOT
|
|
|
226 |
SELECT c.fullname, COUNT() AS Enroled
|
|
|
227 |
FROM prefix_course AS c
|
|
|
228 |
JOIN prefix_enrol AS en ON en.courseid = c.id
|
|
|
229 |
JOIN prefix_user_enrolments AS ue ON ue.enrolid = en.id
|
|
|
230 |
GROUP BY c.id
|
|
|
231 |
ORDER BY c.fullname
|
|
|
232 |
EOT
|
|
|
233 |
,
|
|
|
234 |
'expectedresult' => true
|
|
|
235 |
],
|
|
|
236 |
];
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* Test has_query_order_by
|
|
|
241 |
*
|
|
|
242 |
* @dataProvider has_query_order_by_provider
|
|
|
243 |
* @param string $sql the query
|
|
|
244 |
* @param string $expectedmainquery the expected main query
|
|
|
245 |
* @param bool $expectedresult the expected result
|
|
|
246 |
*/
|
11 |
efrain |
247 |
public function test_has_query_order_by(string $sql, string $expectedmainquery, bool $expectedresult): void {
|
1 |
efrain |
248 |
$mainquery = preg_replace('/\(((?>[^()]+)|(?R))*\)/', '()', $sql);
|
|
|
249 |
$this->assertSame($expectedmainquery, $mainquery);
|
|
|
250 |
|
|
|
251 |
// The has_query_order_by static method is protected. Use Reflection to call the method.
|
|
|
252 |
$method = new \ReflectionMethod('sqlsrv_native_moodle_database', 'has_query_order_by');
|
|
|
253 |
$result = $method->invoke(null, $sql);
|
|
|
254 |
$this->assertSame($expectedresult, $result);
|
|
|
255 |
}
|
|
|
256 |
}
|
|
|
257 |
|
|
|
258 |
/**
|
|
|
259 |
* Test class for testing temptables
|
|
|
260 |
*
|
|
|
261 |
* @copyright 2017 John Okely
|
|
|
262 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
263 |
*/
|
|
|
264 |
class temptables_tester {
|
|
|
265 |
/**
|
|
|
266 |
* Returns if one table, based in the information present in the store, is a temp table
|
|
|
267 |
*
|
|
|
268 |
* For easy testing, anything with the word 'temp' in it is considered temporary.
|
|
|
269 |
*
|
|
|
270 |
* @param string $tablename name without prefix of the table we are asking about
|
|
|
271 |
* @return bool true if the table is a temp table (based in the store info), false if not
|
|
|
272 |
*/
|
|
|
273 |
public function is_temptable($tablename) {
|
|
|
274 |
if (strpos($tablename, 'temp') === false) {
|
|
|
275 |
return false;
|
|
|
276 |
} else {
|
|
|
277 |
return true;
|
|
|
278 |
}
|
|
|
279 |
}
|
|
|
280 |
/**
|
|
|
281 |
* Dispose the temptables
|
|
|
282 |
*
|
|
|
283 |
* @return void
|
|
|
284 |
*/
|
|
|
285 |
public function dispose() {
|
|
|
286 |
}
|
|
|
287 |
}
|