user_id could be an int and meta always returns strings

remove strict comparison in this case and add tests to verify the correct behaviour
This commit is contained in:
Matthias Pfefferle 2023-07-13 10:35:15 +02:00
parent 00fbc296b3
commit 5ae978a8bc
2 changed files with 25 additions and 1 deletions

View file

@ -189,7 +189,8 @@ class Followers {
self::add_error( $follower->get__id(), $error ); self::add_error( $follower->get__id(), $error );
} }
if ( is_array( $meta ) && ! in_array( $user_id, $meta, true ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
if ( is_array( $meta ) && ! in_array( $user_id, $meta ) ) {
add_post_meta( $follower->get__id(), 'activitypub_user_id', $user_id ); add_post_meta( $follower->get__id(), 'activitypub_user_id', $user_id );
wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' ); wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
} }

View file

@ -244,6 +244,29 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase {
$this->assertEquals( 0, count( $followers ) ); $this->assertEquals( 0, count( $followers ) );
} }
public function test_add_duplicate_follower() {
$pre_http_request = new MockAction();
add_filter( 'pre_http_request', array( $pre_http_request, 'filter' ), 10, 3 );
$follower = 'https://12345.example.com';
\Activitypub\Collection\Followers::add_follower( 1, $follower );
\Activitypub\Collection\Followers::add_follower( 1, $follower );
\Activitypub\Collection\Followers::add_follower( 1, $follower );
\Activitypub\Collection\Followers::add_follower( 1, $follower );
\Activitypub\Collection\Followers::add_follower( 1, $follower );
\Activitypub\Collection\Followers::add_follower( 1, $follower );
$db_followers = \Activitypub\Collection\Followers::get_followers( 1 );
$this->assertContains( $follower, $db_followers );
$follower = current( $db_followers );
$meta = get_post_meta( $follower->get__id(), 'activitypub_user_id' );
$this->assertCount( 1, $meta );
}
public static function http_request_host_is_external( $in, $host ) { public static function http_request_host_is_external( $in, $host ) {
if ( in_array( $host, array( 'example.com', 'example.org' ), true ) ) { if ( in_array( $host, array( 'example.com', 'example.org' ), true ) ) {