Skip to content

Commit e4de14f

Browse files
committed
WIP DB/PreparedSQL: add tests for namespaced names
1 parent 5722a14 commit e4de14f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

WordPress/Tests/DB/PreparedSQLUnitTest.1.inc

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ $wpdb
117117
$wpdb?->query( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '" . (int) $foo . "';" ); // OK.
118118
$wpdb?->query( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '" . foo() . "';" ); // Bad.
119119

120-
WPDB::prepare( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '" . foo() . "';" ); // Bad.
120+
\WPDB::prepare( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '" . foo() . "';" ); // Bad.
121121
$wpdb->Query( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '" . foo() . "';" ); // Bad.
122122

123123
$wpdb->query( "SELECT * FROM $wpdb->posts WHERE value = " . {$foo} . ";" ); // Bad - on $foo, not on the {}.
@@ -142,3 +142,16 @@ echo $wpdb::CONSTANT_NAME;
142142

143143
// Not an identifiable method call.
144144
$wpdb->{$methodName}('query');
145+
146+
// TODO: the below currently is flagged but it shouldn't be.
147+
/*
148+
MyNamespace\WPDB::prepare( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '" . foo() . "';" );
149+
\MyNamespace\WPDB::prepare( "SELECT * FROM $wpdb->posts WHERE post_title LIKE '" . foo() . "';" );
150+
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.
151+
*/
152+
153+
$wpdb->query( "SELECT * FROM $wpdb->posts WHERE ID = " . MyNamespace\absint( $foo ) );
154+
$wpdb->query( "SELECT * FROM $wpdb->posts WHERE ID = " . \MyNamespace\absint( $foo ) );
155+
$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.
156+
157+
// TODO: add similar namespace tests as the ones above for PreparedSQLSniff::$SQLAutoEscapedFunctions and FormattingFunctionsHelper::$formattingFunctions.

WordPress/Tests/DB/PreparedSQLUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public function getErrorList( $testFile = '' ) {
6666
124 => 1,
6767
128 => 1,
6868
132 => 2,
69+
153 => 1,
70+
154 => 1,
71+
155 => 1,
6972
);
7073

7174
case 'PreparedSQLUnitTest.2.inc':

0 commit comments

Comments
 (0)