Skip to content

Commit

Permalink
Bug: CoreImage width attribute resolve throws error. (#130)
Browse files Browse the repository at this point in the history
* Bug: CoreImage width attribute resolve throws error.

* Chore: Changeset

* Test: Add unit tests
  • Loading branch information
theodesp committed Jul 17, 2023
1 parent 2c990a7 commit 28fca4a
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-items-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpengine/wp-graphql-content-blocks": patch
---

Bug Fix: CoreImage `width` attribute throws error.
1 change: 1 addition & 0 deletions includes/Blocks/CoreImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ class CoreImage extends Block {
'source' => 'attribute',
'attribute' => 'src',
),
'width' => array( 'type' => 'string' ),
);
}
100 changes: 100 additions & 0 deletions tests/unit/blocks/CoreImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace WPGraphQL\ContentBlocks\Unit;

final class CoreImageTest 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:image {"width":500,"height":500,"sizeSlug":"full","linkDestination":"none"} -->
<figure class="wp-block-image size-full is-resized"><img src="http://mysite.local/wp-content/uploads/2023/05/online-programming-course-hero-section-bg.svg" alt="" class="wp-image-1432" width="500" height="500"/></figure>
<!-- /wp:image -->
'
)
),
'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_image_attributes() {
$query = '
fragment CoreColumnBlockFragment on CoreColumn {
attributes {
width
}
}
fragment CoreImageBlockFragment on CoreImage {
attributes {
width
height
alt
src
style
sizeSlug
linkClass
linkTarget
linkDestination
align
caption
cssClassName
}
}
query GetPosts {
posts(first: 1) {
nodes {
databaseId
editorBlocks {
name
...CoreImageBlockFragment
...CoreColumnBlockFragment
}
}
}
}
';
$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/image' );

$this->assertEquals( $node['editorBlocks'][0]['attributes'], [
"width" => "500",
"height" => 500.0,
"alt" => "",
"src" => "http://mysite.local/wp-content/uploads/2023/05/online-programming-course-hero-section-bg.svg",
"style" => NULL,
"sizeSlug" => "full",
"linkClass" => NULL,
"linkTarget" => NULL,
"linkDestination" => "none",
"align" => NULL,
"caption" => "",
"cssClassName" => "wp-block-image size-full is-resized"
]);
}
}

0 comments on commit 28fca4a

Please sign in to comment.