From b5d0978aa30e1e1c5d2a538b2b4af50ca6d93a97 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Thu, 19 Sep 2024 19:31:34 +0530 Subject: [PATCH 1/8] test : core separator block test --- tests/unit/CoreSeparatorTest.php | 169 +++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 tests/unit/CoreSeparatorTest.php diff --git a/tests/unit/CoreSeparatorTest.php b/tests/unit/CoreSeparatorTest.php new file mode 100644 index 00000000..c43d40ca --- /dev/null +++ b/tests/unit/CoreSeparatorTest.php @@ -0,0 +1,169 @@ +post_id = wp_insert_post( + [ + 'post_title' => 'Post with Separator', + 'post_content' => '', + 'post_status' => 'publish', + ] + ); + + \WPGraphQL::clear_schema(); + } + + public function tearDown(): void { + parent::tearDown(); + + wp_delete_post( $this->post_id, true ); + + \WPGraphQL::clear_schema(); + } + + public function query(): string { + return ' + fragment CoreSeparatorBlockFragment on CoreSeparator { + attributes { + align + anchor + backgroundColor + className + cssClassName + gradient + opacity + style + } + } + + query Post( $id: ID! ) { + post(id: $id, idType: DATABASE_ID) { + databaseId + editorBlocks { + apiVersion + blockEditorCategoryName + clientId + cssClassNames + innerBlocks { + name + } + isDynamic + name + parentClientId + renderedHtml + ...CoreSeparatorBlockFragment + } + } + } + '; + } + + public function test_retrieve_core_separator_attributes() { + $block_content = ' + +
+ + '; + + // Set post content. + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $query = $this->query(); + $variables = [ + 'id' => $this->post_id, + ]; + + $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' ); + + // Verify that the ID of the first post matches the one we just created. + $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); + + // There should be only one block using that query when not using flat: true + $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); + $this->assertEquals( 'core/separator', $actual['data']['post']['editorBlocks'][0]['name'], 'The block name should be core/separator' ); + + // Verify the attributes. + $this->assertEquals( + [ + 'cssClassName' => 'wp-block-separator alignwide has-alpha-channel-opacity', + 'align' => 'wide', + 'anchor' => null, + 'backgroundColor' => null, + 'className' => null, + 'gradient' => null, + 'opacity' => 'alpha-channel', + 'style' => null, + ], + $actual['data']['post']['editorBlocks'][0]['attributes'], + ); + } + + public function test_retrieve_core_separator_attributes_two() { + $block_content = ' + +
+ + '; + + // Set post content. + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $query = $this->query(); + $variables = [ + 'id' => $this->post_id, + ]; + + $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' ); + + // Verify that the ID of the first post matches the one we just created. + $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); + + // There should be only one block using that query when not using flat: true + $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); + $this->assertEquals( 'core/separator', $actual['data']['post']['editorBlocks'][0]['name'], 'The block name should be core/separator' ); + + // Verify the attributes. + $this->assertEquals( + [ + 'cssClassName' => 'wp-block-separator has-alpha-channel-opacity', + 'align' => null, + 'anchor' => null, + 'backgroundColor' => null, + 'className' => null, + 'gradient' => null, + 'opacity' => 'alpha-channel', + 'style' => null, + ], + $actual['data']['post']['editorBlocks'][0]['attributes'], + ); + } +} From 5c924772ad1878d58f223c3e6ae0fccb949bb689 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Thu, 19 Sep 2024 20:42:31 +0530 Subject: [PATCH 2/8] chore : test cleanup --- tests/unit/CoreSeparatorTest.php | 119 +++++++++++++++++-------------- 1 file changed, 67 insertions(+), 52 deletions(-) diff --git a/tests/unit/CoreSeparatorTest.php b/tests/unit/CoreSeparatorTest.php index c43d40ca..42a16f74 100644 --- a/tests/unit/CoreSeparatorTest.php +++ b/tests/unit/CoreSeparatorTest.php @@ -34,47 +34,49 @@ public function tearDown(): void { public function query(): string { return ' - fragment CoreSeparatorBlockFragment on CoreSeparator { - attributes { - align - anchor - backgroundColor - className - cssClassName - gradient - opacity - style - } - } - - query Post( $id: ID! ) { - post(id: $id, idType: DATABASE_ID) { - databaseId - editorBlocks { - apiVersion - blockEditorCategoryName - clientId - cssClassNames - innerBlocks { - name - } - isDynamic - name - parentClientId - renderedHtml - ...CoreSeparatorBlockFragment - } - } - } - '; + fragment CoreSeparatorBlockFragment on CoreSeparator { + attributes { + align + anchor + backgroundColor + className + cssClassName + gradient + lock + # metadata + opacity + style + } + } + + query Post( $id: ID! ) { + post(id: $id, idType: DATABASE_ID) { + databaseId + editorBlocks { + apiVersion + blockEditorCategoryName + clientId + cssClassNames + innerBlocks { + name + } + isDynamic + name + parentClientId + renderedHtml + ...CoreSeparatorBlockFragment + } + } + } + '; } - public function test_retrieve_core_separator_attributes() { + public function test_core_separator_fields_and_attributes() { $block_content = ' - -
- - '; + +
+ + '; // Set post content. wp_update_post( @@ -100,30 +102,42 @@ public function test_retrieve_core_separator_attributes() { // There should be only one block using that query when not using flat: true $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); - $this->assertEquals( 'core/separator', $actual['data']['post']['editorBlocks'][0]['name'], 'The block name should be core/separator' ); + + $block = $actual['data']['post']['editorBlocks'][0]; + + // Verify the block data. + $this->assertNotEmpty( $block['apiVersion'], 'The apiVersion should be present' ); + $this->assertEquals( 'design', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be text' ); + $this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); + $this->assertNotEmpty( $block['cssClassNames'], 'There should be cssClassNames' ); + $this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' ); + $this->assertEquals( 'core/separator', $block['name'], 'The block name should be core/separator' ); + $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); + $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); // Verify the attributes. $this->assertEquals( [ - 'cssClassName' => 'wp-block-separator alignwide has-alpha-channel-opacity', 'align' => 'wide', 'anchor' => null, 'backgroundColor' => null, - 'className' => null, - 'gradient' => null, + 'className' => 'is-style-dots', + 'cssClassName' => 'wp-block-separator alignwide has-alpha-channel-opacity', + 'gradient' => null, // @todo: getting null returned here, but should be the gradient value. 'opacity' => 'alpha-channel', - 'style' => null, + 'style' => '{"color":{"gradient":"linear-gradient(135deg,rgb(6,147,227) 1%,rgb(155,81,224) 100%)"}}', + 'lock' => '{"move":true,"remove":true}', ], - $actual['data']['post']['editorBlocks'][0]['attributes'], + $block['attributes'], ); } - public function test_retrieve_core_separator_attributes_two() { + public function test_core_separator_untested_attributes() { $block_content = ' - -
- - '; + +
+ + '; // Set post content. wp_update_post( @@ -154,14 +168,15 @@ public function test_retrieve_core_separator_attributes_two() { // Verify the attributes. $this->assertEquals( [ - 'cssClassName' => 'wp-block-separator has-alpha-channel-opacity', 'align' => null, - 'anchor' => null, - 'backgroundColor' => null, + 'anchor' => 'test-anchor', + 'backgroundColor' => 'accent-4', 'className' => null, + 'cssClassName' => 'wp-block-separator has-text-color has-accent-4-color has-alpha-channel-opacity has-accent-4-background-color has-background', 'gradient' => null, 'opacity' => 'alpha-channel', 'style' => null, + 'lock' => null, ], $actual['data']['post']['editorBlocks'][0]['attributes'], ); From d9a3eb63332fdfefce2460958428ef54ed31dca6 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Fri, 20 Sep 2024 21:43:59 +0530 Subject: [PATCH 3/8] chore : cleanup --- tests/unit/CoreSeparatorTest.php | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tests/unit/CoreSeparatorTest.php b/tests/unit/CoreSeparatorTest.php index 42a16f74..dd388a4d 100644 --- a/tests/unit/CoreSeparatorTest.php +++ b/tests/unit/CoreSeparatorTest.php @@ -71,6 +71,16 @@ className '; } + /** + * Test the retrieval of core/separator block fields and attributes. + * + * This test verifies that the block attributes for the core/separator block are + * properly returned via the GraphQL query. The test ensures that the block content, + * client ID, block name, and rendered HTML are correctly returned and that the attributes + * (such as align, className, gradient, opacity, style and lock) match the expected values. + * + * @return void + */ public function test_core_separator_fields_and_attributes() { $block_content = ' @@ -123,7 +133,7 @@ public function test_core_separator_fields_and_attributes() { 'backgroundColor' => null, 'className' => 'is-style-dots', 'cssClassName' => 'wp-block-separator alignwide has-alpha-channel-opacity', - 'gradient' => null, // @todo: getting null returned here, but should be the gradient value. + 'gradient' => null, // @todo: Getting null, but should be some valid gradient value. 'opacity' => 'alpha-channel', 'style' => '{"color":{"gradient":"linear-gradient(135deg,rgb(6,147,227) 1%,rgb(155,81,224) 100%)"}}', 'lock' => '{"move":true,"remove":true}', @@ -132,7 +142,17 @@ public function test_core_separator_fields_and_attributes() { ); } - public function test_core_separator_untested_attributes() { + /** + * Test the retrieval of core/separator block fields and attributes. + * + * This test verifies that the block attributes for the core/separator block are + * properly returned via the GraphQL query. The test ensures that the block content, + * client ID, block name, and rendered HTML are correctly returned and that the attributes + * (such as anchor, backgroundColor and cssClassName) match the expected values. + * + * @return void + */ + public function test_core_separator_attributes() { $block_content = '
From c4893e6e077321e2dd5c38f4f261564ba9c3a9a7 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Fri, 20 Sep 2024 21:51:56 +0530 Subject: [PATCH 4/8] chore : use of wp_json_encode --- tests/unit/CoreSeparatorTest.php | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/tests/unit/CoreSeparatorTest.php b/tests/unit/CoreSeparatorTest.php index dd388a4d..8599f2db 100644 --- a/tests/unit/CoreSeparatorTest.php +++ b/tests/unit/CoreSeparatorTest.php @@ -83,9 +83,9 @@ className */ public function test_core_separator_fields_and_attributes() { $block_content = ' - -
- + +
+ '; // Set post content. @@ -106,11 +106,7 @@ public function test_core_separator_fields_and_attributes() { $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' ); - - // Verify that the ID of the first post matches the one we just created. $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); - - // There should be only one block using that query when not using flat: true $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); $block = $actual['data']['post']['editorBlocks'][0]; @@ -125,6 +121,14 @@ public function test_core_separator_fields_and_attributes() { $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); + $style = wp_json_encode( + [ + 'color' => [ + 'gradient' => 'linear-gradient(135deg,rgb(6,147,227) 1%,rgb(155,81,224) 100%)', + ], + ] + ); + // Verify the attributes. $this->assertEquals( [ @@ -135,8 +139,13 @@ public function test_core_separator_fields_and_attributes() { 'cssClassName' => 'wp-block-separator alignwide has-alpha-channel-opacity', 'gradient' => null, // @todo: Getting null, but should be some valid gradient value. 'opacity' => 'alpha-channel', - 'style' => '{"color":{"gradient":"linear-gradient(135deg,rgb(6,147,227) 1%,rgb(155,81,224) 100%)"}}', - 'lock' => '{"move":true,"remove":true}', + 'style' => $style, + 'lock' => wp_json_encode( + [ + 'move' => true, + 'remove' => true, + ] + ), ], $block['attributes'], ); @@ -177,11 +186,7 @@ public function test_core_separator_attributes() { $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' ); - - // Verify that the ID of the first post matches the one we just created. $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); - - // There should be only one block using that query when not using flat: true $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); $this->assertEquals( 'core/separator', $actual['data']['post']['editorBlocks'][0]['name'], 'The block name should be core/separator' ); From 7e9f3097ebf08f0997c4a3a20e7be06c44cb8799 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Mon, 23 Sep 2024 18:39:28 +0530 Subject: [PATCH 5/8] chore : PR Reworks --- tests/unit/CoreSeparatorTest.php | 42 +++++++++++--------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/tests/unit/CoreSeparatorTest.php b/tests/unit/CoreSeparatorTest.php index 8599f2db..5fc19bf9 100644 --- a/tests/unit/CoreSeparatorTest.php +++ b/tests/unit/CoreSeparatorTest.php @@ -73,15 +73,8 @@ className /** * Test the retrieval of core/separator block fields and attributes. - * - * This test verifies that the block attributes for the core/separator block are - * properly returned via the GraphQL query. The test ensures that the block content, - * client ID, block name, and rendered HTML are correctly returned and that the attributes - * (such as align, className, gradient, opacity, style and lock) match the expected values. - * - * @return void */ - public function test_core_separator_fields_and_attributes() { + public function test_retrieve_core_separator_attribute_fields(): void { $block_content = '
@@ -121,14 +114,6 @@ public function test_core_separator_fields_and_attributes() { $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); - $style = wp_json_encode( - [ - 'color' => [ - 'gradient' => 'linear-gradient(135deg,rgb(6,147,227) 1%,rgb(155,81,224) 100%)', - ], - ] - ); - // Verify the attributes. $this->assertEquals( [ @@ -137,9 +122,15 @@ public function test_core_separator_fields_and_attributes() { 'backgroundColor' => null, 'className' => 'is-style-dots', 'cssClassName' => 'wp-block-separator alignwide has-alpha-channel-opacity', - 'gradient' => null, // @todo: Getting null, but should be some valid gradient value. + 'gradient' => null, // @todo: Getting null, but should be some valid gradient value!! 'opacity' => 'alpha-channel', - 'style' => $style, + 'style' => wp_json_encode( + [ + 'color' => [ + 'gradient' => 'linear-gradient(135deg,rgb(6,147,227) 1%,rgb(155,81,224) 100%)', + ], + ] + ), 'lock' => wp_json_encode( [ 'move' => true, @@ -152,16 +143,11 @@ public function test_core_separator_fields_and_attributes() { } /** - * Test the retrieval of core/separator block fields and attributes. - * - * This test verifies that the block attributes for the core/separator block are - * properly returned via the GraphQL query. The test ensures that the block content, - * client ID, block name, and rendered HTML are correctly returned and that the attributes - * (such as anchor, backgroundColor and cssClassName) match the expected values. + * Tests additional CoreSeparatorAttributes values. * - * @return void + * Covers: `anchor`, `backgroundColor`, and `gradient`. */ - public function test_core_separator_attributes() { + public function test_retrieve_core_separator_attributes(): void { $block_content = '
@@ -194,8 +180,8 @@ public function test_core_separator_attributes() { $this->assertEquals( [ 'align' => null, - 'anchor' => 'test-anchor', - 'backgroundColor' => 'accent-4', + 'anchor' => 'test-anchor', // Previously untested. + 'backgroundColor' => 'accent-4', // Previously untested. 'className' => null, 'cssClassName' => 'wp-block-separator has-text-color has-accent-4-color has-alpha-channel-opacity has-accent-4-background-color has-background', 'gradient' => null, From c3bcea440eb299a60f150becbd1ba949eab528d3 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Mon, 23 Sep 2024 20:29:22 +0530 Subject: [PATCH 6/8] fix : asserting top level gradient preset --- tests/unit/CoreSeparatorTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/unit/CoreSeparatorTest.php b/tests/unit/CoreSeparatorTest.php index 5fc19bf9..36c7bffb 100644 --- a/tests/unit/CoreSeparatorTest.php +++ b/tests/unit/CoreSeparatorTest.php @@ -122,7 +122,7 @@ public function test_retrieve_core_separator_attribute_fields(): void { 'backgroundColor' => null, 'className' => 'is-style-dots', 'cssClassName' => 'wp-block-separator alignwide has-alpha-channel-opacity', - 'gradient' => null, // @todo: Getting null, but should be some valid gradient value!! + 'gradient' => null, 'opacity' => 'alpha-channel', 'style' => wp_json_encode( [ @@ -149,7 +149,7 @@ public function test_retrieve_core_separator_attribute_fields(): void { */ public function test_retrieve_core_separator_attributes(): void { $block_content = ' - +
'; @@ -184,7 +184,7 @@ public function test_retrieve_core_separator_attributes(): void { 'backgroundColor' => 'accent-4', // Previously untested. 'className' => null, 'cssClassName' => 'wp-block-separator has-text-color has-accent-4-color has-alpha-channel-opacity has-accent-4-background-color has-background', - 'gradient' => null, + 'gradient' => 'gradient-10', // Previously untested. 'opacity' => 'alpha-channel', 'style' => null, 'lock' => null, From 8b2e16841f453292403150401dc4fd488bbaa40e Mon Sep 17 00:00:00 2001 From: Ta5r Date: Tue, 24 Sep 2024 01:28:34 +0530 Subject: [PATCH 7/8] added changeset --- .changeset/odd-hounds-sing.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/odd-hounds-sing.md diff --git a/.changeset/odd-hounds-sing.md b/.changeset/odd-hounds-sing.md new file mode 100644 index 00000000..71aec841 --- /dev/null +++ b/.changeset/odd-hounds-sing.md @@ -0,0 +1,5 @@ +--- +"@wpengine/wp-graphql-content-blocks": patch +--- + +tests : Core Separator block fields and attributes. From 48488496801bd6d57fee198929f00b3e16aa686e Mon Sep 17 00:00:00 2001 From: Dovid Levine Date: Mon, 23 Sep 2024 23:29:41 +0300 Subject: [PATCH 8/8] Update odd-hounds-sing.md --- .changeset/odd-hounds-sing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/odd-hounds-sing.md b/.changeset/odd-hounds-sing.md index 71aec841..81c39a62 100644 --- a/.changeset/odd-hounds-sing.md +++ b/.changeset/odd-hounds-sing.md @@ -2,4 +2,4 @@ "@wpengine/wp-graphql-content-blocks": patch --- -tests : Core Separator block fields and attributes. +tests : Add tests for `CoreSeparator` block.