Skip to content

Commit f206052

Browse files
committed
Adjust capability checks in AJAX callback tests
1 parent e621ac7 commit f206052

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

tests/BlcAjaxCallbacksTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,14 +1250,24 @@ public function test_edit_link_returns_success_when_post_has_been_deleted(): voi
12501250

12511251
Functions\when('check_ajax_referer')->justReturn(true);
12521252
Functions\expect('get_post')->once()->with(11)->andReturn(null);
1253-
Functions\expect('blc_current_user_can_fix_links')->once()->andReturn(true);
1253+
$capabilityCalls = 0;
1254+
Functions\when('blc_current_user_can_fix_links')->alias(static function () use (&$capabilityCalls) {
1255+
$capabilityCalls++;
1256+
1257+
return true;
1258+
});
12541259

12551260
global $wpdb;
12561261
$wpdb = $this->createAjaxWpdbStub();
12571262
$wpdb->prefix = 'wp_';
12581263
$wpdb->delete_return_value = 1;
12591264

1260-
Functions\expect('wp_send_json_success')->once()->with(['purged' => true])->andReturnUsing(function () {
1265+
Functions\expect('wp_send_json_success')->once()->with([
1266+
'purged' => true,
1267+
'row_removed' => true,
1268+
'rowRemoved' => true,
1269+
'previewOnly' => false,
1270+
])->andReturnUsing(function () {
12611271
throw new \Exception('success');
12621272
});
12631273

@@ -1268,6 +1278,8 @@ public function test_edit_link_returns_success_when_post_has_been_deleted(): voi
12681278
$this->assertSame('success', $exception->getMessage());
12691279
}
12701280

1281+
$this->assertGreaterThanOrEqual(2, $capabilityCalls, 'The capability should be checked for the request and during cleanup.');
1282+
12711283
$this->assertCount(1, $wpdb->delete_calls, 'Cleanup should remove the orphaned database row.');
12721284
$deleted = $wpdb->delete_calls[0];
12731285
$this->assertSame('wp_blc_broken_links', $deleted[0]);
@@ -2063,7 +2075,12 @@ public function test_unlink_returns_success_when_post_has_been_deleted(): void
20632075

20642076
Functions\when('check_ajax_referer')->justReturn(true);
20652077
Functions\expect('get_post')->once()->with(21)->andReturn(null);
2066-
Functions\expect('blc_current_user_can_fix_links')->once()->andReturn(true);
2078+
$capabilityCalls = 0;
2079+
Functions\when('blc_current_user_can_fix_links')->alias(static function () use (&$capabilityCalls) {
2080+
$capabilityCalls++;
2081+
2082+
return true;
2083+
});
20672084

20682085
Functions\when('esc_url_raw')->alias(function ($url) {
20692086
return $url;
@@ -2085,6 +2102,8 @@ public function test_unlink_returns_success_when_post_has_been_deleted(): void
20852102
$this->assertSame('success', $exception->getMessage());
20862103
}
20872104

2105+
$this->assertGreaterThanOrEqual(2, $capabilityCalls, 'The capability should be checked for the request and during cleanup.');
2106+
20882107
$this->assertCount(1, $wpdb->delete_calls, 'Cleanup should remove the orphaned database row.');
20892108
$deleted = $wpdb->delete_calls[0];
20902109
$this->assertSame('wp_blc_broken_links', $deleted[0]);

0 commit comments

Comments
 (0)