Repository navigation
Expand file tree
/
Copy pathWP_MySQL_On_SQLite_Query_Tests.php
More file actions
718 lines (631 loc) · 22.8 KB
/
Copy pathWP_MySQL_On_SQLite_Query_Tests.php
File metadata and controls
718 lines (631 loc) · 22.8 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
<?php
use PHPUnit\Framework\TestCase;
$tables = <<<'SQL'
CREATE TABLE wp_users (
ID bigint(20) unsigned NOT NULL auto_increment,
user_login varchar(60) NOT NULL default '',
user_pass varchar(255) NOT NULL default '',
user_nicename varchar(50) NOT NULL default '',
user_email varchar(100) NOT NULL default '',
user_url varchar(100) NOT NULL default '',
user_registered datetime NOT NULL default '0000-00-00 00:00:00',
user_activation_key varchar(255) NOT NULL default '',
user_status int(11) NOT NULL default '0',
display_name varchar(250) NOT NULL default '',
PRIMARY KEY (ID),
KEY user_login_key (user_login),
KEY user_nicename (user_nicename),
KEY user_email (user_email)
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE TABLE wp_usermeta (
umeta_id bigint(20) unsigned NOT NULL auto_increment,
user_id bigint(20) unsigned NOT NULL default '0',
meta_key varchar(255) default NULL,
meta_value longtext,
PRIMARY KEY (umeta_id),
KEY user_id (user_id),
KEY meta_key (meta_key(191))
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE TABLE wp_posts (
ID bigint(20) unsigned NOT NULL auto_increment,
post_author bigint(20) unsigned NOT NULL default '0',
post_date datetime NOT NULL default '0000-00-00 00:00:00',
post_date_gmt datetime NOT NULL default '0000-00-00 00:00:00',
post_content longtext NOT NULL,
post_title text NOT NULL,
post_excerpt text NOT NULL,
post_status varchar(20) NOT NULL default 'publish',
comment_status varchar(20) NOT NULL default 'open',
ping_status varchar(20) NOT NULL default 'open',
post_password varchar(255) NOT NULL default '',
post_name varchar(200) NOT NULL default '',
to_ping text NOT NULL,
pinged text NOT NULL,
post_modified datetime NOT NULL default '0000-00-00 00:00:00',
post_modified_gmt datetime NOT NULL default '0000-00-00 00:00:00',
post_content_filtered longtext NOT NULL,
post_parent bigint(20) unsigned NOT NULL default '0',
guid varchar(255) NOT NULL default '',
menu_order int(11) NOT NULL default '0',
post_type varchar(20) NOT NULL default 'post',
post_mime_type varchar(100) NOT NULL default '',
comment_count bigint(20) NOT NULL default '0',
PRIMARY KEY (ID),
KEY post_name (post_name(191)),
KEY type_status_date (post_type,post_status,post_date,ID),
KEY post_parent (post_parent),
KEY post_author (post_author),
KEY type_status_author (post_type,post_status,post_author)
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE TABLE wp_postmeta (
meta_id bigint(20) unsigned NOT NULL auto_increment,
post_id bigint(20) unsigned NOT NULL default '0',
meta_key varchar(255) default NULL,
meta_value longtext,
PRIMARY KEY (meta_id),
KEY post_id (post_id),
KEY meta_key (meta_key(191))
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE TABLE wp_options (
option_id bigint(20) unsigned NOT NULL auto_increment,
option_name varchar(191) NOT NULL default '',
option_value longtext NOT NULL,
autoload varchar(20) NOT NULL default 'yes',
PRIMARY KEY (option_id),
UNIQUE KEY option_name (option_name),
KEY autoload (autoload)
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
SQL;
/**
* Unit tests using the WordPress table definitions.
*/
class WP_MySQL_On_SQLite_Query_Tests extends TestCase {
/** @var WP_MySQL_On_SQLite */
private $engine;
/** @var PDO */
private $sqlite;
/** @var mixed */
private $last_result;
/**
* Before each test, we create a new volatile database and WordPress tables.
*
* @return void
* @throws Exception
*/
public function setUp(): void {
/* This is the DDL for WordPress tables in SQLite syntax. */
global $tables;
$queries = explode( ';', $tables );
$pdo_class = PHP_VERSION_ID >= 80400 ? Pdo\Sqlite::class : PDO::class;
$this->sqlite = new $pdo_class( 'sqlite::memory:' );
$this->engine = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:dbname=wp',
null,
null,
array( 'sqlite_pdo' => $this->sqlite )
);
$this->engine->setAttribute( PDO::ATTR_STRINGIFY_FETCHES, true );
try {
$this->engine->beginTransaction();
foreach ( $queries as $query ) {
$query = trim( $query );
if ( empty( $query ) ) {
continue;
}
$this->engine->query( $query );
}
$this->engine->commit();
} catch ( Throwable $e ) {
$message = sprintf(
'Error occurred while creating tables or indexes...<br />Query was: %s<br />',
var_export( $query, true )
);
$message .= sprintf( 'Error message is: %s', $e->getMessage() );
wp_die( $message, 'Database Error!' );
}
/* Mock up some metadata rows. When meta_key starts with _, the custom field isn't visible to the editor. */
for ( $i = 1; $i <= 40; $i++ ) {
$k1 = 'visible_meta_key_' . str_pad( $i, 2, '0', STR_PAD_LEFT );
$k2 = '_invisible_meta_key_%_percent' . str_pad( $i, 2, '0', STR_PAD_LEFT );
$this->assertQuery(
"INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (1, '$k1', '$k1-value');"
);
$this->assertQuery(
"INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (1, '$k2', '$k2-value');"
);
}
/* Mock up some transients for testing. Site transients. Two expired, one in the future. */
$time = - 90;
foreach ( array( 'tag1', 'tag2', 'tag3' ) as $tag ) {
$tv = '_site_transient_' . $tag;
$tt = '_site_transient_timeout_' . $tag;
$this->assertQuery(
"INSERT INTO wp_options (option_name, option_value, autoload) VALUES ('$tv', '$tag', 'no');"
);
$this->assertQuery(
"INSERT INTO wp_options (option_name, option_value, autoload) VALUES ('$tt', UNIX_TIMESTAMP() + $time, 'no');"
);
$time += 60;
}
/* Ordinary transients. */
$time = - 90;
foreach ( array( 'tag4', 'tag5', 'tag6' ) as $tag ) {
$tv = '_transient_' . $tag;
$tt = '_transient_timeout_' . $tag;
$this->assertQuery(
"INSERT INTO wp_options (option_name, option_value, autoload) VALUES ('$tv', '$tag', 'no');"
);
$this->assertQuery(
"INSERT INTO wp_options (option_name, option_value, autoload) VALUES ('$tt', UNIX_TIMESTAMP() + $time, 'no');"
);
$time += 60;
}
}
public function testGreatestLeast() {
$q = <<<'QUERY'
SELECT GREATEST('a', 'b') letter;
QUERY;
$result = $this->assertQuery( $q );
$actual = $this->last_result;
$this->assertEquals( 1, count( $actual ) );
$this->assertEquals( 'b', $actual[0]->letter );
$q = <<<'QUERY'
SELECT LEAST('a', 'b') letter;
QUERY;
$result = $this->assertQuery( $q );
$actual = $this->last_result;
$this->assertEquals( 1, count( $actual ) );
$this->assertEquals( 'a', $actual[0]->letter );
$q = <<<'QUERY'
SELECT GREATEST(2, 1.5) num;
QUERY;
$result = $this->assertQuery( $q );
$actual = $this->last_result;
$this->assertEquals( 1, count( $actual ) );
$this->assertEquals( 2, $actual[0]->num );
$q = <<<'QUERY'
SELECT LEAST(2, 1.5, 1.0) num;
QUERY;
$result = $this->assertQuery( $q );
$actual = $this->last_result;
$this->assertEquals( 1, count( $actual ) );
$this->assertEquals( 1, $actual[0]->num );
}
public function testLikeEscapingSimpleNoSemicolon() {
$q = <<<'QUERY'
SELECT DISTINCT meta_key FROM wp_postmeta WHERE meta_key LIKE '\_%'
QUERY;
$result = $this->assertQuery( $q );
$actual = $this->last_result;
$this->assertEquals( 40, count( $actual ) );
}
public function testLikeEscapingPercent() {
$q = <<<'QUERY'
SELECT DISTINCT meta_key FROM wp_postmeta WHERE meta_key LIKE '%\_\%\_percent%'
QUERY;
$result = $this->assertQuery( $q );
$actual = $this->last_result;
$this->assertEquals( 40, count( $actual ) );
}
public function testLikeEscapingSimpleSemicolon() {
$q = <<<'QUERY'
SELECT DISTINCT meta_key FROM wp_postmeta WHERE meta_key LIKE '\_%';
QUERY;
$result = $this->assertQuery( $q );
$actual = $this->last_result;
$this->assertEquals( 40, count( $actual ) );
}
public function testLikeEscapingBasic() {
$q = <<<'QUERY'
SELECT DISTINCT meta_key FROM wp_postmeta WHERE meta_key NOT BETWEEN '_' AND '_z' AND meta_key NOT LIKE '\_%' ORDER BY meta_key LIMIT 30
QUERY;
$result = $this->assertQuery( $q );
$actual = $this->last_result;
$this->assertEquals( 30, count( $actual ) );
$last = $actual[ count( $actual ) - 1 ]->meta_key;
$this->assertEquals( 'visible_meta_key_30', $last );
}
public function testLikeEscapingParenAfterLike() {
$q = <<<'QUERY'
SELECT DISTINCT meta_key
FROM wp_postmeta
WHERE (meta_key != 'hello' AND meta_key NOT LIKE '\_%') AND meta_id > 0
QUERY;
$this->assertQuery( $q );
$actual = $this->last_result;
$this->assertEquals( 40, count( $actual ) );
$last = $actual[ count( $actual ) - 1 ]->meta_key;
$this->assertEquals( 'visible_meta_key_40', $last );
}
public function testLikeEscapingWithConcatFunction() {
$q = <<<'QUERY'
SELECT DISTINCT meta_key FROM wp_postmeta WHERE meta_key NOT BETWEEN '_' AND '_z' AND meta_key NOT LIKE CONCAT('\_', '%') ORDER BY meta_key LIMIT 30
QUERY;
$result = $this->assertQuery( $q );
$actual = $this->last_result;
$this->assertEquals( 30, count( $actual ) );
$last = $actual[ count( $actual ) - 1 ]->meta_key;
$this->assertEquals( 'visible_meta_key_30', $last );
}
// https://github.com/WordPress/sqlite-database-integration/issues/19
public function testHavingWithoutGroupBy() {
$q = <<<'QUERY'
SELECT DISTINCT meta_key FROM wp_postmeta WHERE meta_key NOT BETWEEN '_' AND '_z' HAVING meta_key NOT LIKE '\_%' ORDER BY meta_key LIMIT 30
QUERY;
$result = $this->assertQuery( $q );
$actual = $this->last_result;
$this->assertEquals( 30, count( $actual ) );
$last = $actual[ count( $actual ) - 1 ]->meta_key;
$this->assertEquals( 'visible_meta_key_30', $last );
$q = <<<'QUERY'
SELECT DISTINCT meta_key FROM wp_postmeta WHERE meta_key NOT BETWEEN '_' AND '_z' HAVING meta_key NOT LIKE CONCAT('\_', '%') ORDER BY meta_key LIMIT 30
QUERY;
$result = $this->assertQuery( $q );
$actual = $this->last_result;
$this->assertEquals( 30, count( $actual ) );
$last = $actual[ count( $actual ) - 1 ]->meta_key;
$this->assertEquals( 'visible_meta_key_30', $last );
}
public function testCharLengthSimple() {
$query = <<<'QUERY'
SELECT * FROM wp_options WHERE LENGTH(option_name) != CHAR_LENGTH(option_name)
QUERY;
$this->assertQuery( $query );
$actual = $this->last_result;
$this->assertEquals( 0, count( $actual ) );
}
public function testSubstringSimple() {
$query = <<<'QUERY'
SELECT SUBSTR(option_name, 1) ss1, SUBSTRING(option_name, 1) sstr1,
SUBSTR(option_name, -2) es1, SUBSTRING(option_name, -2) estr1
FROM wp_options
WHERE SUBSTR(option_name, -2) != SUBSTRING(option_name, -2)
OR SUBSTR(option_name, 1) != SUBSTRING(option_name, 1)
QUERY;
$this->assertQuery( $query );
$actual = $this->last_result;
$this->assertEquals( 0, count( $actual ) );
}
public function testSubstringIndex() {
$query = <<<'QUERY'
SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2) AS two_from_left,
SUBSTRING_INDEX('www.mysql.com', '.', -2) AS two_from_right,
SUBSTRING_INDEX('www.mysql.com', '.', 7) AS beyond_last,
SUBSTRING_INDEX('nosuchdelim', '.', 3) AS no_match,
SUBSTRING_INDEX('a--b--c', '--', -1) AS multi_char,
SUBSTRING_INDEX('a.b.c', '.', 1.6) AS fractional_positive,
SUBSTRING_INDEX('a.b.c', '.', -1.6) AS fractional_negative,
SUBSTRING_INDEX('aaaaa', 'aa', -2) AS overlapping_multibyte,
SUBSTRING_INDEX(BINARY 'aaaaa', 'aa', -2) AS overlapping_binary,
SUBSTRING_INDEX(NULL, '.', 1) AS null_string,
SUBSTRING_INDEX('a.b', NULL, 1) AS null_delimiter,
SUBSTRING_INDEX('a.b', '.', NULL) AS null_count,
SUBSTRING_INDEX('a.b', '.', 0) AS zero_count,
SUBSTRING_INDEX('a.b', '', 1) AS empty_delimiter
QUERY;
$this->assertQuery( $query );
$actual = $this->last_result;
$this->assertEquals( 'www.mysql', $actual[0]->two_from_left );
$this->assertEquals( 'mysql.com', $actual[0]->two_from_right );
$this->assertEquals( 'www.mysql.com', $actual[0]->beyond_last );
$this->assertEquals( 'nosuchdelim', $actual[0]->no_match );
$this->assertEquals( 'c', $actual[0]->multi_char );
$this->assertSame( 'a.b', $actual[0]->fractional_positive );
$this->assertSame( 'b.c', $actual[0]->fractional_negative );
$this->assertSame( 'aaa', $actual[0]->overlapping_multibyte );
$this->assertSame( 'aa', $actual[0]->overlapping_binary );
$this->assertNull( $actual[0]->null_string );
$this->assertNull( $actual[0]->null_delimiter );
$this->assertNull( $actual[0]->null_count );
$this->assertSame( '', $actual[0]->zero_count );
$this->assertSame( '', $actual[0]->empty_delimiter );
}
public function testSubstringIndexDomainExtraction() {
$this->assertQuery(
"INSERT INTO wp_options (option_name, option_value, autoload) VALUES ('_substring_index_probe', 'https://example.com/a?x=1', 'no');"
);
$query = <<<'QUERY'
SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(SUBSTRING_INDEX(option_value, '/', 3), '://', -1), '/', 1) AS domain
FROM wp_options WHERE option_name = '_substring_index_probe'
QUERY;
$this->assertQuery( $query );
$actual = $this->last_result;
$this->assertEquals( 1, count( $actual ) );
$this->assertEquals( 'example.com', $actual[0]->domain );
}
public function testCharLengthComplex() {
$query = <<<'QUERY'
SELECT option_name,
CHAR_LENGTH(
CASE WHEN option_name LIKE '\_site\_transient\_%'
THEN '_site_transient_'
WHEN option_name LIKE '\_transient\_%'
THEN '_transient_'
ELSE '' END
) prefix_length,
SUBSTR(option_name, CHAR_LENGTH(
CASE WHEN option_name LIKE '\_site\_transient\_%'
THEN '_site_transient_'
WHEN option_name LIKE '\_transient\_%'
THEN '_transient_'
ELSE '' END
) + 1) suffix
FROM wp_options
WHERE option_name LIKE '\_%transient\_%'
AND option_name NOT LIKE '%\_transient\_timeout\_%'
QUERY;
$this->assertQuery( $query );
$actual = $this->last_result;
$this->assertEquals( 6, count( $actual ) );
foreach ( $actual as $row ) {
self::assertTrue( str_ends_with( $row->option_name, '_' . $row->suffix ) );
}
}
public function testAllTransients() {
$this->assertQuery(
"SELECT * FROM wp_options WHERE option_name LIKE '\_%transient\_%'"
);
$actual = $this->last_result;
$this->assertEquals( 12, count( $actual ) );
}
public function testExpiredTransients() {
$query = <<<'QUERY'
SELECT a.option_id, a.option_name, a.option_value as option_content, a.autoload, b.option_value as option_timeout,
UNIX_TIMESTAMP() - b.option_value as age,
CONCAT (
CASE WHEN a.option_name LIKE '\_site\_transient\_%'
THEN '_site_transient_timeout_'
ELSE '_transient_timeout_'
END,
SUBSTRING(a.option_name, CHAR_LENGTH(
CASE WHEN a.option_name LIKE '\_site\_transient\_%'
THEN '_site_transient_'
ELSE '_transient_'
END
) + 1)) AS timeout_name
FROM wp_options a LEFT JOIN wp_options b ON b.option_name =
CONCAT(
CASE WHEN a.option_name LIKE '\_site\_transient\_%'
THEN '_site_transient_timeout_'
ELSE '_transient_timeout_'
END
,
SUBSTRING(a.option_name, CHAR_LENGTH(
CASE WHEN a.option_name LIKE '\_site\_transient\_%'
THEN '_site_transient_'
ELSE '_transient_'
END
) + 1)
)
WHERE (a.option_name LIKE '\_transient\_%' OR a.option_name LIKE '\_site\_transient\_%')
AND a.option_name NOT LIKE '%\_transient\_timeout\_%'
AND b.option_value < UNIX_TIMESTAMP()
QUERY;
$this->assertQuery( $query );
$actual = $this->last_result;
$this->assertEquals( 4, count( $actual ) );
foreach ( $actual as $row ) {
self::assertLessThan( time(), $row->option_timeout );
}
}
public function testDeleteExpiredNonSiteTransients() {
$now = time();
/* option.php: delete_expired_transients is the source of this query. */
$query = <<<'QUERY'
DELETE a, b FROM wp_options a, wp_options b
WHERE a.option_name LIKE '\_transient\_%'
AND a.option_name NOT LIKE '\_transient\_timeout_%'
AND b.option_name = CONCAT( '_transient_timeout_', SUBSTRING( a.option_name, 12 ) )
AND b.option_value < UNIX_TIMESTAMP()
QUERY;
$this->assertQuery( $query );
/* are the expired transients gone? */
$query = <<<'QUERY'
SELECT a.option_id, a.option_name, a.option_value as option_content,
a.autoload, b.option_value as option_timeout,
UNIX_TIMESTAMP() - b.option_value as age,
CONCAT (
CASE WHEN a.option_name LIKE '\_site\_transient\_%'
THEN '_site_transient_timeout_'
ELSE '_transient_timeout_'
END,
SUBSTRING(a.option_name, CHAR_LENGTH(
CASE WHEN a.option_name LIKE '\_site\_transient\_%'
THEN '_site_transient_'
ELSE '_transient_'
END
) + 1)) AS timeout_name
FROM wp_options a LEFT JOIN wp_options b ON b.option_name =
CONCAT(
CASE WHEN a.option_name LIKE '\_site\_transient\_%'
THEN '_site_transient_timeout_'
ELSE '_transient_timeout_'
END
,
SUBSTRING(a.option_name, CHAR_LENGTH(
CASE WHEN a.option_name LIKE '\_site\_transient\_%'
THEN '_site_transient_'
ELSE '_transient_'
END
) + 1)
)
WHERE (a.option_name LIKE '\_transient\_%' OR a.option_name LIKE '\_site\_transient\_%')
AND a.option_name NOT LIKE '%\_transient\_timeout\_%'
QUERY;
$this->assertQuery( $query );
$actual = $this->last_result;
$count_unexpired = 0;
foreach ( $actual as $row ) {
if ( str_starts_with( $row->option_name, '_transient' ) ) {
++$count_unexpired;
$this->assertGreaterThan( $now, $row->option_timeout );
}
}
$this->assertEquals( 1, $count_unexpired );
}
public function testUserCountsByRole() {
/* commas appear after the LIKE term sometimes, as here. */
$query = <<<'QUERY'
SELECT COUNT(NULLIF(`meta_value` LIKE '%\"administrator\"%', false)),
COUNT(NULLIF(`meta_value` LIKE '%\"editor\"%', false)),
COUNT(NULLIF(`meta_value` LIKE '%\"author\"%', false)),
COUNT(NULLIF(`meta_value` LIKE '%\"contributor\"%', false)),
COUNT(NULLIF(`meta_value` LIKE '%\"subscriber\"%', false)),
COUNT(NULLIF(`meta_value` = 'a:0:{}', false)),
COUNT(*)
FROM wp_usermeta
INNER JOIN wp_users ON user_id = ID
WHERE meta_key = 'wp_capabilities'
QUERY;
$this->assertQuery( $query );
}
public function testTranscendental() {
$this->markTestIncomplete( 'For some reason sqlite\'s transcendental functions are missing.' );
$this->assertQuery( 'SELECT 2.0, SQRT(2.0) sqr, SIN(0.5) s;' );
}
public function testRecoverSerialized() {
$obj = array(
'this' => 'that',
'that' => array( 'the', 'other', 'thing' ),
'two' => 2,
2 => 'two',
'pi' => pi(),
'moo' => "Mrs O'Leary's cow!",
);
$option_name = 'serialized_option';
$option_value = serialize( $obj );
$option_value_escaped = $this->engine->get_connection()->quote( $option_value );
/* Note well: this is heredoc not nowdoc */
$insert = <<<QUERY
INSERT INTO `wp_options` (`option_name`, `option_value`, `autoload`)
VALUES ('$option_name', $option_value_escaped, 'yes')
ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`),
`option_value` = VALUES(`option_value`),
`autoload` = VALUES(`autoload`)
QUERY;
$this->assertQuery( $insert );
$get = <<<QUERY
SELECT * FROM wp_options WHERE option_name = '$option_name';
QUERY;
$this->assertQuery( $get );
$actual = $this->last_result;
$retrieved_name = $actual[0]->option_name;
$retrieved_string = $actual[0]->option_value;
$this->assertEquals( $option_value, $retrieved_string );
$unserialized = unserialize( $retrieved_string );
$this->assertEquals( $obj, $unserialized );
++$obj ['two'];
$obj ['pi'] *= 2;
$option_value = serialize( $obj );
$option_value_escaped = $this->engine->get_connection()->quote( $option_value );
/* Note well: this is heredoc not nowdoc */
$insert = <<<QUERY
INSERT INTO `wp_options` (`option_name`, `option_value`, `autoload`)
VALUES ('$option_name', $option_value_escaped, 'yes')
ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`),
`option_value` = VALUES(`option_value`),
`autoload` = VALUES(`autoload`)
QUERY;
$this->assertQuery( $insert );
$get = <<<QUERY
SELECT * FROM wp_options WHERE option_name = '$option_name';
QUERY;
$this->assertQuery( $get );
$actual = $this->last_result;
$retrieved_string = $actual[0]->option_value;
$this->assertEquals( $option_value, $retrieved_string );
$unserialized = unserialize( $retrieved_string );
$this->assertEquals( $obj, $unserialized );
}
public function testOnDuplicateKey() {
$this->assertQuery(
'CREATE TABLE `test` (
`id` INT PRIMARY KEY,
`text` VARCHAR(255)
);'
);
// The order is deliberate to test that the query works with the keys in any order.
$this->assertQuery(
'INSERT INTO test (`text`, `id`)
VALUES ("test", 1)
ON DUPLICATE KEY UPDATE `text` = "test1"'
);
}
public function testOnDuplicateKeyWithUnnamedKeys() {
$this->assertQuery(
'CREATE TABLE `test` (
`id` INT,
`name` VARCHAR(255),
`other` VARCHAR(255),
PRIMARY KEY (id),
UNIQUE KEY (name)
);'
);
// The order is deliberate to test that the query works with the keys in any order.
$this->assertQuery(
'INSERT INTO test (id, `name`, other)
VALUES (1, "name", "test")
ON DUPLICATE KEY UPDATE `other` = values(other)'
);
}
public function testOnCreateTableIfNotExistsWithIndexAdded() {
$this->assertQuery(
'CREATE TABLE IF not EXISTS `test` (
`id` INT,
`name` VARCHAR(255),
`other` VARCHAR(255),
PRIMARY KEY (id),
UNIQUE KEY (name)
);'
);
$this->assertQuery(
'CREATE TABLE if NOT ExisTS `test` (
`id` INT,
`name` VARCHAR(255),
`other` VARCHAR(255),
PRIMARY KEY (id),
UNIQUE KEY (name)
);'
);
}
public function testShowColumns() {
$this->assertQuery(
'
CREATE TABLE t (
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
deleted_at TIMESTAMP DEFAULT NULL,
CONSTRAINT name_unique UNIQUE (name),
INDEX name_index (name)
);
'
);
$query = 'SHOW COLUMNS FROM t';
$this->assertQuery( $query );
$actual = $this->last_result;
$this->assertCount( 6, $actual );
foreach ( $actual as $row ) {
$this->assertIsObject( $row );
$this->assertTrue( property_exists( $row, 'Field' ) );
$this->assertTrue( property_exists( $row, 'Type' ) );
$this->assertTrue( property_exists( $row, 'Null' ) );
$this->assertTrue( ( 'NO' === $row->Null ) || ( 'YES' === $row->Null ) );
$this->assertTrue( property_exists( $row, 'Key' ) );
$this->assertTrue( property_exists( $row, 'Default' ) );
}
}
private function assertQuery( $sql ) {
$statement = $this->engine->query( $sql, PDO::FETCH_OBJ );
if ( $statement->columnCount() > 0 ) {
$this->last_result = $statement->fetchAll();
} else {
$this->last_result = $statement->rowCount();
}
$retval = $this->last_result;
$this->assertNotFalse( $retval );
return $retval;
}
}