From 96197c6c0be07b99c891d208053cc8baf2f36f29 Mon Sep 17 00:00:00 2001 From: theodesp <328805+theodesp@users.noreply.github.com> Date: Wed, 26 Jul 2023 12:57:42 +0100 Subject: [PATCH 1/4] [MERL-1134] Fix for resolving boolean attributes. --- includes/Blocks/Block.php | 13 ++-- tests/unit/blocks/CoreVideoTest.php | 95 +++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 tests/unit/blocks/CoreVideoTest.php diff --git a/includes/Blocks/Block.php b/includes/Blocks/Block.php index 8cafff0a..085f62ea 100644 --- a/includes/Blocks/Block.php +++ b/includes/Blocks/Block.php @@ -286,10 +286,15 @@ private function resolve_block_attributes( $block, $attribute_name, $attribute_c break; }//end switch - - // if type is set to integer, get the integer value of the attribute. - if ( 'integer' === $attribute_config['type'] ) { - $value = intval( $value ); + // Post processing of return value based on configured type + switch ( $attribute_config['type'] ) { + case 'integer': + $value = intval( $value ); + break; + case 'boolean': + // Only false when value is not null or actually false + $value = ! ( false === $value || is_null( $value ) ); + break; } return $value; diff --git a/tests/unit/blocks/CoreVideoTest.php b/tests/unit/blocks/CoreVideoTest.php new file mode 100644 index 00000000..ff01100c --- /dev/null +++ b/tests/unit/blocks/CoreVideoTest.php @@ -0,0 +1,95 @@ +post_id = wp_insert_post( + array( + 'post_title' => 'Post Title', + 'post_content' => preg_replace( + '/\s+/', + ' ', + trim( + ' + +
+ + ' + ) + ), + 'post_status' => 'publish', + ) + ); + } + + public function tearDown(): void { + // your tear down methods here + parent::tearDown(); + wp_delete_post( $this->post_id, true ); + } + + public function test_retrieve_core_video_attributes() { + $query = ' + fragment CoreVideoBlockFragment on CoreVideo { + attributes { + align + anchor + autoplay + tracks + muted + caption + preload + src + playsInline + controls + loop + poster + id + } + } + + query GetPosts { + posts(first: 1) { + nodes { + databaseId + editorBlocks { + name + ...CoreVideoBlockFragment + } + } + } + } + '; + $actual = graphql( array( 'query' => $query ) ); + $node = $actual['data']['posts']['nodes'][0]; + + // Verify that the ID of the first post matches the one we just created. + $this->assertEquals( $this->post_id, $node['databaseId'] ); + // There should be only one block using that query when not using flat: true + $this->assertEquals( count( $node['editorBlocks'] ), 1 ); + $this->assertEquals( $node['editorBlocks'][0]['name'], 'core/video' ); + + $this->assertEquals( $node['editorBlocks'][0]['attributes'], [ + 'align' => null, + 'anchor' => null, + 'autoplay' => true, + 'tracks' => '[]', + 'muted' => false, + 'caption' => '', + 'preload' => 'auto', + 'src' => 'http://mysite.local/wp-content/uploads/2023/07/pexels_videos_1860684-1440p.mp4', + 'playsInline' => true, + 'controls' => true, + 'loop' => true, + 'poster' => 'http://mysite.local/wp-content/uploads/2023/05/pexels-egor-komarov-14420089-scaled.jpg', + 'id' => 1636.0, + ]); + } +} From 5b8189ffe7c1962ca396f2021fb44547cc3a7028 Mon Sep 17 00:00:00 2001 From: theodesp <328805+theodesp@users.noreply.github.com> Date: Wed, 26 Jul 2023 12:58:59 +0100 Subject: [PATCH 2/4] Chore: Changeset. --- .changeset/spicy-tables-cross.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/spicy-tables-cross.md diff --git a/.changeset/spicy-tables-cross.md b/.changeset/spicy-tables-cross.md new file mode 100644 index 00000000..24885a2e --- /dev/null +++ b/.changeset/spicy-tables-cross.md @@ -0,0 +1,5 @@ +--- +"@wpengine/wp-graphql-content-blocks": patch +--- + +Bug Fix. Boolean attributes always resolve as false. For example when using the CoreVideo block. From b1af10fb19aa9ea0341cae1d1cdc13c61eb8464c Mon Sep 17 00:00:00 2001 From: Theofanis Despoudis <328805+theodesp@users.noreply.github.com> Date: Wed, 26 Jul 2023 15:43:07 +0100 Subject: [PATCH 3/4] Update .changeset/spicy-tables-cross.md Co-authored-by: John Parris --- .changeset/spicy-tables-cross.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/spicy-tables-cross.md b/.changeset/spicy-tables-cross.md index 24885a2e..2425e4af 100644 --- a/.changeset/spicy-tables-cross.md +++ b/.changeset/spicy-tables-cross.md @@ -2,4 +2,4 @@ "@wpengine/wp-graphql-content-blocks": patch --- -Bug Fix. Boolean attributes always resolve as false. For example when using the CoreVideo block. +Bug Fix. Boolean block attributes no longer always resolve as false. From 06a633e77fb020dffd51610cbe12be6cdbfe3c2f Mon Sep 17 00:00:00 2001 From: theodesp <328805+theodesp@users.noreply.github.com> Date: Thu, 27 Jul 2023 10:36:14 +0100 Subject: [PATCH 4/4] Refactor: Resolve null boolean attributes. --- includes/Blocks/Block.php | 8 ++++++-- tests/unit/blocks/CoreVideoTest.php | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/includes/Blocks/Block.php b/includes/Blocks/Block.php index 085f62ea..c5faaea2 100644 --- a/includes/Blocks/Block.php +++ b/includes/Blocks/Block.php @@ -292,8 +292,12 @@ private function resolve_block_attributes( $block, $attribute_name, $attribute_c $value = intval( $value ); break; case 'boolean': - // Only false when value is not null or actually false - $value = ! ( false === $value || is_null( $value ) ); + // If the value is empty or false return + if ( is_null( $value ) || false === $value ) { + break; + } + // Otherwise it's truthy + $value = true; break; } diff --git a/tests/unit/blocks/CoreVideoTest.php b/tests/unit/blocks/CoreVideoTest.php index ff01100c..96401174 100644 --- a/tests/unit/blocks/CoreVideoTest.php +++ b/tests/unit/blocks/CoreVideoTest.php @@ -19,7 +19,7 @@ public function setUp(): void { trim( ' -
+
' ) @@ -86,7 +86,7 @@ public function test_retrieve_core_video_attributes() { 'preload' => 'auto', 'src' => 'http://mysite.local/wp-content/uploads/2023/07/pexels_videos_1860684-1440p.mp4', 'playsInline' => true, - 'controls' => true, + 'controls' => null, 'loop' => true, 'poster' => 'http://mysite.local/wp-content/uploads/2023/05/pexels-egor-komarov-14420089-scaled.jpg', 'id' => 1636.0,