Repository navigation
Ajax: Add unit tests for wp_ajax_delete_link() #11872
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
Open
pbearne
wants to merge
6
commits into
WordPress:trunk
Choose a base branch
from
pbearne:65252-ajax-actions-delete_link
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
70fa3d2
Ajax: Add unit tests for wp_ajax_delete_link()
665cd22
Merge branch 'trunk' into 65252-ajax-actions-delete_link
pbearne 03bac5b
Merge branch 'trunk' into 65252-ajax-actions-delete_link
pbearne 0fe16b8
Ajax: Update `wp_ajax_delete_link` tests for consistency and improved…
5600c4a
Merge branch 'trunk' into 65252-ajax-actions-delete_link
pbearne 36265c9
Apply suggestion from @mindctrl
pbearne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
154 changes: 154 additions & 0 deletions
154
tests/phpunit/tests/admin/includes/ajax-actions/deleteLink.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Admin Ajax functions to be tested. | ||
| */ | ||
| require_once ABSPATH . 'wp-admin/includes/ajax-actions.php'; | ||
|
|
||
| /** | ||
| * Testing wp_ajax_delete_link() functionality. | ||
| * | ||
| * @package WordPress | ||
| * @subpackage UnitTests | ||
| * @since 3.1.0 | ||
| * | ||
| * @group ajax | ||
| * | ||
| * @covers ::wp_ajax_delete_link | ||
| */ | ||
| class Tests_wp_ajax_delete_link extends WP_Ajax_UnitTestCase { | ||
|
|
||
| /** | ||
| * Administrator user ID. | ||
| * | ||
| * @var int | ||
| */ | ||
| protected static $admin_id; | ||
|
|
||
| /** | ||
| * Subscriber user ID. | ||
| * | ||
| * @var int | ||
| */ | ||
| protected static $subscriber_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' ) ); | ||
| self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Setup before each test method. | ||
| */ | ||
| public function set_up(): void { | ||
| parent::set_up(); | ||
| update_option( 'link_manager_enabled', 1 ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests successful link deletion. | ||
| * | ||
| * @ticket 65252 | ||
| */ | ||
| public function test_delete_link_success(): void { | ||
| wp_set_current_user( self::$admin_id ); | ||
|
|
||
| $link_id = self::factory()->bookmark->create(); | ||
|
|
||
| $_POST = array( | ||
| 'id' => $link_id, | ||
| '_ajax_nonce' => wp_create_nonce( "delete-bookmark_$link_id" ), | ||
| ); | ||
|
|
||
| try { | ||
| $this->_handleAjax( 'delete-link' ); | ||
| } catch ( WPAjaxDieStopException $e ) { | ||
| // Expect success. | ||
| $this->_last_response = $e->getMessage(); | ||
| } catch ( WPAjaxDieContinueException $e ) { | ||
| // Expect success. | ||
| } | ||
|
|
||
| $this->assertSame( '1', $this->_last_response ); | ||
| $this->assertNull( get_bookmark( $link_id ), 'Link should be deleted.' ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests link deletion failure due to invalid nonce. | ||
| * | ||
| * @ticket 65252 | ||
| */ | ||
| public function test_delete_link_invalid_nonce(): void { | ||
| wp_set_current_user( self::$admin_id ); | ||
|
|
||
| $link_id = self::factory()->bookmark->create(); | ||
|
|
||
| $_POST = array( | ||
| 'id' => $link_id, | ||
| '_ajax_nonce' => 'invalid-nonce', | ||
| ); | ||
|
|
||
| try { | ||
| $this->_handleAjax( 'delete-link' ); | ||
| $this->fail( 'Expected link deletion to stop due to an invalid nonce.' ); | ||
| } catch ( WPAjaxDieStopException $e ) { | ||
| $this->assertSame( '-1', $e->getMessage() ); | ||
| } | ||
|
|
||
| $this->assertNotNull( get_bookmark( $link_id ), 'Link should not be deleted when the nonce is invalid.' ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests link deletion failure due to insufficient permissions. | ||
| * | ||
| * @ticket 65252 | ||
| */ | ||
| public function test_delete_link_insufficient_permissions(): void { | ||
| wp_set_current_user( self::$subscriber_id ); | ||
|
|
||
| $link_id = self::factory()->bookmark->create(); | ||
|
|
||
| $_POST = array( | ||
| 'id' => $link_id, | ||
| '_ajax_nonce' => wp_create_nonce( "delete-bookmark_$link_id" ), | ||
| ); | ||
|
|
||
| $this->expectException( WPAjaxDieStopException::class ); | ||
| $this->expectExceptionMessage( '-1' ); | ||
|
|
||
| $this->_handleAjax( 'delete-link' ); | ||
| } | ||
|
|
||
| /** | ||
| * Tests link deletion with non-existent ID. | ||
| * | ||
| * @ticket 65252 | ||
| */ | ||
| public function test_delete_link_non_existent_id(): void { | ||
| wp_set_current_user( self::$admin_id ); | ||
|
|
||
| $link_id = 99999; | ||
|
|
||
| $_POST = array( | ||
| 'id' => $link_id, | ||
| '_ajax_nonce' => wp_create_nonce( "delete-bookmark_$link_id" ), | ||
| ); | ||
|
|
||
| try { | ||
| $this->_handleAjax( 'delete-link' ); | ||
| } catch ( WPAjaxDieStopException $e ) { | ||
| // Expect success. | ||
| $this->_last_response = $e->getMessage(); | ||
| } catch ( WPAjaxDieContinueException $e ) { | ||
| // Expect success. | ||
| } | ||
|
|
||
| $this->assertSame( '1', $this->_last_response ); | ||
| $this->assertNull( get_bookmark( $link_id ), 'no Link and no error.' ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.