diff --git a/src/wp-admin/includes/schema.php b/src/wp-admin/includes/schema.php index 4611eadc9f..ce6d794c0c 100644 --- a/src/wp-admin/includes/schema.php +++ b/src/wp-admin/includes/schema.php @@ -356,12 +356,15 @@ $wp_queries = wp_get_db_schema( 'all' ); * Create WordPress options and set the default values. * * @since 1.5.0 + * @since 5.0.0 The $options parameter has been added. * * @global wpdb $wpdb WordPress database abstraction object. * @global int $wp_db_version * @global int $wp_current_db_version + * + * @param array $options Optional. Custom option $key => $value pairs to use. Default empty array. */ -function populate_options() { +function populate_options( array $options = array() ) { global $wpdb, $wp_db_version, $wp_current_db_version; $guessurl = wp_guess_url(); @@ -406,7 +409,7 @@ function populate_options() { $timezone_string = $offset_or_tz; } - $options = array( + $defaults = array( 'siteurl' => $guessurl, 'home' => $guessurl, 'blogname' => __( 'My Site' ), @@ -542,17 +545,19 @@ function populate_options() { // 3.3 if ( ! is_multisite() ) { - $options['initial_db_version'] = ! empty( $wp_current_db_version ) && $wp_current_db_version < $wp_db_version + $defaults['initial_db_version'] = ! empty( $wp_current_db_version ) && $wp_current_db_version < $wp_db_version ? $wp_current_db_version : $wp_db_version; } // 3.0 multisite if ( is_multisite() ) { /* translators: site tagline */ - $options['blogdescription'] = sprintf( __( 'Just another %s site' ), get_network()->site_name ); - $options['permalink_structure'] = '/%year%/%monthnum%/%day%/%postname%/'; + $defaults['blogdescription'] = sprintf( __( 'Just another %s site' ), get_network()->site_name ); + $defaults['permalink_structure'] = '/%year%/%monthnum%/%day%/%postname%/'; } + $options = wp_parse_args( $options, $defaults ); + // Set autoload to no for these options $fat_options = array( 'moderation_keys', 'recently_edited', 'blacklist_keys', 'uninstall_plugins' ); diff --git a/tests/phpunit/tests/admin/includesSchema.php b/tests/phpunit/tests/admin/includesSchema.php new file mode 100644 index 0000000000..346d1732af --- /dev/null +++ b/tests/phpunit/tests/admin/includesSchema.php @@ -0,0 +1,143 @@ +get_charset_collate(); + $max_index_length = 191; + + $wpdb->query( + " + CREATE TABLE {$options} ( + option_id bigint(20) unsigned NOT NULL auto_increment, + option_name varchar(191) NOT NULL default '', + option_value longtext NOT NULL, + autoload varchar(20) NOT NULL default 'yes', + PRIMARY KEY (option_id), + UNIQUE KEY option_name (option_name) + ) {$charset_collate} + " + ); + } + + /** + * Drop tables that were created before running the tests. + */ + public static function wpTearDownAfterClass() { + global $wpdb; + + $options = self::$options; + + $wpdb->query( "DROP TABLE IF EXISTS {$options}" ); + } + + /** + * @ticket 44893 + * @dataProvider data_populate_options + */ + function test_populate_options( $options, $expected ) { + global $wpdb; + + remove_all_filters( 'option_admin_email' ); + remove_all_filters( 'pre_option_admin_email' ); + remove_all_filters( 'default_option_admin_email' ); + + $orig_options = $wpdb->options; + $wpdb->options = self::$options; + + populate_options( $options ); + + wp_cache_delete( 'alloptions', 'options' ); + + $results = array(); + foreach ( $expected as $option => $value ) { + $results[ $option ] = get_option( $option ); + } + + $wpdb->query( "TRUNCATE TABLE {$wpdb->options}" ); + + $wpdb->options = $orig_options; + + $this->assertEquals( $expected, $results ); + } + + public function data_populate_options() { + return array( + array( + array(), + array( + // Random options to check. + 'posts_per_rss' => 10, + 'rss_use_excerpt' => 0, + 'mailserver_url' => 'mail.example.com', + 'mailserver_login' => 'login@example.com', + 'mailserver_pass' => 'password', + ), + ), + array( + array( + 'posts_per_rss' => 7, + 'rss_use_excerpt' => 1, + ), + array( + // Random options to check. + 'posts_per_rss' => 7, + 'rss_use_excerpt' => 1, + 'mailserver_url' => 'mail.example.com', + 'mailserver_login' => 'login@example.com', + 'mailserver_pass' => 'password', + ), + ), + array( + array( + 'custom_option' => '1', + ), + array( + // Random options to check. + 'custom_option' => '1', + 'posts_per_rss' => 10, + 'rss_use_excerpt' => 0, + 'mailserver_url' => 'mail.example.com', + 'mailserver_login' => 'login@example.com', + 'mailserver_pass' => 'password', + ), + ), + array( + array( + 'use_quicktags' => '1', + ), + array( + // This option is on a blacklist and should never exist. + 'use_quicktags' => false, + ), + ), + array( + array( + 'rss_0123456789abcdef0123456789abcdef' => '1', + 'rss_0123456789abcdef0123456789abcdef_ts' => '1', + ), + array( + // These options would be obsolete magpie cache data and should never exist. + 'rss_0123456789abcdef0123456789abcdef' => false, + 'rss_0123456789abcdef0123456789abcdef_ts' => false, + ), + ), + ); + } +}