user->create( array( 'role' => 'administrator' ) ); } /** * Clean up class fixtures. */ public static function wpTearDownAfterClass() { self::delete_user( self::$admin_id ); } /** * The registry route is registered. */ public function test_route_registered() { $routes = rest_get_server()->get_routes(); $this->assertArrayHasKey( '/wp/v2/knowledge/guideline-scopes', $routes ); } /** * Reads require the knowledge read capability; logged-out users are denied. */ public function test_requires_read_knowledge() { wp_set_current_user( 0 ); $response = rest_get_server()->dispatch( new WP_REST_Request( 'GET', '/wp/v2/knowledge/guideline-scopes' ) ); $this->assertSame( 401, $response->get_status() ); } /** * Administrators receive the default scopes keyed by slug with title, * description, and order fields. */ public function test_returns_default_scopes_for_admin() { wp_set_current_user( self::$admin_id ); $response = rest_get_server()->dispatch( new WP_REST_Request( 'GET', '/wp/v2/knowledge/guideline-scopes' ) ); $this->assertSame( 200, $response->get_status() ); $data = $response->get_data(); $slugs = wp_list_pluck( $data, 'slug' ); $this->assertSame( array( 'site', 'copy', 'images', 'blocks', 'additional' ), $slugs ); $this->assertArrayHasKey( 'title', $data[0] ); $this->assertArrayHasKey( 'description', $data[0] ); $this->assertArrayHasKey( 'order', $data[0] ); $this->assertSame( 'Site', $data[0]['title'] ); $this->assertIsInt( $data[0]['order'] ); } /** * The `wp_guideline_scopes` filter is reflected in the response, so plugins * can register additional scopes. */ public function test_filter_adds_scope() { wp_set_current_user( self::$admin_id ); $callback = static function ( $scopes ) { $scopes['custom'] = array( 'title' => 'Custom', 'description' => 'Custom scope.', 'order' => 99, ); return $scopes; }; add_filter( 'wp_guideline_scopes', $callback ); $data = rest_get_server()->dispatch( new WP_REST_Request( 'GET', '/wp/v2/knowledge/guideline-scopes' ) )->get_data(); $slugs = wp_list_pluck( $data, 'slug' ); remove_filter( 'wp_guideline_scopes', $callback ); $this->assertContains( 'custom', $slugs ); } /** * The `wp_guideline_scopes` filter can remove a default scope, so a plugin * can drop the Blocks section from the Settings page entirely. */ public function test_filter_removes_scope() { wp_set_current_user( self::$admin_id ); add_filter( 'wp_guideline_scopes', static function ( $scopes ) { unset( $scopes['blocks'] ); return $scopes; } ); $data = rest_get_server()->dispatch( new WP_REST_Request( 'GET', '/wp/v2/knowledge/guideline-scopes' ) )->get_data(); $slugs = wp_list_pluck( $data, 'slug' ); $this->assertNotContains( 'blocks', $slugs ); } }