-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWP_SQLite_Information_Schema_Reconstructor_Tests.php
178 lines (162 loc) · 12 KB
/
WP_SQLite_Information_Schema_Reconstructor_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
<?php
use PHPUnit\Framework\TestCase;
class WP_SQLite_Information_Schema_Reconstructor_Tests extends TestCase {
/** @var WP_SQLite_Driver */
private $engine;
/** @var WP_SQLite_Information_Schema_Reconstructor */
private $reconstructor;
/** @var PDO */
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;
}
// Mock symols that are used for WordPress table reconstruction.
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', __DIR__ );
}
if ( ! function_exists( 'wp_installing' ) ) {
function wp_installing() {
return false;
}
}
if ( ! function_exists( 'wp_get_db_schema' ) ) {
function wp_get_db_schema() {
// Output from "wp_get_db_schema" as of WordPress 6.8.0.
// See: https://github.com/WordPress/wordpress-develop/blob/6.8.0/src/wp-admin/includes/schema.php#L36
return "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) ) DEFAULT CHARACTER SET utf8mb4; 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)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_termmeta ( meta_id bigint(20) unsigned NOT NULL auto_increment, term_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (meta_id), KEY term_id (term_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_terms ( term_id bigint(20) unsigned NOT NULL auto_increment, name varchar(200) NOT NULL default '', slug varchar(200) NOT NULL default '', term_group bigint(10) NOT NULL default 0, PRIMARY KEY (term_id), KEY slug (slug(191)), KEY name (name(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_term_taxonomy ( term_taxonomy_id bigint(20) unsigned NOT NULL auto_increment, term_id bigint(20) unsigned NOT NULL default 0, taxonomy varchar(32) NOT NULL default '', description longtext NOT NULL, parent bigint(20) unsigned NOT NULL default 0, count bigint(20) NOT NULL default 0, PRIMARY KEY (term_taxonomy_id), UNIQUE KEY term_id_taxonomy (term_id,taxonomy), KEY taxonomy (taxonomy) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_term_relationships ( object_id bigint(20) unsigned NOT NULL default 0, term_taxonomy_id bigint(20) unsigned NOT NULL default 0, term_order int(11) NOT NULL default 0, PRIMARY KEY (object_id,term_taxonomy_id), KEY term_taxonomy_id (term_taxonomy_id) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_commentmeta ( meta_id bigint(20) unsigned NOT NULL auto_increment, comment_id bigint(20) unsigned NOT NULL default '0', meta_key varchar(255) default NULL, meta_value longtext, PRIMARY KEY (meta_id), KEY comment_id (comment_id), KEY meta_key (meta_key(191)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_comments ( comment_ID bigint(20) unsigned NOT NULL auto_increment, comment_post_ID bigint(20) unsigned NOT NULL default '0', comment_author tinytext NOT NULL, comment_author_email varchar(100) NOT NULL default '', comment_author_url varchar(200) NOT NULL default '', comment_author_IP varchar(100) NOT NULL default '', comment_date datetime NOT NULL default '0000-00-00 00:00:00', comment_date_gmt datetime NOT NULL default '0000-00-00 00:00:00', comment_content text NOT NULL, comment_karma int(11) NOT NULL default '0', comment_approved varchar(20) NOT NULL default '1', comment_agent varchar(255) NOT NULL default '', comment_type varchar(20) NOT NULL default 'comment', comment_parent bigint(20) unsigned NOT NULL default '0', user_id bigint(20) unsigned NOT NULL default '0', PRIMARY KEY (comment_ID), KEY comment_post_ID (comment_post_ID), KEY comment_approved_date_gmt (comment_approved,comment_date_gmt), KEY comment_date_gmt (comment_date_gmt), KEY comment_parent (comment_parent), KEY comment_author_email (comment_author_email(10)) ) DEFAULT CHARACTER SET utf8mb4; CREATE TABLE wp_links ( link_id bigint(20) unsigned NOT NULL auto_increment, link_url varchar(255) NOT NULL default '', link_name varchar(255) NOT NULL default '', link_image varchar(255) NOT NULL default '', link_target varchar(25) NOT NULL default '', link_description varchar(255) NOT NULL default '', link_visible varchar(20) NOT NULL default 'Y', link_owner bigint(20) unsigned NOT NULL default '1', link_rating int(11) NOT NULL default '0', link_updated datetime NOT NULL default '0000-00-00 00:00:00', link_rel varchar(255) NOT NULL default '', link_notes mediumtext NOT NULL, link_rss varchar(255) NOT NULL default '', PRIMARY KEY (link_id), KEY link_visible (link_visible) ) DEFAULT CHARACTER SET utf8mb4; 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) ) DEFAULT CHARACTER SET utf8mb4; 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)) ) DEFAULT CHARACTER SET utf8mb4; 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) ) DEFAULT CHARACTER SET utf8mb4;";
}
}
}
// Before each test, we create a new database
public function setUp(): void {
$this->sqlite = new PDO( 'sqlite::memory:' );
$this->engine = new WP_SQLite_Driver(
array(
'connection' => $this->sqlite,
'database' => 'wp',
)
);
$builder = new WP_SQLite_Information_Schema_Builder(
'wp',
WP_SQLite_Driver::RESERVED_PREFIX,
array( $this->engine, 'execute_sqlite_query' )
);
$this->reconstructor = new WP_SQLite_Information_Schema_Reconstructor(
$this->engine,
$builder
);
}
public function testReconstructInformationSchemaTable(): void {
$this->engine->get_pdo()->exec(
'
CREATE TABLE t (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
role TEXT,
score REAL,
priority INTEGER DEFAULT 0,
data BLOB,
UNIQUE (name)
)
'
);
$this->engine->get_pdo()->exec( 'CREATE INDEX idx_score ON t (score)' );
$this->engine->get_pdo()->exec( 'CREATE INDEX idx_role_score ON t (role, priority)' );
$result = $this->assertQuery( 'SELECT * FROM information_schema.tables WHERE table_name = "t"' );
$this->assertEquals( 0, count( $result ) );
$this->reconstructor->ensure_correct_information_schema();
$result = $this->assertQuery( 'SELECT * FROM information_schema.tables WHERE table_name = "t"' );
$this->assertEquals( 1, count( $result ) );
$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
$this->assertSame(
implode(
"\n",
array(
'CREATE TABLE `t` (',
' `id` int NOT NULL AUTO_INCREMENT,',
' `email` text NOT NULL,',
' `name` text NOT NULL,',
' `role` text DEFAULT NULL,',
' `score` float DEFAULT NULL,',
" `priority` int DEFAULT '0',",
' `data` blob DEFAULT NULL,',
' PRIMARY KEY (`id`),',
' KEY `idx_role_score` (`role`(100), `priority`),',
' KEY `idx_score` (`score`),',
' UNIQUE KEY `sqlite_autoindex_t_2` (`name`(100)),',
' UNIQUE KEY `sqlite_autoindex_t_1` (`email`(100))',
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
)
),
$result[0]->{'Create Table'}
);
}
public function testReconstructInformationSchemaTableWithWpTables(): void {
// Create a WP table with any columns.
$this->engine->get_pdo()->exec( 'CREATE TABLE wp_posts ( id INTEGER )' );
// Reconstruct the information schema.
$this->reconstructor->ensure_correct_information_schema();
$result = $this->assertQuery( 'SELECT * FROM information_schema.tables WHERE table_name = "wp_posts"' );
$this->assertEquals( 1, count( $result ) );
// The reconstructed schema should correspond to the original WP table definition.
$result = $this->assertQuery( 'SHOW CREATE TABLE wp_posts' );
$this->assertSame(
implode(
"\n",
array(
'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`)',
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
)
),
$result[0]->{'Create Table'}
);
}
private function assertQuery( $sql ) {
$retval = $this->engine->query( $sql );
$this->assertNotFalse( $retval );
return $retval;
}
}