Skip to content

Commit

Permalink
fix: Remove align attribute from main query
Browse files Browse the repository at this point in the history
- Align attribute is only supported in WP 6.3+
- Create separate test for align attribute
  • Loading branch information
ashutoshgautams committed Sep 24, 2024
1 parent d5fa769 commit 71d97fc
Showing 1 changed file with 83 additions and 8 deletions.
91 changes: 83 additions & 8 deletions tests/unit/CoreCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function query(): string {
return '
fragment CoreCodeBlockFragment on CoreCode {
attributes {
align
anchor
backgroundColor
borderColor
Expand Down Expand Up @@ -123,7 +122,6 @@ public function test_retrieve_core_code_attributes() {
];

$actual = graphql( compact( 'query', 'variables' ) );
error_log( print_r( $actual, true ) );

$this->assertArrayNotHasKey( 'errors', $actual, 'There should not be any errors' );
$this->assertArrayHasKey( 'data', $actual, 'The data key should be present' );
Expand All @@ -147,7 +145,6 @@ public function test_retrieve_core_code_attributes() {
$attributes = $block['attributes'];
$this->assertEquals(
[
'align' => null,
'anchor' => null,
'backgroundColor' => 'pale-cyan-blue', // previously untested
'borderColor' => null,
Expand All @@ -172,7 +169,6 @@ public function test_retrieve_core_code_attributes() {
* - borderColor
* - style
* - className
* - align
*
* @return void
*/
Expand All @@ -196,7 +192,6 @@ public function test_retrieve_core_code_with_custom_styles() {
];

$actual = graphql( compact( 'query', 'variables' ) );
error_log( print_r( $actual, true ) );

$this->assertArrayNotHasKey( 'errors', $actual, 'There should not be any errors' );
$this->assertArrayHasKey( 'data', $actual, 'The data key should be present' );
Expand All @@ -213,7 +208,6 @@ public function test_retrieve_core_code_with_custom_styles() {
$attributes = $block['attributes'];
$this->assertEquals(
[
'align' => 'wide', // previously untested
'anchor' => null,
'backgroundColor' => null,
'borderColor' => 'vivid-cyan-blue', // previously untested
Expand Down Expand Up @@ -275,7 +269,6 @@ public function test_retrieve_core_code_with_gradient_and_additional_attributes(
];

$actual = graphql( compact( 'query', 'variables' ) );
error_log( print_r( $actual, true ) );

$this->assertArrayNotHasKey( 'errors', $actual, 'There should not be any errors' );
$this->assertArrayHasKey( 'data', $actual, 'The data key should be present' );
Expand All @@ -292,7 +285,6 @@ public function test_retrieve_core_code_with_gradient_and_additional_attributes(
$attributes = $block['attributes'];
$this->assertEquals(
[
'align' => null,
'anchor' => 'test-anchor', // previously untested
'backgroundColor' => null,
'borderColor' => null,
Expand All @@ -309,4 +301,87 @@ public function test_retrieve_core_code_with_gradient_and_additional_attributes(
$attributes
);
}

/**
* Test the retrieval of the align attribute for core/code block.
*/
public function test_retrieve_core_code_align_attribute(): void {
// The align attribute is only supported in WP 6.3+
if ( ! is_wp_version_compatible( '6.3' ) ) {
$this->markTestSkipped( 'This test requires WP 6.3 or higher.' );
}

$block_content = '
<!-- wp:code {"align":"wide","anchor":"test-anchor","gradient":"vivid-cyan-blue-to-vivid-purple","lock":{"move":true,"remove":true}} -->
<pre id="test-anchor" class="wp-block-code alignwide has-vivid-cyan-blue-to-vivid-purple-gradient-background"><code>function test() { return "aligned code"; }</code></pre>
<!-- /wp:code>
';

wp_update_post(
[
'ID' => $this->post_id,
'post_content' => $block_content,
]
);

$query = '
query CoreCodeAlignTest($id: ID!) {
post(id: $id, idType: DATABASE_ID) {
editorBlocks {
... on CoreCode {
attributes {
align
anchor
backgroundColor
borderColor
className
content
cssClassName
fontFamily
fontSize
gradient
lock
style
textColor
}
}
}
}
}
';

$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 response should have a data key' );
$this->assertArrayHasKey( 'post', $actual['data'], 'The data should have a post key' );
$this->assertArrayHasKey( 'editorBlocks', $actual['data']['post'], 'The post should have editorBlocks' );
$this->assertCount( 1, $actual['data']['post']['editorBlocks'], 'There should be one editor block' );

$block = $actual['data']['post']['editorBlocks'][0];
$attributes = $block['attributes'];

$this->assertEquals(
[
'align' => 'wide', // previously untested
'anchor' => 'test-anchor',
'backgroundColor' => null,
'borderColor' => null,
'className' => null,
'content' => 'function test() { return "aligned code"; }',
'cssClassName' => 'wp-block-code alignwide has-vivid-cyan-blue-to-vivid-purple-gradient-background',
'fontFamily' => null,
'fontSize' => null,
'gradient' => 'vivid-cyan-blue-to-vivid-purple',
'lock' => '{"move":true,"remove":true}',
'style' => null,
'textColor' => null,
],
$attributes
);
}
}

0 comments on commit 71d97fc

Please sign in to comment.