Skip to content

Commit c9b443b

Browse files
authored
Merge pull request #32 from Automattic/fix-reflection-exception
Fix some WP unit test errors
2 parents a056876 + ad3beb8 commit c9b443b

File tree

4 files changed

+49
-26
lines changed

4 files changed

+49
-26
lines changed

.github/workflows/wp-tests-phpunit-run.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,11 @@ const fs = require( 'fs' );
1111
const path = require( 'path' );
1212

1313
const expectedErrors = [
14-
'Tests_Admin_wpSiteHealth::test_object_cache_default_thresholds_non_multisite',
15-
'Tests_Admin_wpSiteHealth::test_object_cache_thresholds with data set #0',
16-
'Tests_Admin_wpSiteHealth::test_object_cache_thresholds with data set #1',
17-
'Tests_Admin_wpSiteHealth::test_object_cache_thresholds with data set #2',
18-
'Tests_Admin_wpSiteHealth::test_object_cache_thresholds with data set #3',
19-
'Tests_Admin_wpSiteHealth::test_object_cache_thresholds with data set #4',
2014
'Tests_Comment_WpComment::test_get_instance_should_succeed_for_float_that_is_equal_to_post_id',
2115
'Tests_Cron_getCronArray::test_get_cron_array_output_validation with data set "null"',
2216
'Tests_DB_Charset::test_strip_invalid_text',
2317
'Tests_DB::test_db_reconnect',
2418
'Tests_DB::test_get_col_info',
25-
'Tests_DB::test_prepare_should_respect_the_allow_unsafe_unquoted_parameters_property with data set "escaped-false-1"',
26-
'Tests_DB::test_prepare_should_respect_the_allow_unsafe_unquoted_parameters_property with data set "escaped-false-2"',
27-
'Tests_DB::test_prepare_should_respect_the_allow_unsafe_unquoted_parameters_property with data set "escaped-true-1"',
28-
'Tests_DB::test_prepare_should_respect_the_allow_unsafe_unquoted_parameters_property with data set "escaped-true-2"',
29-
'Tests_DB::test_prepare_should_respect_the_allow_unsafe_unquoted_parameters_property with data set "format-false-1"',
30-
'Tests_DB::test_prepare_should_respect_the_allow_unsafe_unquoted_parameters_property with data set "format-false-2"',
31-
'Tests_DB::test_prepare_should_respect_the_allow_unsafe_unquoted_parameters_property with data set "format-true-1"',
32-
'Tests_DB::test_prepare_should_respect_the_allow_unsafe_unquoted_parameters_property with data set "format-true-2"',
33-
'Tests_DB::test_prepare_should_respect_the_allow_unsafe_unquoted_parameters_property with data set "numbered-false-1"',
34-
'Tests_DB::test_prepare_should_respect_the_allow_unsafe_unquoted_parameters_property with data set "numbered-false-2"',
35-
'Tests_DB::test_prepare_should_respect_the_allow_unsafe_unquoted_parameters_property with data set "numbered-true-1"',
36-
'Tests_DB::test_prepare_should_respect_the_allow_unsafe_unquoted_parameters_property with data set "numbered-true-2"',
3719
'Tests_DB::test_process_fields_value_too_long_for_field with data set "invalid chars"',
3820
'Tests_DB::test_process_fields_value_too_long_for_field with data set "too long"',
3921
'Tests_DB::test_process_fields',
@@ -45,6 +27,8 @@ const expectedErrors = [
4527
];
4628

4729
const expectedFailures = [
30+
'Tests_Admin_wpSiteHealth::test_object_cache_thresholds with data set #2',
31+
'Tests_Admin_wpSiteHealth::test_object_cache_thresholds with data set #3',
4832
'Tests_Comment::test_wp_new_comment_respects_comment_field_lengths',
4933
'Tests_Comment::test_wp_update_comment',
5034
'Tests_DB_dbDelta::test_spatial_indices',

wp-includes/sqlite/class-wp-sqlite-crosscheck-db.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
class WP_SQLite_Crosscheck_DB_ extends WP_SQLite_DB {
44

5-
public function __construct() {
6-
parent::__construct();
5+
public function __construct( string $dbname ) {
6+
parent::__construct( $dbname );
77
$GLOBALS['sqlite'] = $this;
88
$GLOBALS['mysql'] = new wpdb(
99
DB_USER,

wp-includes/sqlite/class-wp-sqlite-db.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,24 @@ class WP_SQLite_DB extends wpdb {
2121
protected $dbh;
2222

2323
/**
24-
* Constructor
24+
* Backward compatibility, see wpdb::$allow_unsafe_unquoted_parameters.
2525
*
26-
* Unlike wpdb, no credentials are needed.
26+
* This property is mirroring "wpdb::$allow_unsafe_unquoted_parameters",
27+
* because some tests are accessing it externally using PHP reflection.
28+
*
29+
* @var
30+
*/
31+
private $allow_unsafe_unquoted_parameters = true;
32+
33+
/**
34+
* Connects to the SQLite database.
35+
*
36+
* Unlike for MySQL, no credentials and host are needed.
37+
*
38+
* @param string $dbname Database name.
2739
*/
28-
public function __construct() {
29-
parent::__construct( '', '', '', '' );
40+
public function __construct( $dbname ) {
41+
parent::__construct( '', '', $dbname, '' );
3042
$this->charset = 'utf8mb4';
3143
}
3244

@@ -289,6 +301,33 @@ public function check_connection( $allow_bail = true ) {
289301
return true;
290302
}
291303

304+
/**
305+
* Prepares a SQL query for safe execution.
306+
*
307+
* See "wpdb::prepare()". This override only fixes a WPDB test issue.
308+
*
309+
* @param string $query Query statement with `sprintf()`-like placeholders.
310+
* @param array|mixed $args The array of variables or the first variable to substitute.
311+
* @param mixed ...$args Further variables to substitute when using individual arguments.
312+
* @return string|void Sanitized query string, if there is a query to prepare.
313+
*/
314+
public function prepare( $query, ...$args ) {
315+
/*
316+
* Sync "$allow_unsafe_unquoted_parameters" with the WPDB parent property.
317+
* This is only needed because some WPDB tests are accessing the private
318+
* property externally via PHP reflection. This should be fixed WP tests.
319+
*/
320+
$wpdb_allow_unsafe_unquoted_parameters = $this->__get( 'allow_unsafe_unquoted_parameters' );
321+
if ( $wpdb_allow_unsafe_unquoted_parameters !== $this->allow_unsafe_unquoted_parameters ) {
322+
$property = new ReflectionProperty( 'wpdb', 'allow_unsafe_unquoted_parameters' );
323+
$property->setAccessible( true );
324+
$property->setValue( $this, $this->allow_unsafe_unquoted_parameters );
325+
$property->setAccessible( false );
326+
}
327+
328+
return parent::prepare( $query, ...$args );
329+
}
330+
292331
/**
293332
* Performs a database query.
294333
*

wp-includes/sqlite/db.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
$crosscheck_tests_file_path = dirname( __DIR__, 2 ) . '/tests/class-wp-sqlite-crosscheck-db.php';
6060
if ( defined( 'SQLITE_DEBUG_CROSSCHECK' ) && SQLITE_DEBUG_CROSSCHECK && file_exists( $crosscheck_tests_file_path ) ) {
6161
require_once $crosscheck_tests_file_path;
62-
$GLOBALS['wpdb'] = new WP_SQLite_Crosscheck_DB();
62+
$GLOBALS['wpdb'] = new WP_SQLite_Crosscheck_DB( DB_NAME );
6363
} else {
64-
$GLOBALS['wpdb'] = new WP_SQLite_DB();
64+
$GLOBALS['wpdb'] = new WP_SQLite_DB( DB_NAME );
6565
}

0 commit comments

Comments
 (0)