Make WordPress Core

Ticket #44896: 44896.diff

File 44896.diff, 4.0 KB (added by flixos90, 8 years ago)
  • src/wp-admin/includes/schema.php

     
    12921292        }
    12931293        $wpdb->query( "INSERT INTO $wpdb->sitemeta ( site_id, meta_key, meta_value ) VALUES " . $insert ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
    12941294}
     1295
     1296/**
     1297 * Creates WordPress site meta and sets the default values.
     1298 *
     1299 * @since 5.0.0
     1300 *
     1301 * @global wpdb $wpdb WordPress database abstraction object.
     1302 *
     1303 * @param int   $site_id Site ID to populate meta for.
     1304 * @param array $meta    Optional. Custom meta $key => $value pairs to use. Default empty array.
     1305 */
     1306function populate_site_meta( $site_id, array $meta = array() ) {
     1307        global $wpdb;
     1308
     1309        $site_id = (int) $site_id;
     1310
     1311        if ( ! is_site_meta_supported() ) {
     1312                return;
     1313        }
     1314
     1315        if ( empty( $meta ) ) {
     1316                return;
     1317        }
     1318
     1319        $insert = '';
     1320        foreach ( $meta as $meta_key => $meta_value ) {
     1321                if ( is_array( $meta_value ) ) {
     1322                        $meta_value = serialize( $meta_value );
     1323                }
     1324                if ( ! empty( $insert ) ) {
     1325                        $insert .= ', ';
     1326                }
     1327                $insert .= $wpdb->prepare( '( %d, %s, %s)', $site_id, $meta_key, $meta_value );
     1328        }
     1329
     1330        $wpdb->query( "INSERT INTO $wpdb->blogmeta ( blog_id, meta_key, meta_value ) VALUES " . $insert ); // phpcs:ignore WordPress.WP.PreparedSQL.NotPrepared
     1331
     1332        wp_cache_set( 'last_changed', microtime(), 'sites' );
     1333}
  • tests/phpunit/tests/admin/includesSchema.php

     
    77class Tests_Admin_Includes_Schema extends WP_UnitTestCase {
    88
    99        private static $options;
     10        private static $blogmeta;
    1011        private static $sitemeta;
    1112
    1213        /**
     
    1617                global $wpdb;
    1718
    1819                self::$options  = 'testprefix_options';
     20                self::$blogmeta = 'testprefix_blogmeta';
    1921                self::$sitemeta = 'testprefix_sitemeta';
    2022
    2123                $options  = self::$options;
     24                $blogmeta = self::$blogmeta;
    2225                $sitemeta = self::$sitemeta;
    2326
    2427                require_once( ABSPATH . 'wp-admin/includes/schema.php' );
     
    4043                );
    4144                $wpdb->query(
    4245                        "
     46                        CREATE TABLE {$blogmeta} (
     47                                meta_id bigint(20) unsigned NOT NULL auto_increment,
     48                                blog_id bigint(20) unsigned NOT NULL default '0',
     49                                meta_key varchar(255) default NULL,
     50                                meta_value longtext,
     51                                PRIMARY KEY  (meta_id),
     52                                KEY meta_key (meta_key({$max_index_length})),
     53                                KEY blog_id (blog_id)
     54                        ) {$charset_collate}
     55                        "
     56                );
     57                $wpdb->query(
     58                        "
    4359                        CREATE TABLE {$sitemeta} (
    4460                                meta_id bigint(20) unsigned NOT NULL auto_increment,
    4561                                site_id bigint(20) unsigned NOT NULL default '0',
     
    6076                global $wpdb;
    6177
    6278                $options  = self::$options;
     79                $blogmeta = self::$blogmeta;
    6380                $sitemeta = self::$sitemeta;
    6481
    6582                $wpdb->query( "DROP TABLE IF EXISTS {$options}" );
     83                $wpdb->query( "DROP TABLE IF EXISTS {$blogmeta}" );
    6684                $wpdb->query( "DROP TABLE IF EXISTS {$sitemeta}" );
    6785        }
    6886
     
    157175        }
    158176
    159177        /**
     178         * @ticket 44896
     179         * @group multisite
     180         * @group ms-required
     181         * @dataProvider data_populate_site_meta
     182         */
     183        function test_populate_site_meta( $meta, $expected ) {
     184                global $wpdb;
     185
     186                $orig_blogmeta  = $wpdb->blogmeta;
     187                $wpdb->blogmeta = self::$blogmeta;
     188
     189                populate_site_meta( 42, $meta );
     190
     191                $results = array();
     192                foreach ( $expected as $meta_key => $value ) {
     193                        $results[ $meta_key ] = get_site_meta( 42, $meta_key, true );
     194                }
     195
     196                $wpdb->query( "TRUNCATE TABLE {$wpdb->blogmeta}" );
     197
     198                $wpdb->blogmeta = $orig_blogmeta;
     199
     200                $this->assertEquals( $expected, $results );
     201        }
     202
     203        public function data_populate_site_meta() {
     204                return array(
     205                        array(
     206                                array(),
     207                                array(
     208                                        'unknown_value' => '',
     209                                ),
     210                        ),
     211                        array(
     212                                array(
     213                                        'custom_meta' => '1',
     214                                ),
     215                                array(
     216                                        'custom_meta' => '1',
     217                                ),
     218                        ),
     219                );
     220        }
     221
     222        /**
    160223         * @ticket 44895
     224         * @group multisite
    161225         * @dataProvider data_populate_network_meta
    162226         */
    163227        function test_populate_network_meta( $meta, $expected ) {