Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit 91ca58f

Browse files
committed
Fix ReflectionException in PHPUnit tests
Fixes "ReflectionException: Property WP_SQLite_DB::$allow_unsafe_unquoted_parameters does not exist" in WordPress PHPUnit tests.
1 parent a056876 commit 91ca58f

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,6 @@ const expectedErrors = [
2222
'Tests_DB_Charset::test_strip_invalid_text',
2323
'Tests_DB::test_db_reconnect',
2424
'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"',
3725
'Tests_DB::test_process_fields_value_too_long_for_field with data set "invalid chars"',
3826
'Tests_DB::test_process_fields_value_too_long_for_field with data set "too long"',
3927
'Tests_DB::test_process_fields',

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ class WP_SQLite_DB extends wpdb {
2020
*/
2121
protected $dbh;
2222

23+
/**
24+
* Backward compatibility, see wpdb::$allow_unsafe_unquoted_parameters.
25+
*
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+
2333
/**
2434
* Constructor
2535
*
@@ -289,6 +299,33 @@ public function check_connection( $allow_bail = true ) {
289299
return true;
290300
}
291301

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

0 commit comments

Comments
 (0)