Skip to content

Commit

Permalink
tests: lint and format
Browse files Browse the repository at this point in the history
  • Loading branch information
justlevine committed Sep 12, 2024
1 parent a06f97a commit 89b6c60
Show file tree
Hide file tree
Showing 19 changed files with 508 additions and 467 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-lions-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wpengine/wp-graphql-content-blocks": patch
---

tests: lint and format PHPUnit tests
2 changes: 1 addition & 1 deletion .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<file>./includes/</file>
<exclude-pattern>/vendor/</exclude-pattern>
<exclude-pattern>/node_modules/</exclude-pattern>
<exclude-pattern>**/tests/**</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 -->
Expand Down
4 changes: 1 addition & 3 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$_tests_dir = getenv( 'WP_TESTS_DIR' );

define( 'WP_TEST_PLUGINS_DIR', '/var/www/html/wp-content/plugins' );
define( 'WP_TEST_DATA_DIR', dirname( __FILE__ ) . '/_data' );
define( 'WP_TEST_DATA_DIR', __DIR__ . '/_data' );

if ( ! $_tests_dir ) {
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
Expand All @@ -21,7 +21,6 @@

require_once $_tests_dir . '/includes/functions.php';


tests_add_filter( 'wp_loaded', '_output_wp_version' );
function _output_wp_version() {
echo 'Running on WordPress ' . esc_html( get_bloginfo( 'version' ) ) . "...\n";
Expand All @@ -43,4 +42,3 @@ function _manually_load_plugin() {
}

require $_tests_dir . '/includes/bootstrap.php';

8 changes: 5 additions & 3 deletions tests/unit/BlockAttributesObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ final class BlockAttributesObjectTest extends PluginTestCase {

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

global $wpdb;

$this->post_id = wp_insert_post(
array(
[
'post_title' => 'Post Title',
'post_content' => preg_replace(
'/\s+/',
Expand All @@ -24,13 +25,14 @@ public function setUp(): void {
)
),
'post_status' => 'publish',
)
]
);
}

public function tearDown(): void {
// your tear down methods here
parent::tearDown();

wp_delete_post( $this->post_id, true );
}

Expand All @@ -52,7 +54,7 @@ public function test_resolve_attribute_object_type() {
}
';

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

// Verify that the ID of the first post matches the one we just created.
Expand Down
50 changes: 26 additions & 24 deletions tests/unit/BlockQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,42 @@ final class BlockQueriesTest extends PluginTestCase {

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

global $wpdb;

$this->post_id = wp_insert_post(
array(
[
'post_title' => 'Post Title',
'post_content' => preg_replace(
'/\s+/',
' ',
trim(
'
<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>Example paragraph in Column 1</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->
<!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>Example paragraph in Column 2</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->'
<!-- wp:columns -->
<div class="wp-block-columns"><!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>Example paragraph in Column 1</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->
<!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>Example paragraph in Column 2</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->
'
)
),
'post_status' => 'publish',
)
]
);
}

public function tearDown(): void {
// your tear down methods here
parent::tearDown();

wp_delete_post( $this->post_id, true );
}

Expand All @@ -50,14 +53,14 @@ public function test_retrieve_non_flatten_editor_blocks() {
posts(first: 1) {
nodes {
databaseId
editorBlocks(flat: false) {
name
}
editorBlocks(flat: false) {
name
}
}
}
}
';
$actual = graphql( array( 'query' => $query ) );
$actual = graphql( [ 'query' => $query ] );
$node = $actual['data']['posts']['nodes'][0];

// Verify that the ID of the first post matches the one we just created.
Expand All @@ -75,15 +78,15 @@ public function test_retrieve_flatten_editor_blocks() {
nodes {
databaseId
editorBlocks(flat: true) {
name
parentClientId
}
name
parentClientId
}
}
}
}
';

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

// Verify that the ID of the first post matches the one we just created.
Expand All @@ -107,5 +110,4 @@ public function test_retrieve_flatten_editor_blocks() {
$this->assertEquals( $node['editorBlocks'][4]['name'], 'core/paragraph' );
$this->assertNotNull( $node['editorBlocks'][4]['parentClientId'] );
}

}
86 changes: 45 additions & 41 deletions tests/unit/BlockSupportsAnchorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,45 @@

namespace WPGraphQL\ContentBlocks\Unit;

use \WPGraphQL\ContentBlocks\Field\BlockSupports\Anchor;
use WPGraphQL\ContentBlocks\Field\BlockSupports\Anchor;

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

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

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

$this->post_id = wp_insert_post(
array(
[
'post_title' => 'Post Title',
'post_content' => preg_replace(
'/\s+/',
' ',
trim(
'
<!-- wp:paragraph -->
<p id="example">Example paragraph with Anchor</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Example paragraph without Anchor</p>
<!-- /wp:paragraph -->
<!-- wp:group -->
<div class="wp-block-group">
<!-- wp:paragraph -->
<p id="example-inner">Example inner block</p>
<p id="example">Example paragraph with Anchor</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Example paragraph without Anchor</p>
<!-- /wp:paragraph -->
<!-- /wp:group -->
'
<!-- wp:group -->
<div class="wp-block-group">
<!-- wp:paragraph -->
<p id="example-inner">Example inner block</p>
<!-- /wp:paragraph -->
<!-- /wp:group -->
'
)
),
'post_status' => 'publish',
)
]
);
$this->instance = new Anchor();
\WPGraphQL::clear_schema();
Expand All @@ -47,6 +49,7 @@ public function setUp(): void {
public function tearDown(): void {
// your tear down methods here
parent::tearDown();

wp_delete_post( $this->post_id, true );
\WPGraphQL::clear_schema();
}
Expand All @@ -61,32 +64,33 @@ public function test_register_anchor_schema_fields() {
// Verify BlockWithSupportsAnchor fields registration
$query = '
query BlockWithSupportsAnchorMeta {
__type(name: "BlockWithSupportsAnchor") {
fields {
name
}
possibleTypes {
name
}
}
}
__type(name: "BlockWithSupportsAnchor") {
fields {
name
}
possibleTypes {
name
}
}
}
';
$actual = graphql( array( 'query' => $query ) );
$expected = array(
'fields' => array(
array(

$actual = graphql( [ 'query' => $query ] );
$expected = [
'fields' => [
[
'name' => 'anchor',
),
),
'possibleTypes' => array(
array(
],
],
'possibleTypes' => [
[
'name' => 'CoreParagraph',
),
array(
],
[
'name' => 'CoreParagraphAttributes',
),
),
);
],
],
];
$this->assertArrayHasKey( 'data', $actual, json_encode( $actual ) );
$this->assertEquals( $actual['data']['__type']['fields'], $expected['fields'] );
$this->assertContains( $expected['possibleTypes'][0], $actual['data']['__type']['possibleTypes'] );
Expand All @@ -105,16 +109,16 @@ public function test_register_anchor_query_field() {
{
posts(first: 1) {
nodes {
editorBlocks {
name
editorBlocks {
name
... on BlockWithSupportsAnchor {
anchor
}
}
}
}
}
}';
$actual = graphql( array( 'query' => $query ) );
$actual = graphql( [ 'query' => $query ] );
$node = $actual['data']['posts']['nodes'][0];

$this->assertEquals( count( $node['editorBlocks'] ), 4 );
Expand Down
Loading

0 comments on commit 89b6c60

Please sign in to comment.