Make WordPress Core


Ignore:
Timestamp:
04/20/2015 10:53:33 AM (10 years ago)
Author:
pento
Message:

Merge the query sanity checks from #21212 to the 4.0 branch.

Props pento, nacin, mdawaffe, DrewAPicture.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/4.0/tests/phpunit/tests/db.php

    r29701 r32181  
    1515
    1616    /**
     17     * Our special WPDB
     18     * @var resource
     19     */
     20    protected static $_wpdb;
     21
     22    public static function setUpBeforeClass() {
     23        self::$_wpdb = new wpdb_exposed_methods_for_testing();
     24    }
     25
     26    /**
    1727     * Set up the test fixture
    1828     */
     
    2737     */
    2838    public function tearDown() {
     39        remove_filter( 'query', array( $this, 'query_filter' ) );
    2940        parent::tearDown();
    30         remove_filter( 'query', array( $this, 'query_filter' ) );
    3141    }
    3242
     
    225235
    226236    /**
     237     * @ticket 21212
     238     */
     239    function test_wpdb_actually_protected_properties() {
     240        global $wpdb;
     241
     242        $new_meta = "HAHA I HOPE THIS DOESN'T WORK";
     243
     244        $col_meta = $wpdb->col_meta;
     245        $wpdb->col_meta = $new_meta;
     246
     247        $this->assertNotEquals( $col_meta, $new_meta );
     248        $this->assertEquals( $col_meta, $wpdb->col_meta );
     249    }
     250
     251    /**
    227252     * @ticket 18510
    228253     */
     
    258283
    259284        $new_modes = array( 'IGNORE_SPACE', 'NO_AUTO_CREATE_USER' );
     285
    260286        $wpdb->set_sql_mode( $new_modes );
     287
    261288        $check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
    262         $this->assertEquals( implode( ',', $new_modes ), $check_new_modes );
     289        $this->assertEqualSets( $new_modes, explode( ',', $check_new_modes ) );
    263290
    264291        $wpdb->set_sql_mode( explode( ',', $current_modes ) );
     
    277304        $wpdb->set_sql_mode( $new_modes );
    278305        $check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
    279         $this->assertFalse( in_array( 'NO_ZERO_DATE', explode( ',', $check_new_modes ) ) );
     306        $this->assertNotContains( 'NO_ZERO_DATE', explode( ',', $check_new_modes ) );
    280307
    281308        $wpdb->set_sql_mode( explode( ',', $current_modes ) );
     
    291318        $current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
    292319
    293         $new_modes = array( 'IGNORE_SPACE', 'NO_ZERO_DATE', 'NO_AUTO_CREATE_USER' );
     320        $new_modes = array( 'IGNORE_SPACE', 'ONLY_FULL_GROUP_BY', 'NO_AUTO_CREATE_USER' );
    294321
    295322        add_filter( 'incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1, 1 );
     
    298325
    299326        $check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
    300         $this->assertTrue( in_array( 'NO_ZERO_DATE', explode( ',', $check_new_modes ) ) );
     327        $this->assertContains( 'ONLY_FULL_GROUP_BY', explode( ',', $check_new_modes ) );
    301328
    302329        $wpdb->set_sql_mode( explode( ',', $current_modes ) );
     
    304331
    305332    public function filter_allowed_incompatible_sql_mode( $modes ) {
    306         $pos = array_search( 'NO_ZERO_DATE', $modes );
     333        $pos = array_search( 'ONLY_FULL_GROUP_BY', $modes );
    307334        $this->assertGreaterThanOrEqual( 0, $pos );
    308335
     
    459486        $wpdb->suppress_errors( $suppress );
    460487    }
     488
     489    /**
     490     * @ticket 21212
     491     */
     492    function data_get_table_from_query() {
     493        $table = 'a_test_table_name';
     494
     495        $queries = array(
     496            // Basic
     497            "SELECT * FROM $table",
     498            "SELECT * FROM `$table`",
     499
     500            "INSERT $table",
     501            "INSERT IGNORE $table",
     502            "INSERT IGNORE INTO $table",
     503            "INSERT INTO $table",
     504            "INSERT LOW_PRIORITY $table",
     505            "INSERT DELAYED $table",
     506            "INSERT HIGH_PRIORITY $table",
     507            "INSERT LOW_PRIORITY IGNORE $table",
     508            "INSERT LOW_PRIORITY INTO $table",
     509            "INSERT LOW_PRIORITY IGNORE INTO $table",
     510
     511            "REPLACE $table",
     512            "REPLACE INTO $table",
     513            "REPLACE LOW_PRIORITY $table",
     514            "REPLACE DELAYED $table",
     515            "REPLACE LOW_PRIORITY INTO $table",
     516
     517            "UPDATE LOW_PRIORITY $table",
     518            "UPDATE LOW_PRIORITY IGNORE $table",
     519
     520            "DELETE $table",
     521            "DELETE IGNORE $table",
     522            "DELETE IGNORE FROM $table",
     523            "DELETE FROM $table",
     524            "DELETE LOW_PRIORITY $table",
     525            "DELETE QUICK $table",
     526            "DELETE IGNORE $table",
     527            "DELETE LOW_PRIORITY FROM $table",
     528
     529            // STATUS
     530            "SHOW TABLE STATUS LIKE '$table'",
     531            "SHOW TABLE STATUS WHERE NAME='$table'",
     532
     533            "SHOW TABLES LIKE '$table'",
     534            "SHOW FULL TABLES LIKE '$table'",
     535            "SHOW TABLES WHERE NAME='$table'",
     536
     537            // Extended
     538            "EXPLAIN SELECT * FROM $table",
     539            "EXPLAIN EXTENDED SELECT * FROM $table",
     540            "EXPLAIN EXTENDED SELECT * FROM `$table`",
     541
     542            "DESCRIBE $table",
     543            "DESC $table",
     544            "EXPLAIN $table",
     545            "HANDLER $table",
     546
     547            "LOCK TABLE $table",
     548            "LOCK TABLES $table",
     549            "UNLOCK TABLE $table",
     550
     551            "RENAME TABLE $table",
     552            "OPTIMIZE TABLE $table",
     553            "BACKUP TABLE $table",
     554            "RESTORE TABLE $table",
     555            "CHECK TABLE $table",
     556            "CHECKSUM TABLE $table",
     557            "ANALYZE TABLE $table",
     558            "REPAIR TABLE $table",
     559
     560            "TRUNCATE $table",
     561            "TRUNCATE TABLE $table",
     562
     563            "CREATE TABLE $table",
     564            "CREATE TEMPORARY TABLE $table",
     565            "CREATE TABLE IF NOT EXISTS $table",
     566
     567            "ALTER TABLE $table",
     568            "ALTER IGNORE TABLE $table",
     569
     570            "DROP TABLE $table",
     571            "DROP TABLE IF EXISTS $table",
     572
     573            "CREATE INDEX foo(bar(20)) ON $table",
     574            "CREATE UNIQUE INDEX foo(bar(20)) ON $table",
     575            "CREATE FULLTEXT INDEX foo(bar(20)) ON $table",
     576            "CREATE SPATIAL INDEX foo(bar(20)) ON $table",
     577
     578            "DROP INDEX foo ON $table",
     579
     580            "LOAD DATA INFILE 'wp.txt' INTO TABLE $table",
     581            "LOAD DATA LOW_PRIORITY INFILE 'wp.txt' INTO TABLE $table",
     582            "LOAD DATA CONCURRENT INFILE 'wp.txt' INTO TABLE $table",
     583            "LOAD DATA LOW_PRIORITY LOCAL INFILE 'wp.txt' INTO TABLE $table",
     584            "LOAD DATA INFILE 'wp.txt' REPLACE INTO TABLE $table",
     585            "LOAD DATA INFILE 'wp.txt' IGNORE INTO TABLE $table",
     586
     587            "GRANT ALL ON TABLE $table",
     588            "REVOKE ALL ON TABLE $table",
     589
     590            "SHOW COLUMNS FROM $table",
     591            "SHOW FULL COLUMNS FROM $table",
     592            "SHOW CREATE TABLE $table",
     593            "SHOW INDEX FROM $table",
     594        );
     595
     596        foreach ( $queries as &$query ) {
     597            $query = array( $query, $table );
     598        }
     599        return $queries;
     600    }
     601
     602    /**
     603     * @dataProvider data_get_table_from_query
     604     * @ticket 21212
     605     */
     606    function test_get_table_from_query( $query, $table ) {
     607        $this->assertEquals( $table, self::$_wpdb->get_table_from_query( $query ) );
     608    }
     609
     610    function data_get_table_from_query_false() {
     611        $table = 'a_test_table_name';
     612        return array(
     613            array( "LOL THIS ISN'T EVEN A QUERY $table" ),
     614        );
     615    }
     616
     617    /**
     618     * @dataProvider data_get_table_from_query_false
     619     * @ticket 21212
     620     */
     621    function test_get_table_from_query_false( $query ) {
     622        $this->assertFalse( self::$_wpdb->get_table_from_query( $query ) );
     623    }
     624
     625    /**
     626     * @ticket 21212
     627     */
     628    function data_process_field_formats() {
     629        $core_db_fields_no_format_specified = array(
     630            array( 'post_content' => 'foo', 'post_parent' => 0 ),
     631            null,
     632            array(
     633                'post_content' => array( 'value' => 'foo', 'format' => '%s' ),
     634                'post_parent' => array( 'value' => 0, 'format' => '%d' ),
     635            )
     636        );
     637
     638        $core_db_fields_formats_specified = array(
     639            array( 'post_content' => 'foo', 'post_parent' => 0 ),
     640            array( '%d', '%s' ), // These override core field_types
     641            array(
     642                'post_content' => array( 'value' => 'foo', 'format' => '%d' ),
     643                'post_parent' => array( 'value' => 0, 'format' => '%s' ),
     644            )
     645        );
     646
     647        $misc_fields_no_format_specified = array(
     648            array( 'this_is_not_a_core_field' => 'foo', 'this_is_not_either' => 0 ),
     649            null,
     650            array(
     651                'this_is_not_a_core_field' => array( 'value' => 'foo', 'format' => '%s' ),
     652                'this_is_not_either' => array( 'value' => 0, 'format' => '%s' ),
     653            )
     654        );
     655
     656        $misc_fields_formats_specified = array(
     657            array( 'this_is_not_a_core_field' => 0, 'this_is_not_either' => 1.2 ),
     658            array( '%d', '%f' ),
     659            array(
     660                'this_is_not_a_core_field' => array( 'value' => 0, 'format' => '%d' ),
     661                'this_is_not_either' => array( 'value' => 1.2, 'format' => '%f' ),
     662            )
     663        );
     664
     665        $misc_fields_insufficient_formats_specified = array(
     666            array( 'this_is_not_a_core_field' => 0, 'this_is_not_either' => 's', 'nor_this' => 1 ),
     667            array( '%d', '%s' ), // The first format is used for the third
     668            array(
     669                'this_is_not_a_core_field' => array( 'value' => 0, 'format' => '%d' ),
     670                'this_is_not_either' => array( 'value' => 's', 'format' => '%s' ),
     671                'nor_this' => array( 'value' => 1, 'format' => '%d' ),
     672            )
     673        );
     674
     675        $vars = get_defined_vars();
     676        // Push the variable name onto the end for assertSame $message
     677        foreach ( $vars as $var_name => $var ) {
     678            $vars[ $var_name ][] = $var_name;
     679        }
     680        return array_values( $vars );
     681    }
     682
     683    /**
     684     * @dataProvider data_process_field_formats
     685     * @ticket 21212
     686     */
     687    function test_process_field_formats( $data, $format, $expected, $message ) {
     688        $actual = self::$_wpdb->process_field_formats( $data, $format );
     689        $this->assertSame( $expected, $actual, $message );
     690    }
     691
     692    /**
     693     * @ticket 21212
     694     */
     695    function test_process_fields() {
     696        global $wpdb;
     697
     698        if ( $wpdb->charset ) {
     699            $expected_charset = $wpdb->charset;
     700        } else {
     701            $expected_charset = $wpdb->get_col_charset( $wpdb->posts, 'post_content' );
     702        }
     703
     704        if ( ! in_array( $expected_charset, array( 'utf8', 'utf8mb4', 'latin1' ) ) ) {
     705            $this->markTestSkipped( "This test only works with utf8, utf8mb4 or latin1 character sets" );
     706        }
     707
     708        $data = array( 'post_content' => '¡foo foo foo!' );
     709        $expected = array(
     710            'post_content' => array(
     711                'value' => '¡foo foo foo!',
     712                'format' => '%s',
     713                'charset' => $expected_charset,
     714                'ascii' => false,
     715            )
     716        );
     717
     718        $this->assertSame( $expected, self::$_wpdb->process_fields( $wpdb->posts, $data, null ) );
     719    }
     720
     721    /**
     722     * @ticket 21212
     723     * @depends test_process_fields
     724     */
     725    function test_process_fields_on_nonexistent_table( $data ) {
     726        self::$_wpdb->suppress_errors( true );
     727        $data = array( 'post_content' => '¡foo foo foo!' );
     728        $this->assertFalse( self::$_wpdb->process_fields( 'nonexistent_table', $data, null ) );
     729        self::$_wpdb->suppress_errors( false );
     730    }
     731
     732    /**
     733     * @ticket 21212
     734     */
     735    function test_pre_get_table_charset_filter() {
     736        add_filter( 'pre_get_table_charset', array( $this, 'filter_pre_get_table_charset' ), 10, 2 );
     737        $charset = self::$_wpdb->get_table_charset( 'some_table' );
     738        remove_filter( 'pre_get_table_charset', array( $this, 'filter_pre_get_table_charset' ), 10 );
     739
     740        $this->assertEquals( $charset, 'fake_charset' );
     741    }
     742    function filter_pre_get_table_charset( $charset, $table ) {
     743        return 'fake_charset';
     744    }
     745
     746    /**
     747     * @ticket 21212
     748     */
     749    function test_pre_get_col_charset_filter() {
     750        add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset' ), 10, 3 );
     751        $charset = self::$_wpdb->get_col_charset( 'some_table', 'some_col' );
     752        remove_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset' ), 10 );
     753
     754        $this->assertEquals( $charset, 'fake_col_charset' );
     755    }
     756    function filter_pre_get_col_charset( $charset, $table, $column ) {
     757        return 'fake_col_charset';
     758    }
    461759}
     760
Note: See TracChangeset for help on using the changeset viewer.