From 5c36c626c699b975770403694648cd57ff56561f Mon Sep 17 00:00:00 2001 From: Ta5r Date: Tue, 24 Sep 2024 01:24:49 +0530 Subject: [PATCH] refactor : Core list item tests moved to core list test, fixed formatting. --- tests/unit/CoreListTest.php | 228 ++++++++++++++++++++++++++++++++++-- 1 file changed, 215 insertions(+), 13 deletions(-) diff --git a/tests/unit/CoreListTest.php b/tests/unit/CoreListTest.php index 55ff7345..366355e3 100644 --- a/tests/unit/CoreListTest.php +++ b/tests/unit/CoreListTest.php @@ -56,16 +56,38 @@ className } } + fragment CoreListItemBlockFragment on CoreListItem { + attributes { + className + content + fontFamily + fontSize + lock + # metadata + placeholder + style + } + } + query Post( $id: ID! ) { post(id: $id, idType: DATABASE_ID) { databaseId - editorBlocks { + editorBlocks( flat: false ) { apiVersion blockEditorCategoryName clientId cssClassNames innerBlocks { + apiVersion + blockEditorCategoryName + clientId + cssClassNames name + parentClientId + renderedHtml + ... on CoreListItem { + ...CoreListItemBlockFragment + } } isDynamic name @@ -111,18 +133,22 @@ public function test_retrieve_core_list_fields_and_attribute(): void { $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( 3, count( $actual['data']['post']['editorBlocks'] ) ); + + $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( 'text', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be text' ); - $this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); - $this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' ); - $this->assertNotEmpty( $block['innerBlocks'], 'There should be ListItem inner blocks' ); - $this->assertEquals( 'core/list', $block['name'], 'The block name should be core/list' ); - $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); - $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); + + $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' ); + $this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' ); + + $this->assertNotEmpty( $block['innerBlocks'], 'There should be ListItem inner blocks' ); + $this->assertEquals( 'core/list', $block['name'], 'The block name should be core/list' ); + $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); + $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); $this->assertEquals( [ @@ -180,9 +206,24 @@ public function test_retrieve_core_list_attributes_typography_and_lock(): void { $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( 'text', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be text' ); + $this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); + + // @todo : his is not working. + // $this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' ); + + $this->assertNotEmpty( $block['innerBlocks'], 'There should be no inner blocks' ); + $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); + $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); + $this->assertEquals( 'core/list', $block['name'], 'The block name should be core/list' ); $this->assertEquals( @@ -210,7 +251,7 @@ public function test_retrieve_core_list_attributes_typography_and_lock(): void { /** * Test case for retrieving core list block fields and attributes. - * + * * Covers : 'ordered' and 'reversed'. * * @todo : The 'placeholder' attribute is not tested as it is getting returned as null. @@ -244,11 +285,24 @@ public function test_retrieve_core_list_attributes_ordered_and_reversed(): void $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( 4, count( $actual['data']['post']['editorBlocks'] ) ); + + $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( 'text', $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->assertNotEmpty( $block['innerBlocks'], 'There should be no inner blocks' ); $this->assertEquals( 'core/list', $block['name'], 'The block name should be core/list' ); + $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); + $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); $this->assertEquals( [ @@ -306,12 +360,25 @@ public function test_retrieve_core_list_attributes_start_and_styles(): void { $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( 3, count( $actual['data']['post']['editorBlocks'] ) ); + + $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( 'text', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be text' ); + $this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); + $this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' ); + $this->assertNotEmpty( $block['innerBlocks'], 'There should be no inner blocks' ); $this->assertEquals( 'core/list', $block['name'], 'The block name should be core/list' ); + // @todo : The 'parentClientId' attribute is NULL in the response. + // $this->assertNotEmpty( $block['parentClientId'], 'There should be no parentClientId' ); + + $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); + $this->assertEquals( [ 'anchor' => null, @@ -348,4 +415,139 @@ public function test_retrieve_core_list_attributes_start_and_styles(): void { $block['attributes'], ); } + + /** + * Test case for retrieving core list block fields and attributes. + * + * Covers : 'className', 'content', 'fontSize', 'fontFamily' and 'lock' attributes. + */ + public function test_retrieve_core_list_item_fields_and_attribute(): void { + $block_content = ' + + + + '; + + // Set post content. + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $query = $this->query(); + $variables = [ + 'id' => $this->post_id, + ]; + + $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->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]['innerBlocks'][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' ); + $this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' ); + $this->assertEquals( 'core/list-item', $block['name'], 'The block name should be core/list' ); + + // @todo : The 'parentClientId' attribute is NULL in the response. + // $this->assertNotEmpty( $block['parentClientId'], 'There should be some parentClientId for the block' ); + + $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); + $this->assertEquals( + [ + 'className' => 'test-css-class-item-1', + 'content' => 'List item 1', + 'fontFamily' => 'heading', + 'fontSize' => 'large', + 'lock' => wp_json_encode( + [ + 'move' => true, + 'remove' => true, + ] + ), + 'placeholder' => null, + 'style' => null, + ], + $block['attributes'], + ); + } + + /** + * Test case for retrieving core list item block fields and attributes. + * + * Covers : 'placeholder' and 'style' attribute. + */ + public function test_retrieve_core_list_item_untested_attributes(): void { + $block_content = ' + + + + '; + + // Set post content. + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $query = $this->query(); + $variables = [ + 'id' => $this->post_id, + ]; + + $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->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]['innerBlocks'][0]; + + $this->assertNotEmpty( $block['apiVersion'], 'The apiVersion should be present' ); + $this->assertEquals( 'text', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be text' ); + $this->assertEmpty( $block['cssClassNames'], 'The cssClassNames should be present' ); + $this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); + $this->assertEquals( 'core/list-item', $block['name'], 'The block name should be core/list' ); + + // @todo : The 'parentClientId' attribute is NULL in the response. + // $this->assertNotEmpty( $block['parentClientId'], 'There should be some parentClientId for the block' ); + + $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); + + $this->assertEquals( + [ + 'className' => null, + 'content' => 'List Item 2', + 'fontFamily' => null, + 'fontSize' => null, + 'lock' => null, + 'placeholder' => null, // Previously untested. + 'style' => wp_json_encode( [ 'typography' => [ 'textDecoration' => 'underline' ] ] ), // Previously untested. + ], + $block['attributes'], + ); + } }