Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Skip when blog mode is disabled
Browse files Browse the repository at this point in the history
obenland committed Jan 27, 2025
1 parent 6b7f67f commit 077e4be
Showing 5 changed files with 56 additions and 9 deletions.
10 changes: 7 additions & 3 deletions includes/functions.php
Original file line number Diff line number Diff line change
@@ -1580,9 +1580,13 @@ function add_to_outbox( $data, $activity_type = 'Create', $user_id = 0, $content
return false;
}

// If the user is disabled, use the blog user.
if ( is_user_disabled( $user_id ) && ! is_user_disabled( Actors::BLOG_USER_ID ) ) {
$user_id = Actors::BLOG_USER_ID;
// If the user is disabled, fall back to the blog user when available.
if ( is_user_disabled( $user_id ) ) {
if ( is_user_disabled( Actors::BLOG_USER_ID ) ) {
return false;
} else {
$user_id = Actors::BLOG_USER_ID;
}
}

set_wp_object_state( $data, 'federate' );
7 changes: 4 additions & 3 deletions tests/includes/class-test-signature.php
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@ public function test_signature_legacy() {
$this->assertEquals( $key_pair['private_key'], $private_key );

// Check application user.
$user = Actors::get_by_id( -1 );
$user = Actors::get_by_id( Actors::APPLICATION_USER_ID );

$public_key = 'public key ' . $user->get__id();
$private_key = 'private key ' . $user->get__id();
@@ -73,8 +73,9 @@ public function test_signature_legacy() {
$this->assertEquals( $key_pair['private_key'], $private_key );

// Check blog user.
\define( 'ACTIVITYPUB_DISABLE_BLOG_USER', false );
$user = Actors::get_by_id( 0 );
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE );
$user = Actors::get_by_id( Actors::BLOG_USER_ID );
\delete_option( 'activitypub_actor_mode' );

$public_key = 'public key ' . $user->get__id();
$private_key = 'private key ' . $user->get__id();
42 changes: 42 additions & 0 deletions tests/includes/collection/class-test-outbox.php
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ public static function set_up_before_class() {
* Clean up test resources.
*/
public static function tear_down_after_class() {
\delete_option( 'activitypub_actor_mode' );
\wp_delete_user( self::$user_id );
\remove_filter( 'pre_schedule_event', '__return_false' );
}
@@ -53,6 +54,8 @@ public static function tear_down_after_class() {
* @param string $json The JSON representation of the data.
*/
public function test_add( $data, $type, $user_id, $json ) {
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE );

$id = \Activitypub\add_to_outbox( $data, $type, $user_id );

$this->assertIsInt( $id );
@@ -104,4 +107,43 @@ public function activity_object_provider() {
),
);
}

/**
* Test add an item to the outbox with a user.
*
* @covers ::add
* @dataProvider author_object_provider
*
* @param string $mode The actor mode.
* @param int $user_id The user ID.
* @param string $expected_actor The expected actor.
*/
public function test_author_fallbacks( $mode, $user_id, $expected_actor ) {
\update_option( 'activitypub_actor_mode', $mode );

$user_id = $user_id ?? self::$user_id;
$data = array(
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => 'https://example.com/' . $user_id,
'type' => 'Note',
'content' => '<p>This is a note</p>',
);

$id = \Activitypub\add_to_outbox( $data, 'Create', $user_id );
$this->assertEquals( $expected_actor, \get_post_meta( $id, '_activitypub_activity_actor', true ) );
}

/**
* Data provider for test_author_fallbacks.
*
* @return array[]
*/
public function author_object_provider() {
return array(
array( ACTIVITYPUB_ACTOR_AND_BLOG_MODE, self::$user_id, 'user' ), // Not sure why we can't access self::$user_id directly.
array( ACTIVITYPUB_ACTOR_AND_BLOG_MODE, 90210, 'blog' ),
array( ACTIVITYPUB_BLOG_MODE, 90210, 'blog' ),
array( ACTIVITYPUB_ACTOR_MODE, 90210, false ),
);
}
}
4 changes: 2 additions & 2 deletions tests/includes/scheduler/class-test-actor.php
Original file line number Diff line number Diff line change
@@ -88,6 +88,7 @@ public function test_user_update() {
* @covers ::blog_user_update
*/
public function test_blog_user_update() {
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE );
$test_value = 'test value';
$result = \Activitypub\Scheduler\Actor::blog_user_update( $test_value );

@@ -160,7 +161,7 @@ public function test_schedule_post_activity_extra_fields() {
* @covers ::schedule_post_activity
*/
public function test_schedule_post_activity_extra_field_blog() {
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_BLOG_MODE );
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE );
$blog_post_id = self::factory()->post->create( array( 'post_type' => Extra_Fields::BLOG_POST_TYPE ) );
$activitpub_id = Actors::get_by_id( Actors::BLOG_USER_ID )->get_id();

@@ -169,6 +170,5 @@ public function test_schedule_post_activity_extra_field_blog() {

// Clean up.
\wp_delete_post( $blog_post_id, true );
\update_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_AND_BLOG_MODE );
}
}
2 changes: 1 addition & 1 deletion tests/includes/scheduler/class-test-post.php
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ class Test_Post extends \Activitypub\Tests\ActivityPub_Outbox_TestCase {
* @covers ::schedule_post_activity
*/
public function test_schedule_post_activity_regular_post() {
$post_id = self::factory()->post->create();
$post_id = self::factory()->post->create( array( 'post_author' => self::$user_id ) );
$activitpub_id = \add_query_arg( 'p', $post_id, \home_url( '/' ) );

$post = $this->get_latest_outbox_item( $activitpub_id );

0 comments on commit 077e4be

Please sign in to comment.