Skip to content

Commit

Permalink
fix : test , and attributes with their respective supported WordPress…
Browse files Browse the repository at this point in the history
… versions.
  • Loading branch information
Ta5r committed Oct 1, 2024
1 parent d1ec4fc commit c4b2d94
Showing 1 changed file with 119 additions and 14 deletions.
133 changes: 119 additions & 14 deletions tests/unit/CoreImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public function tearDown(): void {
parent::tearDown();
}

public function query() {
/**
* Get the query for the CoreImage block.
*
* @param string $attributes The attributes to add to query.
*/
public function query( $attributes = '' ): string {
return '
fragment CoreImageBlockFragment on CoreImage {
attributes {
Expand All @@ -55,9 +60,7 @@ public function query() {
src
style
sizeSlug
# lightbox # not supported yet
# aspectRatio # not supported yet
# scale # not supported yet
' . $attributes . '
linkClass
linkTarget
linkDestination
Expand Down Expand Up @@ -97,7 +100,7 @@ className

/**
* Test that the CoreImage block is retrieved correctly.
*
*
* Covers the following attributes:
* - apiVersion
* - blockEditorCategoryName
Expand All @@ -108,7 +111,6 @@ className
* - parentClientId
* - renderedHtml
* - attributes
*
*/
public function test_retrieve_core_image_fields_attributes(): void {
$block_content = '
Expand All @@ -128,7 +130,6 @@ public function test_retrieve_core_image_fields_attributes(): void {
]
);

$query = $query;
$variables = [
'id' => $this->post_id,
];
Expand Down Expand Up @@ -190,11 +191,10 @@ public function test_retrieve_core_image_fields_attributes(): void {

/**
* Test that the CoreImage block mediaDetails are retrieved correctly.
*
*
* Covers the following attributes:
* - height
* - width
*
*/
public function test_retrieve_core_image_media_details(): void {
$block_content = '
Expand Down Expand Up @@ -264,7 +264,7 @@ public function test_retrieve_core_image_media_details(): void {

/**
* Test that the CoreImage block attributes are retrieved correctly.
*
*
* Covers the following attributes:
* - width
* - height
Expand All @@ -286,7 +286,6 @@ public function test_retrieve_core_image_media_details(): void {
* - anchor
* - rel
* - href
*
*/
public function test_retrieve_core_image_attributes(): void {

Expand All @@ -309,15 +308,12 @@ public function test_retrieve_core_image_attributes(): void {
]
);

$query = $query;
$variables = [
'id' => $this->post_id,
];

$actual = graphql( compact( 'query', 'variables' ) );

$node = $actual['data']['post'];

$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' );
Expand Down Expand Up @@ -369,4 +365,113 @@ public function test_retrieve_core_image_attributes(): void {
$block['attributes']
);
}

/**
* Test that the CoreImage block previously untested attributes are retrieved correctly.
*
* Covers the following attributes:
* - aspectRatio
* - scale
* - lightbox
*/
public function test_retrieve_core_untested_attributes(): void {
$block_content = '
<!-- wp:image {"lightbox":{"enabled":false},"align":"left","width":500,"height":500,"aspectRatio":"4/3","scale":"cover","sizeSlug":"full","linkDestination":"none", "id":' . $this->attachment_id . ',"className":"is-style-rounded", "style":{"color":{"duotone":"var:preset|duotone|purple-green"}},"borderColor":"vivid-red","lock":{"move":true,"remove":true},"className":"test-css-class-name"} -->
<figure class="wp-block-image size-full is-resized" id="test-anchor">
<a class="test-link-css-class" href="http://decoupled.local/dcf-1-0/" target="_blank" rel="https://www.youtube.com/ noreferrer noopener">
<img src="http://mysite.local/wp-content/uploads/2023/05/online-programming-course-hero-section-bg.svg" alt="alt-text" class="wp-image-1432" width="500" height="500" title="test-title"/></figure>
</a>
<figcaption class="wp-element-caption">Align left</figcaption>
<!-- /wp:image -->';

// Update the post content with the block content.
wp_update_post(
[
'ID' => $this->post_id,
'post_content' => $block_content,
]
);

$variables = [
'id' => $this->post_id,
];

// `aspectRatio` is only supported in WP 6.3+.
if ( is_wp_version_compatible( '6.3' ) ) {
$query = '
fragment CoreImageBlockFragment on CoreImage {
attributes {
aspectRatio
scale
}
}
query Post( $id: ID! ) {
post(id: $id, idType: DATABASE_ID) {
databaseId
editorBlocks {
name
...CoreImageBlockFragment
}
}
}';

$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' );

$block = $actual['data']['post']['editorBlocks'][0];

$this->assertEquals(
[
'aspectRatio' => '4/3', // Previously untested.
'scale' => 'cover', // Previously untested.

],
$block['attributes']
);
}

// `lightbox` is only supported in WP 6.4+.
if ( is_wp_version_compatible( '6.4' ) ) {
$query = '
fragment CoreImageBlockFragment on CoreImage {
attributes {
lightbox
}
}
query Post( $id: ID! ) {
post(id: $id, idType: DATABASE_ID) {
databaseId
editorBlocks {
name
...CoreImageBlockFragment
}
}
}';

$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' );

$block = $actual['data']['post']['editorBlocks'][0];

$this->assertEquals(
[
'lightbox' => wp_json_encode( // Previously untested.
[
'enabled' => false,
]
),

],
$block['attributes']
);
}
}
}

0 comments on commit c4b2d94

Please sign in to comment.