diff --git a/tests/parser/test-content-parser.php b/tests/parser/test-content-parser.php index a5b90d0..d11b65e 100644 --- a/tests/parser/test-content-parser.php +++ b/tests/parser/test-content-parser.php @@ -103,4 +103,63 @@ public function test_parse_multiple_blocks() { $this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) ); $this->assertArraySubset( $expected_blocks, $blocks['blocks'], true ); } + + /* Default values and missing values */ + + public function test_parse_block_missing_attributes_and_defaults() { + $this->register_block_with_attributes( 'test/block-with-empty-attributes', [ + 'attributeOneWithDefaultValueAndSource' => [ + 'type' => 'string', + 'source' => 'attribute', + 'selector' => 'div', + 'attribute' => 'data-attr-one', + 'default' => 'Default Attribute 1 Value', + ], + 'attributeTwoWithDefaultValueAndNoSource' => [ + 'type' => 'string', + 'source' => 'attribute', + 'selector' => 'div', + 'attribute' => 'data-attr-two', + 'default' => 'Default Attribute 2 Value', + ], + 'attributeThreeWithNoDefaultValueAndSource' => [ + 'type' => 'string', + 'source' => 'attribute', + 'selector' => 'div', + 'attribute' => 'data-attr-three', + ], + 'attributeFourWithNoDefaultValueAndNoSource' => [ + 'type' => 'string', + 'source' => 'attribute', + 'selector' => 'div', + 'attribute' => 'data-attr-four', + ], + ] ); + + $html = ' + +
Content
+ + '; + + $expected_blocks = [ + [ + 'name' => 'test/block-with-empty-attributes', + 'attributes' => [ + 'attributeOneWithDefaultValueAndSource' => 'Attribute 1 Value', + 'attributeTwoWithDefaultValueAndNoSource' => 'Default Attribute 2 Value', + 'attributeThreeWithNoDefaultValueAndSource' => 'Attribute 3 Value', + // attributeFourWithNoDefaultValueAndNoSource has no default, not represented + ], + ], + ]; + + $content_parser = new ContentParser( $this->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 ); + } }