[wp-trac] [WordPress Trac] #42040: `$wpdb->prepare` returns blank without notice if less number of arguments are passed. should give a notice or exception
WordPress Trac
noreply at wordpress.org
Sat Sep 30 17:05:49 UTC 2017
#42040: `$wpdb->prepare` returns blank without notice if less number of arguments
are passed. should give a notice or exception
--------------------------------------+------------------------------
Reporter: thekt12 | Owner:
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Database | Version: 4.8.2
Severity: normal | Resolution:
Keywords: has-patch has-unit-tests | Focuses:
--------------------------------------+------------------------------
Changes (by thekt12):
* keywords: has-patch needs-unit-tests => has-patch has-unit-tests
Comment:
@swissspidy test42040-new.2.diff is the actual test file. If possible
please remove other test files as I have accidentally uploaded changes
made to the config file.
@jdgrimes thanks for the share it was the one i really needed, but I have
found a new bug in that testing @expectedIncorrectUsage usage.
Let me explain
Case 1:
{{{#!php
<?php
/**
* @ticket 42040
* @expectedIncorrectUsage wpdb::prepare
*/
public function test_prepare_invalid_args_count()
{
global $wpdb;
// this assertion fails as it dose not returns notice
$prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users
WHERE id = %d AND user_login = %s", 1, "admin", "extra-arg");
$this->assertEquals("SELECT * FROM $wpdb->users WHERE id =
1 AND user_login = 'admin'", $prepared);
// This returns argument count mismatch notice
$prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users
WHERE id = %d AND user_login = %s", 1, "admin", "extra-arg");
$this->assertEquals("SELECT * FROM $wpdb->users WHERE id =
1 AND user_login = 'admin'", $prepared);
}
}}}
The above case returns
{{{
FAILURES!
Tests: 1, Assertions: 3, Failures: 1.
}}}
Which is correct
Case 2:
{{{#!php
<?php
/**
* @ticket 42040
* @expectedIncorrectUsage wpdb::prepare
*/
public function test_prepare_invalid_args_count()
{
global $wpdb;
// this assertion fails as it dose not returns notice
$prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users
WHERE id = %d AND user_login = %s", 1, "admin", "extra-arg");
$this->assertEquals("SELECT * FROM $wpdb->users WHERE id =
1 AND user_login = 'admin'", $prepared);
// This gives two notices, one - due to the use of array
as parameter and second due to argument mismatch
$prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users
WHERE id = %d and user_nicename = %s and user_status =%d and user_login =
%s", array( 1 ), "admin", 0);
$this->assertEquals("", $prepared);
}
}}}
{{{
OK (1 test, 3 assertions)
}}}
Whereas it should actually give me
{{{
FAILURES!
Tests: 1, Assertions: 3, Failures: 1.
}}}
`@expectedIncorrectUsage` is only checking the number of times
`_doing_it_wrong` is called from the test function and matches it to the
number of assertion in that unit test function(also valid if the number of
assertion is less than the call to _doing_it_wrong).
It should be actually testing if an assertion is producing a notice or not
and number of notices produced by an assertion.
I'll create a new ticket for this bug.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/42040#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list