-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Added cssClassName attribute to CoreGroup #328
Merged
colinmurphy
merged 4 commits into
main
from
feature-add-css-classname-to-core-group-attributes
Jan 14, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e4b826d
Added CoreGroup block to add cssClassName to the CoreGroupAttributes
colinmurphy 05c8e35
Fixed tests. Issue with asserting equals on cssClassName as it varies…
colinmurphy c027d13
Update tests/unit/CoreGroupTest.php
colinmurphy c0528c6
Create proud-fireants-buy.md
colinmurphy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@wpengine/wp-graphql-content-blocks": patch | ||
--- | ||
|
||
feat: Added a `CoreGroup` block class to fix an issue with a missing attribute `cssClassName` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
/** | ||
* Core Group Block | ||
* | ||
* @package WPGraphQL\ContentBlocks\Blocks | ||
*/ | ||
|
||
namespace WPGraphQL\ContentBlocks\Blocks; | ||
|
||
/** | ||
* Class CoreGroup | ||
*/ | ||
class CoreGroup extends Block { | ||
/** | ||
* {@inheritDoc} | ||
* | ||
* Note that no selector is set as it can be a variety of selectors | ||
* | ||
* @var array|null | ||
*/ | ||
protected ?array $additional_block_attributes = [ | ||
'cssClassName' => [ | ||
'type' => 'string', | ||
'source' => 'attribute', | ||
'attribute' => 'class', | ||
], | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
<?php | ||
|
||
namespace WPGraphQL\ContentBlocks\Unit; | ||
|
||
/** | ||
* @group block | ||
* @group core-group | ||
*/ | ||
final class CoreGroupTest extends PluginTestCase { | ||
|
||
/** | ||
* 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 { | ||
parent::setUp(); | ||
|
||
$this->post_id = wp_insert_post( | ||
[ | ||
'post_title' => 'Post Title', | ||
'post_content' => '', | ||
'post_status' => 'publish', | ||
] | ||
); | ||
|
||
\WPGraphQL::clear_schema(); | ||
} | ||
|
||
public function tearDown(): void { | ||
// your tear down methods here | ||
wp_delete_post( $this->post_id, true ); | ||
\WPGraphQL::clear_schema(); | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
/** | ||
* Get the query for the CoreGroup block. | ||
* | ||
* @param string $attributes The attributes to add to query. | ||
*/ | ||
public function query(): string { | ||
return ' | ||
fragment CoreGroupFragment on CoreGroup { | ||
attributes { | ||
align | ||
backgroundColor | ||
borderColor | ||
className | ||
cssClassName | ||
fontFamily | ||
fontSize | ||
gradient | ||
layout | ||
lock | ||
style | ||
tagName | ||
textColor | ||
} | ||
} | ||
|
||
query Post($id: ID!) { | ||
post(id: $id, idType: DATABASE_ID) { | ||
databaseId | ||
editorBlocks(flat:true) { | ||
apiVersion | ||
blockEditorCategoryName | ||
clientId | ||
cssClassNames | ||
innerBlocks { | ||
name | ||
} | ||
name | ||
parentClientId | ||
renderedHtml | ||
type | ||
...CoreGroupFragment | ||
} | ||
} | ||
} | ||
'; | ||
} | ||
|
||
/** | ||
* Test that the CoreGroup block is retrieved correctly. | ||
* | ||
* Covers the following attributes: | ||
* - apiVersion | ||
* - blockEditorCategoryName | ||
* - clientId | ||
* - cssClassNames | ||
* - innerBlocks | ||
* - name | ||
* - parentClientId | ||
* - renderedHtml | ||
* - type | ||
* - attributes | ||
*/ | ||
|
||
public function test_retrieve_core_group_fields_attributes(): void { | ||
$block_content = <<<HTML | ||
<!-- wp:group {"tagName":"header","className":"test-group-class is-style-default","layout":{"type":"flex","flexWrap":"wrap","justifyContent":"center"}} --> | ||
<header id="test-group-id" class="wp-block-group test-group-class is-style-default"><!-- wp:paragraph --> | ||
<p>This is the left paragraph.</p> | ||
<!-- /wp:paragraph --> | ||
|
||
<!-- wp:paragraph --> | ||
<p>This is the right paragraph</p> | ||
<!-- /wp:paragraph --></header> | ||
<!-- /wp:group --> | ||
|
||
<!-- wp:paragraph {"className":"example-class"} --> | ||
<p class="example-class">This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:</p> | ||
<!-- /wp:paragraph --> | ||
HTML; | ||
|
||
$query = $this->query(); | ||
|
||
// Update the post content with the block content. | ||
wp_update_post( | ||
[ | ||
'ID' => $this->post_id, | ||
'post_content' => $block_content, | ||
] | ||
); | ||
|
||
$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' ); | ||
$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'], 'The post ID should match' ); | ||
$this->assertEquals( 'core/group', $node['editorBlocks'][0]['name'], 'The block name should match core/group' ); | ||
$this->assertEquals( 'CoreGroup', $node['editorBlocks'][0]['type'], 'The block type should match CoreGroup' ); | ||
$this->assertNotEmpty( $node['editorBlocks'], 'The node should have an array with the key editorBlocks which is not empty' ); | ||
|
||
// Check Block nodes | ||
$block = $node['editorBlocks'][0]; | ||
$this->assertNotEmpty( $block['apiVersion'], 'The apiVersion should be present' ); | ||
$this->assertEquals( 'design', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be media' ); | ||
$this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); | ||
$this->assertNotEmpty( $block['cssClassNames'], 'The cssClassNames should be present' ); | ||
$this->assertNotEmpty( $block['innerBlocks'], 'The innerBlocks should be an array' ); | ||
$this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); | ||
$this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); | ||
|
||
// Check child blocks | ||
$clientId = $block['clientId']; | ||
$childBlock1 = $node['editorBlocks'][1]; | ||
$this->assertNotEmpty( $childBlock1, 'Child block 1 should be present' ); | ||
$this->assertEquals( $clientId, $childBlock1['parentClientId'], 'Child block 1 parentClientId should match the parent block clientId' ); | ||
$this->assertEquals('core/paragraph', $childBlock1['name'], 'Child block 1 should be a core/paragraph' ); | ||
|
||
$childBlock2 = $node['editorBlocks'][1]; | ||
$this->assertNotEmpty( $childBlock2, 'Child block 2 should be present' ); | ||
$this->assertEquals( $clientId, $childBlock1['parentClientId'], 'Child block 2 parentClientId should match the parent block clientId' ); | ||
$this->assertEquals('core/paragraph', $childBlock1['name'], 'Child block 2 should be a core/paragraph' ); | ||
|
||
// Check attributes | ||
$blockAttributes = $node['editorBlocks'][0]['attributes']; | ||
$this->assertEquals(null, $blockAttributes['align']); | ||
$this->assertEquals(null, $blockAttributes['backgroundColor']); | ||
$this->assertEquals(null, $blockAttributes['borderColor']); | ||
$this->assertEquals('test-group-class is-style-default', $blockAttributes['className']); | ||
$this->assertStringContainsString('wp-block-group test-group-class is-style-default', $blockAttributes['cssClassName']); // Class name varies slightly between WP versions | ||
$this->assertEquals(null, $blockAttributes['fontFamily']); | ||
$this->assertEquals(null, $blockAttributes['fontSize']); | ||
$this->assertEquals("{\"type\":\"flex\",\"flexWrap\":\"wrap\",\"justifyContent\":\"center\"}", $blockAttributes['layout']); | ||
$this->assertEquals(null, $blockAttributes['style']); | ||
$this->assertEquals('header', $blockAttributes['tagName']); | ||
$this->assertEquals(null, $blockAttributes['textColor']); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(this isn't a list of attributes.
Not saying you need one - we added it to track what Core*Attributes are actually being tested since this project doesn't have accurate codecov )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @justlevine
I will update the comment and that's good to know about the code coverage.
Also thanks for the code review 👍