-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWP_SQLite_Metadata_Tests.php
385 lines (325 loc) · 9.34 KB
/
WP_SQLite_Metadata_Tests.php
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
use PHPUnit\Framework\TestCase;
class WP_SQLite_Metadata_Tests extends TestCase {
private $engine;
private $sqlite;
public static function setUpBeforeClass(): void {
// if ( ! defined( 'PDO_DEBUG' )) {
// define( 'PDO_DEBUG', true );
// }
if ( ! defined( 'FQDB' ) ) {
define( 'FQDB', ':memory:' );
define( 'FQDBDIR', __DIR__ . '/../testdb' );
}
error_reporting( E_ALL & ~E_DEPRECATED );
if ( ! isset( $GLOBALS['table_prefix'] ) ) {
$GLOBALS['table_prefix'] = 'wptests_';
}
if ( ! isset( $GLOBALS['wpdb'] ) ) {
$GLOBALS['wpdb'] = new stdClass();
$GLOBALS['wpdb']->suppress_errors = false;
$GLOBALS['wpdb']->show_errors = true;
}
return;
}
// Before each test, we create a new database
public function setUp(): void {
global $blog_tables;
$queries = explode( ';', $blog_tables );
$this->sqlite = new PDO( 'sqlite::memory:' );
$this->engine = new WP_SQLite_Translator( $this->sqlite );
$translator = $this->engine;
try {
$translator->begin_transaction();
foreach ( $queries as $query ) {
$query = trim( $query );
if ( empty( $query ) ) {
continue;
}
$result = $translator->execute_sqlite_query( $query );
if ( false === $result ) {
throw new PDOException( $translator->get_error_message() );
}
}
$translator->commit();
} catch ( PDOException $err ) {
$err_data =
$err->errorInfo; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$err_code = $err_data[1];
$translator->rollback();
$message = sprintf(
'Error occurred while creating tables or indexes...<br />Query was: %s<br />',
var_export( $query, true )
);
$message .= sprintf( 'Error message is: %s', $err_data[2] );
wp_die( $message, 'Database Error!' );
}
}
public function testCountTables() {
$this->assertQuery( "SELECT count(*) FROM information_schema.tables WHERE table_schema = 'wpdata'" );
$actual = $this->engine->get_query_results();
$count = array_values( get_object_vars( $actual[0] ) )[0];
self::assertIsNumeric( $count );
}
public function testInformationSchemaTables() {
$result = $this->assertQuery( "SELECT * FROM information_schema.tables WHERE TABLE_NAME = 'wp_options'" );
$this->assertEquals(
array(
'TABLE_CATALOG' => 'def',
'TABLE_SCHEMA' => '',
'TABLE_NAME' => 'wp_options',
'TABLE_TYPE' => 'BASE TABLE',
'ENGINE' => 'InnoDB',
'ROW_FORMAT' => 'Dynamic',
'TABLE_COLLATION' => 'utf8mb4_general_ci',
'AUTO_INCREMENT' => null,
'CREATE_TIME' => null,
'UPDATE_TIME' => null,
'CHECK_TIME' => null,
'TABLE_ROWS' => '0',
'AVG_ROW_LENGTH' => '0',
'DATA_LENGTH' => '0',
'MAX_DATA_LENGTH' => '0',
'INDEX_LENGTH' => '0',
'DATA_FREE' => '0',
'CHECKSUM' => null,
'CREATE_OPTIONS' => '',
'VERSION' => '10',
'TABLE_COMMENT' => '',
),
(array) $result[0]
);
$result = $this->assertQuery(
"SELECT
table_name as 'name',
engine AS 'engine',
FLOOR( data_length / 1024 / 1024 ) 'data'
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'wp_posts'
ORDER BY name ASC;"
);
$this->assertEquals(
array(
'name' => 'wp_posts',
'engine' => 'InnoDB',
'data' => '0',
),
(array) $result[0]
);
}
public function testInformationSchemaQueryHidesSqliteSystemTables() {
/**
* By default, system tables are not returned.
*/
$result = $this->assertQuery( "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'sqlite_sequence'" );
$this->assertEquals( 0, count( $result ) );
/**
* If we use a custom name for the table_name column, system tables are returned.
*/
$result = $this->assertQuery( "SELECT TABLE_NAME as custom_name FROM INFORMATION_SCHEMA.TABLES WHERE custom_name = 'sqlite_sequence'" );
$this->assertEquals( 1, count( $result ) );
}
private function assertQuery( $sql, $error_substring = null ) {
$retval = $this->engine->query( $sql );
if ( null === $error_substring ) {
$this->assertEquals(
'',
$this->engine->get_error_message()
);
$this->assertNotFalse(
$retval
);
} else {
$this->assertStringContainsStringIgnoringCase( $error_substring, $this->engine->get_error_message() );
}
return $retval;
}
public function testCheckTable() {
/* a good table */
$table_name = 'wp_options';
$expected_result = array(
(object) array(
'Table' => $table_name,
'Op' => 'check',
'Msg_type' => 'status',
'Msg_text' => 'OK',
),
);
$this->assertQuery(
"CHECK TABLE $table_name;"
);
$this->assertEquals(
$expected_result,
$this->engine->get_query_results()
);
/* a different good table */
$table_name = 'wp_postmeta';
$expected_result = array(
(object) array(
'Table' => $table_name,
'Op' => 'check',
'Msg_type' => 'status',
'Msg_text' => 'OK',
),
);
$this->assertQuery(
"CHECK TABLE $table_name;"
);
$this->assertEquals(
$expected_result,
$this->engine->get_query_results()
);
/* a bogus, missing, table */
$table_name = 'wp_sqlite_rocks';
$expected_result = array(
(object) array(
'Table' => $table_name,
'Op' => 'check',
'Msg_type' => 'Error',
'Msg_text' => "Table '$table_name' doesn't exist",
),
(object) array(
'Table' => $table_name,
'Op' => 'check',
'Msg_type' => 'status',
'Msg_text' => 'Operation failed',
),
);
$this->assertQuery(
"CHECK TABLE $table_name;"
);
$this->assertEquals(
$expected_result,
$this->engine->get_query_results()
);
}
public function testOptimizeTable() {
/* a good table */
$table_name = 'wp_options';
$this->assertQuery(
"OPTIMIZE TABLE $table_name;"
);
$actual = $this->engine->get_query_results();
array_map(
function ( $row ) {
$this->assertIsObject( $row );
$row = (array) $row;
$this->assertIsString( $row['Table'] );
$this->assertIsString( $row['Op'] );
$this->assertIsString( $row['Msg_type'] );
$this->assertIsString( $row['Msg_text'] );
},
$actual
);
$ok = array_filter(
$actual,
function ( $row ) {
$row = (array) $row;
return strtolower( $row['Msg_type'] ) === 'status' && strtolower( $row['Msg_text'] ) === 'ok';
}
);
$this->assertIsArray( $ok );
$this->assertGreaterThan( 0, count( $ok ) );
}
public function testRepairTable() {
/* a good table */
$table_name = 'wp_options';
$this->assertQuery(
"REPAIR TABLE $table_name;"
);
$actual = $this->engine->get_query_results();
array_map(
function ( $r ) {
$this->assertIsObject( $r );
$row = $r;
$row = (array) $row;
$this->assertIsString( $row['Table'] );
$this->assertIsString( $row['Op'] );
$this->assertIsString( $row['Msg_type'] );
$this->assertIsString( $row['Msg_text'] );
},
$actual
);
$ok = array_filter(
$actual,
function ( $row ) {
return strtolower( $row->Msg_type ) === 'status' && strtolower( $row->Msg_text ) === 'ok';
}
);
$this->assertIsArray( $ok );
$this->assertGreaterThan( 0, count( $ok ) );
}
// this tests for successful rejection of a bad query
public function testShowTableStatus() {
$this->assertQuery(
"INSERT INTO wp_comments ( comment_author, comment_content ) VALUES ( 'PhpUnit', 'Testing' )"
);
$this->assertQuery(
"INSERT INTO wp_comments ( comment_author, comment_content ) VALUES ( 'PhpUnit0', 'Testing0' ), ( 'PhpUnit1', 'Testing1' ), ( 'PhpUnit2', 'Testing2' )"
);
$this->assertTableEmpty( 'wp_comments', false );
$this->assertQuery(
'SHOW TABLE STATUS FROM wp;'
);
$actual = $this->engine->get_query_results();
$this->assertIsArray( $actual );
$this->assertGreaterThanOrEqual(
1,
count( $actual )
);
$this->assertIsObject( $actual[0] );
$rows = array_values(
array_filter(
$actual,
function ( $row ) {
$this->assertIsObject( $row );
$this->assertIsString( $row->Name );
$this->assertIsNumeric( $row->Rows );
return str_ends_with( $row->Name, 'comments' );
}
)
);
$this->assertEquals( 'wp_comments', $rows[0]->Name );
$this->assertEquals( 4, $rows[0]->Rows );
}
private function assertTableEmpty( $table_name, $empty_var ) {
$this->assertQuery(
"SELECT COUNT(*) num FROM $table_name"
);
$actual = $this->engine->get_query_results();
if ( $empty_var ) {
$this->assertEquals( 0, $actual[0]->num, "$table_name is not empty" );
} else {
$this->assertGreaterThan( 0, $actual[0]->num, "$table_name is empty" );
}
}
public function testTruncateTable() {
$this->assertQuery(
"INSERT INTO wp_comments ( comment_author, comment_content ) VALUES ( 'PhpUnit', 'Testing' )"
);
$this->assertQuery(
"INSERT INTO wp_comments ( comment_author, comment_content ) VALUES ( 'PhpUnit0', 'Testing0' ), ( 'PhpUnit1', 'Testing1' ), ( 'PhpUnit2', 'Testing2' )"
);
$this->assertTableEmpty( 'wp_comments', false );
$this->assertQuery(
'TRUNCATE TABLE wp_comments;'
);
$actual = $this->engine->get_query_results();
$this->assertEquals(
true,
$actual
);
$this->assertTableEmpty( 'wp_comments', true );
}
public function testBogusQuery() {
$this->assertQuery(
'SELECT 1, BOGUS(1) FROM bogus;',
'no such table: bogus'
);
$actual = $this->engine->get_query_results();
$this->assertEquals(
null,
$actual
);
}
}