WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/03/2015 08:46:48 PM (7 years ago)
Author:
westonruter
Message:

Customizer: Fix saving menus with empty names or names that are already used.

Adds validation for initially-supplied nav menu name, blocking empty names from being supplied. If later an empty name is supplied and the nav menu is saved, the name "(unnamed)" will be supplied instead and supplied back to the client. If a name is supplied for the menu which is currently used by another menu, then the name conflict is resolved by adding a numerical counter similar to how post_name conflicts are resolved. Includes unit tests.

Fixes #32760.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/customize/nav-menu-setting.php

    r32806 r33071  
    9595        do_action( 'customize_register', $this->wp_customize );
    9696        $_wp_customize = $this->wp_customize;
    97         unset($_wp_customize->nav_menus);
     97        unset( $_wp_customize->nav_menus );
    9898
    9999        $exception = null;
     
    311311        $this->assertEquals( true, $sanitized['auto_add'] );
    312312        $this->assertEqualSets( array( 'name', 'description', 'parent', 'auto_add' ), array_keys( $sanitized ) );
     313
     314        $value['name'] = '    '; // Blank spaces.
     315        $sanitized = $setting->sanitize( $value );
     316        $this->assertEquals( '(unnamed)', $sanitized['name'] );
    313317    }
    314318
     
    361365        $this->assertArrayHasKey( 'error', $update_result );
    362366        $this->assertArrayHasKey( 'status', $update_result );
     367        $this->assertArrayHasKey( 'saved_value', $update_result );
     368        $this->assertEquals( $new_value, $update_result['saved_value'] );
    363369
    364370        $this->assertEquals( $menu_id, $update_result['term_id'] );
     
    411417        $this->assertArrayHasKey( 'error', $update_result );
    412418        $this->assertArrayHasKey( 'status', $update_result );
     419        $this->assertArrayHasKey( 'saved_value', $update_result );
     420        $this->assertEquals( $setting->value(), $update_result['saved_value'] );
    413421
    414422        $this->assertEquals( $menu->term_id, $update_result['term_id'] );
     
    416424        $this->assertNull( $update_result['error'] );
    417425        $this->assertEquals( 'inserted', $update_result['status'] );
     426    }
     427
     428    /**
     429     * Test saving a new name that conflicts with an existing nav menu's name.
     430     *
     431     * @see WP_Customize_Nav_Menu_Setting::update()
     432     */
     433    function test_save_inserted_conflicted_name() {
     434        do_action( 'customize_register', $this->wp_customize );
     435
     436        $menu_name = 'Foo';
     437        wp_update_nav_menu_object( 0, array( 'menu-name' => $menu_name ) );
     438
     439        $menu_id = -123;
     440        $setting_id = "nav_menu[$menu_id]";
     441        $setting = new WP_Customize_Nav_Menu_Setting( $this->wp_customize, $setting_id );
     442        $this->wp_customize->set_post_value( $setting->id, array( 'name' => $menu_name ) );
     443        $setting->save();
     444
     445        $expected_resolved_menu_name = "$menu_name (2)";
     446        $new_menu = wp_get_nav_menu_object( $setting->term_id );
     447        $this->assertEquals( $expected_resolved_menu_name, $new_menu->name );
     448
     449        $save_response = apply_filters( 'customize_save_response', array() );
     450        $this->assertEquals( $expected_resolved_menu_name, $save_response['nav_menu_updates'][0]['saved_value']['name'] );
    418451    }
    419452
     
    449482        $this->assertArrayHasKey( 'error', $update_result );
    450483        $this->assertArrayHasKey( 'status', $update_result );
     484        $this->assertArrayHasKey( 'saved_value', $update_result );
     485        $this->assertNull( $update_result['saved_value'] );
    451486
    452487        $this->assertEquals( $menu_id, $update_result['term_id'] );
Note: See TracChangeset for help on using the changeset viewer.