Skip to content

Commit 9ff7bbb

Browse files
committed
DB/PreparedSQL: add tests for namespaced names
1 parent e5e311f commit 9ff7bbb

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

WordPress/Tests/DB/PreparedSQLUnitTest.1.inc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,38 @@ echo $wpdb::CONSTANT_NAME;
142142

143143
// Not an identifiable method call.
144144
$wpdb->{$methodName}('query');
145+
146+
/*
147+
* Safeguard correct handling of all types of namespaced calls to the WPDB::prepare() method.
148+
*
149+
* Note that calling wpdb::prepare() statically will result in an error. Still, the tests are included here since the
150+
* sniff handles those calls.
151+
*/
152+
\WPDB::prepare( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '" . foo() . "';" );
153+
MyNamespace\WPDB::prepare( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '" . foo() . "';" );
154+
\MyNamespace\wpdb::prepare( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '" . foo() . "';" );
155+
namespace\wpdb::prepare( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '" . foo() . "';" ); // This should be flagged in the future once the sniff is able to resolve relative namespaces.
156+
157+
/*
158+
* Safeguard correct handling of all types of namespaced calls to PreparedSQLSniff::$SQLEscapingFunctions.
159+
*/
160+
$wpdb->query( "SELECT * FROM $wpdb->posts WHERE ID = " . \absint( $foo ) );
161+
$wpdb->query( "SELECT * FROM $wpdb->posts WHERE ID = " . MyNamespace\absint( $foo ) );
162+
$wpdb->query( "SELECT * FROM $wpdb->posts WHERE ID = " . \MyNamespace\absint( $foo ) );
163+
$wpdb->query( "SELECT * FROM $wpdb->posts WHERE ID = " . namespace\absint( $foo ) ); // This should NOT be flagged in the future once the sniff is able to resolve relative namespaces.
164+
165+
/*
166+
* Safeguard correct handling of all types of namespaced calls to PreparedSQLSniff::$SQLAutoEscapedFunctions.
167+
*/
168+
$wpdb->query( "SELECT * FROM $wpdb->posts WHERE ID = " . \count( $foo ) );
169+
$wpdb->query( "SELECT * FROM $wpdb->posts WHERE ID = " . MyNamespace\count( $foo ) );
170+
$wpdb->query( "SELECT * FROM $wpdb->posts WHERE ID = " . \MyNamespace\count( $foo ) );
171+
$wpdb->query( "SELECT * FROM $wpdb->posts WHERE ID = " . namespace\count( $foo ) ); // This should NOT be flagged in the future once the sniff is able to resolve relative namespaces.
172+
173+
/*
174+
* Safeguard correct handling of all types of namespaced calls to FormattingFunctionsHelper::$formattingFunctions.
175+
*/
176+
$wpdb->get_results( \sprintf( "SELECT * FROM $wpdb->posts WHERE ID = %s", intval( $id ) ) );
177+
$wpdb->get_results( MyNamespace\sprintf( "SELECT * FROM $wpdb->posts WHERE ID = %s", intval( $id ) ) );
178+
$wpdb->get_results( \MyNamespace\sprintf( "SELECT * FROM $wpdb->posts WHERE ID = %s", intval( $id ) ) );
179+
$wpdb->get_results( namespace\sprintf( "SELECT * FROM $wpdb->posts WHERE ID = %s", intval( $id ) ) ); // This should NOT be flagged in the future once the sniff is able to resolve relative namespaces.

WordPress/Tests/DB/PreparedSQLUnitTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ public function getErrorList( $testFile = '' ) {
6666
124 => 1,
6767
128 => 1,
6868
132 => 2,
69+
152 => 1,
70+
161 => 1,
71+
162 => 1,
72+
163 => 1,
73+
169 => 1,
74+
170 => 1,
75+
171 => 1,
76+
177 => 1,
77+
178 => 1,
78+
179 => 1,
6979
);
7080

7181
case 'PreparedSQLUnitTest.2.inc':

0 commit comments

Comments
 (0)