|
| 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