Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 89 additions & 5 deletions tests/phpunit/tests/icons/wpRestIconCollectionsController.php
Comment thread
t-hamano marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,22 @@ public function test_get_item_requires_authentication() {
$this->assertErrorResponse( 'rest_cannot_view', $response, 401 );
}

/**
* Tests that getting a specific collection requires edit_posts capability.
*
* @ticket 66287
*
* @covers ::get_item_permissions_check
*/
public function test_get_item_requires_permissions() {
wp_set_current_user( self::$subscriber_id );

$request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/core' );
$response = rest_get_server()->dispatch( $request );

$this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
}

/**
* @ticket 64847
*
Expand All @@ -222,40 +238,74 @@ public function test_prepare_item() {

/**
* @ticket 64847
* @ticket 66287
*
* @covers ::prepare_item_for_response
*/
public function test_get_items_fields_parameter() {
wp_set_current_user( self::$editor_id );

$request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections' );
$request->set_param( '_fields', 'slug' );
$request->set_param( '_fields', 'slug,label' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 200, $response->get_status() );
$this->assertIsArray( $data );
$this->assertNotEmpty( $data );

foreach ( $data as $collection ) {
$this->assertArrayHasKey( 'slug', $collection );
$this->assertArrayNotHasKey( 'label', $collection );
$this->assertArrayHasKey( 'label', $collection );
$this->assertArrayNotHasKey( 'description', $collection );
}
}

/**
* Tests that the _fields parameter filters single collection response fields.
*
* @ticket 66287
*
* @covers ::prepare_item_for_response
*/
public function test_get_item_fields_parameter() {
wp_set_current_user( self::$editor_id );

$request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/core' );
$request->set_param( '_fields', 'label' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertSame( 200, $response->get_status() );
$this->assertArrayHasKey( 'label', $data );
$this->assertArrayNotHasKey( 'slug', $data );
$this->assertArrayNotHasKey( 'description', $data );
}

/**
* @ticket 64847
* @ticket 66287
*
* @covers ::get_item_schema
*/
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/icon-collections' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$schema = $data['schema'];

$properties = $data['schema']['properties'];
$this->assertSame( 'icon-collection', $schema['title'] );
$this->assertSame( 'object', $schema['type'] );

$properties = $schema['properties'];
$this->assertCount( 3, $properties );
$this->assertArrayHasKey( 'slug', $properties );
$this->assertArrayHasKey( 'label', $properties );
$this->assertArrayHasKey( 'description', $properties );

$this->assertTrue( $properties['slug']['readonly'] );
$this->assertTrue( $properties['label']['readonly'] );
$this->assertTrue( $properties['description']['readonly'] );
}

/**
Expand Down Expand Up @@ -306,6 +356,7 @@ public function test_delete_item() {
* Test that non-public collections are omitted from the collection list.
*
* @ticket 66087
* @ticket 66287
*
* @covers ::get_items
*/
Expand All @@ -322,27 +373,45 @@ public function test_get_items_omits_non_public_collections() {
$slugs = wp_list_pluck( $response->get_data(), 'slug' );
$this->assertContains( 'rest-visibility-public', $slugs );
$this->assertNotContains( 'rest-visibility-private', $slugs );
$this->assertNotContains( 'core-admin', $slugs );
}

/**
* Test that a non-public collection is reported as not found by slug.
*
* @ticket 66087
* @ticket 66287
*
* @dataProvider data_non_public_collections
*
* @covers ::get_item
* @covers ::get_icon_collection
*
* @param string $slug Icon collection slug.
*/
public function test_get_item_returns_404_for_non_public_collection() {
public function test_get_item_returns_404_for_non_public_collection( $slug ) {
$this->register_visibility_collections();

wp_set_current_user( self::$editor_id );

$request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/rest-visibility-private' );
$request = new WP_REST_Request( 'GET', '/wp/v2/icon-collections/' . $slug );
$response = rest_get_server()->dispatch( $request );

$this->assertErrorResponse( 'rest_icon_collection_not_found', $response, 404 );
}

/**
* Data provider of non-public collection slugs.
*
* @return array[]
*/
public function data_non_public_collections() {
return array(
'custom non-public collection' => array( 'rest-visibility-private' ),
'built-in core-admin' => array( 'core-admin' ),
);
}

/**
* Registers a public and a non-public collection.
*/
Expand Down Expand Up @@ -376,4 +445,19 @@ public function test_context_param() {
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}

/**
* Tests that collection query parameters include context with view default.
*
* @ticket 66287
*
* @covers ::get_collection_params
*/
public function test_get_collection_params() {
$controller = new WP_REST_Icon_Collections_Controller();
$params = $controller->get_collection_params();

$this->assertArrayHasKey( 'context', $params );
$this->assertSame( 'view', $params['context']['default'] );
}
}
Loading