|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | namespace { |
| 4 | + if (!class_exists('WP_Error')) { |
| 5 | + class WP_Error |
| 6 | + { |
| 7 | + /** @var string */ |
| 8 | + private $code; |
| 9 | + |
| 10 | + /** @var string */ |
| 11 | + private $message; |
| 12 | + |
| 13 | + /** @var mixed */ |
| 14 | + private $data; |
| 15 | + |
| 16 | + public function __construct($code = '', $message = '', $data = null) |
| 17 | + { |
| 18 | + $this->code = (string) $code; |
| 19 | + $this->message = (string) $message; |
| 20 | + $this->data = $data; |
| 21 | + } |
| 22 | + |
| 23 | + public function get_error_code() |
| 24 | + { |
| 25 | + return $this->code; |
| 26 | + } |
| 27 | + |
| 28 | + public function get_error_message() |
| 29 | + { |
| 30 | + return $this->message; |
| 31 | + } |
| 32 | + |
| 33 | + public function get_error_data() |
| 34 | + { |
| 35 | + return $this->data; |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
4 | 40 | if (!function_exists('__')) { |
5 | 41 | function __($text, $domain = null) |
6 | 42 | { |
@@ -396,6 +432,33 @@ public function test_edit_link_returns_error_when_required_param_is_missing(arra |
396 | 432 | blc_ajax_edit_link_callback(); |
397 | 433 | } |
398 | 434 |
|
| 435 | + public function test_perform_link_update_requires_fix_links_capability_even_if_user_can_edit_post(): void |
| 436 | + { |
| 437 | + Functions\when('blc_current_user_can_fix_links')->justReturn(false); |
| 438 | + Functions\when('current_user_can')->alias(static function ($capability) { |
| 439 | + return $capability === 'edit_post'; |
| 440 | + }); |
| 441 | + |
| 442 | + global $wpdb; |
| 443 | + $wpdb = (object) ['prefix' => 'wp_']; |
| 444 | + |
| 445 | + Functions\when('get_post')->justReturn(null); |
| 446 | + |
| 447 | + $result = \blc_perform_link_update([ |
| 448 | + 'post_id' => 21, |
| 449 | + 'row_id' => 21, |
| 450 | + 'row' => ['url' => 'http://example.com'], |
| 451 | + 'old_url' => 'http://example.com', |
| 452 | + 'new_url' => 'http://example.org', |
| 453 | + ]); |
| 454 | + |
| 455 | + $this->assertInstanceOf(\WP_Error::class, $result); |
| 456 | + $this->assertSame('blc_forbidden', $result->get_error_code()); |
| 457 | + $data = $result->get_error_data('blc_forbidden'); |
| 458 | + $this->assertIsArray($data); |
| 459 | + $this->assertSame(BLC_HTTP_FORBIDDEN, $data['status'] ?? null); |
| 460 | + } |
| 461 | + |
399 | 462 | public function unlinkMissingParamProvider(): array |
400 | 463 | { |
401 | 464 | return [ |
@@ -456,6 +519,23 @@ public function test_unlink_returns_error_when_required_param_is_missing(array $ |
456 | 519 | blc_ajax_unlink_callback(); |
457 | 520 | } |
458 | 521 |
|
| 522 | + public function test_ajax_unlink_requires_fix_links_capability_even_if_user_can_edit_post(): void |
| 523 | + { |
| 524 | + Functions\when('blc_current_user_can_fix_links')->justReturn(false); |
| 525 | + Functions\when('current_user_can')->alias(static function ($capability) { |
| 526 | + return $capability === 'edit_post'; |
| 527 | + }); |
| 528 | + Functions\when('check_ajax_referer')->justReturn(true); |
| 529 | + Functions\expect('wp_send_json_error')->once()->with([ |
| 530 | + 'message' => 'Permissions insuffisantes.', |
| 531 | + ], BLC_HTTP_FORBIDDEN)->andReturnUsing(static function () { |
| 532 | + throw new \RuntimeException('unlink-forbidden'); |
| 533 | + }); |
| 534 | + |
| 535 | + $this->expectExceptionMessage('unlink-forbidden'); |
| 536 | + blc_ajax_unlink_callback(); |
| 537 | + } |
| 538 | + |
459 | 539 | public function test_edit_link_denied_for_user_without_permission(): void |
460 | 540 | { |
461 | 541 | $_POST['post_id'] = 1; |
@@ -2153,6 +2233,40 @@ public function test_unlink_scheme_relative_url_updates_content_and_database(): |
2153 | 2233 | $this->assertSame(['id' => 8], $wpdb->delete_args[1]); |
2154 | 2234 | $this->assertSame(['%d'], $wpdb->delete_args[2]); |
2155 | 2235 | } |
| 2236 | + |
| 2237 | + public function test_ajax_ignore_requires_fix_links_capability_even_if_user_can_edit_post(): void |
| 2238 | + { |
| 2239 | + Functions\when('blc_current_user_can_fix_links')->justReturn(false); |
| 2240 | + Functions\when('current_user_can')->alias(static function ($capability) { |
| 2241 | + return $capability === 'edit_post'; |
| 2242 | + }); |
| 2243 | + Functions\when('check_ajax_referer')->justReturn(true); |
| 2244 | + Functions\expect('wp_send_json_error')->once()->with([ |
| 2245 | + 'message' => 'Permissions insuffisantes.', |
| 2246 | + ], BLC_HTTP_FORBIDDEN)->andReturnUsing(static function () { |
| 2247 | + throw new \RuntimeException('ignore-forbidden'); |
| 2248 | + }); |
| 2249 | + |
| 2250 | + $this->expectExceptionMessage('ignore-forbidden'); |
| 2251 | + blc_ajax_ignore_link_callback(); |
| 2252 | + } |
| 2253 | + |
| 2254 | + public function test_ajax_recheck_requires_fix_links_capability_even_if_user_can_edit_post(): void |
| 2255 | + { |
| 2256 | + Functions\when('blc_current_user_can_fix_links')->justReturn(false); |
| 2257 | + Functions\when('current_user_can')->alias(static function ($capability) { |
| 2258 | + return $capability === 'edit_post'; |
| 2259 | + }); |
| 2260 | + Functions\when('check_ajax_referer')->justReturn(true); |
| 2261 | + Functions\expect('wp_send_json_error')->once()->with([ |
| 2262 | + 'message' => 'Permissions insuffisantes.', |
| 2263 | + ], BLC_HTTP_FORBIDDEN)->andReturnUsing(static function () { |
| 2264 | + throw new \RuntimeException('recheck-forbidden'); |
| 2265 | + }); |
| 2266 | + |
| 2267 | + $this->expectExceptionMessage('recheck-forbidden'); |
| 2268 | + blc_ajax_recheck_link_callback(); |
| 2269 | + } |
2156 | 2270 | } |
2157 | 2271 |
|
2158 | 2272 | } |
0 commit comments