-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug Fix: Parsing boolean block attributes like in CoreVideo always return false. #138
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@wpengine/wp-graphql-content-blocks": patch | ||
--- | ||
|
||
Bug Fix. Boolean block attributes no longer always resolve as false. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace WPGraphQL\ContentBlocks\Unit; | ||
|
||
final class CoreVideoTest extends PluginTestCase { | ||
public $instance; | ||
public $post_id; | ||
|
||
public function setUp(): void { | ||
parent::setUp(); | ||
global $wpdb; | ||
|
||
$this->post_id = wp_insert_post( | ||
array( | ||
'post_title' => 'Post Title', | ||
'post_content' => preg_replace( | ||
'/\s+/', | ||
' ', | ||
trim( | ||
' | ||
<!-- wp:video {"id":1636} --> | ||
<figure class="wp-block-video"><video autoplay controls loop poster="http://mysite.local/wp-content/uploads/2023/05/pexels-egor-komarov-14420089-scaled.jpg" preload="auto" src="http://mysite.local/wp-content/uploads/2023/07/pexels_videos_1860684-1440p.mp4" playsinline></video></figure> | ||
<!-- /wp:video --> | ||
' | ||
) | ||
), | ||
'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, | ||
]); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to be concerned with other falsey values like an empty string?
Im not too familiar with the Block Attribute type validation, but WP is usually horrible at strict booleans.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is specifically targeted for empty strings. An empty string in the context of parsing a boolean attribute like
<video autoplay />
hereautoplay=""
which according to the HTML spec it's enabled. So in that case we returntrue
. So unless the value is specificallyfalse
ornull
it is considered truthy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remember that attributes here refer to all block attributes, and just not HTML attributes, so I wouldn't rely on that assumption that the underlying WordPress parser is making that distinction when it comes to the HTML spec.
Any while you've labeled this PR (and the associated test) as specific to
CoreVideo
, you're actually addingbool
handling to all Block attributes plus passing them through helper libraries like DiDom.It should be easy enough to test and confirm either way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the code to handle null returns now. This should work as expected.