Skip to content
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

tests: Add tests for CoreHeading block #10

Merged
merged 14 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions phpcs.xml
justlevine marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards for WPGraphQL Plugins" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/squizlabs/PHP_CodeSniffer/master/phpcs.xsd">
<description>Sniffs for WPGraphQL Content Blocks</description>

<!-- What to scan: include any root-level PHP files, and the /includes folder -->
<file>./wp-graphql-content-blocks.php</file>
<file>./includes/</file>
<exclude-pattern>/vendor/</exclude-pattern>
<exclude-pattern>/node_modules/</exclude-pattern>
<!-- <exclude-pattern>**/tests/**</exclude-pattern> -->

<!-- How to scan: include CLI args so you don't need to pass them manually -->
<!-- Usage instructions: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
<!-- Annotated ruleset: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->

<!-- Show sniff and progress -->
<arg value="sp"/>
<!-- Strip the file paths down to the relevant bit -->
<arg name="basepath" value="./"/>
<!-- Enable colors in report -->
<arg name="colors"/>
<!-- Only lint php files by default -->
<arg name="extensions" value="php"/>
<!-- Whenever possible, cache the scan results and re-use those for unchanged files on the next scan. -->
<arg name="cache" value="tests/_output/cache.json" />
<!-- Enables parallel processing when available for faster results. -->
<arg name="parallel" value="20"/>
<!-- Set severity to 1 to see everything that isn't effectively turned off. -->
<arg name="severity" value="1" />

<!-- Ruleset Config: set these to match your project constraints-->

<!--
Tests for PHP version compatibility.
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards#Recomended-additional-rulesets
-->
<config name="testVersion" value="7.4-"/>

<!--
Tests for WordPress version compatibility.
https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties
-->
<config name="minimum_supported_wp_version" value="5.3"/>

<!-- Rules: WPGraphQL Coding Standards -->
<!-- https://github.com/AxeWP/WPGraphQL-Coding-Standards/WPGraphQL/ruleset.xml -->
<rule ref="WPGraphQL">
<!-- The is the only thing missing from WPEngine-Strict -->
<exclude name="Squiz.Commenting.InlineComment.InvalidEndChar" />
<!-- This should be excluded upstream in the ruleset-->
<exclude name="WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase" />
<!-- These maybe should be added back later -->
<exclude name="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification" />
<exclude name="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification" />
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification" />
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint" />
<exclude name="WordPressVIPMinimum.Functions.RestrictedFunctions.wp_remote_get_wp_remote_get" />
</rule>

<!-- Individual rule configuration -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<!-- Value: replace the function, class, and variable prefixes used. Separate multiple prefixes with a comma. -->
<property name="prefixes" type="array">
<element value="wpgraphql\content_blocks" />
<element value="WPGRAPHQL\CONTENT_BLOCKS" />
<element value="WPGraphQL\ContentBlocks" />
<element value="wpgraphql_content_blocks_" />
<element value="WPGraphQLContentBlocks" />
</property>
</properties>
</rule>

<rule ref="WordPress.WP.I18n">
<properties>
<!-- Value: replace the text domain used. -->
<property name="text_domain" type="array" value="wp-graphql-content-blocks" />
</properties>
</rule>


<!-- Exclude rules that break PHP Unit test conventions or result in redundant comments. -->
<!-- Test functions and properties are self-describing. -->
<!-- @todo These shouldnt be necessary, since tests are excluded -->
<rule ref="Generic.Commenting.DocComment.MissingShort">
<exclude-pattern>/tests/</exclude-pattern>
</rule>

<!-- Test file names are self-describing. -->
<rule ref="Squiz.Commenting.FileComment">
<exclude-pattern>/tests/</exclude-pattern>
</rule>

<!-- Test class names are self-describing. -->
<rule ref="Squiz.Commenting.ClassComment.Missing">
<exclude-pattern>/tests/</exclude-pattern>
</rule>

<!-- Test function names are self-describing. -->
<rule ref="Squiz.Commenting.FunctionComment">
<exclude-pattern>/tests/</exclude-pattern>
</rule>

<!-- Test variables should be self-describing. -->
<rule ref="Squiz.Commenting.VariableComment.Missing">
<exclude-pattern>/tests/</exclude-pattern>
</rule>

<!-- Test functions are not public so do not require @throws docblock annotations. -->
<rule ref="Squiz.Commenting.FunctionCommentThrowTag.Missing">
<exclude-pattern>/tests/</exclude-pattern>
</rule>

<!-- Multiple assignments are used as part of test setup. -->
<rule ref="Squiz.PHP.DisallowMultipleAssignments.Found">
<exclude-pattern>/tests/</exclude-pattern>
</rule>

<rule ref="WordPress.NamingConventions">
<exclude-pattern>/includes/utilities/DomHelpers.php</exclude-pattern>
</rule>

<!-- We follow the Codeception class name convention instead of the WordPress one. -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound">
<exclude-pattern>/tests/</exclude-pattern>
</rule>

<!-- Test globals don't require prefixes. -->
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<exclude-pattern>/tests/</exclude-pattern>
</rule>

<!-- `json_encode()` is fine for tests. -->
<rule ref="WordPress.WP.AlternativeFunctions.json_encode_json_encode">
<exclude-pattern>/tests/</exclude-pattern>
</rule>

<!-- Allow queries, DROP and general database manipulation in tests. -->
<rule ref="WordPress.DB">
<exclude-pattern>/tests/</exclude-pattern>
</rule>

<!-- Suppress warnings about the _before() test methods used in Codeception tests. -->
<rule ref="PSR2.Methods.MethodDeclaration.Underscore">
<exclude-pattern>/tests/</exclude-pattern>
</rule>

</ruleset>
193 changes: 167 additions & 26 deletions tests/unit/CoreHeadingTest.php
justlevine marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace WPGraphQL\ContentBlocks\Unit;

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

public function setUp(): void {
parent::setUp();
Expand Down Expand Up @@ -109,9 +109,6 @@ public function test_retrieve_core_heading_attributes() {
$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->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' );
Expand Down Expand Up @@ -176,7 +173,7 @@ public function test_retrieve_core_heading_with_colors_and_alignment() {
[
'align' => 'wide', // Previously untested
'anchor' => null,
'backgroundColor' => null, // @todo: This is not returning correctly.
'backgroundColor' => null,
justlevine marked this conversation as resolved.
Show resolved Hide resolved
'className' => null,
'content' => 'Colored Heading',
'cssClassName' => 'wp-block-heading has-text-align-right has-text-color has-background alignwide',
Expand All @@ -186,14 +183,62 @@ public function test_retrieve_core_heading_with_colors_and_alignment() {
'level' => 3.0,
'lock' => null,
'placeholder' => null,
'style' => wp_json_encode([
'color' => [
'background' => '#cf2e2e',
'text' => '#ffffff',
],
]),
'style' => wp_json_encode(
[
'color' => [
'background' => '#cf2e2e',
'text' => '#ffffff',
],
]
),
'textAlign' => 'right',
'textColor' => null, // @todo: This is not returning correctly.
'textColor' => null,
justlevine marked this conversation as resolved.
Show resolved Hide resolved
],
$attributes
);
}

public function test_retrieve_core_heading_with_background_text_color() {
$block_content = '
<!-- wp:heading {"textAlign":"right","level":3,"align":"wide","backgroundColor":"accent-4","textColor":"accent-3"} -->
<h3 class="wp-block-heading alignwide has-text-align-right has-accent-3-color has-accent-4-background-color has-text-color has-background">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(
[
'align' => 'wide',
'anchor' => null,
'backgroundColor' => 'accent-4', // Previously untested
'className' => null,
'content' => 'Colored Heading',
'cssClassName' => 'wp-block-heading alignwide has-text-align-right has-accent-3-color has-accent-4-background-color has-text-color has-background',
'fontFamily' => null,
'fontSize' => null,
'gradient' => null,
'level' => 3.0,
'lock' => null,
'placeholder' => null,
'style' => null,
'textAlign' => 'right',
'textColor' => 'accent-3', // Previously untested
],
$attributes
);
Expand Down Expand Up @@ -231,26 +276,120 @@ public function test_retrieve_core_heading_with_font_and_anchor() {
'className' => null,
'content' => 'Custom Font Heading',
'cssClassName' => 'wp-block-heading',
'fontFamily' => null, // @todo: This is not returning correctly.
'fontSize' => null, // @todo: This is not returning correctly.
'fontFamily' => null,
'fontSize' => null,
justlevine marked this conversation as resolved.
Show resolved Hide resolved
'gradient' => null,
'level' => 2.0,
'lock' => null,
'placeholder' => null,
'style' => wp_json_encode([
'typography' => [
'fontFamily' => 'Arial', // Previously untested
'fontSize' => '32px', // Previously untested
],
]),
'style' => wp_json_encode(
[
'typography' => [
'fontFamily' => 'Arial', // Previously untested
'fontSize' => '32px', // Previously untested
],
]
),
'textAlign' => null,
'textColor' => null,
],
$attributes
);
}

public function test_retrieve_core_heading_with_font_family_and_size() {
$block_content = '
<!-- wp:heading {"className":"is-style-default","backgroundColor":"accent","textColor":"contrast-2","fontSize":"xx-large","fontFamily":"system-sans-serif"} -->
<h2 class="wp-block-heading is-style-default has-contrast-2-color has-accent-background-color has-text-color has-background has-system-sans-serif-font-family has-xx-large-font-size">hurrah</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(
[
'align' => null,
'anchor' => null,
'backgroundColor' => 'accent',
'className' => 'is-style-default',
'content' => 'hurrah',
'cssClassName' => 'wp-block-heading is-style-default has-contrast-2-color has-accent-background-color has-text-color has-background has-system-sans-serif-font-family has-xx-large-font-size',
'fontFamily' => 'system-sans-serif', // Previously untested
'fontSize' => 'xx-large', // Previously untested
'gradient' => null,
'level' => 2.0,
'lock' => null,
'placeholder' => null,
'style' => null,
'textAlign' => null,
'textColor' => 'contrast-2',
],
$attributes
);
}

public function test_retrieve_core_heading_with_gradient() {
$block_content = '
<!-- wp:heading {"gradient":"gradient-3","fontSize":"medium"} -->
<h2 class="wp-block-heading has-gradient-3-gradient-background has-background has-medium-font-size">hello</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(
[
'align' => null,
'anchor' => null,
'backgroundColor' => null,
'className' => null,
'content' => 'hello',
'cssClassName' => 'wp-block-heading has-gradient-3-gradient-background has-background has-medium-font-size',
'fontFamily' => null,
'fontSize' => 'medium',
'gradient' => 'gradient-3', // Previously untested
'level' => 2.0,
'lock' => null,
'placeholder' => null,
'style' => null,
'textAlign' => null,
'textColor' => null,
],
$attributes
);
}

public function test_retrieve_core_heading_with_custom_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>
Expand Down Expand Up @@ -284,15 +423,17 @@ public function test_retrieve_core_heading_with_gradient() {
'cssClassName' => 'wp-block-heading has-background',
'fontFamily' => null,
'fontSize' => null,
'gradient' => null, // @todo: This is not returning correctly.
'gradient' => null,
'level' => 2.0,
'lock' => null,
'placeholder' => null,
'style' => wp_json_encode([
'color' => [
'gradient' => 'linear-gradient(135deg,rgb(6,147,227) 0%,rgb(155,81,224) 100%)',
],
]),
'style' => wp_json_encode(
[
'color' => [
'gradient' => 'linear-gradient(135deg,rgb(6,147,227) 0%,rgb(155,81,224) 100%)',
],
]
),
'textAlign' => null,
'textColor' => null,
],
Expand Down