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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
<?php
/**
* Tests for DbSearch.class.php
*
* @package PhpMyAdmin-test
*/
/*
* Include to test.
*/
require_once 'libraries/DbSearch.class.php';
require_once 'libraries/php-gettext/gettext.inc';
require_once 'libraries/Util.class.php';
require_once 'libraries/url_generating.lib.php';
require_once 'libraries/core.lib.php';
require_once 'libraries/Theme.class.php';
require_once 'libraries/database_interface.inc.php';
require_once 'libraries/Tracker.class.php';
/**
* Tests for database search.
*
* @package PhpMyAdmin-test
*/
class PMA_DbSearch_Test extends PHPUnit_Framework_TestCase
{
/**
* @access protected
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*
* @access protected
* @return void
*/
protected function setUp()
{
$this->object = new PMA_DbSearch('pma_test');
$GLOBALS['server'] = 0;
$GLOBALS['cfg']['ServerDefault'] = 1;
$GLOBALS['cfg']['ShowHint'] = true;
$GLOBALS['db'] = 'pma';
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*
* @access protected
* @return void
*/
protected function tearDown()
{
unset($this->object);
}
/**
* Call protected functions by setting visibility to public.
*
* @param string $name method name
* @param array $params parameters for the invocation
*
* @return the output from the protected method.
*/
private function _callProtectedFunction($name, $params)
{
$class = new ReflectionClass('PMA_DbSearch');
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method->invokeArgs($this->object, $params);
}
/**
* Test for _getSearchSqls
*
* @return void
*/
public function testGetSearchSqls()
{
//mock DBI
$dbi = $this->getMockBuilder('PMA_DatabaseInterface')
->disableOriginalConstructor()
->getMock();
$dbi->expects($this->any())
->method('getColumns')
->with('pma', 'table1')
->will($this->returnValue(array()));
$GLOBALS['dbi'] = $dbi;
$this->assertEquals(
array (
'select_columns' => 'SELECT * FROM `pma`.`table1` WHERE FALSE',
'select_count' => 'SELECT COUNT(*) AS `count` FROM `pma`.`table1` ' .
'WHERE FALSE',
'delete' => 'DELETE FROM `pma`.`table1` WHERE FALSE'
),
$this->_callProtectedFunction(
'_getSearchSqls',
array('table1')
)
);
}
/**
* Test for getSearchResults
*
* @return void
*/
public function testGetSearchResults()
{
$this->assertEquals(
'<br /><table class="data"><caption class="tblHeaders">Search results '
. 'for "<i></i>" :</caption></table>',
$this->object->getSearchResults()
);
}
/**
* Test for _getResultsRow
*
* @param string $each_table Tables on which search is to be performed
* @param array $newsearchsqls Contains SQL queries
* @param bool $odd_row For displaying contrasting table rows
* @param string $output Expected HTML output
*
* @return void
*
* @dataProvider providerForTestGetResultsRow
*/
public function testGetResultsRow(
$each_table, $newsearchsqls, $odd_row, $output
) {
$this->assertEquals(
$output,
$this->_callProtectedFunction(
'_getResultsRow',
array($each_table, $newsearchsqls, $odd_row, 2)
)
);
}
/**
* Data provider for testGetResultRow
*
* @return array provider for testGetResultsRow
*/
public function providerForTestGetResultsRow()
{
return array(
array(
'table1',
array(
'SELECT * FROM `pma`.`table1` WHERE FALSE',
'SELECT COUNT(*) AS `count` FROM `pma`.`table1` WHERE FALSE',
'select_count' => 2,
'select_columns' => 'column1',
'delete' => 'column2'
),
true,
'<tr class="noclick odd"><td>2 matches in <strong>table1</strong>'
. '</td><td><a name="browse_search" class="ajax" '
. 'href="sql.php?db=pma&table'
. '=table1&goto=db_sql.php&pos=0&is_js_confirmed=0&'
. 'sql_query=column1&server=0&lang=en&'
. 'collation_connection=utf-8&token=token" '
. 'onclick="loadResult(\'sql.php?db=pma&table=table1&goto='
. 'db_sql.php&pos=0&is_js_confirmed=0&sql_query=column1'
. '&server=0&lang=en&collation_connection=utf-8'
. '&token=token\',\'table1\',\'?db=pma'
. '&table=table1&server=0&lang=en'
. '&collation_connection=utf-8&token=token\');'
. 'return false;" >Browse</a></td><td>'
. '<a name="delete_search" class="ajax" href'
. '="sql.php?db=pma&table=table1&goto=db_sql.php&pos=0'
. '&is_js_confirmed=0&sql_query=column2&server=0&'
. 'lang=en&collation_connection=utf-8&token=token"'
. ' onclick="deleteResult(\'sql.php?db=pma'
. '&table=table1&goto=db_sql.php&pos=0&is_js_'
. 'confirmed=0&sql_query=column2&server=0&lang=en'
. '&collation_connection=utf-8&'
. 'token=token\' , \'Delete the matches for the table1 table?\');'
. 'return false;">Delete</a></td></tr>'
)
);
}
/**
* Test for getSelectionForm
*
* @return void
*/
public function testGetSelectionForm()
{
$_SESSION['PMA_Theme'] = new PMA_Theme();
$GLOBALS['pmaThemeImage'] = 'themes/dot.gif';
$this->assertEquals(
'<a id="db_search"></a><form id="db_search_form" class="ajax lock-page" '
. 'method="post" action="db_search.php" name="db_search">'
. '<input type="hidden" name="db" value="pma" />'
. '<input type="hidden" name="lang" value="en" />'
. '<input type="hidden" name="collation_connection" value="utf-8" />'
. '<input type="hidden" name="token" value="token" />'
. '<fieldset><legend>Search in database</legend><table class='
. '"formlayout"><tr><td>Words or values to search for (wildcard: "%"):'
. '</td><td><input type="text" name="criteriaSearchString" size="60" '
. 'value="" /></td></tr><tr><td class="right vtop">Find:</td><td><input '
. 'type="radio" name="criteriaSearchType" id="criteriaSearchType_1" '
. 'value="1" checked="checked" />' . "\n"
. '<label for="criteriaSearchType_1">at least one of the words<span '
. 'class="pma_hint"><img src="themes/dot.gifb_help.png" title="" alt="" '
. '/><span class="hide">Words are separated by a space character (" ").'
. '</span></span></label><br />' . "\n"
. '<input type="radio" name="criteriaSearchType" id="criteriaSearchType'
. '_2" value="2" />' . "\n"
. '<label for="criteriaSearchType_2">all words<span class="pma_hint">'
. '<img src="themes/dot.gifb_help.png" title="" alt="" /><span class'
. '="hide">Words are separated by a space character (" ").</span></span>'
. '</label><br />' . "\n"
. '<input type="radio" name="criteriaSearchType" id="criteriaSearchType'
. '_3" value="3" />' . "\n"
. '<label for="criteriaSearchType_3">the exact phrase</label><br />'
. "\n" . '<input type="radio" name="criteriaSearchType" id="criteria'
. 'SearchType_4" value="4" />' . "\n"
. '<label for="criteriaSearchType_4">as regular expression <a href='
. '"./url.php?url=http%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.6%2Fen'
. '%2Fregexp.html" target='
. '"mysql_doc"><img src="themes/dot.gifb_help.png" title="Documentation"'
. ' alt="Documentation" /></a></label><br />' . "\n"
. '</td></tr><tr><td class="right vtop">Inside tables:</td>'
. '<td rowspan="2"><select name="criteriaTables[]" size="6" '
. 'multiple="multiple"><option value="table1">table1</option>'
. '<option value="table2">table2</option></select></td></tr><tr>'
. '<td class="right vbottom"><a href="#" onclick="setSelectOptions'
. '(\'db_search\', \'criteriaTables[]\', true); return false;">Select '
. 'All</a> / <a href="#" onclick="setSelectOptions'
. '(\'db_search\', \'criteriaTables[]\', false); return false;">Unselect'
. ' All</a></td></tr><tr><td class="right">Inside column:</td><td>'
. '<input type="text" name="criteriaColumnName" size="60"value="" />'
. '</td></tr></table></fieldset><fieldset class="tblFooters"><input '
. 'type="submit" name="submit_search" value="Go" id="buttonGo" />'
. '</fieldset></form><div id="togglesearchformdiv">'
. '<a id="togglesearchformlink"></a></div>',
$this->object->getSelectionForm()
);
}
/**
* Test for getResultDivs
*
* @return void
*/
public function testGetResultDivs()
{
$this->assertEquals(
'<!-- These two table-image and table-link elements display the '
. 'table name in browse search results --><div id="table-info">'
. '<a class="item" id="table-link" ></a></div><div id="browse-results">'
. '<!-- this browse-results div is used to load the browse and delete '
. 'results in the db search --></div><br class="clearfloat" />'
. '<div id="sqlqueryform"><!-- this sqlqueryform div is used to load the'
. ' delete form in the db search --></div><!-- toggle query box link-->'
. '<a id="togglequerybox"></a>',
$this->_callProtectedFunction(
'getResultDivs',
array()
)
);
}
}
|