diff --git a/tests/unit/CorePreformattedTest.php b/tests/unit/CorePreformattedTest.php index 6d0ee511..027c01a7 100644 --- a/tests/unit/CorePreformattedTest.php +++ b/tests/unit/CorePreformattedTest.php @@ -45,31 +45,44 @@ public function tearDown(): void { */ public function query(): string { return ' - fragment CorePreformattedBlockFragment on CorePreformatted { - attributes { - anchor - backgroundColor - className - content - fontFamily - fontSize - gradient - lock - metadata - style - textColor - } - } - query Post( $id: ID! ) { - post(id: $id, idType: DATABASE_ID) { - databaseId - editorBlocks { - name - ...CorePreformattedBlockFragment - } - } - } - '; + fragment CorePreformattedBlockFragment on CorePreformatted { + attributes { + anchor + backgroundColor + className + content + fontFamily + fontSize + gradient + lock + # metadata + style + textColor + } + } + query Post( $id: ID! ) { + post(id: $id, idType: DATABASE_ID) { + databaseId + editorBlocks { + apiVersion + blockEditorCategoryName + clientId + cssClassNames + innerBlocks { + name + } + isDynamic + name + parentClientId + renderedHtml + ... on BlockWithSupportsAnchor { + anchor + } + ...CorePreformattedBlockFragment + } + } + } + '; } /** @@ -87,13 +100,13 @@ className */ public function test_retrieve_core_preformatted_attributes() { $block_content = ' - -
This is a + +This is a preformatted block with multiple lines and preserved spacing.- - '; + + '; wp_update_post( [ @@ -102,30 +115,45 @@ public function test_retrieve_core_preformatted_attributes() { ] ); - $actual = graphql( - [ - 'query' => $this->query(), - 'variables' => [ 'id' => $this->post_id ], - ] - ); + $query = $this->query(); + $variables = [ + 'id' => $this->post_id, + ]; - $block = $actual['data']['post']['editorBlocks'][0]; - $attributes = $block['attributes']; + $actual = graphql( compact( 'query', 'variables' ) ); + + $this->assertArrayNotHasKey( 'errors', $actual, 'There should not be any errors' ); + $this->assertArrayHasKey( 'data', $actual, 'The data key should be present' ); + $this->assertArrayHasKey( 'post', $actual['data'], 'The post key should be present' ); + + $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); + + $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); + + $block = $actual['data']['post']['editorBlocks'][0]; - $this->assertEquals( 'core/preformatted', $block['name'] ); + $this->assertNotEmpty( $block['apiVersion'], 'The apiVersion should be present' ); + $this->assertEquals( 'text', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be text' ); + $this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); + + $this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' ); + $this->assertEquals( 'core/preformatted', $block['name'], 'The block name should be core/preformatted' ); + $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); + $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); + + $attributes = $block['attributes']; $this->assertEquals( [ 'anchor' => null, - 'backgroundColor' => 'pale-cyan-blue', - 'className' => 'custom-class', - 'content' => "This is a\npreformatted block\n with multiple lines\n and preserved spacing.", - 'fontFamily' => 'monospace', - 'fontSize' => 'large', + 'backgroundColor' => 'pale-cyan-blue', // previously untested + 'className' => 'custom-class', // previously untested + 'content' => "This is a\npreformatted block\n with multiple lines\n and preserved spacing.", // previously untested + 'fontFamily' => 'monospace', // previously untested + 'fontSize' => 'large', // previously untested 'gradient' => null, 'lock' => null, - 'metadata' => null, 'style' => null, - 'textColor' => 'vivid-red', + 'textColor' => 'vivid-red', // previously untested ], $attributes ); @@ -143,12 +171,12 @@ public function test_retrieve_core_preformatted_attributes() { */ public function test_retrieve_core_preformatted_with_custom_styles() { $block_content = ' - -This preformatted block + +This preformatted block has a gradient background and custom border style.- - '; + + '; wp_update_post( [ @@ -157,28 +185,37 @@ public function test_retrieve_core_preformatted_with_custom_styles() { ] ); - $actual = graphql( - [ - 'query' => $this->query(), - 'variables' => [ 'id' => $this->post_id ], - ] - ); + $query = $this->query(); + $variables = [ + 'id' => $this->post_id, + ]; - $block = $actual['data']['post']['editorBlocks'][0]; - $attributes = $block['attributes']; + $actual = graphql( compact( 'query', 'variables' ) ); + + $this->assertArrayNotHasKey( 'errors', $actual, 'There should not be any errors' ); + $this->assertArrayHasKey( 'data', $actual, 'The data key should be present' ); + $this->assertArrayHasKey( 'post', $actual['data'], 'The post key should be present' ); + $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); + + $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); + + $block = $actual['data']['post']['editorBlocks'][0]; + + $this->assertEquals( 'core/preformatted', $block['name'], 'The block name should be core/preformatted' ); + + $attributes = $block['attributes']; $this->assertEquals( [ - 'anchor' => 'custom-anchor', + 'anchor' => 'custom-anchor', // previously untested 'backgroundColor' => null, 'className' => null, 'content' => "This preformatted block\nhas a gradient background\nand custom border style.", 'fontFamily' => null, 'fontSize' => null, - 'gradient' => 'vivid-cyan-blue-to-vivid-purple', + 'gradient' => 'vivid-cyan-blue-to-vivid-purple', // previously untested 'lock' => null, - 'metadata' => null, - 'style' => wp_json_encode( + 'style' => wp_json_encode( // previously untested [ 'border' => [ 'width' => '2px', @@ -201,21 +238,20 @@ public function test_retrieve_core_preformatted_with_custom_styles() { } /** - * Test retrieval of core/preformatted block with lock and metadata. + * Test retrieval of core/preformatted block with lock. * * Attributes covered: * - lock - * - metadata * * @return void */ - public function test_retrieve_core_preformatted_with_lock_and_metadata() { + public function test_retrieve_core_preformatted_with_lock() { $block_content = ' - -This is a locked preformatted block + +This is a locked preformatted block with metadata.- - '; + + '; wp_update_post( [ @@ -224,16 +260,26 @@ public function test_retrieve_core_preformatted_with_lock_and_metadata() { ] ); - $actual = graphql( - [ - 'query' => $this->query(), - 'variables' => [ 'id' => $this->post_id ], - ] - ); + $query = $this->query(); + $variables = [ + 'id' => $this->post_id, + ]; - $block = $actual['data']['post']['editorBlocks'][0]; - $attributes = $block['attributes']; + $actual = graphql( compact( 'query', 'variables' ) ); + + $this->assertArrayNotHasKey( 'errors', $actual, 'There should not be any errors' ); + $this->assertArrayHasKey( 'data', $actual, 'The data key should be present' ); + $this->assertArrayHasKey( 'post', $actual['data'], 'The post key should be present' ); + + $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); + + $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); + $block = $actual['data']['post']['editorBlocks'][0]; + + $this->assertEquals( 'core/preformatted', $block['name'], 'The block name should be core/preformatted' ); + + $attributes = $block['attributes']; $this->assertEquals( [ 'anchor' => null, @@ -243,8 +289,7 @@ public function test_retrieve_core_preformatted_with_lock_and_metadata() { 'fontFamily' => null, 'fontSize' => null, 'gradient' => null, - 'lock' => '{"move":true,"remove":true}', - 'metadata' => '{"key1":"value1","key2":"value2"}', + 'lock' => '{"move":true,"remove":true}', // previously untested 'style' => null, 'textColor' => null, ],