From cdec9ee302ae34473c3f7eae5f5636b867732dfa Mon Sep 17 00:00:00 2001 From: Ashutosh Gautam Date: Wed, 18 Sep 2024 20:04:25 +0530 Subject: [PATCH] feat: Add tests for additional snippets - cover the attributes that are null --- tests/unit/CoreHeadingTest.php | 223 ++++++++++++++++++++++++++++++--- 1 file changed, 206 insertions(+), 17 deletions(-) diff --git a/tests/unit/CoreHeadingTest.php b/tests/unit/CoreHeadingTest.php index 3ca5e058..d8f03259 100644 --- a/tests/unit/CoreHeadingTest.php +++ b/tests/unit/CoreHeadingTest.php @@ -49,7 +49,6 @@ className textColor } } - query Post( $id: ID! ) { post(id: $id, idType: DATABASE_ID) { databaseId @@ -82,7 +81,6 @@ public function test_retrieve_core_heading_attributes() { '; - // Update the post content with the block content. wp_update_post( [ 'ID' => $this->post_id, @@ -95,34 +93,31 @@ public function test_retrieve_core_heading_attributes() { 'id' => $this->post_id, ]; - // Test the query. - $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' ); - // Verify that the ID of the first post matches the one we just created. $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); - // There should be only one block using that query when not using flat: true $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); - // Verify the block data. - $this->assertNotEmpty( $actual['data']['post']['editorBlocks'][0]['apiVersion'], 'The apiVersion should be present' ); - $this->assertEquals( 'text', $actual['data']['post']['editorBlocks'][0]['blockEditorCategoryName'], 'The blockEditorCategoryName should be text' ); - $this->assertNotEmpty( $actual['data']['post']['editorBlocks'][0]['clientId'], 'The clientId should be present' ); + $block = $actual['data']['post']['editorBlocks'][0]; + + $this->assertNotEmpty( $block['apiVersion'], 'The apiVersion should be present' ); + $this->assertEquals( 'text', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be text' ); + $this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); // @todo this is not working - // $this->assertNotEmpty( $actual['data']['post']['editorBlocks'][0]['cssClassNames'], 'The cssClassNames should be present' ); + // $this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' ); - $this->assertEmpty( $actual['data']['post']['editorBlocks'][0]['innerBlocks'], 'There should be no inner blocks' ); - $this->assertEquals( 'core/heading', $actual['data']['post']['editorBlocks'][0]['name'], 'The block name should be core/heading' ); - $this->assertEmpty( $actual['data']['post']['editorBlocks'][0]['parentClientId'], 'There should be no parentClientId' ); - $this->assertNotEmpty( $actual['data']['post']['editorBlocks'][0]['renderedHtml'], 'The renderedHtml should be present' ); + $this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' ); + $this->assertEquals( 'core/heading', $block['name'], 'The block name should be core/heading' ); + $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); + $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); - // Verify the attributes. + $attributes = $block['attributes']; $this->assertEquals( [ 'align' => null, @@ -149,7 +144,201 @@ public function test_retrieve_core_heading_attributes() { 'textAlign' => 'center', 'textColor' => null, ], - $actual['data']['post']['editorBlocks'][0]['attributes'], + $attributes, + ); + } + + public function test_retrieve_core_heading_with_colors_and_alignment() { + $block_content = ' + +

Colored Heading

+ + '; + + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $actual = graphql([ + 'query' => $this->query(), + 'variables' => ['id' => $this->post_id], + ]); + + $block = $actual['data']['post']['editorBlocks'][0]; + $attributes = $block['attributes']; + + $this->assertEquals( 'Colored Heading', $attributes['content'] ); + $this->assertEquals( 3.0, $attributes['level'] ); + $this->assertEquals( 'right', $attributes['textAlign'] ); + $this->assertEquals( 'wide', $attributes['align'] ); + + $style = json_decode($attributes['style'], true); + $this->assertEquals('#cf2e2e', $style['color']['background']); + $this->assertEquals('#ffffff', $style['color']['text']); + } + + public function test_retrieve_core_heading_with_font_and_anchor() { + $block_content = ' + +

Custom Font Heading

+ + '; + + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $actual = graphql([ + 'query' => $this->query(), + 'variables' => ['id' => $this->post_id], + ]); + + $block = $actual['data']['post']['editorBlocks'][0]; + $attributes = $block['attributes']; + + $this->assertEquals( 'Custom Font Heading', $attributes['content'] ); + $this->assertEquals( 'custom-id', $attributes['anchor'] ); + + $style = json_decode($attributes['style'], true); + $this->assertEquals('Arial', $style['typography']['fontFamily']); + $this->assertEquals('32px', $style['typography']['fontSize']); + } + + public function test_retrieve_core_heading_with_gradient() { + $block_content = ' + +

Gradient Heading

+ + '; + + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $actual = graphql([ + 'query' => $this->query(), + 'variables' => ['id' => $this->post_id], + ]); + + $block = $actual['data']['post']['editorBlocks'][0]; + $attributes = $block['attributes']; + + $this->assertEquals( 'Gradient Heading', $attributes['content'] ); + + $style = json_decode($attributes['style'], true); + $this->assertEquals('linear-gradient(135deg,rgb(6,147,227) 0%,rgb(155,81,224) 100%)', $style['color']['gradient']); + } + + public function test_retrieve_core_heading_with_background_color() { + $block_content = ' + +

Heading with Background Color

+ +

Heading with Text Color

+ +

Heading with Font Size

+ +

Heading with Custom Class

+