Make WordPress Core

Changeset 39276


Ignore:
Timestamp:
11/17/2016 08:17:57 AM (8 years ago)
Author:
westonruter
Message:

Customize: Add unit tests for importing theme starter content.

Props welcher, westonruter.
See #38114, #38533, #38615.
Fixes #38540.

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r39272 r39276  
    896896     * @var array
    897897     */
    898     protected $starter_content_settings_ids = array();
     898    protected $pending_starter_content_settings_ids = array();
    899899
    900900    /**
     
    954954                if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
    955955                    $this->set_post_value( $setting_id, $setting_value );
    956                     $this->starter_content_settings_ids[] = $setting_id;
     956                    $this->pending_starter_content_settings_ids[] = $setting_id;
    957957                }
    958958                $sidebar_widget_ids[] = $widget_id;
     
    962962            if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
    963963                $this->set_post_value( $setting_id, $sidebar_widget_ids );
    964                 $this->starter_content_settings_ids[] = $setting_id;
     964                $this->pending_starter_content_settings_ids[] = $setting_id;
    965965            }
    966966        }
     
    10161016                $nav_menus_created_posts = array_unique( array_merge( $nav_menus_created_posts, wp_list_pluck( $posts, 'ID' ) ) );
    10171017                $this->set_post_value( $setting_id, array_values( $nav_menus_created_posts ) );
    1018                 $this->starter_content_settings_ids[] = $setting_id;
     1018                $this->pending_starter_content_settings_ids[] = $setting_id;
    10191019            }
    10201020        }
     
    10571057                'name' => isset( $nav_menu['name'] ) ? $nav_menu['name'] : $nav_menu_location,
    10581058            ) );
    1059             $this->starter_content_settings_ids[] = $nav_menu_setting_id;
     1059            $this->pending_starter_content_settings_ids[] = $nav_menu_setting_id;
    10601060
    10611061            // @todo Add support for menu_item_parent.
     
    10841084                if ( empty( $changeset_data[ $nav_menu_item_setting_id ] ) || ! empty( $changeset_data[ $nav_menu_item_setting_id ]['starter_content'] ) ) {
    10851085                    $this->set_post_value( $nav_menu_item_setting_id, $nav_menu_item );
    1086                     $this->starter_content_settings_ids[] = $nav_menu_item_setting_id;
     1086                    $this->pending_starter_content_settings_ids[] = $nav_menu_item_setting_id;
    10871087                }
    10881088            }
     
    10911091            if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
    10921092                $this->set_post_value( $setting_id, $nav_menu_term_id );
    1093                 $this->starter_content_settings_ids[] = $setting_id;
     1093                $this->pending_starter_content_settings_ids[] = $setting_id;
    10941094            }
    10951095        }
     
    11031103            if ( empty( $changeset_data[ $name ] ) || ! empty( $changeset_data[ $name ]['starter_content'] ) ) {
    11041104                $this->set_post_value( $name, $value );
    1105                 $this->starter_content_settings_ids[] = $name;
     1105                $this->pending_starter_content_settings_ids[] = $name;
    11061106            }
    11071107        }
     
    11151115            if ( empty( $changeset_data[ $name ] ) || ! empty( $changeset_data[ $name ]['starter_content'] ) ) {
    11161116                $this->set_post_value( $name, $value );
    1117                 $this->starter_content_settings_ids[] = $name;
    1118             }
    1119         }
    1120 
    1121         if ( ! empty( $this->starter_content_settings_ids ) ) {
     1117                $this->pending_starter_content_settings_ids[] = $name;
     1118            }
     1119        }
     1120
     1121        if ( ! empty( $this->pending_starter_content_settings_ids ) ) {
    11221122            if ( did_action( 'customize_register' ) ) {
    11231123                $this->_save_starter_content_changeset();
     
    11361136    public function _save_starter_content_changeset() {
    11371137
    1138         if ( empty( $this->starter_content_settings_ids ) ) {
     1138        if ( empty( $this->pending_starter_content_settings_ids ) ) {
    11391139            return;
    11401140        }
    11411141
    11421142        $this->save_changeset_post( array(
    1143             'data' => array_fill_keys( $this->starter_content_settings_ids, array( 'starter_content' => true ) ),
     1143            'data' => array_fill_keys( $this->pending_starter_content_settings_ids, array( 'starter_content' => true ) ),
    11441144            'starter_content' => true,
    11451145        ) );
     1146
     1147        $this->pending_starter_content_settings_ids = array();
    11461148    }
    11471149
  • trunk/src/wp-includes/theme.php

    r39272 r39276  
    18251825function get_theme_starter_content() {
    18261826    $theme_support = get_theme_support( 'starter-content' );
    1827     if ( ! empty( $theme_support ) ) {
     1827    if ( is_array( $theme_support ) && ! empty( $theme_support[0] ) && is_array( $theme_support[0] ) ) {
    18281828        $config = $theme_support[0];
    18291829    } else {
  • trunk/tests/phpunit/tests/customize/manager.php

    r39181 r39276  
    164164        $this->assertContains( 'Invalid changeset UUID', $exception->getMessage() );
    165165
     166        update_option( 'fresh_site', 0 );
    166167        $wp_customize = new WP_Customize_Manager();
    167168        $wp_customize->setup_theme();
     169        $this->assertFalse( has_action( 'after_setup_theme', array( $wp_customize, 'import_theme_starter_content' ) ) );
     170
     171        // Make sure that starter content import gets queued on a fresh site.
     172        update_option( 'fresh_site', 1 );
     173        $wp_customize->setup_theme();
     174        $this->assertEquals( 100, has_action( 'after_setup_theme', array( $wp_customize, 'import_theme_starter_content' ) ) );
    168175    }
    169176
     
    297304        $wp_customize = new WP_Customize_Manager( array( 'changeset_uuid' => $uuid ) );
    298305        $this->assertEquals( $data, $wp_customize->changeset_data() );
     306    }
     307
     308    /**
     309     * Test WP_Customize_Manager::import_theme_starter_content().
     310     *
     311     * @covers WP_Customize_Manager::import_theme_starter_content()
     312     * @covers WP_Customize_Manager::_save_starter_content_changeset()
     313     */
     314    function test_import_theme_starter_content() {
     315        wp_set_current_user( self::$admin_user_id );
     316
     317        global $wp_customize;
     318        $wp_customize = new WP_Customize_Manager();
     319        $starter_content_config = array(
     320            'widgets' => array(
     321                'sidebar-1' => array(
     322                    'text_business_info',
     323                    'meta_custom' => array( 'meta', array(
     324                        'title' => 'Pre-hydrated meta widget.',
     325                    ) ),
     326                ),
     327            ),
     328            'nav_menus' => array(
     329                'top' => array(
     330                    'name'  => 'Menu Name',
     331                    'items' => array(
     332                        'page_home',
     333                        'page_about',
     334                        'page_blog',
     335                        'link_email',
     336                        'link_facebook',
     337                        'link_custom' => array(
     338                            'title' => 'Custom',
     339                            'url' => 'https://custom.example.com/',
     340                        ),
     341                    ),
     342                ),
     343            ),
     344            'posts' => array(
     345                'home',
     346                'about',
     347                'blog',
     348                'custom' => array(
     349                    'post_type' => 'post',
     350                    'post_title' => 'Custom',
     351                ),
     352            ),
     353            'options' => array(
     354                'blogname' => 'Starter Content Title',
     355                'blogdescription' => 'Starter Content Tagline',
     356                'show_on_front'  => 'page',
     357                'page_on_front'  => '{{home}}',
     358                'page_for_posts' => '{{blog}}',
     359            ),
     360        );
     361
     362        add_theme_support( 'starter-content', $starter_content_config );
     363        $this->assertEmpty( $wp_customize->unsanitized_post_values() );
     364        $wp_customize->import_theme_starter_content();
     365        $changeset_values = $wp_customize->unsanitized_post_values();
     366        $expected_setting_ids = array(
     367            'blogname',
     368            'blogdescription',
     369            'widget_text[2]',
     370            'widget_meta[3]',
     371            'sidebars_widgets[sidebar-1]',
     372            'nav_menus_created_posts',
     373            'nav_menu[-1]',
     374            'nav_menu_item[-1]',
     375            'nav_menu_item[-2]',
     376            'nav_menu_item[-3]',
     377            'nav_menu_item[-4]',
     378            'nav_menu_item[-5]',
     379            'nav_menu_item[-6]',
     380            'nav_menu_locations[top]',
     381            'show_on_front',
     382            'page_on_front',
     383            'page_for_posts',
     384        );
     385        $this->assertEqualSets( $expected_setting_ids, array_keys( $changeset_values ) );
     386
     387        foreach ( array( 'widget_text[2]', 'widget_meta[3]' ) as $setting_id ) {
     388            $this->assertInternalType( 'array', $changeset_values[ $setting_id ] );
     389            $instance_data = $wp_customize->widgets->sanitize_widget_instance( $changeset_values[ $setting_id ] );
     390            $this->assertInternalType( 'array', $instance_data );
     391            $this->assertArrayHasKey( 'title', $instance_data );
     392        }
     393
     394        $this->assertEquals( array( 'text-2', 'meta-3' ), $changeset_values['sidebars_widgets[sidebar-1]'] );
     395
     396        $posts_by_name = array();
     397        foreach ( $changeset_values['nav_menus_created_posts'] as $post_id ) {
     398            $post = get_post( $post_id );
     399            $this->assertEquals( 'auto-draft', $post->post_status );
     400            $posts_by_name[ $post->post_name ] = $post->ID;
     401        }
     402
     403        $this->assertEquals( 'page', $changeset_values['show_on_front'] );
     404        $this->assertEquals( $posts_by_name['home'], $changeset_values['page_on_front'] );
     405        $this->assertEquals( $posts_by_name['blog'], $changeset_values['page_for_posts'] );
     406
     407        $this->assertEquals( -1, $changeset_values['nav_menu_locations[top]'] );
     408        $this->assertEquals( $posts_by_name['home'], $changeset_values['nav_menu_item[-1]']['object_id'] );
     409
     410        $this->assertEmpty( $wp_customize->changeset_data() );
     411        $this->assertNull( $wp_customize->changeset_post_id() );
     412        $this->assertEquals( 1000, has_action( 'customize_register', array( $wp_customize, '_save_starter_content_changeset' ) ) );
     413        do_action( 'customize_register', $wp_customize ); // This will trigger the changeset save.
     414        $this->assertInternalType( 'int', $wp_customize->changeset_post_id() );
     415        $this->assertNotEmpty( $wp_customize->changeset_data() );
     416        foreach ( $wp_customize->changeset_data() as $setting_id => $setting_params ) {
     417            $this->assertArrayHasKey( 'starter_content', $setting_params );
     418            $this->assertTrue( $setting_params['starter_content'] );
     419        }
     420
     421        // Test that saving non-starter content on top of the changeset clears the starter_content flag.
     422        $wp_customize->save_changeset_post( array(
     423            'data' => array(
     424                'blogname' => array( 'value' => 'Starter Content Modified' ),
     425            ),
     426        ) );
     427        $changeset_data = $wp_customize->changeset_data();
     428        $this->assertArrayNotHasKey( 'starter_content', $changeset_data['blogname'] );
     429        $this->assertArrayHasKey( 'starter_content', $changeset_data['blogdescription'] );
     430
     431        // Test that adding blogname starter content is ignored now that it is modified, but updating a non-modified starter content blog description passes.
     432        $previous_blogname = $changeset_data['blogname']['value'];
     433        $previous_blogdescription = $changeset_data['blogdescription']['value'];
     434        $wp_customize->import_theme_starter_content( array(
     435            'options' => array(
     436                'blogname' => 'Newer Starter Content Title',
     437                'blogdescription' => 'Newer Starter Content Description',
     438            ),
     439        ) );
     440        $changeset_data = $wp_customize->changeset_data();
     441        $this->assertEquals( $previous_blogname, $changeset_data['blogname']['value'] );
     442        $this->assertArrayNotHasKey( 'starter_content', $changeset_data['blogname'] );
     443        $this->assertNotEquals( $previous_blogdescription, $changeset_data['blogdescription']['value'] );
     444        $this->assertArrayHasKey( 'starter_content', $changeset_data['blogdescription'] );
    299445    }
    300446
Note: See TracChangeset for help on using the changeset viewer.