WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/19/2016 06:40:06 PM (6 years ago)
Author:
westonruter
Message:

Customize: Add selective refresh framework with implementation for widgets and re-implementation for nav menus.

See https://make.wordpress.org/core/2016/02/16/selective-refresh-in-the-customizer/.

Props westonruter, valendesigns, DrewAPicture, ocean90.
Fixes #27355.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/customize/widgets.php

    r35754 r36586  
    4040        remove_action( 'after_setup_theme', 'twentyfifteen_setup' );
    4141        remove_action( 'after_setup_theme', 'twentysixteen_setup' );
     42        remove_action( 'customize_register', 'twentysixteen_customize_register', 11 );
    4243
    4344        $user_id = self::factory()->user->create( array( 'role' => 'administrator' ) );
     
    122123            'type' => 'option',
    123124            'capability' => 'edit_theme_options',
    124             'transport' => 'refresh',
     125            'transport' => 'postMessage',
    125126            'default' => array(),
    126127            'sanitize_callback' => array( $this->manager->widgets, 'sanitize_widget_instance' ),
     
    151152            'type' => 'option',
    152153            'capability' => 'edit_theme_options',
    153             'transport' => 'refresh',
     154            'transport' => 'postMessage',
    154155            'default' => array(),
    155156            'sanitize_callback' => array( $this->manager->widgets, 'sanitize_sidebar_widgets' ),
     
    347348        $this->assertEquals( $post_value, $this->manager->widgets->sanitize_widget_js_instance( $instance ) );
    348349    }
     350
     351    /**
     352     * Test WP_Customize_Widgets::customize_dynamic_partial_args().
     353     *
     354     * @see WP_Customize_Widgets::customize_dynamic_partial_args()
     355     */
     356    function test_customize_dynamic_partial_args() {
     357        do_action( 'customize_register', $this->manager );
     358
     359        $args = apply_filters( 'customize_dynamic_partial_args', false, 'widget[search-2]' );
     360        $this->assertInternalType( 'array', $args );
     361        $this->assertEquals( 'widget', $args['type'] );
     362        $this->assertEquals( array( $this->manager->widgets, 'render_widget_partial' ), $args['render_callback'] );
     363        $this->assertTrue( $args['container_inclusive'] );
     364
     365        $args = apply_filters( 'customize_dynamic_partial_args', array( 'fallback_refresh' => false ), 'widget[search-2]' );
     366        $this->assertInternalType( 'array', $args );
     367        $this->assertEquals( 'widget', $args['type'] );
     368        $this->assertEquals( array( $this->manager->widgets, 'render_widget_partial' ), $args['render_callback'] );
     369        $this->assertTrue( $args['container_inclusive'] );
     370        $this->assertFalse( $args['fallback_refresh'] );
     371    }
     372
     373    /**
     374     * Test WP_Customize_Widgets::selective_refresh_init().
     375     *
     376     * @see WP_Customize_Widgets::selective_refresh_init()
     377     */
     378    function test_selective_refresh_init() {
     379        $this->manager->widgets->selective_refresh_init();
     380        $this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $this->manager->widgets, 'customize_preview_enqueue_deps' ) ) );
     381        $this->assertEquals( 10, has_action( 'dynamic_sidebar_before', array( $this->manager->widgets, 'start_dynamic_sidebar' ) ) );
     382        $this->assertEquals( 10, has_action( 'dynamic_sidebar_after', array( $this->manager->widgets, 'end_dynamic_sidebar' ) ) );
     383        $this->assertEquals( 10, has_filter( 'dynamic_sidebar_params', array( $this->manager->widgets, 'filter_dynamic_sidebar_params' ) ) );
     384        $this->assertEquals( 10, has_filter( 'wp_kses_allowed_html', array( $this->manager->widgets, 'filter_wp_kses_allowed_data_attributes' ) ) );
     385    }
     386
     387    /**
     388     * Test WP_Customize_Widgets::customize_preview_enqueue_deps().
     389     *
     390     * @see WP_Customize_Widgets::customize_preview_enqueue_deps()
     391     */
     392    function test_customize_preview_enqueue_deps() {
     393        $this->manager->widgets->customize_preview_enqueue_deps();
     394        $this->assertTrue( wp_script_is( 'customize-preview-widgets', 'enqueued' ) );
     395        $this->assertTrue( wp_style_is( 'customize-preview', 'enqueued' ) );
     396        $script = wp_scripts()->registered['customize-preview-widgets'];
     397        $this->assertContains( 'customize-selective-refresh', $script->deps );
     398    }
     399
     400    /**
     401     * Test extensions to dynamic_sidebar().
     402     *
     403     * @see WP_Customize_Widgets::filter_dynamic_sidebar_params()
     404     * @see WP_Customize_Widgets::start_dynamic_sidebar()
     405     * @see WP_Customize_Widgets::end_dynamic_sidebar()
     406     */
     407    function test_filter_dynamic_sidebar_params() {
     408        global $wp_registered_sidebars;
     409        register_sidebar( array(
     410            'id' => 'foo',
     411        ) );
     412
     413        $this->manager->widgets->selective_refresh_init();
     414
     415        $params = array(
     416            array_merge(
     417                $wp_registered_sidebars['foo'],
     418                array(
     419                    'widget_id' => 'search-2',
     420                )
     421            ),
     422            array(),
     423        );
     424        $this->assertEquals( $params, $this->manager->widgets->filter_dynamic_sidebar_params( $params ), 'Expected short-circuit if not called after dynamic_sidebar_before.' );
     425
     426        ob_start();
     427        do_action( 'dynamic_sidebar_before', 'foo' );
     428        $output = ob_get_clean();
     429        $this->assertEquals( '<!--dynamic_sidebar_before:foo:1-->', trim( $output ) );
     430
     431        $bad_params = $params;
     432        unset( $bad_params[0]['id'] );
     433        $this->assertEquals( $bad_params, $this->manager->widgets->filter_dynamic_sidebar_params( $bad_params ) );
     434
     435        $bad_params = $params;
     436        $bad_params[0]['id'] = 'non-existing';
     437        $this->assertEquals( $bad_params, $this->manager->widgets->filter_dynamic_sidebar_params( $bad_params ) );
     438
     439        $bad_params = $params;
     440        $bad_params[0]['before_widget'] = '   <oops>';
     441        $this->assertEquals( $bad_params, $this->manager->widgets->filter_dynamic_sidebar_params( $bad_params ) );
     442
     443        $filtered_params = $this->manager->widgets->filter_dynamic_sidebar_params( $params );
     444        $this->assertNotEquals( $params, $filtered_params );
     445        ob_start();
     446        do_action( 'dynamic_sidebar_after', 'foo' );
     447        $output = ob_get_clean();
     448        $this->assertEquals( '<!--dynamic_sidebar_after:foo:1-->', trim( $output ) );
     449
     450        $output = wp_kses_post( $filtered_params[0]['before_widget'] );
     451        $this->assertContains( 'data-customize-partial-id="widget[search-2]"', $output );
     452        $this->assertContains( 'data-customize-partial-type="widget"', $output );
     453    }
     454
     455    /**
     456     * Test WP_Customize_Widgets::render_widget_partial() method.
     457     *
     458     * @see WP_Customize_Widgets::render_widget_partial()
     459     */
     460    function test_render_widget_partial() {
     461        $this->manager->widgets->selective_refresh_init();
     462
     463        $partial_id = 'widget[search-2]';
     464        $partials = $this->manager->selective_refresh->add_dynamic_partials( array( $partial_id ) );
     465        $this->assertNotEmpty( $partials );
     466        $partial = array_shift( $partials );
     467        $this->assertEquals( $partial_id, $partial->id );
     468
     469        $this->assertFalse( $this->manager->widgets->render_widget_partial( $partial, array() ) );
     470        $this->assertFalse( $this->manager->widgets->render_widget_partial( $partial, array( 'sidebar_id' => 'non-existing' ) ) );
     471
     472        $output = $this->manager->widgets->render_widget_partial( $partial, array( 'sidebar_id' => 'sidebar-1' ) );
     473
     474        $this->assertEquals( 1, substr_count( $output, 'data-customize-partial-id' ) );
     475        $this->assertEquals( 1, substr_count( $output, 'data-customize-partial-type="widget"' ) );
     476        $this->assertContains( ' id="search-2"', $output );
     477    }
     478
     479    /**
     480     * Test deprecated methods.
     481     */
     482    public function test_deprecated_methods() {
     483        $this->setExpectedDeprecated( 'WP_Customize_Widgets::setup_widget_addition_previews' );
     484        $this->setExpectedDeprecated( 'WP_Customize_Widgets::prepreview_added_sidebars_widgets' );
     485        $this->setExpectedDeprecated( 'WP_Customize_Widgets::prepreview_added_widget_instance' );
     486        $this->setExpectedDeprecated( 'WP_Customize_Widgets::remove_prepreview_filters' );
     487        $this->manager->widgets->setup_widget_addition_previews();
     488        $this->manager->widgets->prepreview_added_sidebars_widgets();
     489        $this->manager->widgets->prepreview_added_widget_instance();
     490        $this->manager->widgets->remove_prepreview_filters();
     491    }
    349492}
Note: See TracChangeset for help on using the changeset viewer.