Upgrade/Install: Add support for an optional `$options` parameter to `populate_options()`.

Fixes #44893. See #41333.


git-svn-id: https://develop.svn.wordpress.org/trunk@43627 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz 2018-09-05 09:38:04 +00:00
parent 811eef33a3
commit f1e1b6299e
2 changed files with 153 additions and 5 deletions

View File

@ -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' );

View File

@ -0,0 +1,143 @@
<?php
/**
* @group admin
*/
class Tests_Admin_Includes_Schema extends WP_UnitTestCase {
private static $options;
/**
* Make sure the schema code is loaded before the tests are run.
*/
public static function wpSetUpBeforeClass() {
global $wpdb;
self::$options = 'testprefix_options';
$options = self::$options;
require_once( ABSPATH . 'wp-admin/includes/schema.php' );
$charset_collate = $wpdb->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,
),
),
);
}
}