Skip to content

Commit d9a7c66

Browse files
authored
Make list of moderators filterable (#1047)
* Make list of moderators filterable See #1046 * add changelog * added unit tests * phpcs fixes
1 parent 356823b commit d9a7c66

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
* Added screen reader text to the "Follow Me" block for improved accessibility
1717
* Added `media_type` support to Activity-Object-Transformers
1818
* Clarified settings page text around which users get Activitypub profiles
19+
* Add a filter to the REST API moderators list
1920

2021
### Fixed
2122

includes/rest/class-collection.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,12 +278,20 @@ public static function moderators_get() {
278278
'orderedItems' => array(),
279279
);
280280

281-
$users = Actors::get_collection();
281+
$users = Actors::get_collection();
282+
$actors = array();
282283

283284
foreach ( $users as $user ) {
284-
$response['orderedItems'][] = $user->get_id();
285+
$actors[] = $user->get_id();
285286
}
286287

288+
/**
289+
* Filter the list of moderators.
290+
*
291+
* @param array $actors The list of moderators.
292+
*/
293+
$response['orderedItems'] = apply_filters( 'activitypub_rest_moderators', $actors );
294+
287295
$rest_response = new WP_REST_Response( $response, 200 );
288296
$rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
289297

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ For reasons of data protection, it is not possible to see the followers of other
138138
* Improved: Added screen reader text for the "Follow Me" block for improved accessibility
139139
* Improved: Added `media_type` support to Activity-Object-Transformers
140140
* Improved: Clarified settings page text around which users get Activitypub profiles
141+
* Improved: Add a filter to the REST API moderators list
141142
* Fixed: Prevent hex color codes in HTML attributes from being added as post tags
142143
* Fixed: A typo in the custom post content settings
143144
* Fixed: Prevent draft posts from being federated when bulk deleted
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Test Moderators REST Endpoint.
4+
*
5+
* @package ActivityPub
6+
*/
7+
8+
namespace Activitypub\Tests;
9+
10+
use WP_UnitTestCase;
11+
use WP_REST_Request;
12+
use WP_REST_Server;
13+
use Activitypub\Rest\Collection;
14+
use Activitypub\Activity\Actor;
15+
16+
/**
17+
* Test Moderators REST Endpoint.
18+
*/
19+
class Test_Activitypub_Rest_Moderators extends WP_UnitTestCase {
20+
/**
21+
* The REST Server.
22+
*
23+
* @var WP_REST_Server
24+
*/
25+
protected $server;
26+
27+
/**
28+
* A user with activitypub capability.
29+
*
30+
* @var \WP_User
31+
*/
32+
protected static $user_with_cap;
33+
34+
/**
35+
* A user without activitypub capability.
36+
*
37+
* @var \WP_User
38+
*/
39+
protected static $user_without_cap;
40+
41+
/**
42+
* Create fake data before tests run.
43+
*
44+
* @param \WP_UnitTest_Factory $factory Helper that creates fake data.
45+
*/
46+
public static function wpSetUpBeforeClass( $factory ) {
47+
self::$user_with_cap = $factory->user->create_and_get(
48+
array(
49+
'role' => 'administrator',
50+
)
51+
);
52+
self::$user_with_cap->add_cap( 'activitypub' );
53+
54+
self::$user_without_cap = $factory->user->create_and_get(
55+
array(
56+
'role' => 'subscriber',
57+
)
58+
);
59+
}
60+
61+
/**
62+
* Clean up after tests.
63+
*/
64+
public static function wpTearDownAfterClass() {
65+
self::delete_user( self::$user_with_cap->ID );
66+
self::delete_user( self::$user_without_cap->ID );
67+
}
68+
69+
/**
70+
* Set up before each test.
71+
*/
72+
public function set_up() {
73+
parent::set_up();
74+
75+
global $wp_rest_server;
76+
77+
$wp_rest_server = new WP_REST_Server();
78+
$this->server = $wp_rest_server;
79+
80+
do_action( 'rest_api_init' );
81+
}
82+
83+
/**
84+
* Test moderators endpoint response structure.
85+
*/
86+
public function test_moderators_get() {
87+
new WP_REST_Request( 'GET', '/activitypub/1.0/collections/moderators' );
88+
$response = Collection::moderators_get();
89+
90+
$this->assertEquals( 200, $response->get_status() );
91+
$this->assertEquals( 'application/activity+json; charset=' . get_option( 'blog_charset' ), $response->get_headers()['Content-Type'] );
92+
93+
$data = $response->get_data();
94+
95+
// Test response structure.
96+
$this->assertArrayHasKey( '@context', $data );
97+
$this->assertEquals( Actor::JSON_LD_CONTEXT, $data['@context'] );
98+
$this->assertArrayHasKey( 'id', $data );
99+
$this->assertArrayHasKey( 'type', $data );
100+
$this->assertEquals( 'OrderedCollection', $data['type'] );
101+
$this->assertArrayHasKey( 'orderedItems', $data );
102+
$this->assertIsArray( $data['orderedItems'] );
103+
104+
// Test that user with cap is in the list.
105+
$user_id = home_url( '?author=' . self::$user_with_cap->ID );
106+
$this->assertContains( $user_id, $data['orderedItems'] );
107+
108+
// Test that user without cap is not in the list.
109+
$user_id = home_url( '?author=' . self::$user_without_cap->ID );
110+
$this->assertNotContains( $user_id, $data['orderedItems'] );
111+
}
112+
}

0 commit comments

Comments
 (0)