From 232f0cac8d1f2936d386a2f7d6dd1d5737087c2f Mon Sep 17 00:00:00 2001 From: Ashutosh Gautam Date: Tue, 24 Sep 2024 16:16:27 +0530 Subject: [PATCH 1/3] feat: Add tests for CorePreformatted Block --- tests/unit/CorePreformattedTest.php | 254 ++++++++++++++++++++++++++++ 1 file changed, 254 insertions(+) create mode 100644 tests/unit/CorePreformattedTest.php diff --git a/tests/unit/CorePreformattedTest.php b/tests/unit/CorePreformattedTest.php new file mode 100644 index 00000000..6d0ee511 --- /dev/null +++ b/tests/unit/CorePreformattedTest.php @@ -0,0 +1,254 @@ +post_id = wp_insert_post( + [ + 'post_title' => 'Post with Preformatted Block', + 'post_content' => '', + 'post_status' => 'publish', + ] + ); + + \WPGraphQL::clear_schema(); + } + + /** + * Tear down the test environment. + */ + public function tearDown(): void { + parent::tearDown(); + + wp_delete_post( $this->post_id, true ); + + \WPGraphQL::clear_schema(); + } + + /** + * Provide the GraphQL query for testing. + * + * @return string The GraphQL query. + */ + 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 + } + } + } + '; + } + + /** + * Test the retrieval of core/preformatted block attributes. + * + * Attributes covered: + * - content + * - backgroundColor + * - textColor + * - fontSize + * - fontFamily + * - className + * + * @return void + */ + public function test_retrieve_core_preformatted_attributes() { + $block_content = ' + +
This is a
+preformatted block
+    with multiple lines
+        and preserved spacing.
+ + '; + + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $actual = graphql( + [ + 'query' => $this->query(), + 'variables' => [ 'id' => $this->post_id ], + ] + ); + + $block = $actual['data']['post']['editorBlocks'][0]; + $attributes = $block['attributes']; + + $this->assertEquals( 'core/preformatted', $block['name'] ); + $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', + 'gradient' => null, + 'lock' => null, + 'metadata' => null, + 'style' => null, + 'textColor' => 'vivid-red', + ], + $attributes + ); + } + + /** + * Test retrieval of core/preformatted block with custom styles and gradient. + * + * Attributes covered: + * - gradient + * - style + * - anchor + * + * @return void + */ + public function test_retrieve_core_preformatted_with_custom_styles() { + $block_content = ' + +
This preformatted block
+has a gradient background
+and custom border style.
+ + '; + + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $actual = graphql( + [ + 'query' => $this->query(), + 'variables' => [ 'id' => $this->post_id ], + ] + ); + + $block = $actual['data']['post']['editorBlocks'][0]; + $attributes = $block['attributes']; + + $this->assertEquals( + [ + 'anchor' => 'custom-anchor', + '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', + 'lock' => null, + 'metadata' => null, + 'style' => wp_json_encode( + [ + 'border' => [ + 'width' => '2px', + 'style' => 'dashed', + ], + 'spacing' => [ + 'padding' => [ + 'top' => '20px', + 'right' => '20px', + 'bottom' => '20px', + 'left' => '20px', + ], + ], + ] + ), + 'textColor' => null, + ], + $attributes + ); + } + + /** + * Test retrieval of core/preformatted block with lock and metadata. + * + * Attributes covered: + * - lock + * - metadata + * + * @return void + */ + public function test_retrieve_core_preformatted_with_lock_and_metadata() { + $block_content = ' + +
This is a locked preformatted block
+with metadata.
+ + '; + + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $actual = graphql( + [ + 'query' => $this->query(), + 'variables' => [ 'id' => $this->post_id ], + ] + ); + + $block = $actual['data']['post']['editorBlocks'][0]; + $attributes = $block['attributes']; + + $this->assertEquals( + [ + 'anchor' => null, + 'backgroundColor' => null, + 'className' => null, + 'content' => "This is a locked preformatted block\nwith metadata.", + 'fontFamily' => null, + 'fontSize' => null, + 'gradient' => null, + 'lock' => '{"move":true,"remove":true}', + 'metadata' => '{"key1":"value1","key2":"value2"}', + 'style' => null, + 'textColor' => null, + ], + $attributes + ); + } +} From 5915c0636e8c17e4839f57495a6bbaad9899f317 Mon Sep 17 00:00:00 2001 From: Ashutosh Gautam Date: Tue, 24 Sep 2024 16:18:09 +0530 Subject: [PATCH 2/3] fix: Add Changeset --- .changeset/orange-buses-help.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/orange-buses-help.md diff --git a/.changeset/orange-buses-help.md b/.changeset/orange-buses-help.md new file mode 100644 index 00000000..c581d9f9 --- /dev/null +++ b/.changeset/orange-buses-help.md @@ -0,0 +1,5 @@ +--- +"@wpengine/wp-graphql-content-blocks": patch +--- + +tests: Add tests for CorePreformatted Block From 4e5fcfb4b3a3f90ecdbf504bbc02c056c508fc83 Mon Sep 17 00:00:00 2001 From: Ashutosh Gautam Date: Wed, 25 Sep 2024 00:21:07 +0530 Subject: [PATCH 3/3] fix: Add missing asserts --- tests/unit/CorePreformattedTest.php | 201 +++++++++++++++++----------- 1 file changed, 123 insertions(+), 78 deletions(-) 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, ],