|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Admin Ajax functions to be tested. |
| 5 | + */ |
| 6 | +require_once ABSPATH . 'wp-admin/includes/ajax-actions.php'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Testing wp_ajax_delete_link() functionality. |
| 10 | + * |
| 11 | + * @package WordPress |
| 12 | + * @subpackage UnitTests |
| 13 | + * @since 3.1.0 |
| 14 | + * |
| 15 | + * @group ajax |
| 16 | + * |
| 17 | + * @covers ::wp_ajax_delete_link |
| 18 | + */ |
| 19 | +class Tests_wp_ajax_delete_link extends WP_Ajax_UnitTestCase { |
| 20 | + |
| 21 | + /** |
| 22 | + * Administrator user ID. |
| 23 | + * |
| 24 | + * @var int |
| 25 | + */ |
| 26 | + protected static $admin_id; |
| 27 | + |
| 28 | + /** |
| 29 | + * Subscriber user ID. |
| 30 | + * |
| 31 | + * @var int |
| 32 | + */ |
| 33 | + protected static $subscriber_id; |
| 34 | + |
| 35 | + /** |
| 36 | + * Setup test fixtures. |
| 37 | + * |
| 38 | + * @param WP_UnitTest_Factory $factory |
| 39 | + */ |
| 40 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ): void { |
| 41 | + self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); |
| 42 | + self::$subscriber_id = $factory->user->create( array( 'role' => 'subscriber' ) ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Setup before each test method. |
| 47 | + */ |
| 48 | + public function set_up(): void { |
| 49 | + parent::set_up(); |
| 50 | + update_option( 'link_manager_enabled', 1 ); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Tests successful link deletion. |
| 55 | + * |
| 56 | + * @ticket 65252 |
| 57 | + */ |
| 58 | + public function test_delete_link_success(): void { |
| 59 | + wp_set_current_user( self::$admin_id ); |
| 60 | + |
| 61 | + $link_id = self::factory()->bookmark->create(); |
| 62 | + |
| 63 | + $_POST = array( |
| 64 | + 'id' => $link_id, |
| 65 | + '_ajax_nonce' => wp_create_nonce( "delete-bookmark_$link_id" ), |
| 66 | + ); |
| 67 | + |
| 68 | + try { |
| 69 | + $this->_handleAjax( 'delete-link' ); |
| 70 | + } catch ( WPAjaxDieStopException $e ) { |
| 71 | + // Expect success. |
| 72 | + $this->_last_response = $e->getMessage(); |
| 73 | + } catch ( WPAjaxDieContinueException $e ) { |
| 74 | + // Expect success. |
| 75 | + } |
| 76 | + |
| 77 | + $this->assertSame( '1', $this->_last_response ); |
| 78 | + $this->assertNull( get_bookmark( $link_id ), 'Link should be deleted.' ); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Tests link deletion failure due to invalid nonce. |
| 83 | + * |
| 84 | + * @ticket 65252 |
| 85 | + */ |
| 86 | + public function test_delete_link_invalid_nonce(): void { |
| 87 | + wp_set_current_user( self::$admin_id ); |
| 88 | + |
| 89 | + $link_id = self::factory()->bookmark->create(); |
| 90 | + |
| 91 | + $_POST = array( |
| 92 | + 'id' => $link_id, |
| 93 | + '_ajax_nonce' => 'invalid-nonce', |
| 94 | + ); |
| 95 | + |
| 96 | + try { |
| 97 | + $this->_handleAjax( 'delete-link' ); |
| 98 | + $this->fail( 'Expected link deletion to stop due to an invalid nonce.' ); |
| 99 | + } catch ( WPAjaxDieStopException $e ) { |
| 100 | + $this->assertSame( '-1', $e->getMessage() ); |
| 101 | + } |
| 102 | + |
| 103 | + $this->assertNotNull( get_bookmark( $link_id ), 'Link should not be deleted when the nonce is invalid.' ); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Tests link deletion failure due to insufficient permissions. |
| 108 | + * |
| 109 | + * @ticket 65252 |
| 110 | + */ |
| 111 | + public function test_delete_link_insufficient_permissions(): void { |
| 112 | + wp_set_current_user( self::$subscriber_id ); |
| 113 | + |
| 114 | + $link_id = self::factory()->bookmark->create(); |
| 115 | + |
| 116 | + $_POST = array( |
| 117 | + 'id' => $link_id, |
| 118 | + '_ajax_nonce' => wp_create_nonce( "delete-bookmark_$link_id" ), |
| 119 | + ); |
| 120 | + |
| 121 | + $this->expectException( WPAjaxDieStopException::class ); |
| 122 | + $this->expectExceptionMessage( '-1' ); |
| 123 | + |
| 124 | + $this->_handleAjax( 'delete-link' ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Tests link deletion with non-existent ID. |
| 129 | + * |
| 130 | + * @ticket 65252 |
| 131 | + */ |
| 132 | + public function test_delete_link_non_existent_id(): void { |
| 133 | + wp_set_current_user( self::$admin_id ); |
| 134 | + |
| 135 | + $link_id = 99999; |
| 136 | + |
| 137 | + $_POST = array( |
| 138 | + 'id' => $link_id, |
| 139 | + '_ajax_nonce' => wp_create_nonce( "delete-bookmark_$link_id" ), |
| 140 | + ); |
| 141 | + |
| 142 | + try { |
| 143 | + $this->_handleAjax( 'delete-link' ); |
| 144 | + } catch ( WPAjaxDieStopException $e ) { |
| 145 | + // Expect success. |
| 146 | + $this->_last_response = $e->getMessage(); |
| 147 | + } catch ( WPAjaxDieContinueException $e ) { |
| 148 | + // Expect success. |
| 149 | + } |
| 150 | + |
| 151 | + $this->assertSame( '1', $this->_last_response ); |
| 152 | + $this->assertNull( get_bookmark( $link_id ), 'no Link and no error.' ); |
| 153 | + } |
| 154 | +} |
0 commit comments