1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
<?php
/**
* Test class to test rcube_db class
*
* @package Tests
*/
class Framework_DB extends PHPUnit_Framework_TestCase
{
/**
* Class constructor test
*/
function test_class()
{
$object = new rcube_db('test');
$this->assertInstanceOf('rcube_db', $object, "Class constructor");
}
/**
* Test script execution and table_prefix replacements
*/
function test_exec_script()
{
$db = new rcube_db_test_wrapper('test');
$db->set_option('table_prefix', 'prefix_');
$db->set_option('identifier_start', '`');
$db->set_option('identifier_end', '`');
$script = implode("\n", array(
"CREATE TABLE `xxx` (test int, INDEX xxx (test));",
"-- test comment",
"ALTER TABLE `xxx` CHANGE test test int;",
"TRUNCATE xxx;",
"DROP TABLE `vvv`;",
"CREATE TABLE `i` (test int CONSTRAINT `iii`
FOREIGN KEY (`test`) REFERENCES `xxx`(`test`) ON DELETE CASCADE ON UPDATE CASCADE);",
"INSERT INTO xxx test = 1;",
"SELECT test FROM xxx;",
));
$output = implode("\n", array(
"CREATE TABLE `prefix_xxx` (test int, INDEX prefix_xxx (test))",
"ALTER TABLE `prefix_xxx` CHANGE test test int",
"TRUNCATE prefix_xxx",
"DROP TABLE `prefix_vvv`",
"CREATE TABLE `prefix_i` (test int CONSTRAINT `prefix_iii`
FOREIGN KEY (`test`) REFERENCES `prefix_xxx`(`test`) ON DELETE CASCADE ON UPDATE CASCADE)",
"INSERT INTO prefix_xxx test = 1",
"SELECT test FROM prefix_xxx",
));
$result = $db->exec_script($script);
$out = array();
foreach ($db->queries as $q) {
$out[] = $q;
}
$this->assertTrue($result, "Execute SQL script (result)");
$this->assertSame(implode("\n", $out), $output, "Execute SQL script (content)");
}
/**
* Test query parsing and arguments quoting
*/
function test_query_parsing()
{
$db = new rcube_db_test_wrapper('test');
$db->set_option('identifier_start', '`');
$db->set_option('identifier_end', '`');
$db->query("SELECT ?", "test`test");
$db->query("SELECT ?", "test?test");
$db->query("SELECT ?", "test``test");
$db->query("SELECT ?", "test??test");
$db->query("SELECT `test` WHERE 'test``test'");
$db->query("SELECT `test` WHERE 'test??test'");
$db->query("SELECT `test` WHERE `test` = ?", "`te``st`");
$db->query("SELECT `test` WHERE `test` = ?", "?test?");
$db->query("SELECT `test` WHERE `test` = ?", "????");
$expected = implode("\n", array(
"SELECT 'test`test'",
"SELECT 'test?test'",
"SELECT 'test``test'",
"SELECT 'test??test'",
"SELECT `test` WHERE 'test`test'",
"SELECT `test` WHERE 'test?test'",
"SELECT `test` WHERE `test` = '`te``st`'",
"SELECT `test` WHERE `test` = '?test?'",
"SELECT `test` WHERE `test` = '????'",
));
$this->assertSame($expected, implode("\n", $db->queries), "Query parsing [1]");
$db->set_option('identifier_start', '"');
$db->set_option('identifier_end', '"');
$db->queries = array();
$db->query("SELECT ?", "test`test");
$db->query("SELECT ?", "test?test");
$db->query("SELECT ?", "test``test");
$db->query("SELECT ?", "test??test");
$db->query("SELECT `test` WHERE 'test``test'");
$db->query("SELECT `test` WHERE 'test??test'");
$db->query("SELECT `test` WHERE `test` = ?", "`te``st`");
$db->query("SELECT `test` WHERE `test` = ?", "?test?");
$db->query("SELECT `test` WHERE `test` = ?", "????");
$expected = implode("\n", array(
"SELECT 'test`test'",
"SELECT 'test?test'",
"SELECT 'test``test'",
"SELECT 'test??test'",
"SELECT \"test\" WHERE 'test`test'",
"SELECT \"test\" WHERE 'test?test'",
"SELECT \"test\" WHERE \"test\" = '`te``st`'",
"SELECT \"test\" WHERE \"test\" = '?test?'",
"SELECT \"test\" WHERE \"test\" = '????'",
));
$this->assertSame($expected, implode("\n", $db->queries), "Query parsing [2]");
}
}
/**
* rcube_db wrapper to test some protected methods
*/
class rcube_db_test_wrapper extends rcube_db
{
public $queries = array();
protected function query_execute($query)
{
$this->queries[] = $query;
}
public function db_connect($mode, $force = false)
{
$this->dbh = new rcube_db_test_dbh();
}
public function is_connected()
{
return true;
}
protected function debug($data)
{
}
}
class rcube_db_test_dbh
{
public function quote($data, $type)
{
return "'$data'";
}
}
|