diff --git a/tests/phpunit/tests/admin/includes/ajax-actions/menuLocationsSave.php b/tests/phpunit/tests/admin/includes/ajax-actions/menuLocationsSave.php new file mode 100644 index 0000000000000..12e1c2b67523b --- /dev/null +++ b/tests/phpunit/tests/admin/includes/ajax-actions/menuLocationsSave.php @@ -0,0 +1,141 @@ +user->create( array( 'role' => 'administrator' ) ); + self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) ); + } + + /** + * Tests successful saving of menu locations. + * + * @ticket 65252 + */ + public function test_menu_locations_save_success(): void { + wp_set_current_user( self::$admin_id ); + + $menu_id = self::factory()->term->create( + array( + 'name' => 'Test Menu', + 'taxonomy' => 'nav_menu', + ) + ); + + $_POST = array( + 'action' => 'menu-locations-save', + 'menu-settings-column-nonce' => wp_create_nonce( 'add-menu_item' ), + 'menu-locations' => array( + 'primary' => $menu_id, + ), + ); + + try { + $this->_handleAjax( 'menu-locations-save' ); + } catch ( WPAjaxDieStopException $e ) { + $this->assertSame( '1', $e->getMessage(), 'The AJAX response should be 1.' ); + } + + $locations = get_theme_mod( 'nav_menu_locations' ); + $this->assertIsArray( $locations ); + $this->assertSame( $menu_id, $locations['primary'] ); + } + + /** + * Tests failure due to invalid nonce. + * + * @ticket 65252 + */ + public function test_menu_locations_save_invalid_nonce(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'menu-locations-save', + 'menu-settings-column-nonce' => 'invalid-nonce', + 'menu-locations' => array( + 'primary' => 1, + ), + ); + + $this->expectException( WPAjaxDieStopException::class ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'menu-locations-save' ); + } + + /** + * Tests failure due to missing menu-locations. + * + * @ticket 65252 + */ + public function test_menu_locations_save_missing_locations(): void { + wp_set_current_user( self::$admin_id ); + + $_POST = array( + 'action' => 'menu-locations-save', + 'menu-settings-column-nonce' => wp_create_nonce( 'add-menu_item' ), + ); + + $this->expectException( WPAjaxDieStopException::class ); + $this->expectExceptionMessage( '0' ); + + $this->_handleAjax( 'menu-locations-save' ); + } + + /** + * Tests failure due to insufficient permissions. + * + * @ticket 65252 + */ + public function test_menu_locations_save_insufficient_permissions(): void { + wp_set_current_user( self::$subscriber_id ); + + $_POST = array( + 'action' => 'menu-locations-save', + 'menu-settings-column-nonce' => wp_create_nonce( 'add-menu_item' ), + 'menu-locations' => array( + 'primary' => 1, + ), + ); + + $this->expectException( WPAjaxDieStopException::class ); + $this->expectExceptionMessage( '-1' ); + + $this->_handleAjax( 'menu-locations-save' ); + } +}