Make WordPress Core

Ticket #42040: test42040-new.diff

File test42040-new.diff, 4.2 KB (added by thekt12, 7 years ago)

Proper Test File

  • tests/phpunit/tests/db.php

    diff --git tests/phpunit/tests/db.php tests/phpunit/tests/db.php
    index 01dd368..4b34839 100644
    class Tests_DB extends WP_UnitTestCase { 
    395395                $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared );
    396396        }
    397397
     398
     399            /**
     400             * @ticket 42040
     401             * @expectedIncorrectUsage wpdb::prepare
     402             */
     403            public function test_prepare_invalid_args_count()
     404            {
     405                global $wpdb;
     406
     407                /*
     408                 * cases with more number of argument than the patterns found
     409                 */
     410                $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, "admin", "extra-arg");
     411                $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared);
     412
     413                $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1,"admin", "extra-arg"));
     414                $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared);
     415
     416
     417                //case with double IncorrectUsage
     418                $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1 ), "admin", "extra-arg");
     419                $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared);
     420
     421                //random case to check if it ignores %%
     422                $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND %% AND user_login = %s", 1, "admin", "extra-arg");
     423                $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 1 AND % AND user_login = 'admin'", $prepared);
     424
     425                //case with multiple patterns clubbed together and also Testing %F pattern detection
     426                // Note: Floats getting converted to 6 decimal place after - which is not 42040 patch is about
     427                $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %%%d AND %F AND %f AND user_login = %s", 1, 2.3, "4.5", "admin", "extra-arg");
     428                $this->assertEquals("SELECT * FROM wptests_users WHERE id = %1 AND 2.300000 AND 4.500000 AND user_login = 'admin'", $prepared);
     429
     430
     431
     432                /*
     433                 * cases with less number of argument than the patterns found
     434                 */
     435                $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d and user_nicename = %s and user_status =%d and user_login = %s", 1, "admin", 0);
     436                $this->assertEquals("", $prepared);
     437
     438                $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 ));
     439                $this->assertEquals("", $prepared);
     440
     441                //case with double notices
     442                $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);
     443                $this->assertEquals("", $prepared);
     444
     445                $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d and %% and user_login = %s and user_status =%d and user_login = %s", 1, "admin", "extra-arg");
     446                $this->assertEquals("", $prepared);
     447
     448                // $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, "admin");
     449                // $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared);
     450            }
     451
     452
    398453        function test_db_version() {
    399454                global $wpdb;
    400455
  • wp-tests-config-sample.php

    diff --git wp-tests-config-sample.php wp-tests-config-sample.php
    index 9716b3e..05e2f20 100644
    define( 'WP_DEBUG', true ); 
    3131// These tests will DROP ALL TABLES in the database with the prefix named below.
    3232// DO NOT use a production database or one that is shared with something else.
    3333
    34 define( 'DB_NAME', 'youremptytestdbnamehere' );
    35 define( 'DB_USER', 'yourusernamehere' );
    36 define( 'DB_PASSWORD', 'yourpasswordhere' );
     34define( 'DB_NAME', 'wp-test-db' );
     35define( 'DB_USER', 'root' );
     36define( 'DB_PASSWORD', '8o1o6o251' );
    3737define( 'DB_HOST', 'localhost' );
    3838define( 'DB_CHARSET', 'utf8' );
    3939define( 'DB_COLLATE', '' );
    4040
    4141$table_prefix  = 'wptests_';   // Only numbers, letters, and underscores please!
    4242
    43 define( 'WP_TESTS_DOMAIN', 'example.org' );
     43define( 'WP_TESTS_DOMAIN', 'wp-test.dev' );
    4444define( 'WP_TESTS_EMAIL', 'admin@example.org' );
    4545define( 'WP_TESTS_TITLE', 'Test Blog' );
    4646