Repository navigation
Expand file tree
/
Copy pathWP_SQLite_DB_Tests.php
More file actions
165 lines (136 loc) · 4.64 KB
/
Copy pathWP_SQLite_DB_Tests.php
File metadata and controls
165 lines (136 loc) · 4.64 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
<?php
use PHPUnit\Framework\TestCase;
require_once __DIR__ . '/fixtures/class-wpdb-stub.php';
require_once __DIR__ . '/../../plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-db.php';
class WP_SQLite_DB_Tests extends TestCase {
/** @var WP_MySQL_On_SQLite */
private $driver;
public function setUp(): void {
$pdo_class = PHP_VERSION_ID >= 80400 ? Pdo\Sqlite::class : PDO::class;
$pdo = new $pdo_class( 'sqlite::memory:' );
$this->driver = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:dbname=wp',
null,
null,
array( 'sqlite_pdo' => $pdo )
);
}
public function test_exposes_mysql_on_sqlite_driver(): void {
$wpdb = new class( $this->driver ) extends WP_SQLite_DB {
public function __construct( WP_MySQL_On_SQLite $driver ) {
$this->dbh = $driver;
}
};
$this->assertSame( $this->driver, $wpdb->get_driver() );
}
public function test_reports_emulated_mysql_version(): void {
$driver = new WP_MySQL_On_SQLite(
'mysql-on-sqlite:dbname=wp',
null,
null,
array(
'pdo' => $this->driver->get_sqlite_pdo(),
'mysql_version' => 50744,
)
);
$wpdb = new class( $driver ) extends WP_SQLite_DB {
public function __construct( WP_MySQL_On_SQLite $driver ) {
$this->dbh = $driver;
}
};
$this->assertSame( '5.7.44', $wpdb->db_version() );
$this->assertSame( '5.7.44-mysql-on-sqlite-' . SQLITE_DRIVER_VERSION, $wpdb->db_server_info() );
}
public function test_reports_empty_version_without_database_connection(): void {
$wpdb = new class() extends WP_SQLite_DB {
public function __construct() {
$this->dbh = null;
}
};
$this->assertSame( '', $wpdb->db_version() );
$this->assertSame( '', $wpdb->db_server_info() );
}
public function test_rejects_driver_access_without_database_connection(): void {
$wpdb = new class() extends WP_SQLite_DB {
public function __construct() {
$this->dbh = null;
}
};
$this->expectException( RuntimeException::class );
$this->expectExceptionMessage( 'Cannot access the driver without an active database connection.' );
$wpdb->get_driver();
}
public function test_load_col_info_without_result(): void {
$wpdb = new class( $this->driver ) extends WP_SQLite_DB {
public $col_info;
public $last_error;
public $last_query;
public $last_result;
public $num_rows;
public $result;
public $rows_affected;
public function __construct( WP_MySQL_On_SQLite $driver ) {
$this->dbh = $driver;
}
public function get_loaded_col_info(): array {
$this->load_col_info();
return $this->col_info;
}
};
$this->assertSame( array(), $wpdb->get_loaded_col_info() );
$wpdb->flush();
$this->assertSame( array(), $wpdb->get_loaded_col_info() );
}
/**
* @dataProvider dataMysqlEscaping
*/
public function testRealEscapeMatchesMysql( string $value, string $expected ): void {
$wpdb = new class( $this->driver ) extends WP_SQLite_DB {
public function __construct( ?WP_MySQL_On_SQLite $driver ) {
$this->dbh = $driver;
}
public function add_placeholder_escape( $query ) {
return $query;
}
};
$this->assertSame( $expected, $wpdb->_real_escape( $value ) );
}
public static function dataMysqlEscaping(): array {
return array(
'ASCII null' => array( chr( 0 ), '\\0' ),
'newline' => array( "\n", '\\n' ),
'carriage return' => array( "\r", '\\r' ),
'backslash' => array( '\\', '\\\\' ),
'single quote' => array( "'", "\\'" ),
'double quote' => array( '"', '\\"' ),
'Control+Z' => array( chr( 26 ), '\\Z' ),
'backspace' => array( chr( 8 ), chr( 8 ) ),
'tab' => array( "\t", "\t" ),
'UTF-8' => array( 'Ʈềʂᴛ🙂', 'Ʈềʂᴛ🙂' ),
);
}
public function testRealEscapePreservesValueInSqlStringLiteral(): void {
$wpdb = new class( $this->driver ) extends WP_SQLite_DB {
public function __construct( ?WP_MySQL_On_SQLite $driver ) {
$this->dbh = $driver;
}
public function add_placeholder_escape( $query ) {
return $query;
}
};
$value = chr( 0 ) . "\n\r\\'\"" . chr( 26 ) . chr( 8 ) . "\tƮềʂᴛ🙂";
$escaped = $wpdb->_real_escape( $value );
$result = $this->driver->query( "SELECT '$escaped'" );
$this->assertSame( $value, $result->fetchColumn() );
}
public function testRealEscapeRequiresDatabaseConnection(): void {
$wpdb = new class( null ) extends WP_SQLite_DB {
public function __construct( ?WP_MySQL_On_SQLite $driver ) {
$this->dbh = $driver;
}
};
$this->expectException( RuntimeException::class );
$this->expectExceptionMessage( 'Cannot escape data without an active database connection.' );
$wpdb->_real_escape( 'value' );
}
}