-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make list of moderators filterable (#1047)
* Make list of moderators filterable See #1046 * add changelog * added unit tests * phpcs fixes
- Loading branch information
Showing
4 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] ); | ||
} | ||
} |