Skip to content

Commit

Permalink
Make list of moderators filterable (#1047)
Browse files Browse the repository at this point in the history
* Make list of moderators filterable

See #1046

* add changelog

* added unit tests

* phpcs fixes
  • Loading branch information
pfefferle authored Dec 6, 2024
1 parent 356823b commit d9a7c66
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added screen reader text to the "Follow Me" block for improved accessibility
* Added `media_type` support to Activity-Object-Transformers
* Clarified settings page text around which users get Activitypub profiles
* Add a filter to the REST API moderators list

### Fixed

Expand Down
12 changes: 10 additions & 2 deletions includes/rest/class-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,20 @@ public static function moderators_get() {
'orderedItems' => array(),
);

$users = Actors::get_collection();
$users = Actors::get_collection();
$actors = array();

foreach ( $users as $user ) {
$response['orderedItems'][] = $user->get_id();
$actors[] = $user->get_id();
}

/**
* Filter the list of moderators.
*
* @param array $actors The list of moderators.
*/
$response['orderedItems'] = apply_filters( 'activitypub_rest_moderators', $actors );

$rest_response = new WP_REST_Response( $response, 200 );
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );

Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ For reasons of data protection, it is not possible to see the followers of other
* Improved: Added screen reader text for the "Follow Me" block for improved accessibility
* Improved: Added `media_type` support to Activity-Object-Transformers
* Improved: Clarified settings page text around which users get Activitypub profiles
* Improved: Add a filter to the REST API moderators list
* Fixed: Prevent hex color codes in HTML attributes from being added as post tags
* Fixed: A typo in the custom post content settings
* Fixed: Prevent draft posts from being federated when bulk deleted
Expand Down
112 changes: 112 additions & 0 deletions tests/class-test-activitypub-rest-moderators.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
/**
* Test Moderators REST Endpoint.
*
* @package ActivityPub
*/

namespace Activitypub\Tests;

use WP_UnitTestCase;
use WP_REST_Request;
use WP_REST_Server;
use Activitypub\Rest\Collection;
use Activitypub\Activity\Actor;

/**
* Test Moderators REST Endpoint.
*/
class Test_Activitypub_Rest_Moderators extends WP_UnitTestCase {
/**
* The REST Server.
*
* @var WP_REST_Server
*/
protected $server;

/**
* A user with activitypub capability.
*
* @var \WP_User
*/
protected static $user_with_cap;

/**
* A user without activitypub capability.
*
* @var \WP_User
*/
protected static $user_without_cap;

/**
* Create fake data before tests run.
*
* @param \WP_UnitTest_Factory $factory Helper that creates fake data.
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$user_with_cap = $factory->user->create_and_get(
array(
'role' => 'administrator',
)
);
self::$user_with_cap->add_cap( 'activitypub' );

self::$user_without_cap = $factory->user->create_and_get(
array(
'role' => 'subscriber',
)
);
}

/**
* Clean up after tests.
*/
public static function wpTearDownAfterClass() {
self::delete_user( self::$user_with_cap->ID );
self::delete_user( self::$user_without_cap->ID );
}

/**
* Set up before each test.
*/
public function set_up() {
parent::set_up();

global $wp_rest_server;

$wp_rest_server = new WP_REST_Server();
$this->server = $wp_rest_server;

do_action( 'rest_api_init' );
}

/**
* Test moderators endpoint response structure.
*/
public function test_moderators_get() {
new WP_REST_Request( 'GET', '/activitypub/1.0/collections/moderators' );
$response = Collection::moderators_get();

$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( 'application/activity+json; charset=' . get_option( 'blog_charset' ), $response->get_headers()['Content-Type'] );

$data = $response->get_data();

// Test response structure.
$this->assertArrayHasKey( '@context', $data );
$this->assertEquals( Actor::JSON_LD_CONTEXT, $data['@context'] );
$this->assertArrayHasKey( 'id', $data );
$this->assertArrayHasKey( 'type', $data );
$this->assertEquals( 'OrderedCollection', $data['type'] );
$this->assertArrayHasKey( 'orderedItems', $data );
$this->assertIsArray( $data['orderedItems'] );

// Test that user with cap is in the list.
$user_id = home_url( '?author=' . self::$user_with_cap->ID );
$this->assertContains( $user_id, $data['orderedItems'] );

// Test that user without cap is not in the list.
$user_id = home_url( '?author=' . self::$user_without_cap->ID );
$this->assertNotContains( $user_id, $data['orderedItems'] );
}
}

0 comments on commit d9a7c66

Please sign in to comment.