Skip to content

Commit

Permalink
Merge branch 'core-heading-test' of https://github.com/rtCamp/wp-grap…
Browse files Browse the repository at this point in the history
…hql-content-blocks into core-heading-test
  • Loading branch information
ashutoshgautams committed Sep 18, 2024
2 parents 8ae47f1 + 92e7ae6 commit ecd8b31
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 137 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-cobras-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpengine/wp-graphql-content-blocks": patch
---

tests: fix PHP deprecation notices
2 changes: 1 addition & 1 deletion tests/unit/BlockSupportsAnchorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class BlockSupportsAnchorTest extends PluginTestCase {
public function setUp(): void {
parent::setUp();

$settings = get_option( 'graphql_general_settings' );
$settings = get_option( 'graphql_general_settings', [] );
$settings['public_introspection_enabled'] = 'on';
update_option( 'graphql_general_settings', $settings );

Expand Down
260 changes: 150 additions & 110 deletions tests/unit/CoreHeadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,114 +2,154 @@

namespace WPGraphQL\ContentBlocks\Unit;

final class CoreHeadingTest extends PluginTestCase
{
public $instance;
public $post_id;

public function setUp(): void
{
parent::setUp();
global $wpdb;

$this->post_id = wp_insert_post(
array(
'post_title' => 'Post with Heading',
'post_content' => preg_replace(
'/\s+/',
' ',
trim(
'
<!-- wp:heading {"level":2,"textAlign":"center","style":{"typography":{"fontSize":"28px","fontStyle":"normal","fontWeight":"700"}}} -->
<h2 class="wp-block-heading has-text-align-center" style="font-size:28px;font-style:normal;font-weight:700">Sample Heading</h2>
<!-- /wp:heading -->
'
)
),
'post_status' => 'publish',
)
);
}

public function tearDown(): void
{
parent::tearDown();
wp_delete_post($this->post_id, true);
}

public function test_retrieve_core_heading_attributes()
{
$query = '
fragment CoreHeadingBlockFragment on CoreHeading {
attributes {
content
level
textAlign
style
}
}
query GetPosts {
posts(first: 1) {
nodes {
databaseId
editorBlocks {
name
...CoreHeadingBlockFragment
}
}
}
}
';

$actual = graphql(array('query' => $query));
$node = $actual['data']['posts']['nodes'][0];

// Verify that the ID of the first post matches the one we just created.
$this->assertEquals($this->post_id, $node['databaseId']);

// There should be only one block using that query when not using flat: true
$this->assertEquals(count($node['editorBlocks']), 1);
$this->assertEquals($node['editorBlocks'][0]['name'], 'core/heading');

$this->assertEquals(
$node['editorBlocks'][0]['attributes'],
[
'content' => 'Sample Heading',
'level' => 2,
'textAlign' => 'center',
'style' => [
'typography' => [
'fontSize' => '28px',
'fontStyle' => 'normal',
'fontWeight' => '700',
],
],
]
);
}

public function test_retrieve_core_heading_content()
{
$query = '
fragment CoreHeadingBlockFragment on CoreHeading {
content
}
query GetPosts {
posts(first: 1) {
nodes {
editorBlocks {
...CoreHeadingBlockFragment
}
}
}
}
';

$actual = graphql(array('query' => $query));
$node = $actual['data']['posts']['nodes'][0];

$this->assertEquals($node['editorBlocks'][0]['content'], 'Sample Heading');
}
final class CoreHeadingTest extends PluginTestCase {
public $post_id;

public function setUp(): void {
parent::setUp();

$this->post_id = wp_insert_post(
[
'post_title' => 'Post with Heading',
'post_content' => '',
'post_status' => 'publish',
]
);

\WPGraphQL::clear_schema();
}

public function tearDown(): void {
parent::tearDown();

wp_delete_post( $this->post_id, true );

\WPGraphQL::clear_schema();
}

public function query(): string {
return '
fragment CoreHeadingBlockFragment on CoreHeading {
attributes {
align
anchor
backgroundColor
className
content
cssClassName
fontFamily
fontSize
gradient
level
lock
# metadata
placeholder
style
textAlign
textColor
}
}
query Post( $id: ID! ) {
post(id: $id, idType: DATABASE_ID) {
databaseId
editorBlocks {
apiVersion
blockEditorCategoryName
clientId
cssClassNames
innerBlocks {
name
}
isDynamic
name
parentClientId
renderedHtml
... on BlockWithSupportsAnchor {
anchor
}
...CoreHeadingBlockFragment
}
}
}
';
}

public function test_retrieve_core_heading_attributes() {
$block_content = '
<!-- wp:heading {"level":2,"textAlign":"center","style":{"typography":{"fontSize":"28px","fontStyle":"normal","fontWeight":"700"}}} -->
<h2 class="wp-block-heading has-text-align-center" style="font-size:28px;font-style:normal;font-weight:700">Sample Heading</h2>
<!-- /wp:heading -->
';

// Update the post content with the block content.
wp_update_post(
[
'ID' => $this->post_id,
'post_content' => $block_content,
]
);

$query = $this->query();
$variables = [
'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' );

// @todo this is not working
// $this->assertNotEmpty( $actual['data']['post']['editorBlocks'][0]['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' );

// Verify the attributes.
$this->assertEquals(
[
'align' => null,
'anchor' => null,
'backgroundColor' => null,
'className' => null,
'content' => 'Sample Heading',
'cssClassName' => 'wp-block-heading has-text-align-center',
'fontFamily' => null,
'fontSize' => null,
'gradient' => null,
'level' => 2.0, // @todo this should be an integer
'lock' => null,
'placeholder' => null,
'style' => wp_json_encode(
[
'typography' => [
'fontSize' => '28px',
'fontStyle' => 'normal',
'fontWeight' => '700',
],
],
),
'textAlign' => 'center',
'textColor' => null,
],
$actual['data']['post']['editorBlocks'][0]['attributes'],
);
}
}
2 changes: 1 addition & 1 deletion tests/unit/EditorBlockInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class EditorBlockInterfaceTest extends PluginTestCase {
public function setUp(): void {
parent::setUp();

$settings = get_option( 'graphql_general_settings' );
$settings = get_option( 'graphql_general_settings', [] );
$settings['public_introspection_enabled'] = 'on';
update_option( 'graphql_general_settings', $settings );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/PostTypeBlockInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class PostTypeBlockInterfaceTest extends PluginTestCase {
public function setUp(): void {
parent::setUp();

$settings = get_option( 'graphql_general_settings' );
$settings = get_option( 'graphql_general_settings', [] );
$settings['public_introspection_enabled'] = 'on';
update_option( 'graphql_general_settings', $settings );

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/RegistryTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class RegistryTestCase extends PluginTestCase {
public function setUp(): void {
parent::setUp();

$settings = get_option( 'graphql_general_settings' );
$settings = get_option( 'graphql_general_settings', [] );
$settings['public_introspection_enabled'] = 'on';
update_option( 'graphql_general_settings', $settings );

Expand Down
22 changes: 0 additions & 22 deletions tests/unit/UpdateCalbacksTest.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/unit/UpdateCallbacksTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Class TestUpdateCallbacks
* Class UpdateCallbacksTest
*/
class UpdateCallbacksTest extends WP_UnitTestCase {
public function test_pre_set_site_transient_update_plugins_has_filter_added(): void {
Expand Down

0 comments on commit ecd8b31

Please sign in to comment.