-
Notifications
You must be signed in to change notification settings - Fork 3.5k
65341 ajax actions widgets order #11963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
Changes from all commits
f888576
97a6be7
388b875
211aa63
754efcb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Admin Ajax functions to be tested. | ||
| */ | ||
| require_once ABSPATH . 'wp-admin/includes/ajax-actions.php'; | ||
|
|
||
| /** | ||
| * Testing wp_ajax_widgets_order() functionality. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage UnitTests | ||
| * @since 3.1.0 | ||
| * | ||
| * @group ajax | ||
| * | ||
| * @covers ::wp_ajax_widgets_order | ||
| */ | ||
| class Tests_wp_ajax_widgets_order extends WP_Ajax_UnitTestCase { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the classname, or filename, needs to be changed? If running these tests in isolation, by referencing the filename, you get an error about being unable to find class |
||
|
|
||
| /** | ||
| * Administrator user ID. | ||
| * | ||
| * @var int | ||
| */ | ||
| protected static $admin_id; | ||
|
|
||
| /** | ||
| * Setup test fixtures. | ||
| * | ||
| * @param WP_UnitTest_Factory $factory | ||
| */ | ||
| public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void { | ||
| self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests successful widgets order saving. | ||
| * | ||
| * @ticket 65252 | ||
| */ | ||
| public function test_widgets_order_success(): void { | ||
| wp_set_current_user( self::$admin_id ); | ||
|
|
||
| // Mock sidebars. | ||
| $sidebars = array( | ||
| 'sidebar-1' => 'widget-1_text-1,widget-2_text-2', | ||
| 'sidebar-2' => 'widget-3_search-1', | ||
| ); | ||
|
|
||
| $_POST = array( | ||
| 'action' => 'widgets-order', | ||
| 'savewidgets' => wp_create_nonce( 'save-sidebar-widgets' ), | ||
| 'sidebars' => $sidebars, | ||
| ); | ||
|
|
||
| $this->expectException( WPAjaxDieStopException::class ); | ||
| $this->expectExceptionMessage( '1' ); | ||
|
|
||
| $this->_handleAjax( 'widgets-order' ); | ||
|
|
||
| $this->assertSame( '1', $this->_last_response ); | ||
|
|
||
| $updated_sidebars = wp_get_sidebars_widgets(); | ||
| $this->assertContains( 'text-1', $updated_sidebars['sidebar-1'] ); | ||
| $this->assertContains( 'text-2', $updated_sidebars['sidebar-1'] ); | ||
| $this->assertContains( 'search-1', $updated_sidebars['sidebar-2'] ); | ||
|
Comment on lines
+65
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think these assertions run because they come after phpunit catches the exception thrown by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this is the best/proper approach, but if you do the test like this, it will run the other assertions: |
||
| } | ||
|
|
||
| /** | ||
| * Tests failure due to invalid nonce. | ||
| * | ||
| * @ticket 65252 | ||
| */ | ||
| public function test_widgets_order_invalid_nonce(): void { | ||
| wp_set_current_user( self::$admin_id ); | ||
|
|
||
| $_POST = array( | ||
| 'action' => 'widgets-order', | ||
| 'savewidgets' => 'invalid-nonce', | ||
| ); | ||
|
|
||
| $this->expectException( WPAjaxDieStopException::class ); | ||
| $this->expectExceptionMessage( '-1' ); | ||
|
|
||
| $this->_handleAjax( 'widgets-order' ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests failure due to insufficient permissions. | ||
| * | ||
| * @ticket 65252 | ||
| */ | ||
| public function test_widgets_order_insufficient_permissions(): void { | ||
| $user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) ); | ||
| wp_set_current_user( $user_id ); | ||
|
|
||
| $_POST = array( | ||
| 'action' => 'widgets-order', | ||
| 'savewidgets' => wp_create_nonce( 'save-sidebar-widgets' ), | ||
| ); | ||
|
|
||
| $this->expectException( WPAjaxDieStopException::class ); | ||
| $this->expectExceptionMessage( '-1' ); | ||
|
|
||
| $this->_handleAjax( 'widgets-order' ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests behavior when sidebars parameter is missing. | ||
| * | ||
| * @ticket 65252 | ||
| */ | ||
| public function test_widgets_order_missing_sidebars(): void { | ||
| wp_set_current_user( self::$admin_id ); | ||
|
|
||
| $_POST = array( | ||
| 'action' => 'widgets-order', | ||
| 'savewidgets' => wp_create_nonce( 'save-sidebar-widgets' ), | ||
| ); | ||
|
|
||
| $this->expectException( WPAjaxDieStopException::class ); | ||
| $this->expectExceptionMessage( '-1' ); | ||
|
|
||
| $this->_handleAjax( 'widgets-order' ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the correct
@sinceversion?