Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Updates to REST API tests
  • Loading branch information
hellofromtonya committed Sep 20, 2022
commit 5ba2daf65cf1895732d7cc3e47ddea4d9db4d761
34 changes: 20 additions & 14 deletions tests/phpunit/tests/rest-api/rest-post-types-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,25 +156,31 @@ public function test_prepare_item_limit_fields() {
);
}

/**
* @ticket 56467
*
* @covers WP_REST_Post_Types_Controller::get_item_schema
*/
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/types' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertCount( 13, $properties );
$this->assertArrayHasKey( 'capabilities', $properties, '`capabilities` should be included in the schema.' );
$this->assertArrayHasKey( 'description', $properties, '`description` should be included in the schema.' );
$this->assertArrayHasKey( 'hierarchical', $properties, '`hierarchical` should be included in the schema.' );
$this->assertArrayHasKey( 'viewable', $properties, '`viewable` should be included in the schema.' );
$this->assertArrayHasKey( 'labels', $properties, '`labels` should be included in the schema.' );
$this->assertArrayHasKey( 'name', $properties, '`name` should be included in the schema.' );
$this->assertArrayHasKey( 'slug', $properties, '`slug` should be included in the schema.' );
$this->assertArrayHasKey( 'supports', $properties, '`supports` should be included in the schema.' );
$this->assertArrayHasKey( 'taxonomies', $properties, '`taxonomies` should be included in the schema.' );
$this->assertArrayHasKey( 'rest_base', $properties, '`rest_base` should be included in the schema.' );
$this->assertArrayHasKey( 'rest_namespace', $properties, '`rest_namespace` should be included in the schema.' );
$this->assertArrayHasKey( 'visibility', $properties, '`visibility` should be included in the schema.' );
$this->assertArrayHasKey( 'icon', $properties, '`icon` should be included in the schema.' );

$this->assertCount( 13, $properties, 'Schema should have 13 properties' );
$this->assertArrayHasKey( 'capabilities', $properties, '`capabilities` should be included in the schema' );
$this->assertArrayHasKey( 'description', $properties, '`description` should be included in the schema' );
$this->assertArrayHasKey( 'hierarchical', $properties, '`hierarchical` should be included in the schema' );
$this->assertArrayHasKey( 'viewable', $properties, '`viewable` should be included in the schema' );
$this->assertArrayHasKey( 'labels', $properties, '`labels` should be included in the schema' );
$this->assertArrayHasKey( 'name', $properties, '`name` should be included in the schema' );
$this->assertArrayHasKey( 'slug', $properties, '`slug` should be included in the schema' );
$this->assertArrayHasKey( 'supports', $properties, '`supports` should be included in the schema' );
$this->assertArrayHasKey( 'taxonomies', $properties, '`taxonomies` should be included in the schema' );
$this->assertArrayHasKey( 'rest_base', $properties, '`rest_base` should be included in the schema' );
$this->assertArrayHasKey( 'rest_namespace', $properties, '`rest_namespace` should be included in the schema' );
$this->assertArrayHasKey( 'visibility', $properties, '`visibility` should be included in the schema' );
$this->assertArrayHasKey( 'icon', $properties, '`icon` should be included in the schema' );
}

public function test_get_additional_field_registration() {
Expand Down
80 changes: 49 additions & 31 deletions tests/phpunit/tests/rest-api/wpRestTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,26 +685,35 @@ protected function find_and_normalize_template_by_id( $templates, $id ) {
}

/**
* @dataProvider data_create_item_with_is_wp_suggestion
* @ticket 56467
* @covers WP_REST_Templates_Controller::create_item
*
* @param array $body_params Data set to test.
* @param array $expected Expected results.
*/
public function test_create_item_with_is_wp_suggestion() {
public function test_create_item_with_is_wp_suggestion( array $body_params, array $expected ) {
// Set up the user.
$body_params['author'] = self::$admin_id;
$expected['author'] = self::$admin_id;
wp_set_current_user( self::$admin_id );

$request = new WP_REST_Request( 'POST', '/wp/v2/templates' );
// `is_wp_suggestion` true.
$body_params = array(
'slug' => 'page-rigas',
'description' => 'Just a description',
'title' => 'My Template',
'content' => 'Content',
'is_wp_suggestion' => true,
'author' => self::$admin_id,
);
$request->set_body_params( $body_params );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
unset( $data['_links'] );
unset( $data['wp_id'] );

$this->assertSame( $expected, $data );
}

/**
* Data provider.
*
* @return array
*/
public function data_create_item_with_is_wp_suggestion() {
$expected = array(
'id' => 'default//page-rigas',
'theme' => 'default',
Expand All @@ -723,31 +732,40 @@ public function test_create_item_with_is_wp_suggestion() {
'status' => 'publish',
'has_theme_file' => false,
'is_custom' => false,
'author' => self::$admin_id,
'author' => null,
);
$this->assertSame( $expected, $data, 'Incorrect response when `is_wp_suggestion:true` is set.' );
// `is_wp_suggestion` false.
$body_params = wp_parse_args(
array(
'slug' => 'page-hi',
'is_wp_suggestion' => false,

return array(
'is_wp_suggestion: true' => array(
'body_params' => array(
'slug' => 'page-rigas',
'description' => 'Just a description',
'title' => 'My Template',
'content' => 'Content',
'is_wp_suggestion' => true,
'author' => null,
),
'expected' => $expected,
),
$body_params
);
$request->set_body_params( $body_params );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
unset( $data['_links'] );
unset( $data['wp_id'] );
$expected = wp_parse_args(
array(
'id' => 'default//page-hi',
'slug' => 'page-hi',
'is_custom' => true,
'is_wp_suggestion: false' => array(
'body_params' => array(
'slug' => 'page-hi',
'description' => 'Just a description',
'title' => 'My Template',
'content' => 'Content',
'is_wp_suggestion' => false,
'author' => null,
),
'expected' => array_merge(
$expected,
array(
'id' => 'default//page-hi',
'slug' => 'page-hi',
'is_custom' => true,
)
),
),
$expected
);
$this->assertSame( $expected, $data, 'Incorrect response when `is_wp_suggestion:false` is set.' );
}

/**
Expand Down