From 2e1a8d1a5735e3ca18b57cd0bef157c6cb543897 Mon Sep 17 00:00:00 2001 From: Ta5r Date: Fri, 27 Sep 2024 13:45:16 +0530 Subject: [PATCH] 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'] ); } }