Make WordPress Core

Ticket #44893: 44893.diff

File 44893.diff, 5.6 KB (added by flixos90, 6 years ago)
  • src/wp-admin/includes/schema.php

     
    356356 * Create WordPress options and set the default values.
    357357 *
    358358 * @since 1.5.0
     359 * @since 5.0.0 The $options parameter has been added.
    359360 *
    360361 * @global wpdb $wpdb WordPress database abstraction object.
    361362 * @global int  $wp_db_version
    362363 * @global int  $wp_current_db_version
     364 *
     365 * @param array $options Optional. Custom option $key => $value pairs to use. Default empty array.
    363366 */
    364 function populate_options() {
     367function populate_options( array $options = array() ) {
    365368        global $wpdb, $wp_db_version, $wp_current_db_version;
    366369
    367370        $guessurl = wp_guess_url();
     
    406409                        $timezone_string = $offset_or_tz;
    407410        }
    408411
    409         $options = array(
     412        $defaults = array(
    410413                'siteurl'                         => $guessurl,
    411414                'home'                            => $guessurl,
    412415                'blogname'                        => __( 'My Site' ),
     
    542545
    543546        // 3.3
    544547        if ( ! is_multisite() ) {
    545                 $options['initial_db_version'] = ! empty( $wp_current_db_version ) && $wp_current_db_version < $wp_db_version
     548                $defaults['initial_db_version'] = ! empty( $wp_current_db_version ) && $wp_current_db_version < $wp_db_version
    546549                        ? $wp_current_db_version : $wp_db_version;
    547550        }
    548551
    549552        // 3.0 multisite
    550553        if ( is_multisite() ) {
    551554                /* translators: site tagline */
    552                 $options['blogdescription']     = sprintf( __( 'Just another %s site' ), get_network()->site_name );
    553                 $options['permalink_structure'] = '/%year%/%monthnum%/%day%/%postname%/';
     555                $defaults['blogdescription']     = sprintf( __( 'Just another %s site' ), get_network()->site_name );
     556                $defaults['permalink_structure'] = '/%year%/%monthnum%/%day%/%postname%/';
    554557        }
    555558
     559        $options = wp_parse_args( $options, $defaults );
     560
    556561        // Set autoload to no for these options
    557562        $fat_options = array( 'moderation_keys', 'recently_edited', 'blacklist_keys', 'uninstall_plugins' );
    558563
  • tests/phpunit/tests/admin/includesSchema.php

     
     1<?php
     2
     3/**
     4 * @group admin
     5 */
     6class Tests_Admin_Includes_Schema extends WP_UnitTestCase {
     7
     8        private static $options;
     9
     10        /**
     11         * Make sure the schema code is loaded before the tests are run.
     12         */
     13        public static function wpSetUpBeforeClass() {
     14                global $wpdb;
     15
     16                self::$options  = 'testprefix_options';
     17
     18                $options = self::$options;
     19
     20                require_once( ABSPATH . 'wp-admin/includes/schema.php' );
     21
     22                $charset_collate  = $wpdb->get_charset_collate();
     23                $max_index_length = 191;
     24
     25                $wpdb->query(
     26                        "
     27                        CREATE TABLE {$options} (
     28                                option_id bigint(20) unsigned NOT NULL auto_increment,
     29                                option_name varchar(191) NOT NULL default '',
     30                                option_value longtext NOT NULL,
     31                                autoload varchar(20) NOT NULL default 'yes',
     32                                PRIMARY KEY  (option_id),
     33                                UNIQUE KEY option_name (option_name)
     34                        ) {$charset_collate}
     35                        "
     36                );
     37        }
     38
     39        /**
     40         * Drop tables that were created before running the tests.
     41         */
     42        public static function wpTearDownAfterClass() {
     43                global $wpdb;
     44
     45                $options = self::$options;
     46
     47                $wpdb->query( "DROP TABLE IF EXISTS {$options}" );
     48        }
     49
     50        /**
     51         * @ticket 44893
     52         * @dataProvider data_populate_options
     53         */
     54        function test_populate_options( $options, $expected ) {
     55                global $wpdb;
     56
     57                remove_all_filters( 'option_admin_email' );
     58                remove_all_filters( 'pre_option_admin_email' );
     59                remove_all_filters( 'default_option_admin_email' );
     60
     61                $orig_options  = $wpdb->options;
     62                $wpdb->options = self::$options;
     63
     64                populate_options( $options );
     65
     66                wp_cache_delete( 'alloptions', 'options' );
     67
     68                $results = array();
     69                foreach ( $expected as $option => $value ) {
     70                        $results[ $option ] = get_option( $option );
     71                }
     72
     73                $wpdb->query( "TRUNCATE TABLE {$wpdb->options}" );
     74
     75                $wpdb->options = $orig_options;
     76
     77                $this->assertEquals( $expected, $results );
     78        }
     79
     80        public function data_populate_options() {
     81                return array(
     82                        array(
     83                                array(),
     84                                array(
     85                                        // Random options to check.
     86                                        'posts_per_rss'    => 10,
     87                                        'rss_use_excerpt'  => 0,
     88                                        'mailserver_url'   => 'mail.example.com',
     89                                        'mailserver_login' => 'login@example.com',
     90                                        'mailserver_pass'  => 'password',
     91                                ),
     92                        ),
     93                        array(
     94                                array(
     95                                        'posts_per_rss'    => 7,
     96                                        'rss_use_excerpt'  => 1,
     97                                ),
     98                                array(
     99                                        // Random options to check.
     100                                        'posts_per_rss'    => 7,
     101                                        'rss_use_excerpt'  => 1,
     102                                        'mailserver_url'   => 'mail.example.com',
     103                                        'mailserver_login' => 'login@example.com',
     104                                        'mailserver_pass'  => 'password',
     105                                ),
     106                        ),
     107                        array(
     108                                array(
     109                                        'custom_option' => '1',
     110                                ),
     111                                array(
     112                                        // Random options to check.
     113                                        'custom_option'    => '1',
     114                                        'posts_per_rss'    => 10,
     115                                        'rss_use_excerpt'  => 0,
     116                                        'mailserver_url'   => 'mail.example.com',
     117                                        'mailserver_login' => 'login@example.com',
     118                                        'mailserver_pass'  => 'password',
     119                                ),
     120                        ),
     121                        array(
     122                                array(
     123                                        'use_quicktags' => '1',
     124                                ),
     125                                array(
     126                                        // This option is on a blacklist and should never exist.
     127                                        'use_quicktags' => false,
     128                                ),
     129                        ),
     130                        array(
     131                                array(
     132                                        'rss_0123456789abcdef0123456789abcdef'    => '1',
     133                                        'rss_0123456789abcdef0123456789abcdef_ts' => '1',
     134                                ),
     135                                array(
     136                                        // These options would be obsolete magpie cache data and should never exist.
     137                                        'rss_0123456789abcdef0123456789abcdef'    => false,
     138                                        'rss_0123456789abcdef0123456789abcdef_ts' => false,
     139                                ),
     140                        ),
     141                );
     142        }
     143}