From 11e0a1727924a8b293203044b3fcacbedf8f8bef Mon Sep 17 00:00:00 2001 From: Ta5r Date: Thu, 26 Sep 2024 15:56:42 +0530 Subject: [PATCH 01/10] test : Backfill core image test --- tests/unit/CoreImageTest.php | 299 ++++++++++++++++++++++++++--------- 1 file changed, 221 insertions(+), 78 deletions(-) diff --git a/tests/unit/CoreImageTest.php b/tests/unit/CoreImageTest.php index 18c32a8f..77ee5f50 100644 --- a/tests/unit/CoreImageTest.php +++ b/tests/unit/CoreImageTest.php @@ -4,7 +4,19 @@ final class CoreImageTest extends PluginTestCase { public $instance; + + /** + * The ID of the post created for the test. + * + * @var int + */ public $post_id; + + /** + * The ID of the attachment created for the test. + * + * @var int + */ public $attachment_id; public function setUp(): void { @@ -15,17 +27,7 @@ public function setUp(): void { $this->post_id = wp_insert_post( [ 'post_title' => 'Post Title', - 'post_content' => preg_replace( - '/\s+/', - ' ', - trim( - ' - -
- - ' - ) - ), + 'post_content' => '', 'post_status' => 'publish', ] ); @@ -36,79 +38,178 @@ public function setUp(): void { public function tearDown(): void { // your tear down methods here wp_delete_post( $this->post_id, true ); - \WPGraphQL::clear_schema(); parent::tearDown(); + + \WPGraphQL::clear_schema(); } - public function test_retrieve_core_image_media_details() { - $query = ' - fragment CoreImageBlockFragment on CoreImage { - attributes { - id - } - mediaDetails { - height - width - } - } - - query GetPosts { - posts(first: 1) { - nodes { - editorBlocks { - ...CoreImageBlockFragment - } - } - } - } - '; - $actual = graphql( [ 'query' => $query ] ); - $node = $actual['data']['posts']['nodes'][0]; + public function test_retrieve_core_image_media_details(): void { + $block_content = ' +
+ + alt-text
+ +
Align left
+ '; + + $query = ' + fragment CoreImageBlockFragment on CoreImage { + attributes { + id + } + mediaDetails { + height + width + } + } + + query Post( $id: ID! ) { + post(id: $id, idType: DATABASE_ID) { + editorBlocks { + # databaseId + apiVersion + blockEditorCategoryName + clientId + cssClassNames + name + innerBlocks { + name + } + parentClientId + renderedHtml + ...CoreImageBlockFragment + } + } + } + '; + + // Set post content. + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $query = $query; + $variables = [ + 'id' => $this->post_id, + ]; + + $actual = graphql( compact( 'query', 'variables' ) ); + error_log( print_r( $actual, true ) ); + // $actual = $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' ); + + // @todo : fix + // $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); + + $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); + + $block = $actual['data']['post']['editorBlocks'][0]; + + $this->assertNotEmpty( $block['apiVersion'], 'The apiVersion should be present' ); + $this->assertEquals( 'media', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be text' ); + $this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); + + // @todo this is not working + $this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' ); + + $this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' ); + $this->assertEquals( 'core/image', $block['name'], 'The block name should be core/image' ); + $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); + $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); $this->assertEquals( [ 'width' => 50, 'height' => 50, ], - $node['editorBlocks'][0]['mediaDetails'] + $block['mediaDetails'] ); } - public function test_retrieve_core_image_attributes() { - $query = ' - fragment CoreImageBlockFragment on CoreImage { - attributes { - id - width - height - alt - src - style - sizeSlug - linkClass - linkTarget - linkDestination - align - caption - cssClassName - } - } - - query GetPosts { - posts(first: 1) { - nodes { - databaseId - editorBlocks { - name - ...CoreImageBlockFragment - } - } - } - } - '; - $actual = graphql( [ 'query' => $query ] ); - $node = $actual['data']['posts']['nodes'][0]; + public function test_retrieve_core_image_attributes(): void { + + $block_content = ' +
+ + alt-text
+ +
Align left
+ '; + + $query = ' + fragment CoreImageBlockFragment on CoreImage { + attributes { + id + width + height + alt + align + src + style + sizeSlug + # lightbox # not supported yet + # aspectRatio # not supported yet + # scale # not supported yet + linkClass + linkTarget + linkDestination + borderColor + caption + className + cssClassName + url + rel + href + title + lock + anchor + } + } + + query Post( $id: ID! ) { + post(id: $id, idType: DATABASE_ID) { + databaseId + editorBlocks { + apiVersion + blockEditorCategoryName + clientId + cssClassNames + innerBlocks { + name + } + parentClientId + renderedHtml + name + ...CoreImageBlockFragment + } + } + } + '; + + // Set post content. + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $query = $query; + $variables = [ + 'id' => $this->post_id, + ]; + + $actual = graphql( compact( 'query', 'variables' ) ); + + error_log( print_r( $actual, true ) ); + $node = $actual['data']['post']; // Verify that the ID of the first post matches the one we just created. $this->assertEquals( $this->post_id, $node['databaseId'] ); @@ -116,21 +217,63 @@ public function test_retrieve_core_image_attributes() { $this->assertEquals( 1, count( $node['editorBlocks'] ) ); $this->assertEquals( 'core/image', $node['editorBlocks'][0]['name'] ); + $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' ); + + $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); + + $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); + + $block = $actual['data']['post']['editorBlocks'][0]; + + $this->assertNotEmpty( $block['apiVersion'], 'The apiVersion should be present' ); + $this->assertEquals( 'media', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be media' ); + $this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); + + // @todo this is not working + // $this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' ); + + $this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' ); + $this->assertEquals( 'core/image', $block['name'], 'The block name should be core/image' ); + $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); + $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); $this->assertEquals( [ 'width' => '500', 'height' => 500.0, - 'alt' => '', + 'alt' => 'alt-text', 'id' => $this->attachment_id, 'src' => 'http://mysite.local/wp-content/uploads/2023/05/online-programming-course-hero-section-bg.svg', - 'style' => null, + 'style' => wp_json_encode( + [ + 'color' => [ + 'duotone' => 'var:preset|duotone|purple-green', + ], + ] + ), 'sizeSlug' => 'full', - 'linkClass' => null, - 'linkTarget' => null, + 'linkClass' => 'test-link-css-class', + 'linkTarget' => '_blank', 'linkDestination' => 'none', - 'align' => null, - 'caption' => '', - 'cssClassName' => 'wp-block-image size-full is-resized', + 'align' => 'left', + 'caption' => 'Align left', + 'className' => 'test-css-class-name', + 'cssClassName' => ( ! is_wp_version_compatible( '6.3' ) ) ? 'wp-duotone-varpresetduotonepurple-green-19 wp-block-image size-full is-resized wp-duotone-purple-green' : 'wp-block-image size-full is-resized wp-duotone-purple-green', // This uses the old class name for WP < 6.3 which is wp-duotone-varpresetduotonepurple-green-19. + 'url' => 'http://mysite.local/wp-content/uploads/2023/05/online-programming-course-hero-section-bg.svg', + 'borderColor' => 'vivid-red', + 'title' => 'test-title', + 'lock' => wp_json_encode( + [ + 'move' => true, + 'remove' => true, + ] + ), + 'anchor' => 'test-anchor', + + 'rel' => 'https://www.youtube.com/ noreferrer noopener', + 'href' => 'http://decoupled.local/dcf-1-0/', + ], $node['editorBlocks'][0]['attributes'] ); From 44cf4596899c1dbe701f6eb12f72a7ae577a2b07 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Thu, 26 Sep 2024 16:07:00 +0530 Subject: [PATCH 02/10] temp : fix formatting, failing cssClassName assert --- tests/unit/CoreImageTest.php | 180 +++++++++++++++++------------------ 1 file changed, 90 insertions(+), 90 deletions(-) diff --git a/tests/unit/CoreImageTest.php b/tests/unit/CoreImageTest.php index 77ee5f50..eab7b5a5 100644 --- a/tests/unit/CoreImageTest.php +++ b/tests/unit/CoreImageTest.php @@ -46,43 +46,43 @@ public function tearDown(): void { public function test_retrieve_core_image_media_details(): void { $block_content = ' -
- - alt-text
- -
Align left
- '; +
+ + alt-text
+ +
Align left
+ '; $query = ' - fragment CoreImageBlockFragment on CoreImage { - attributes { - id - } - mediaDetails { - height - width - } - } - - query Post( $id: ID! ) { - post(id: $id, idType: DATABASE_ID) { - editorBlocks { - # databaseId - apiVersion - blockEditorCategoryName - clientId - cssClassNames - name - innerBlocks { - name - } - parentClientId - renderedHtml - ...CoreImageBlockFragment - } - } - } - '; + fragment CoreImageBlockFragment on CoreImage { + attributes { + id + } + mediaDetails { + height + width + } + } + + query Post( $id: ID! ) { + post(id: $id, idType: DATABASE_ID) { + editorBlocks { + # databaseId + apiVersion + blockEditorCategoryName + clientId + cssClassNames + name + innerBlocks { + name + } + parentClientId + renderedHtml + ...CoreImageBlockFragment + } + } + } + '; // Set post content. wp_update_post( @@ -136,62 +136,62 @@ public function test_retrieve_core_image_media_details(): void { public function test_retrieve_core_image_attributes(): void { $block_content = ' -
- - alt-text
- -
Align left
- '; +
+ + alt-text
+ +
Align left
+ '; $query = ' - fragment CoreImageBlockFragment on CoreImage { - attributes { - id - width - height - alt - align - src - style - sizeSlug - # lightbox # not supported yet - # aspectRatio # not supported yet - # scale # not supported yet - linkClass - linkTarget - linkDestination - borderColor - caption - className - cssClassName - url - rel - href - title - lock - anchor - } - } - - query Post( $id: ID! ) { - post(id: $id, idType: DATABASE_ID) { - databaseId - editorBlocks { - apiVersion - blockEditorCategoryName - clientId - cssClassNames - innerBlocks { - name - } - parentClientId - renderedHtml - name - ...CoreImageBlockFragment - } - } - } - '; + fragment CoreImageBlockFragment on CoreImage { + attributes { + id + width + height + alt + align + src + style + sizeSlug + # lightbox # not supported yet + # aspectRatio # not supported yet + # scale # not supported yet + linkClass + linkTarget + linkDestination + borderColor + caption + className + cssClassName + url + rel + href + title + lock + anchor + } + } + + query Post( $id: ID! ) { + post(id: $id, idType: DATABASE_ID) { + databaseId + editorBlocks { + apiVersion + blockEditorCategoryName + clientId + cssClassNames + innerBlocks { + name + } + parentClientId + renderedHtml + name + ...CoreImageBlockFragment + } + } + } + '; // Set post content. wp_update_post( @@ -259,7 +259,7 @@ className 'align' => 'left', 'caption' => 'Align left', 'className' => 'test-css-class-name', - 'cssClassName' => ( ! is_wp_version_compatible( '6.3' ) ) ? 'wp-duotone-varpresetduotonepurple-green-19 wp-block-image size-full is-resized wp-duotone-purple-green' : 'wp-block-image size-full is-resized wp-duotone-purple-green', // This uses the old class name for WP < 6.3 which is wp-duotone-varpresetduotonepurple-green-19. + 'cssClassName' => ( ! is_wp_version_compatible( '6.3' ) ) ? 'wp-duotone-varpresetduotonepurple-green-19 wp-block-image size-full is-resized wp-duotone-purple-green' : 'wp-block-image size-full is-resized', // This uses the old class name for WP < 6.3 which is wp-duotone-varpresetduotonepurple-green-19. 'url' => 'http://mysite.local/wp-content/uploads/2023/05/online-programming-course-hero-section-bg.svg', 'borderColor' => 'vivid-red', 'title' => 'test-title', From 44ebf8a7be9f25e36420462449282a56d58284db Mon Sep 17 00:00:00 2001 From: Ta5r Date: Thu, 26 Sep 2024 16:12:38 +0530 Subject: [PATCH 03/10] temp : fix typo --- tests/unit/CoreImageTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/CoreImageTest.php b/tests/unit/CoreImageTest.php index eab7b5a5..1381a396 100644 --- a/tests/unit/CoreImageTest.php +++ b/tests/unit/CoreImageTest.php @@ -259,7 +259,7 @@ className 'align' => 'left', 'caption' => 'Align left', 'className' => 'test-css-class-name', - 'cssClassName' => ( ! is_wp_version_compatible( '6.3' ) ) ? 'wp-duotone-varpresetduotonepurple-green-19 wp-block-image size-full is-resized wp-duotone-purple-green' : 'wp-block-image size-full is-resized', // This uses the old class name for WP < 6.3 which is wp-duotone-varpresetduotonepurple-green-19. + 'cssClassName' => ( ! is_wp_version_compatible( '6.3' ) ) ? 'wp-duotone-varpresetduotonepurple-green-19 wp-block-image size-full is-resized' : 'wp-block-image size-full is-resized wp-duotone-purple-green', // This uses the old class name for WP < 6.3 which is wp-duotone-varpresetduotonepurple-green-19. 'url' => 'http://mysite.local/wp-content/uploads/2023/05/online-programming-course-hero-section-bg.svg', 'borderColor' => 'vivid-red', 'title' => 'test-title', From 2e1a8d1a5735e3ca18b57cd0bef157c6cb543897 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Fri, 27 Sep 2024 13:45:16 +0530 Subject: [PATCH 04/10] chore : add a barebones test case and fix cssClassName assert --- tests/unit/CoreImageTest.php | 223 +++++++++++++++++++++-------------- 1 file changed, 134 insertions(+), 89 deletions(-) diff --git a/tests/unit/CoreImageTest.php b/tests/unit/CoreImageTest.php index 1381a396..d0ada042 100644 --- a/tests/unit/CoreImageTest.php +++ b/tests/unit/CoreImageTest.php @@ -44,45 +44,68 @@ public function tearDown(): void { \WPGraphQL::clear_schema(); } - public function test_retrieve_core_image_media_details(): void { - $block_content = ' -
- - alt-text
- -
Align left
- '; - - $query = ' + public function query() { + return ' fragment CoreImageBlockFragment on CoreImage { attributes { id - } - mediaDetails { - height width + height + alt + align + src + style + sizeSlug + # lightbox # not supported yet + # aspectRatio # not supported yet + # scale # not supported yet + linkClass + linkTarget + linkDestination + borderColor + caption + className + cssClassName + url + rel + href + title + lock + anchor } } - + query Post( $id: ID! ) { post(id: $id, idType: DATABASE_ID) { + databaseId editorBlocks { - # databaseId apiVersion blockEditorCategoryName clientId cssClassNames - name innerBlocks { name } parentClientId renderedHtml + name ...CoreImageBlockFragment } } } '; + } + + public function test_retrieve_core_image_fields_attributes(): void { + + $block_content = ' + +
+ +
+ '; + + $query = $this->query(); // Set post content. wp_update_post( @@ -98,95 +121,90 @@ public function test_retrieve_core_image_media_details(): void { ]; $actual = graphql( compact( 'query', 'variables' ) ); - error_log( print_r( $actual, true ) ); - // $actual = $actual['data']['post']; + + $node = $actual['data']['post']; + + // Verify that the ID of the first post matches the one we just created. + $this->assertEquals( $this->post_id, $node['databaseId'] ); + $this->assertEquals( 1, count( $node['editorBlocks'] ) ); + $this->assertEquals( 'core/image', $node['editorBlocks'][0]['name'] ); $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' ); - // @todo : fix - // $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); + $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); $block = $actual['data']['post']['editorBlocks'][0]; $this->assertNotEmpty( $block['apiVersion'], 'The apiVersion should be present' ); - $this->assertEquals( 'media', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be text' ); + $this->assertEquals( 'media', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be media' ); $this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); - - // @todo this is not working $this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' ); - $this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' ); $this->assertEquals( 'core/image', $block['name'], 'The block name should be core/image' ); $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); - $this->assertEquals( [ - 'width' => 50, - 'height' => 50, + 'align' => 'left', + 'id' => $this->attachment_id, + 'className' => 'test-css-class-name', + 'src' => 'http://mysite.local/wp-content/uploads/2023/05/online-programming-course-hero-section-bg.svg', + 'url' => 'http://mysite.local/wp-content/uploads/2023/05/online-programming-course-hero-section-bg.svg', + 'width' => null, + 'height' => null, + 'alt' => '', + 'style' => null, + 'sizeSlug' => null, + 'linkClass' => null, + 'linkTarget' => null, + 'linkDestination' => null, + 'borderColor' => null, + 'caption' => '', + 'cssClassName' => 'wp-block-image', + 'rel' => null, + 'href' => null, + 'title' => null, + 'lock' => null, + 'anchor' => null, ], - $block['mediaDetails'] + $node['editorBlocks'][0]['attributes'] ); } - public function test_retrieve_core_image_attributes(): void { - - $block_content = ' -
- - alt-text
- -
Align left
- '; + public function test_retrieve_core_image_media_details(): void { + $block_content = ' + +
+ + alt-text
+ +
Align left
+ '; $query = ' fragment CoreImageBlockFragment on CoreImage { attributes { id - width + } + mediaDetails { height - alt - align - src - style - sizeSlug - # lightbox # not supported yet - # aspectRatio # not supported yet - # scale # not supported yet - linkClass - linkTarget - linkDestination - borderColor - caption - className - cssClassName - url - rel - href - title - lock - anchor + width } } - + query Post( $id: ID! ) { post(id: $id, idType: DATABASE_ID) { - databaseId editorBlocks { apiVersion blockEditorCategoryName - clientId - cssClassNames + name innerBlocks { name } - parentClientId - renderedHtml - name ...CoreImageBlockFragment } } @@ -208,36 +226,64 @@ className $actual = graphql( compact( 'query', 'variables' ) ); - error_log( print_r( $actual, true ) ); - $node = $actual['data']['post']; - - // 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( 1, count( $node['editorBlocks'] ) ); - $this->assertEquals( 'core/image', $node['editorBlocks'][0]['name'] ); - $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' ); - $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); + $block = $actual['data']['post']['editorBlocks'][0]; - $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); + $this->assertEquals( + [ + 'width' => 50, + 'height' => 50, + ], + $block['mediaDetails'] + ); + } - $block = $actual['data']['post']['editorBlocks'][0]; + public function test_retrieve_core_image_attributes(): void { - $this->assertNotEmpty( $block['apiVersion'], 'The apiVersion should be present' ); - $this->assertEquals( 'media', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be media' ); - $this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); + $block_content = ' + +
+ + alt-text
+ +
Align left
+ '; - // @todo this is not working - // $this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' ); + $query = $this->query(); + + // Set post content. + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $query = $query; + $variables = [ + 'id' => $this->post_id, + ]; + + $actual = graphql( compact( 'query', 'variables' ) ); + + // error_log( print_r( $actual, true ) ); + $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' ); + + $block = $actual['data']['post']['editorBlocks'][0]; + + // WordPress 6.4+ adds layout styles, so `cssClassName` needs to be checked separately. + $this->assertStringContainsString( 'wp-block-image', $block['attributes']['cssClassName'] ); + $this->assertStringContainsString( 'size-full', $block['attributes']['cssClassName'] ); + $this->assertStringContainsString( 'is-resized', $block['attributes']['cssClassName'] ); + unset( $block['attributes']['cssClassName'] ); - $this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' ); - $this->assertEquals( 'core/image', $block['name'], 'The block name should be core/image' ); - $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); - $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); $this->assertEquals( [ 'width' => '500', @@ -259,7 +305,6 @@ className 'align' => 'left', 'caption' => 'Align left', 'className' => 'test-css-class-name', - 'cssClassName' => ( ! is_wp_version_compatible( '6.3' ) ) ? 'wp-duotone-varpresetduotonepurple-green-19 wp-block-image size-full is-resized' : 'wp-block-image size-full is-resized wp-duotone-purple-green', // This uses the old class name for WP < 6.3 which is wp-duotone-varpresetduotonepurple-green-19. 'url' => 'http://mysite.local/wp-content/uploads/2023/05/online-programming-course-hero-section-bg.svg', 'borderColor' => 'vivid-red', 'title' => 'test-title', @@ -275,7 +320,7 @@ className 'href' => 'http://decoupled.local/dcf-1-0/', ], - $node['editorBlocks'][0]['attributes'] + $block['attributes'] ); } } From 3bf0950c4406b7736581b55899e368998a123f7a Mon Sep 17 00:00:00 2001 From: Ta5r Date: Fri, 27 Sep 2024 17:47:01 +0530 Subject: [PATCH 05/10] chore : comments and cleanup --- tests/unit/CoreImageTest.php | 63 +++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/tests/unit/CoreImageTest.php b/tests/unit/CoreImageTest.php index d0ada042..23a011df 100644 --- a/tests/unit/CoreImageTest.php +++ b/tests/unit/CoreImageTest.php @@ -96,8 +96,22 @@ className '; } + /** + * Test that the CoreImage block is retrieved correctly. + * + * Covers the following attributes: + * - apiVersion + * - blockEditorCategoryName + * - clientId + * - cssClassNames + * - innerBlocks + * - name + * - parentClientId + * - renderedHtml + * - attributes + * + */ public function test_retrieve_core_image_fields_attributes(): void { - $block_content = '
@@ -107,7 +121,7 @@ public function test_retrieve_core_image_fields_attributes(): void { $query = $this->query(); - // Set post content. + // Update the post content with the block content. wp_update_post( [ 'ID' => $this->post_id, @@ -175,6 +189,14 @@ 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 = ' @@ -211,7 +233,7 @@ public function test_retrieve_core_image_media_details(): void { } '; - // Set post content. + // Update the post content with the block content. wp_update_post( [ 'ID' => $this->post_id, @@ -234,13 +256,39 @@ public function test_retrieve_core_image_media_details(): void { $this->assertEquals( [ - 'width' => 50, - 'height' => 50, + 'width' => 50, // Previously untested. + 'height' => 50, // Previously untested. ], $block['mediaDetails'] ); } + /** + * Test that the CoreImage block attributes are retrieved correctly. + * + * Covers the following attributes: + * - width + * - height + * - alt + * - id + * - src + * - style + * - sizeSlug + * - linkClass + * - linkTarget + * - linkDestination + * - align + * - caption + * - className + * - url + * - borderColor + * - title + * - lock + * - anchor + * - rel + * - href + * + */ public function test_retrieve_core_image_attributes(): void { $block_content = ' @@ -254,7 +302,7 @@ public function test_retrieve_core_image_attributes(): void { $query = $this->query(); - // Set post content. + // Update the post content with the block content. wp_update_post( [ 'ID' => $this->post_id, @@ -269,7 +317,6 @@ public function test_retrieve_core_image_attributes(): void { $actual = graphql( compact( 'query', 'variables' ) ); - // error_log( print_r( $actual, true ) ); $node = $actual['data']['post']; $this->assertArrayNotHasKey( 'errors', $actual, 'There should not be any errors' ); @@ -284,7 +331,7 @@ public function test_retrieve_core_image_attributes(): void { $this->assertStringContainsString( 'is-resized', $block['attributes']['cssClassName'] ); unset( $block['attributes']['cssClassName'] ); - $this->assertEquals( + $this->assertEquals( // Previously untested. [ 'width' => '500', 'height' => 500.0, From d1ec4fc9c7b6fdc31b3712e63e95511ffd8b3c71 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Tue, 1 Oct 2024 11:59:26 +0530 Subject: [PATCH 06/10] fix : tearDown action before `parent::tearTown()` --- tests/unit/CoreImageTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/unit/CoreImageTest.php b/tests/unit/CoreImageTest.php index 23a011df..c9d97aec 100644 --- a/tests/unit/CoreImageTest.php +++ b/tests/unit/CoreImageTest.php @@ -38,10 +38,9 @@ public function setUp(): void { public function tearDown(): void { // your tear down methods here wp_delete_post( $this->post_id, true ); + \WPGraphQL::clear_schema(); parent::tearDown(); - - \WPGraphQL::clear_schema(); } public function query() { From c4b2d94109105364509bf8b3dfa5b0b17094b155 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Tue, 1 Oct 2024 15:46:56 +0530 Subject: [PATCH 07/10] fix : test , and attributes with their respective supported WordPress versions. --- tests/unit/CoreImageTest.php | 133 +++++++++++++++++++++++++++++++---- 1 file changed, 119 insertions(+), 14 deletions(-) diff --git a/tests/unit/CoreImageTest.php b/tests/unit/CoreImageTest.php index c9d97aec..6215b858 100644 --- a/tests/unit/CoreImageTest.php +++ b/tests/unit/CoreImageTest.php @@ -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 { @@ -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 @@ -97,7 +100,7 @@ className /** * Test that the CoreImage block is retrieved correctly. - * + * * Covers the following attributes: * - apiVersion * - blockEditorCategoryName @@ -108,7 +111,6 @@ className * - parentClientId * - renderedHtml * - attributes - * */ public function test_retrieve_core_image_fields_attributes(): void { $block_content = ' @@ -128,7 +130,6 @@ public function test_retrieve_core_image_fields_attributes(): void { ] ); - $query = $query; $variables = [ 'id' => $this->post_id, ]; @@ -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 = ' @@ -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 @@ -286,7 +286,6 @@ public function test_retrieve_core_image_media_details(): void { * - anchor * - rel * - href - * */ public function test_retrieve_core_image_attributes(): void { @@ -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' ); @@ -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 = ' + +
+ + alt-text
+ +
Align left
+ '; + + // 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'] + ); + } + } } From a53dcb406c0c117406dbf45237bbb255a58f6e06 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Tue, 1 Oct 2024 16:06:19 +0530 Subject: [PATCH 08/10] chore : remove unused vars and params. --- tests/unit/CoreImageTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/unit/CoreImageTest.php b/tests/unit/CoreImageTest.php index 6215b858..e9ef21d7 100644 --- a/tests/unit/CoreImageTest.php +++ b/tests/unit/CoreImageTest.php @@ -3,7 +3,6 @@ namespace WPGraphQL\ContentBlocks\Unit; final class CoreImageTest extends PluginTestCase { - public $instance; /** * The ID of the post created for the test. @@ -48,7 +47,7 @@ public function tearDown(): void { * * @param string $attributes The attributes to add to query. */ - public function query( $attributes = '' ): string { + public function query(): string { return ' fragment CoreImageBlockFragment on CoreImage { attributes { @@ -60,7 +59,6 @@ public function query( $attributes = '' ): string { src style sizeSlug - ' . $attributes . ' linkClass linkTarget linkDestination From 92057d3a21d5441c5e6855a101cc8d2f791ce3d5 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Tue, 1 Oct 2024 16:15:29 +0530 Subject: [PATCH 09/10] fix : split tests for WP6.3 and WP6.4+ --- tests/unit/CoreImageTest.php | 154 +++++++++++++++++++++-------------- 1 file changed, 93 insertions(+), 61 deletions(-) diff --git a/tests/unit/CoreImageTest.php b/tests/unit/CoreImageTest.php index e9ef21d7..35236b1c 100644 --- a/tests/unit/CoreImageTest.php +++ b/tests/unit/CoreImageTest.php @@ -370,9 +370,13 @@ public function test_retrieve_core_image_attributes(): void { * Covers the following attributes: * - aspectRatio * - scale - * - lightbox */ - public function test_retrieve_core_untested_attributes(): void { + public function test_retrieve_core_aspectratio_scale_attributes(): void { + // `aspectRatio` and `scale` are only supported in WP 6.3+. + if ( ! is_wp_version_compatible( '6.3' ) ) { + $this->markTestSkipped( 'The aspectRatio and scale attributes are only supported in WP 6.3+' ); + } + $block_content = '
@@ -394,82 +398,110 @@ public function test_retrieve_core_untested_attributes(): void { '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 = ' + fragment CoreImageBlockFragment on CoreImage { + attributes { + aspectRatio + scale } + } - query Post( $id: ID! ) { - post(id: $id, idType: DATABASE_ID) { - databaseId - editorBlocks { - name - ...CoreImageBlockFragment - } + query Post( $id: ID! ) { + post(id: $id, idType: DATABASE_ID) { + databaseId + editorBlocks { + name + ...CoreImageBlockFragment } - }'; + } + }'; - $actual = graphql( compact( 'query', 'variables' ) ); + $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' ); - $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]; - $block = $actual['data']['post']['editorBlocks'][0]; + $this->assertEquals( + [ + 'aspectRatio' => '4/3', // Previously untested. + 'scale' => 'cover', // Previously untested. - $this->assertEquals( - [ - 'aspectRatio' => '4/3', // Previously untested. - 'scale' => 'cover', // Previously untested. + ], + $block['attributes'] + ); + } - ], - $block['attributes'] - ); + /** + * Test that the CoreImage block previously untested attributes are retrieved correctly. + * + * Covers the following attributes: + * - lightbox + */ + public function test_retrieve_core_lightbox_attribute(): void { + // `lightbox` is only supported in WP 6.4+. + if ( ! is_wp_version_compatible( '6.4' ) ) { + $this->markTestSkipped( 'The lightbox attribute is only supported in WP 6.4+' ); } - // `lightbox` is only supported in WP 6.4+. - if ( is_wp_version_compatible( '6.4' ) ) { - $query = ' - fragment CoreImageBlockFragment on CoreImage { - attributes { - lightbox - } + $block_content = ' + +
+ + alt-text
+ +
Align left
+ '; + + // Update the post content with the block content. + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $variables = [ + 'id' => $this->post_id, + ]; + + $query = ' + fragment CoreImageBlockFragment on CoreImage { + attributes { + lightbox } + } - query Post( $id: ID! ) { - post(id: $id, idType: DATABASE_ID) { - databaseId - editorBlocks { - name - ...CoreImageBlockFragment - } + query Post( $id: ID! ) { + post(id: $id, idType: DATABASE_ID) { + databaseId + editorBlocks { + name + ...CoreImageBlockFragment } - }'; + } + }'; - $actual = graphql( compact( 'query', 'variables' ) ); + $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' ); + $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]; + $block = $actual['data']['post']['editorBlocks'][0]; - $this->assertEquals( - [ - 'lightbox' => wp_json_encode( // Previously untested. - [ - 'enabled' => false, - ] - ), + $this->assertEquals( + [ + 'lightbox' => wp_json_encode( // Previously untested. + [ + 'enabled' => false, + ] + ), - ], - $block['attributes'] - ); - } + ], + $block['attributes'] + ); } } From 3b32f069a1d05cb78e7e1cd34479ebb0ff46c686 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Tue, 1 Oct 2024 16:29:34 +0530 Subject: [PATCH 10/10] chore : Added changeset --- .changeset/fair-scissors-teach.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/fair-scissors-teach.md diff --git a/.changeset/fair-scissors-teach.md b/.changeset/fair-scissors-teach.md new file mode 100644 index 00000000..8bd1ad48 --- /dev/null +++ b/.changeset/fair-scissors-teach.md @@ -0,0 +1,5 @@ +--- +"@wpengine/wp-graphql-content-blocks": patch +--- + +tests : Backfill tests for Core Image block.