Skip to content

Commit 2440d58

Browse files
authored
Fix: Clear Errors when a Follower update is successfull (#1668)
* Fix: Reset Errors when Follower update is successfull * Add changelog * phpcs fixes * Fix phpcs issues
1 parent fd8547e commit 2440d58

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Prevent accidental follower removal by resetting errors properly.

includes/class-scheduler.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ public static function update_followers() {
133133
} else {
134134
$follower->from_array( $meta );
135135
$follower->update();
136+
137+
$follower->clear_errors();
136138
}
137139
}
138140
}

includes/collection/class-followers.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,15 @@ public static function add_error( $post_id, $error ) {
480480
$error_message
481481
);
482482
}
483+
484+
/**
485+
* Clear the errors for a Follower.
486+
*
487+
* @param int $post_id The ID of the WordPress Custom-Post-Type.
488+
*
489+
* @return bool True on success, false on failure.
490+
*/
491+
public static function clear_errors( $post_id ) {
492+
return \delete_post_meta( $post_id, '_activitypub_errors' );
493+
}
483494
}

includes/model/class-follower.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ public function get_errors() {
5151
return get_post_meta( $this->_id, '_activitypub_errors', false );
5252
}
5353

54+
/**
55+
* Clear the errors for the current Follower.
56+
*
57+
* @return bool True on success, false on failure.
58+
*/
59+
public function clear_errors() {
60+
if ( ! $this->_id ) {
61+
\_doing_it_wrong( __METHOD__, 'Follower ID is not set.', 'unreleased' );
62+
63+
return false;
64+
}
65+
66+
return Followers::clear_errors( $this->_id );
67+
}
68+
5469
/**
5570
* Get the Summary.
5671
*

tests/includes/collection/class-test-followers.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,4 +713,61 @@ public function extract_name_from_uri_content_provider() {
713713
array( 'https://example.com', 'https://example.com' ),
714714
);
715715
}
716+
717+
/**
718+
* Tests clear_errors.
719+
*
720+
* @covers ::clear_errors
721+
*/
722+
public function test_clear_errors() {
723+
$follower = 'https://example.com/author/jon';
724+
$result = Followers::add_follower( 1, $follower );
725+
$this->assertNotWPError( $result );
726+
727+
// Add some errors.
728+
Followers::add_error( $result->get__id(), 'Test error 1' );
729+
Followers::add_error( $result->get__id(), 'Test error 2' );
730+
731+
// Verify errors were added.
732+
$errors = get_post_meta( $result->get__id(), '_activitypub_errors', false );
733+
$this->assertCount( 2, $errors );
734+
735+
// Clear errors.
736+
$cleared = Followers::clear_errors( $result->get__id() );
737+
$this->assertTrue( $cleared );
738+
739+
// Verify errors were cleared.
740+
$errors = get_post_meta( $result->get__id(), '_activitypub_errors', false );
741+
$this->assertEmpty( $errors );
742+
}
743+
744+
/**
745+
* Tests clear_errors with no errors.
746+
*
747+
* @covers ::clear_errors
748+
*/
749+
public function test_clear_errors_no_errors() {
750+
$follower = 'https://example.com/author/jon';
751+
$result = Followers::add_follower( 1, $follower );
752+
$this->assertNotWPError( $result );
753+
754+
// Clear errors when none exist.
755+
$cleared = Followers::clear_errors( $result->get__id() );
756+
$this->assertFalse( $cleared );
757+
758+
// Verify no errors exist.
759+
$errors = get_post_meta( $result->get__id(), '_activitypub_errors', false );
760+
$this->assertEmpty( $errors );
761+
}
762+
763+
/**
764+
* Tests clear_errors with invalid follower ID.
765+
*
766+
* @covers ::clear_errors
767+
*/
768+
public function test_clear_errors_invalid_id() {
769+
// Try to clear errors for non-existent follower.
770+
$cleared = Followers::clear_errors( 99999 );
771+
$this->assertFalse( $cleared );
772+
}
716773
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Test file for Activitypub Follower.
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
namespace Activitypub\Tests\Model;
9+
10+
use Activitypub\Model\Follower;
11+
use Activitypub\Collection\Followers;
12+
13+
/**
14+
* Tests the Follower class.
15+
*
16+
* @package Activitypub
17+
*/
18+
class Test_Follower extends \WP_UnitTestCase {
19+
/**
20+
* Tests clear_errors.
21+
*
22+
* @covers ::clear_errors
23+
*/
24+
public function test_clear_errors() {
25+
// Mock request.
26+
$follower = new Follower();
27+
$follower->from_array(
28+
array(
29+
'id' => 'https://example.com/author/jon',
30+
'type' => 'Person',
31+
'name' => 'Jon Doe',
32+
'preferredUsername' => 'jon',
33+
'inbox' => 'https://example.com/author/jon/inbox',
34+
'publicKey' => 'publicKey',
35+
'publicKeyPem' => 'publicKeyPem',
36+
)
37+
);
38+
39+
$id = $follower->upsert();
40+
$this->assertNotWPError( $id );
41+
42+
// Add some errors.
43+
Followers::add_error( $follower->get__id(), 'Test error 1' );
44+
Followers::add_error( $follower->get__id(), 'Test error 2' );
45+
46+
// Verify errors were added.
47+
$errors = $follower->get_errors();
48+
$this->assertCount( 2, $errors );
49+
50+
// Clear errors.
51+
$cleared = $follower->clear_errors();
52+
$this->assertTrue( $cleared );
53+
54+
// Verify errors were cleared.
55+
$errors = $follower->get_errors();
56+
$this->assertEmpty( $errors );
57+
}
58+
59+
/**
60+
* Tests clear_errors with no errors.
61+
*
62+
* @covers ::clear_errors
63+
*/
64+
public function test_clear_errors_no_errors() {
65+
$follower = new Follower();
66+
$follower->from_array(
67+
array(
68+
'id' => 'https://example.com/author/jon',
69+
'type' => 'Person',
70+
'name' => 'Jon Doe',
71+
'preferredUsername' => 'jon',
72+
'inbox' => 'https://example.com/author/jon/inbox',
73+
'publicKey' => 'publicKey',
74+
'publicKeyPem' => 'publicKeyPem',
75+
)
76+
);
77+
$id = $follower->upsert();
78+
$this->assertNotWPError( $id );
79+
80+
// Clear errors when none exist.
81+
$cleared = $follower->clear_errors();
82+
$this->assertFalse( $cleared );
83+
84+
// Verify no errors exist.
85+
$errors = $follower->get_errors();
86+
$this->assertEmpty( $errors );
87+
}
88+
89+
/**
90+
* Tests clear_errors triggers _doing_it_wrong when ID is not set.
91+
*
92+
* @covers ::clear_errors
93+
*/
94+
public function test_clear_errors_doing_it_wrong() {
95+
$this->setExpectedIncorrectUsage( 'Activitypub\Model\Follower::clear_errors' );
96+
97+
$follower = new Follower();
98+
$follower->clear_errors();
99+
}
100+
}

0 commit comments

Comments
 (0)