@@ -425,18 +425,85 @@ public function test_permission_callback_returns_false_for_malformed_input( $inp
425425 }
426426
427427 /**
428- * The permission callback is only a coarse gate: it lets a logged-in subscriber through, and
429- * the execute callback decides on the post.
428+ * The permission callback returns false for a post the user cannot delete, in every form the
429+ * post can be named in input that was not prepared.
430+ *
431+ * @ticket 66706
432+ * @dataProvider data_unprepared_where_forms
433+ * @since 7.3.0
434+ *
435+ * @param string $id_type The type to send the ID as.
436+ * @param bool $as_object Whether to send `where` as an object.
437+ */
438+ public function test_permission_callback_returns_false_for_a_post_the_user_cannot_delete ( string $ id_type , bool $ as_object ): void {
439+ $ this ->login_as ( 'contributor ' );
440+
441+ $ permission = $ this ->get_ability_callbacks ()['permission_callback ' ];
442+ $ where = $ this ->build_unprepared_where ( self ::$ post_ids ['admin_post ' ], $ id_type , $ as_object );
443+
444+ $ this ->assertFalse ( $ permission ( array ( 'where ' => $ where ) ), 'The permission callback should return false. ' );
445+ }
446+
447+ /**
448+ * The permission callback returns true for a post the user can delete.
430449 *
431450 * @ticket 66706
432451 * @since 7.3.0
433452 */
434- public function test_permission_callback_returns_true_for_a_logged_in_user_and_an_exposed_type (): void {
453+ public function test_permission_callback_returns_true_for_a_post_the_user_can_delete (): void {
454+ $ this ->login_as ( 'author ' );
455+
456+ $ permission = $ this ->get_ability_callbacks ()['permission_callback ' ];
457+
458+ $ this ->assertTrue ( $ permission ( $ this ->delete_input ( self ::$ post_ids ['author:draft ' ] ) ), 'The permission callback should return true. ' );
459+ }
460+
461+ /**
462+ * The permission callback lets a post that does not exist through, also for a user who may
463+ * delete nothing.
464+ *
465+ * @ticket 66706
466+ * @since 7.3.0
467+ */
468+ public function test_permission_callback_lets_a_missing_post_through (): void {
435469 $ this ->login_as ( 'subscriber ' );
436470
437471 $ permission = $ this ->get_ability_callbacks ()['permission_callback ' ];
438472
439- $ this ->assertTrue ( $ permission ( $ this ->delete_input ( self ::$ post_ids ['admin_post ' ] ) ), 'The permission callback should return true. ' );
473+ $ this ->assertTrue (
474+ $ permission (
475+ array (
476+ 'where ' => array (
477+ 'type ' => 'post ' ,
478+ 'id ' => self ::MISSING_ID ,
479+ ),
480+ )
481+ ),
482+ 'The permission callback should return true. '
483+ );
484+ }
485+
486+ /**
487+ * A permission result that a filter turned into a grant does not skip the capability check
488+ * on the post.
489+ *
490+ * @ticket 66706
491+ * @since 7.3.0
492+ */
493+ public function test_granted_permission_result_does_not_skip_the_capability_check (): void {
494+ $ post_id = self ::$ post_ids ['admin_post ' ];
495+
496+ $ this ->login_as ( 'contributor ' );
497+ $ this ->register_abilities ();
498+ add_filter ( 'wp_ability_permission_result ' , '__return_true ' );
499+
500+ $ before = $ this ->snapshot ( $ post_id );
501+ $ trash = $ this ->run_ability ( $ this ->delete_input ( $ post_id ) );
502+ $ permanent = $ this ->run_ability ( $ this ->delete_input ( $ post_id , true ) );
503+
504+ $ this ->assert_cannot_delete ( $ trash , 'A trash granted by a filter ' );
505+ $ this ->assert_cannot_delete ( $ permanent , 'A permanent delete granted by a filter ' );
506+ $ this ->assertSame ( $ before , $ this ->snapshot ( $ post_id ), 'Nothing should be written. ' );
440507 }
441508
442509 /**
@@ -603,7 +670,7 @@ public function test_direct_call_with_a_malformed_where_is_not_found( array $inp
603670
604671 /**
605672 * The lookup comes before the capability: a user who may delete nothing gets the not-found
606- * error for a missing post, and the 403 error for an existing post.
673+ * error for a missing post, and is refused for an existing post.
607674 *
608675 * @ticket 66706
609676 * @since 7.3.0
@@ -624,7 +691,7 @@ public function test_lookup_comes_before_the_capability(): void {
624691 $ existing = $ this ->run_ability ( $ this ->delete_input ( self ::$ post_ids ['admin_post ' ] ) );
625692
626693 $ this ->assert_not_found ( $ missing , 'A missing post ' );
627- $ this ->assert_cannot_delete ( $ existing , 'An existing post the user may not delete ' );
694+ $ this ->assert_refused ( $ existing , 'An existing post the user may not delete ' );
628695 $ this ->assertSame ( $ before , $ this ->snapshot ( self ::$ post_ids ['admin_post ' ] ), 'The refused post should still exist, unchanged. ' );
629696 }
630697
@@ -710,7 +777,7 @@ public static function data_delete_matrix(): array {
710777
711778 /**
712779 * Each role may trash the posts it may delete, gets the 410 error for those already in the
713- * Trash, and the 403 error for the others, whatever their status.
780+ * Trash, and is refused for the others, whatever their status.
714781 *
715782 * @ticket 66706
716783 * @dataProvider data_delete_matrix
@@ -738,14 +805,14 @@ public function test_trash_matrix( string $role, string $owner, string $status,
738805 if ( $ may_delete ) {
739806 $ this ->assert_error ( $ result , 'content_already_trashed ' , 410 , 'Trashing a trashed post the role may delete ' );
740807 } else {
741- $ this ->assert_cannot_delete ( $ result , 'Trashing a post the role may not delete ' );
808+ $ this ->assert_refused ( $ result , 'Trashing a post the role may not delete ' );
742809 }
743810 $ this ->assertSame ( $ before , $ this ->snapshot ( $ post_id ), 'Nothing should be written. ' );
744811 }
745812
746813 /**
747814 * Each role may delete permanently the posts it may delete, including posts in the Trash, and
748- * gets the 403 error for the others, whatever their status.
815+ * is refused for the others, whatever their status.
749816 *
750817 * @ticket 66706
751818 * @dataProvider data_delete_matrix
@@ -770,7 +837,7 @@ public function test_permanent_delete_matrix( string $role, string $owner, strin
770837 return ;
771838 }
772839
773- $ this ->assert_cannot_delete ( $ result , 'Deleting a post the role may not delete ' );
840+ $ this ->assert_refused ( $ result , 'Deleting a post the role may not delete ' );
774841 $ this ->assertSame ( $ before , $ this ->snapshot ( $ post_id ), 'Nothing should be written. ' );
775842 }
776843
@@ -863,8 +930,8 @@ public function test_editor_cannot_delete_a_special_page( string $page ): void {
863930 $ trash = $ this ->run_ability ( $ this ->delete_input ( $ post_id ) );
864931 $ permanent = $ this ->run_ability ( $ this ->delete_input ( $ post_id , true ) );
865932
866- $ this ->assert_cannot_delete ( $ trash , 'An editor trashing a special page ' );
867- $ this ->assert_cannot_delete ( $ permanent , 'An editor deleting a special page ' );
933+ $ this ->assert_refused ( $ trash , 'An editor trashing a special page ' );
934+ $ this ->assert_refused ( $ permanent , 'An editor deleting a special page ' );
868935 $ this ->assertSame ( $ before , $ this ->snapshot ( $ post_id ), 'Nothing should be written. ' );
869936 }
870937
@@ -955,7 +1022,7 @@ public function test_site_administrator_deletes_special_pages_on_multisite( stri
9551022 return ;
9561023 }
9571024
958- $ this ->assert_cannot_delete ( $ result , 'A site administrator trashing the privacy policy page ' );
1025+ $ this ->assert_refused ( $ result , 'A site administrator trashing the privacy policy page ' );
9591026 $ this ->assertSame ( $ before , $ this ->snapshot ( $ post_id ), 'Nothing should be written. ' );
9601027 }
9611028
0 commit comments