Make WordPress Core


Ignore:
Timestamp:
04/20/2015 05:08:00 AM (10 years ago)
Author:
pento
Message:

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

Props pento, nacin, mdawaffe, DrewAPicture.

File:
1 edited

Legend:

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

    r30807 r32163  
    233233        $this->assertNotEmpty( $wpdb->dbh );
    234234    }
     235
     236    /**
     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
    235251    /**
    236252     * @ticket 18510
     
    505521        $wpdb->suppress_errors( $suppress );
    506522    }
     523
     524    /**
     525     * @ticket 21212
     526     */
     527    function data_get_table_from_query() {
     528        $table = 'a_test_table_name';
     529
     530        $queries = array(
     531            // Basic
     532            "SELECT * FROM $table",
     533            "SELECT * FROM `$table`",
     534
     535            "INSERT $table",
     536            "INSERT IGNORE $table",
     537            "INSERT IGNORE INTO $table",
     538            "INSERT INTO $table",
     539            "INSERT LOW_PRIORITY $table",
     540            "INSERT DELAYED $table",
     541            "INSERT HIGH_PRIORITY $table",
     542            "INSERT LOW_PRIORITY IGNORE $table",
     543            "INSERT LOW_PRIORITY INTO $table",
     544            "INSERT LOW_PRIORITY IGNORE INTO $table",
     545
     546            "REPLACE $table",
     547            "REPLACE INTO $table",
     548            "REPLACE LOW_PRIORITY $table",
     549            "REPLACE DELAYED $table",
     550            "REPLACE LOW_PRIORITY INTO $table",
     551
     552            "UPDATE LOW_PRIORITY $table",
     553            "UPDATE LOW_PRIORITY IGNORE $table",
     554
     555            "DELETE $table",
     556            "DELETE IGNORE $table",
     557            "DELETE IGNORE FROM $table",
     558            "DELETE FROM $table",
     559            "DELETE LOW_PRIORITY $table",
     560            "DELETE QUICK $table",
     561            "DELETE IGNORE $table",
     562            "DELETE LOW_PRIORITY FROM $table",
     563
     564            // STATUS
     565            "SHOW TABLE STATUS LIKE '$table'",
     566            "SHOW TABLE STATUS WHERE NAME='$table'",
     567
     568            "SHOW TABLES LIKE '$table'",
     569            "SHOW FULL TABLES LIKE '$table'",
     570            "SHOW TABLES WHERE NAME='$table'",
     571
     572            // Extended
     573            "EXPLAIN SELECT * FROM $table",
     574            "EXPLAIN EXTENDED SELECT * FROM $table",
     575            "EXPLAIN EXTENDED SELECT * FROM `$table`",
     576
     577            "DESCRIBE $table",
     578            "DESC $table",
     579            "EXPLAIN $table",
     580            "HANDLER $table",
     581
     582            "LOCK TABLE $table",
     583            "LOCK TABLES $table",
     584            "UNLOCK TABLE $table",
     585
     586            "RENAME TABLE $table",
     587            "OPTIMIZE TABLE $table",
     588            "BACKUP TABLE $table",
     589            "RESTORE TABLE $table",
     590            "CHECK TABLE $table",
     591            "CHECKSUM TABLE $table",
     592            "ANALYZE TABLE $table",
     593            "REPAIR TABLE $table",
     594
     595            "TRUNCATE $table",
     596            "TRUNCATE TABLE $table",
     597
     598            "CREATE TABLE $table",
     599            "CREATE TEMPORARY TABLE $table",
     600            "CREATE TABLE IF NOT EXISTS $table",
     601
     602            "ALTER TABLE $table",
     603            "ALTER IGNORE TABLE $table",
     604
     605            "DROP TABLE $table",
     606            "DROP TABLE IF EXISTS $table",
     607
     608            "CREATE INDEX foo(bar(20)) ON $table",
     609            "CREATE UNIQUE INDEX foo(bar(20)) ON $table",
     610            "CREATE FULLTEXT INDEX foo(bar(20)) ON $table",
     611            "CREATE SPATIAL INDEX foo(bar(20)) ON $table",
     612
     613            "DROP INDEX foo ON $table",
     614
     615            "LOAD DATA INFILE 'wp.txt' INTO TABLE $table",
     616            "LOAD DATA LOW_PRIORITY INFILE 'wp.txt' INTO TABLE $table",
     617            "LOAD DATA CONCURRENT INFILE 'wp.txt' INTO TABLE $table",
     618            "LOAD DATA LOW_PRIORITY LOCAL INFILE 'wp.txt' INTO TABLE $table",
     619            "LOAD DATA INFILE 'wp.txt' REPLACE INTO TABLE $table",
     620            "LOAD DATA INFILE 'wp.txt' IGNORE INTO TABLE $table",
     621
     622            "GRANT ALL ON TABLE $table",
     623            "REVOKE ALL ON TABLE $table",
     624
     625            "SHOW COLUMNS FROM $table",
     626            "SHOW FULL COLUMNS FROM $table",
     627            "SHOW CREATE TABLE $table",
     628            "SHOW INDEX FROM $table",
     629        );
     630
     631        foreach ( $queries as &$query ) {
     632            $query = array( $query, $table );
     633        }
     634        return $queries;
     635    }
     636
     637    /**
     638     * @dataProvider data_get_table_from_query
     639     * @ticket 21212
     640     */
     641    function test_get_table_from_query( $query, $table ) {
     642        $this->assertEquals( $table, self::$_wpdb->get_table_from_query( $query ) );
     643    }
     644
     645    function data_get_table_from_query_false() {
     646        $table = 'a_test_table_name';
     647        return array(
     648            array( "LOL THIS ISN'T EVEN A QUERY $table" ),
     649        );
     650    }
     651
     652    /**
     653     * @dataProvider data_get_table_from_query_false
     654     * @ticket 21212
     655     */
     656    function test_get_table_from_query_false( $query ) {
     657        $this->assertFalse( self::$_wpdb->get_table_from_query( $query ) );
     658    }
     659
     660    /**
     661     * @ticket 21212
     662     */
     663    function data_process_field_formats() {
     664        $core_db_fields_no_format_specified = array(
     665            array( 'post_content' => 'foo', 'post_parent' => 0 ),
     666            null,
     667            array(
     668                'post_content' => array( 'value' => 'foo', 'format' => '%s' ),
     669                'post_parent' => array( 'value' => 0, 'format' => '%d' ),
     670            )
     671        );
     672
     673        $core_db_fields_formats_specified = array(
     674            array( 'post_content' => 'foo', 'post_parent' => 0 ),
     675            array( '%d', '%s' ), // These override core field_types
     676            array(
     677                'post_content' => array( 'value' => 'foo', 'format' => '%d' ),
     678                'post_parent' => array( 'value' => 0, 'format' => '%s' ),
     679            )
     680        );
     681
     682        $misc_fields_no_format_specified = array(
     683            array( 'this_is_not_a_core_field' => 'foo', 'this_is_not_either' => 0 ),
     684            null,
     685            array(
     686                'this_is_not_a_core_field' => array( 'value' => 'foo', 'format' => '%s' ),
     687                'this_is_not_either' => array( 'value' => 0, 'format' => '%s' ),
     688            )
     689        );
     690
     691        $misc_fields_formats_specified = array(
     692            array( 'this_is_not_a_core_field' => 0, 'this_is_not_either' => 1.2 ),
     693            array( '%d', '%f' ),
     694            array(
     695                'this_is_not_a_core_field' => array( 'value' => 0, 'format' => '%d' ),
     696                'this_is_not_either' => array( 'value' => 1.2, 'format' => '%f' ),
     697            )
     698        );
     699
     700        $misc_fields_insufficient_formats_specified = array(
     701            array( 'this_is_not_a_core_field' => 0, 'this_is_not_either' => 's', 'nor_this' => 1 ),
     702            array( '%d', '%s' ), // The first format is used for the third
     703            array(
     704                'this_is_not_a_core_field' => array( 'value' => 0, 'format' => '%d' ),
     705                'this_is_not_either' => array( 'value' => 's', 'format' => '%s' ),
     706                'nor_this' => array( 'value' => 1, 'format' => '%d' ),
     707            )
     708        );
     709
     710        $vars = get_defined_vars();
     711        // Push the variable name onto the end for assertSame $message
     712        foreach ( $vars as $var_name => $var ) {
     713            $vars[ $var_name ][] = $var_name;
     714        }
     715        return array_values( $vars );
     716    }
     717
     718    /**
     719     * @dataProvider data_process_field_formats
     720     * @ticket 21212
     721     */
     722    function test_process_field_formats( $data, $format, $expected, $message ) {
     723        $actual = self::$_wpdb->process_field_formats( $data, $format );
     724        $this->assertSame( $expected, $actual, $message );
     725    }
     726
     727    /**
     728     * @ticket 21212
     729     */
     730    function test_process_fields() {
     731        global $wpdb;
     732
     733        if ( $wpdb->charset ) {
     734            $expected_charset = $wpdb->charset;
     735        } else {
     736            $expected_charset = $wpdb->get_col_charset( $wpdb->posts, 'post_content' );
     737        }
     738
     739        if ( ! in_array( $expected_charset, array( 'utf8', 'utf8mb4', 'latin1' ) ) ) {
     740            $this->markTestSkipped( "This test only works with utf8, utf8mb4 or latin1 character sets" );
     741        }
     742
     743        $data = array( 'post_content' => '¡foo foo foo!' );
     744        $expected = array(
     745            'post_content' => array(
     746                'value' => '¡foo foo foo!',
     747                'format' => '%s',
     748                'charset' => $expected_charset,
     749                'ascii' => false,
     750            )
     751        );
     752
     753        $this->assertSame( $expected, self::$_wpdb->process_fields( $wpdb->posts, $data, null ) );
     754    }
     755
     756    /**
     757     * @ticket 21212
     758     * @depends test_process_fields
     759     */
     760    function test_process_fields_on_nonexistent_table( $data ) {
     761        self::$_wpdb->suppress_errors( true );
     762        $data = array( 'post_content' => '¡foo foo foo!' );
     763        $this->assertFalse( self::$_wpdb->process_fields( 'nonexistent_table', $data, null ) );
     764        self::$_wpdb->suppress_errors( false );
     765    }
     766
     767    /**
     768     * @ticket 21212
     769     */
     770    function test_pre_get_table_charset_filter() {
     771        add_filter( 'pre_get_table_charset', array( $this, 'filter_pre_get_table_charset' ), 10, 2 );
     772        $charset = self::$_wpdb->get_table_charset( 'some_table' );
     773        remove_filter( 'pre_get_table_charset', array( $this, 'filter_pre_get_table_charset' ), 10 );
     774
     775        $this->assertEquals( $charset, 'fake_charset' );
     776    }
     777    function filter_pre_get_table_charset( $charset, $table ) {
     778        return 'fake_charset';
     779    }
     780
     781    /**
     782     * @ticket 21212
     783     */
     784    function test_pre_get_col_charset_filter() {
     785        add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset' ), 10, 3 );
     786        $charset = self::$_wpdb->get_col_charset( 'some_table', 'some_col' );
     787        remove_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset' ), 10 );
     788
     789        $this->assertEquals( $charset, 'fake_col_charset' );
     790    }
     791    function filter_pre_get_col_charset( $charset, $table, $column ) {
     792        return 'fake_col_charset';
     793    }
    507794}
    508795
Note: See TracChangeset for help on using the changeset viewer.