Skip to content

Commit

Permalink
refactor : Core list item tests moved to core list test, fixed format…
Browse files Browse the repository at this point in the history
…ting.
  • Loading branch information
Ta5r committed Sep 23, 2024
1 parent c9da6c6 commit 5c36c62
Showing 1 changed file with 215 additions and 13 deletions.
228 changes: 215 additions & 13 deletions tests/unit/CoreListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
[
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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(
[
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = '
<!-- wp:list -->
<ul class="wp-block-list">
<!-- wp:list-item {"lock":{"move":true,"remove":true},"className":"test-css-class-item-1","fontSize":"large","fontFamily":"heading"} -->
<li class="test-css-class-item-1 has-heading-font-family has-large-font-size">List item 1</li>
<!-- /wp:list-item -->
</ul>
<!-- /wp:list -->
';

// 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 = '
<!-- wp:list -->
<ul class="wp-block-list">
<!-- wp:list-item {"style":{"typography":{"textDecoration":"underline"}}} -->
<li style="text-decoration:underline">List Item 2</li>
<!-- /wp:list-item -->
</ul>
<!-- /wp:list -->
';

// 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'],
);
}
}

0 comments on commit 5c36c62

Please sign in to comment.