Skip to content

Commit

Permalink
feat: Add tests for additional snippets
Browse files Browse the repository at this point in the history
- cover the attributes that are null
  • Loading branch information
ashutoshgautams committed Sep 18, 2024
1 parent 92e7ae6 commit cdec9ee
Showing 1 changed file with 206 additions and 17 deletions.
223 changes: 206 additions & 17 deletions tests/unit/CoreHeadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ className
textColor
}
}
query Post( $id: ID! ) {
post(id: $id, idType: DATABASE_ID) {
databaseId
Expand Down Expand Up @@ -82,7 +81,6 @@ public function test_retrieve_core_heading_attributes() {
<!-- /wp:heading -->
';

// Update the post content with the block content.
wp_update_post(
[
'ID' => $this->post_id,
Expand All @@ -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,
Expand All @@ -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 = '
<!-- wp:heading {"level":3,"textAlign":"right","align":"wide","style":{"color":{"background":"#cf2e2e","text":"#ffffff"}}} -->
<h3 class="wp-block-heading has-text-align-right has-text-color has-background alignwide" style="background-color:#cf2e2e;color:#ffffff">Colored Heading</h3>
<!-- /wp: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 = '
<!-- wp:heading {"anchor":"custom-id","style":{"typography":{"fontFamily":"Arial","fontSize":"32px"}}} -->
<h2 class="wp-block-heading" id="custom-id" style="font-family:Arial;font-size:32px">Custom Font Heading</h2>
<!-- /wp: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 = '
<!-- wp:heading {"style":{"color":{"gradient":"linear-gradient(135deg,rgb(6,147,227) 0%,rgb(155,81,224) 100%)"}}} -->
<h2 class="wp-block-heading has-background" style="background:linear-gradient(135deg,rgb(6,147,227) 0%,rgb(155,81,224) 100%)">Gradient Heading</h2>
<!-- /wp: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 = '
<!-- wp:heading {"backgroundColor":"vivid-red-background-color"} -->
<h2 class="wp-block-heading has-vivid-red-background-color has-background">Heading with Background Color</h2>
<!-- /wp: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('Heading with Background Color', $attributes['content']);
$this->assertEquals('vivid-red-background-color', $attributes['backgroundColor']);
}

public function test_retrieve_core_heading_with_text_color() {
$block_content = '
<!-- wp:heading {"textColor":"vivid-red"} -->
<h2 class="wp-block-heading has-vivid-red-color has-text-color">Heading with Text Color</h2>
<!-- /wp: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('Heading with Text Color', $attributes['content']);
$this->assertEquals('vivid-red', $attributes['textColor']);
}

public function test_retrieve_core_heading_with_font_size() {
$block_content = '
<!-- wp:heading {"fontSize":"large"} -->
<h2 class="wp-block-heading has-large-font-size">Heading with Font Size</h2>
<!-- /wp: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('Heading with Font Size', $attributes['content']);
$this->assertEquals('large', $attributes['fontSize']);
}

public function test_retrieve_core_heading_with_class_name() {
$block_content = '
<!-- wp:heading {"className":"custom-class"} -->
<h2 class="wp-block-heading custom-class">Heading with Custom Class</h2>
<!-- /wp: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('Heading with Custom Class', $attributes['content']);
$this->assertEquals('custom-class', $attributes['className']);
}
}

0 comments on commit cdec9ee

Please sign in to comment.