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/nav-menus.php

    r36414 r36586  
    354354        $expected = array( 'type' => 'nav_menu_item' );
    355355        $results = $menus->filter_dynamic_setting_args( $this->wp_customize, 'nav_menu_item[123]' );
    356         $this->assertEquals( $expected, $results );
     356        $this->assertEquals( $expected['type'], $results['type'] );
    357357
    358358        $expected = array( 'type' => 'nav_menu' );
    359359        $results = $menus->filter_dynamic_setting_args( $this->wp_customize, 'nav_menu[123]' );
    360         $this->assertEquals( $expected, $results );
     360        $this->assertEquals( $expected['type'], $results['type'] );
    361361    }
    362362
     
    524524
    525525    /**
     526     * Test WP_Customize_Nav_Menus::customize_dynamic_partial_args().
     527     *
     528     * @see WP_Customize_Nav_Menus::customize_dynamic_partial_args()
     529     */
     530    function test_customize_dynamic_partial_args() {
     531        do_action( 'customize_register', $this->wp_customize );
     532
     533        $args = apply_filters( 'customize_dynamic_partial_args', false, 'nav_menu_instance[68b329da9893e34099c7d8ad5cb9c940]' );
     534        $this->assertInternalType( 'array', $args );
     535        $this->assertEquals( 'nav_menu_instance', $args['type'] );
     536        $this->assertEquals( array( $this->wp_customize->nav_menus, 'render_nav_menu_partial' ), $args['render_callback'] );
     537        $this->assertTrue( $args['container_inclusive'] );
     538
     539        $args = apply_filters( 'customize_dynamic_partial_args', array( 'fallback_refresh' => false ), 'nav_menu_instance[4099c7d8ad5cb9c94068b329da9893e3]' );
     540        $this->assertInternalType( 'array', $args );
     541        $this->assertEquals( 'nav_menu_instance', $args['type'] );
     542        $this->assertEquals( array( $this->wp_customize->nav_menus, 'render_nav_menu_partial' ), $args['render_callback'] );
     543        $this->assertTrue( $args['container_inclusive'] );
     544        $this->assertFalse( $args['fallback_refresh'] );
     545    }
     546
     547    /**
    526548     * Test the customize_preview_init method.
    527549     *
     
    533555
    534556        $menus->customize_preview_init();
    535         $this->assertEquals( 10, has_action( 'template_redirect', array( $menus, 'render_menu' ) ) );
    536557        $this->assertEquals( 10, has_action( 'wp_enqueue_scripts', array( $menus, 'customize_preview_enqueue_deps' ) ) );
    537 
    538         if ( ! isset( $_REQUEST[ WP_Customize_Nav_Menus::RENDER_QUERY_VAR ] ) ) {
    539             $this->assertEquals( 1000, has_filter( 'wp_nav_menu_args', array( $menus, 'filter_wp_nav_menu_args' ) ) );
    540             $this->assertEquals( 10, has_filter( 'wp_nav_menu', array( $menus, 'filter_wp_nav_menu' ) ) );
    541         }
     558        $this->assertEquals( 1000, has_filter( 'wp_nav_menu_args', array( $menus, 'filter_wp_nav_menu_args' ) ) );
     559        $this->assertEquals( 10, has_filter( 'wp_nav_menu', array( $menus, 'filter_wp_nav_menu' ) ) );
    542560    }
    543561
     
    549567    function test_filter_wp_nav_menu_args() {
    550568        do_action( 'customize_register', $this->wp_customize );
    551         $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
     569        $menus = $this->wp_customize->nav_menus;
    552570
    553571        $results = $menus->filter_wp_nav_menu_args( array(
     
    556574            'walker'          => '',
    557575            'menu'            => wp_create_nav_menu( 'Foo' ),
     576            'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    558577        ) );
    559         $this->assertEquals( 1, $results['can_partial_refresh'] );
    560 
    561         $expected = array(
    562             'echo',
    563             'can_partial_refresh',
    564             'fallback_cb',
    565             'instance_number',
    566             'walker',
    567         );
     578        $this->assertArrayHasKey( 'customize_preview_nav_menus_args', $results );
     579
    568580        $results = $menus->filter_wp_nav_menu_args( array(
    569581            'echo'            => false,
    570582            'fallback_cb'     => 'wp_page_menu',
    571583            'walker'          => new Walker_Nav_Menu(),
     584            'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    572585        ) );
    573         $this->assertEqualSets( $expected, array_keys( $results ) );
     586        $this->assertArrayNotHasKey( 'customize_preview_nav_menus_args', $results );
    574587        $this->assertEquals( 'wp_page_menu', $results['fallback_cb'] );
    575         $this->assertEquals( 0, $results['can_partial_refresh'] );
    576 
    577         $this->assertNotEmpty( $menus->preview_nav_menu_instance_args[ $results['instance_number'] ] );
    578         $preview_nav_menu_instance_args = $menus->preview_nav_menu_instance_args[ $results['instance_number'] ];
    579         $this->assertEquals( '', $preview_nav_menu_instance_args['fallback_cb'] );
    580         $this->assertEquals( '', $preview_nav_menu_instance_args['walker'] );
    581         $this->assertNotEmpty( $preview_nav_menu_instance_args['args_hash'] );
     588
     589        $nav_menu_term = get_term( wp_create_nav_menu( 'Bar' ) );
     590        $results = $menus->filter_wp_nav_menu_args( array(
     591            'echo'            => true,
     592            'fallback_cb'     => 'wp_page_menu',
     593            'walker'          => '',
     594            'menu'            => $nav_menu_term,
     595            'items_wrap'      => '<ul id="%1$s" class="%2$s">%3$s</ul>',
     596        ) );
     597        $this->assertArrayHasKey( 'customize_preview_nav_menus_args', $results );
     598        $this->assertEquals( $nav_menu_term->term_id, $results['customize_preview_nav_menus_args']['menu'] );
    582599    }
    583600
     
    596613            'fallback_cb' => 'wp_page_menu',
    597614            'walker'      => '',
     615            'items_wrap'  => '<ul id="%1$s" class="%2$s">%3$s</ul>',
    598616        ) );
    599617
     
    602620        $nav_menu_content = ob_get_clean();
    603621
    604         $object_args = json_decode( json_encode( $args ), false );
    605         $result = $menus->filter_wp_nav_menu( $nav_menu_content, $object_args );
    606         $expected = sprintf(
    607             '<div class="partial-refreshable-nav-menu partial-refreshable-nav-menu-%1$d menu">',
    608             $args['instance_number']
    609         );
    610         $this->assertStringStartsWith( $expected, $result );
     622        $result = $menus->filter_wp_nav_menu( $nav_menu_content, (object) $args );
     623
     624        $this->assertContains( sprintf( ' data-customize-partial-id="nav_menu_instance[%s]"', $args['customize_preview_nav_menus_args']['args_hmac'] ), $result );
     625        $this->assertContains( ' data-customize-partial-type="nav_menu_instance"', $result );
     626        $this->assertContains( ' data-customize-partial-placement-context="', $result );
    611627    }
    612628
     
    623639
    624640        $this->assertTrue( wp_script_is( 'customize-preview-nav-menus' ) );
    625         $this->assertEquals( 10, has_action( 'wp_print_footer_scripts', array( $menus, 'export_preview_data' ) ) );
    626     }
    627 
    628     /**
    629      * Test the export_preview_data method.
     641    }
     642
     643    /**
     644     * Test WP_Customize_Nav_Menus::export_preview_data() method.
    630645     *
    631646     * @see WP_Customize_Nav_Menus::export_preview_data()
    632647     */
    633648    function test_export_preview_data() {
    634         do_action( 'customize_register', $this->wp_customize );
    635         $menus = new WP_Customize_Nav_Menus( $this->wp_customize );
    636 
    637         $request_uri = $_SERVER['REQUEST_URI'];
    638 
    639         ob_start();
    640         $_SERVER['REQUEST_URI'] = '/wp-admin';
    641         $menus->export_preview_data();
    642         $data = ob_get_clean();
    643 
    644         $_SERVER['REQUEST_URI'] = $request_uri;
    645 
    646         $this->assertContains( '_wpCustomizePreviewNavMenusExports', $data );
    647         $this->assertContains( 'renderQueryVar', $data );
    648         $this->assertContains( 'renderNonceValue', $data );
    649         $this->assertContains( 'renderNoncePostKey', $data );
    650         $this->assertContains( 'navMenuInstanceArgs', $data );
     649        $this->setExpectedDeprecated( 'WP_Customize_Nav_Menus::export_preview_data' );
     650        $this->wp_customize->nav_menus->export_preview_data();
     651    }
     652
     653    /**
     654     * Test WP_Customize_Nav_Menus::render_nav_menu_partial() method.
     655     *
     656     * @see WP_Customize_Nav_Menus::render_nav_menu_partial()
     657     */
     658    function test_render_nav_menu_partial() {
     659        $this->wp_customize->nav_menus->customize_preview_init();
     660
     661        $menu = wp_create_nav_menu( 'Foo' );
     662        wp_update_nav_menu_item( $menu, 0, array(
     663            'menu-item-type' => 'custom',
     664            'menu-item-title' => 'WordPress.org',
     665            'menu-item-url' => 'https://wordpress.org',
     666            'menu-item-status' => 'publish',
     667        ) );
     668
     669        $nav_menu_args = $this->wp_customize->nav_menus->filter_wp_nav_menu_args( array(
     670            'echo'        => true,
     671            'menu'        => $menu,
     672            'fallback_cb' => 'wp_page_menu',
     673            'walker'      => '',
     674            'items_wrap'  => '<ul id="%1$s" class="%2$s">%3$s</ul>',
     675        ) );
     676
     677        $partial_id = sprintf( 'nav_menu_instance[%s]', $nav_menu_args['customize_preview_nav_menus_args']['args_hmac'] );
     678        $partials = $this->wp_customize->selective_refresh->add_dynamic_partials( array( $partial_id ) );
     679        $this->assertNotEmpty( $partials );
     680        $partial = array_shift( $partials );
     681        $this->assertEquals( $partial_id, $partial->id );
     682
     683        $missing_args_hmac_args = array_merge(
     684            $nav_menu_args['customize_preview_nav_menus_args'],
     685            array( 'args_hmac' => null )
     686        );
     687        $this->assertFalse( $partial->render( $missing_args_hmac_args ) );
     688
     689        $args_hmac_mismatch_args = array_merge(
     690            $nav_menu_args['customize_preview_nav_menus_args'],
     691            array( 'args_hmac' => strrev( $nav_menu_args['customize_preview_nav_menus_args']['args_hmac'] ) )
     692        );
     693        $this->assertFalse( $partial->render( $args_hmac_mismatch_args ) );
     694
     695        $rendered = $partial->render( $nav_menu_args['customize_preview_nav_menus_args'] );
     696        $this->assertContains( 'data-customize-partial-type="nav_menu_instance"', $rendered );
     697        $this->assertContains( 'WordPress.org', $rendered );
    651698    }
    652699}
Note: See TracChangeset for help on using the changeset viewer.