Skip to content

Commit

Permalink
Simplify and improve RegistryTestCase
Browse files Browse the repository at this point in the history
- Remove unnecessary "global" methods
- Use WP_Block_Type_Registry methods to aid in unregistration
- Create get_block_registry() method to aid in future mocking
  • Loading branch information
chriszarate committed Aug 23, 2024
1 parent 23f508a commit 40ff457
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 128 deletions.
8 changes: 4 additions & 4 deletions tests/graphql/test-graphql-api-v1.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function test_is_graphql_enabled_false() {
// get_blocks_data() tests

public function test_get_blocks_data() {
$this->register_global_block_with_attributes( 'test/custom-paragraph', [
$this->register_block_with_attributes( 'test/custom-paragraph', [
'content' => [
'type' => 'rich-text',
'source' => 'rich-text',
Expand All @@ -53,7 +53,7 @@ public function test_get_blocks_data() {
],
] );

$this->register_global_block_with_attributes( 'test/custom-quote', [
$this->register_block_with_attributes( 'test/custom-quote', [
'value' => [
'type' => 'string',
'source' => 'html',
Expand All @@ -70,7 +70,7 @@ public function test_get_blocks_data() {
],
] );

$this->register_global_block_with_attributes( 'test/custom-heading', [
$this->register_block_with_attributes( 'test/custom-heading', [
'content' => [
'type' => 'rich-text',
'source' => 'rich-text',
Expand Down Expand Up @@ -192,7 +192,7 @@ public function test_get_blocks_data() {
// get_blocks_data() attribute type tests

public function test_array_data_in_attribute() {
$this->register_global_block_with_attributes( 'test/custom-table', [
$this->register_block_with_attributes( 'test/custom-table', [
'head' => [
'type' => 'array',
'default' => [],
Expand Down
14 changes: 7 additions & 7 deletions tests/graphql/test-graphql-api-v2.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function test_is_graphql_enabled_false() {
// get_blocks_data() tests

public function test_get_blocks_data() {
$this->register_global_block_with_attributes( 'test/custom-paragraph', [
$this->register_block_with_attributes( 'test/custom-paragraph', [
'content' => [
'type' => 'rich-text',
'source' => 'rich-text',
Expand All @@ -53,7 +53,7 @@ public function test_get_blocks_data() {
],
] );

$this->register_global_block_with_attributes( 'test/custom-quote', [
$this->register_block_with_attributes( 'test/custom-quote', [
'value' => [
'type' => 'string',
'source' => 'html',
Expand All @@ -70,7 +70,7 @@ public function test_get_blocks_data() {
],
] );

$this->register_global_block_with_attributes( 'test/custom-heading', [
$this->register_block_with_attributes( 'test/custom-heading', [
'content' => [
'type' => 'rich-text',
'source' => 'rich-text',
Expand Down Expand Up @@ -193,7 +193,7 @@ public function test_get_blocks_data() {
// get_blocks_data() attribute type tests

public function test_array_data_in_attribute() {
$this->register_global_block_with_attributes( 'test/custom-table', [
$this->register_block_with_attributes( 'test/custom-table', [
'head' => [
'type' => 'array',
'default' => [],
Expand Down Expand Up @@ -339,7 +339,7 @@ public function test_array_data_in_attribute() {
}

public function test_get_block_data_with_boolean_attributes() {
$this->register_global_block_with_attributes( 'test/toggle-text', [
$this->register_block_with_attributes( 'test/toggle-text', [
'isVisible' => [
'type' => 'boolean',
],
Expand Down Expand Up @@ -386,7 +386,7 @@ public function test_get_block_data_with_boolean_attributes() {
}

public function test_get_block_data_with_number_attributes() {
$this->register_global_block_with_attributes( 'test/gallery-block', [
$this->register_block_with_attributes( 'test/gallery-block', [
'tileCount' => [
'type' => 'number',
],
Expand Down Expand Up @@ -441,7 +441,7 @@ public function test_get_block_data_with_number_attributes() {
}

public function test_get_block_data_with_string_attribute() {
$this->register_global_block_with_attributes( 'test/custom-block', [
$this->register_block_with_attributes( 'test/custom-block', [
'myComment' => [
'type' => 'string',
],
Expand Down
2 changes: 1 addition & 1 deletion tests/parser/blocks/test-unregistered-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function test_parse_unregistered_block() {
'Block type "test/unknown-block" is not server-side registered. Sourced block attributes will not be available.',
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArrayHasKey( 'warnings', $blocks, sprintf( 'Expected parser to have warnings, none received: %s', wp_json_encode( $blocks ) ) );
Expand Down
6 changes: 3 additions & 3 deletions tests/parser/sources/test-source-attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function test_parse_attribute_source() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -69,7 +69,7 @@ public function test_parse_attribute_source__with_default_value() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -100,7 +100,7 @@ public function test_parse_attribute_source__with_asterisk_selector() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down
8 changes: 4 additions & 4 deletions tests/parser/sources/test-source-children.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function test_parse_children__with_list_elements() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -86,7 +86,7 @@ public function test_parse_children__with_single_child() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -126,7 +126,7 @@ public function test_parse_children__with_mixed_nodes_and_text() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -157,7 +157,7 @@ public function test_parse_children__with_default_value() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/sources/test-source-delimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function test_parse_block_delimiter_attributes() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -75,7 +75,7 @@ public function test_parse_block_delimiter_attributes__are_overridden_by_sourced
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down
6 changes: 3 additions & 3 deletions tests/parser/sources/test-source-html.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function test_parse_html_source() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -71,7 +71,7 @@ public function test_parse_html_source__with_multiline_selector() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -102,7 +102,7 @@ public function test_parse_html_source__with_default_value() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/sources/test-source-meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function test_parse_meta_source() {
return $post_id;
};

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html, $post_id );

$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
Expand Down Expand Up @@ -69,7 +69,7 @@ public function test_parse_meta_source__with_default_value() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html, $post_id );

$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
Expand Down
2 changes: 1 addition & 1 deletion tests/parser/sources/test-source-node.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function test_parse_node__with_object_value() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down
6 changes: 3 additions & 3 deletions tests/parser/sources/test-source-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function test_parse_query_source() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );

$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
Expand Down Expand Up @@ -145,7 +145,7 @@ public function test_parse_query_source__with_nested_query() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );

$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
Expand Down Expand Up @@ -193,7 +193,7 @@ public function test_parse_query_source__with_default_value() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );

$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
Expand Down
4 changes: 2 additions & 2 deletions tests/parser/sources/test-source-raw.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function test_parse_raw_source() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -74,7 +74,7 @@ public function test_parse_raw_source__nested() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down
8 changes: 4 additions & 4 deletions tests/parser/sources/test-source-rich-text.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function test_parse_rich_text_source() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -69,7 +69,7 @@ public function test_parse_rich_text_source__with_formatting() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -100,7 +100,7 @@ public function test_parse_rich_text_source__with_default_value() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -131,7 +131,7 @@ public function test_parse_rich_text_source__with_default_value_with_formatting(
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down
6 changes: 3 additions & 3 deletions tests/parser/sources/test-source-tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function test_parse_tag_source() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -79,7 +79,7 @@ public function test_parse_tag_source__in_query() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand All @@ -106,7 +106,7 @@ public function test_parse_tag_source__with_default_value() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down
6 changes: 3 additions & 3 deletions tests/parser/sources/test-source-text.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function test_parse_text_source() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -72,7 +72,7 @@ public function test_parse_text_source__with_html_tags() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down Expand Up @@ -104,7 +104,7 @@ public function test_parse_text_source__with_default_value() {
],
];

$content_parser = new ContentParser( $this->registry );
$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );
$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], true );
Expand Down
Loading

0 comments on commit 40ff457

Please sign in to comment.