Skip to content

Commit

Permalink
Sketch out test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gwwar committed Feb 23, 2021
1 parent 4834b40 commit 3561c57
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/phpunit/tests/rest-api/rest-block-type-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public function test_get_item_invalid() {
'styles' => 'invalid_styles',
'render_callback' => 'invalid_callback',
'textdomain' => true,
'variations' => 'invalid_variations',
);
register_block_type( $block_type, $settings );
wp_set_current_user( self::$admin_id );
Expand All @@ -249,6 +250,9 @@ public function test_get_item_invalid() {
$this->assertNull( $data['category'] );
$this->assertNull( $data['textdomain'] );
$this->assertFalse( $data['is_dynamic'] );
// TODO: this is unexpected, but it looks like we're transforming scalars to arrays in rest_sanitize_value_from_schema
// This actually returns array( array() ); Need to step through to see what's happening
$this->assertSameSets( array(), $data['variations'] );
}

/**
Expand Down Expand Up @@ -305,6 +309,45 @@ public function test_get_item_defaults() {
$this->assertSameSets( array(), $data['variations'] );
}

public function test_get_variation() {
$block_type = 'fake/variations';
$settings = array(
'title' => 'variations block test',
'description' => 'a variations block test',
'attributes' => array( 'kind' => array( 'type' => 'string' ) ),
'variations' => array(
array(
'name' => 'Foo',
'title' => 'Foo Variation',
'attributes' => array( 'kind' => 'foo' ),
),
),
);
register_block_type( $block_type, $settings );
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_type );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( $block_type, $data['name'] );
$this->assertArrayHasKey( 'variations', $data );
$this->assertSame( 1, count( $data['variations'] ) );
$variation = $data['variations'][0];
$this->assertArrayHasKey( 'attributes', $variation );
//TODO: this is getting wiped by:
// $data[ $extra_field ] = rest_sanitize_value_from_schema( $field, $schema['properties'][ $extra_field ] );
// in class-wp-rest-block-types-controller.php
$this->assertSameSets(
array(
array(
'name' => 'Foo',
'title' => 'Foo Variation',
'attributes' => array( 'kind' => 'foo' ),
),
),
$variation['attributes']
);
}

/**
* @ticket 47620
*/
Expand Down

0 comments on commit 3561c57

Please sign in to comment.