diff --git a/.changeset/silly-lions-study.md b/.changeset/silly-lions-study.md new file mode 100644 index 00000000..a9763a7f --- /dev/null +++ b/.changeset/silly-lions-study.md @@ -0,0 +1,5 @@ +--- +"@wpengine/wp-graphql-content-blocks": patch +--- + +tests: lint and format PHPUnit tests diff --git a/tests/bootstrap.php b/tests/bootstrap.php index a302df94..10b1d3d3 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -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'; @@ -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"; @@ -43,4 +42,3 @@ function _manually_load_plugin() { } require $_tests_dir . '/includes/bootstrap.php'; - diff --git a/tests/unit/BlockAttributesObjectTest.php b/tests/unit/BlockAttributesObjectTest.php index 2fb69eb8..d75a3119 100644 --- a/tests/unit/BlockAttributesObjectTest.php +++ b/tests/unit/BlockAttributesObjectTest.php @@ -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+/', @@ -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 ); } @@ -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. diff --git a/tests/unit/BlockQueriesTest.php b/tests/unit/BlockQueriesTest.php index 2140cb8f..9d0f2e21 100644 --- a/tests/unit/BlockQueriesTest.php +++ b/tests/unit/BlockQueriesTest.php @@ -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( ' - -
-
-

Example paragraph in Column 1

-
- - - -
-

Example paragraph in Column 2

-
-
- ' + +
+
+

Example paragraph in Column 1

+
+ + + +
+

Example paragraph in Column 2

+
+
+ + ' ) ), 'post_status' => 'publish', - ) + ] ); } public function tearDown(): void { // your tear down methods here parent::tearDown(); + wp_delete_post( $this->post_id, true ); } @@ -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. @@ -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. @@ -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'] ); } - } diff --git a/tests/unit/BlockSupportsAnchorTest.php b/tests/unit/BlockSupportsAnchorTest.php index b6f07593..69f44d6f 100644 --- a/tests/unit/BlockSupportsAnchorTest.php +++ b/tests/unit/BlockSupportsAnchorTest.php @@ -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( ' - -

Example paragraph with Anchor

- - -

Example paragraph without Anchor

- - - -
-

Example inner block

+

Example paragraph with Anchor

+ + +

Example paragraph without Anchor

- - ' + + +
+ +

Example inner block

+ + + ' ) ), 'post_status' => 'publish', - ) + ] ); $this->instance = new Anchor(); \WPGraphQL::clear_schema(); @@ -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(); } @@ -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'] ); @@ -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 ); diff --git a/tests/unit/ContentBlocksResolverTest.php b/tests/unit/ContentBlocksResolverTest.php index c8cc3e40..5920d69f 100644 --- a/tests/unit/ContentBlocksResolverTest.php +++ b/tests/unit/ContentBlocksResolverTest.php @@ -13,72 +13,73 @@ final class ContentBlocksResolverTest extends PluginTestCase { public function setUp(): void { parent::setUp(); + $this->reusable_block_id = wp_insert_post( - array( - 'post_title' => 'Reusable Block', - 'post_type' => 'wp_block', + [ + 'post_title' => 'Reusable Block', + 'post_type' => 'wp_block', 'post_content' => preg_replace( '/\s+/', ' ', trim( ' - -
-
-

Example paragraph in Column 1

-
- - ' + +
+
+

Example paragraph in Column 1

+
+ + ' ) - ) - ) + ), + ] ); - $this->reusable_post_id = wp_insert_post( - array( + $this->reusable_post_id = wp_insert_post( + [ 'post_title' => 'Post Title', 'post_content' => '', 'post_status' => 'publish', - ) + ] ); - $this->post_id = wp_insert_post( - array( + $this->post_id = wp_insert_post( + [ 'post_title' => 'Post Title', 'post_content' => preg_replace( '/\s+/', ' ', trim( ' - - - - - - - - - -
-
-

Example paragraph in Column 1

-
- - - -
-

Example paragraph in Column 2

-
-
- - - -

Hello Classic Block

- ' + + + + + + + + + +
+
+

Example paragraph in Column 1

+
+ + + +
+

Example paragraph in Column 2

+
+
+ + + +

Hello Classic Block

+ ' ) ), 'post_status' => 'publish', - ) + ] ); $this->instance = new ContentBlocksResolver(); @@ -87,23 +88,24 @@ public function setUp(): void { public function tearDown(): void { // your tear down methods here parent::tearDown(); + wp_delete_post( $this->post_id, true ); wp_delete_post( $this->reusable_post_id, true ); wp_delete_post( $this->reusable_block_id, true ); } - public function test_resolve_content_blocks_resolves_reusable_blocks() { - $post_model = new Post( get_post( $this->reusable_post_id ) ); - $actual = $this->instance->resolve_content_blocks( $post_model, array( 'flat' => true ) ); - - // There should return only the non-empty blocks + public function test_resolve_content_blocks_resolves_reusable_blocks() { + $post_model = new Post( get_post( $this->reusable_post_id ) ); + $actual = $this->instance->resolve_content_blocks( $post_model, [ 'flat' => true ] ); + + // There should return only the non-empty blocks $this->assertEquals( 3, count( $actual ) ); $this->assertEquals( 'core/columns', $actual[0]['blockName'] ); - } + } public function test_resolve_content_blocks_filters_empty_blocks() { $post_model = new Post( get_post( $this->post_id ) ); - $actual = $this->instance->resolve_content_blocks( $post_model, array( 'flat' => true ) ); + $actual = $this->instance->resolve_content_blocks( $post_model, [ 'flat' => true ] ); // There should return only the non-empty blocks $this->assertEquals( 6, count( $actual ) ); $this->assertEquals( 'core/columns', $actual[0]['blockName'] ); @@ -111,19 +113,19 @@ public function test_resolve_content_blocks_filters_empty_blocks() { public function test_resolve_content_blocks_resolves_classic_blocks() { $post_model = new Post( get_post( $this->post_id ) ); - $actual = $this->instance->resolve_content_blocks( $post_model, array( 'flat' => true ) ); + $actual = $this->instance->resolve_content_blocks( $post_model, [ 'flat' => true ] ); $this->assertEquals( 'core/freeform', $actual[5]['blockName'] ); } public function test_resolve_content_blocks_filters_blocks_not_from_allow_list() { $post_model = new Post( get_post( $this->post_id ) ); - $allowed = array( 'core/column', 'core/paragraph' ); - $parsed_blocks = $this->instance->resolve_content_blocks( $post_model, array( 'flat' => true ), $allowed ); + $allowed = [ 'core/column', 'core/paragraph' ]; + $parsed_blocks = $this->instance->resolve_content_blocks( $post_model, [ 'flat' => true ], $allowed ); $actual_block_names = array_values( array_unique( array_map( - function ( $parsed_block ) { + static function ( $parsed_block ) { return $parsed_block['blockName']; }, $parsed_blocks, @@ -132,6 +134,6 @@ function ( $parsed_block ) { ); // There should return only blocks from the allow list $this->assertEquals( 4, count( $parsed_blocks ) ); - $this->assertEquals( $allowed, $actual_block_names ); + $this->assertEquals( $allowed, $actual_block_names ); } } diff --git a/tests/unit/CoreImageTest.php b/tests/unit/CoreImageTest.php index f1b6f23c..0962a20a 100644 --- a/tests/unit/CoreImageTest.php +++ b/tests/unit/CoreImageTest.php @@ -9,100 +9,104 @@ final class CoreImageTest extends PluginTestCase { public function setUp(): void { parent::setUp(); + global $wpdb; $this->attachment_id = $this->factory->attachment->create_upload_object( WP_TEST_DATA_DIR . '/images/test-image.jpg' ); $this->post_id = wp_insert_post( - array( - 'post_title' => 'Post Title', + [ + 'post_title' => 'Post Title', 'post_content' => preg_replace( '/\s+/', ' ', trim( ' - -
- - ' + +
+ + ' ) ), - 'post_status' => 'publish', - ) + 'post_status' => 'publish', + ] ); } public function tearDown(): void { // your tear down methods here parent::tearDown(); + wp_delete_post( $this->post_id, true ); } public function test_retrieve_core_image_media_details() { - $query = ' - fragment CoreImageBlockFragment on CoreImage { - attributes { - id - } - mediaDetails { - height - width + $query = ' + fragment CoreImageBlockFragment on CoreImage { + attributes { + id + } + mediaDetails { + height + width + } } - } - - query GetPosts { - posts(first: 1) { - nodes { - editorBlocks { - ...CoreImageBlockFragment + + query GetPosts { + posts(first: 1) { + nodes { + editorBlocks { + ...CoreImageBlockFragment + } + } } - } } - } '; - $actual = graphql( array( 'query' => $query ) ); - $node = $actual['data']['posts']['nodes'][0]; + $actual = graphql( [ 'query' => $query ] ); + $node = $actual['data']['posts']['nodes'][0]; - $this->assertEquals( $node['editorBlocks'][0]['mediaDetails'], [ - "width" => 50, - "height" => 50, - ] ); + $this->assertEquals( + $node['editorBlocks'][0]['mediaDetails'], + [ + 'width' => 50, + 'height' => 50, + ] + ); } - public function test_retrieve_core_image_attributes() { - $query = ' - fragment CoreImageBlockFragment on CoreImage { - attributes { - id - width - height - alt - src - style - sizeSlug - linkClass - linkTarget - linkDestination - align - caption - cssClassName + $query = ' + fragment CoreImageBlockFragment on CoreImage { + attributes { + id + width + height + alt + src + style + sizeSlug + linkClass + linkTarget + linkDestination + align + caption + cssClassName + } } - } - - query GetPosts { - posts(first: 1) { - nodes { - databaseId - editorBlocks { - name - ...CoreImageBlockFragment + + query GetPosts { + posts(first: 1) { + nodes { + databaseId + editorBlocks { + name + ...CoreImageBlockFragment + } + } } - } } - } '; - $actual = graphql( array( 'query' => $query ) ); - $node = $actual['data']['posts']['nodes'][0]; + $actual = graphql( [ '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'] ); @@ -110,20 +114,23 @@ public function test_retrieve_core_image_attributes() { $this->assertEquals( count( $node['editorBlocks'] ), 1 ); $this->assertEquals( $node['editorBlocks'][0]['name'], 'core/image' ); - $this->assertEquals( $node['editorBlocks'][0]['attributes'], [ - "width" => "500", - "height" => 500.0, - "alt" => "", - "id" => $this->attachment_id, - "src" => "http://mysite.local/wp-content/uploads/2023/05/online-programming-course-hero-section-bg.svg", - "style" => NULL, - "sizeSlug" => "full", - "linkClass" => NULL, - "linkTarget" => NULL, - "linkDestination" => "none", - "align" => NULL, - "caption" => "", - "cssClassName" => "wp-block-image size-full is-resized" - ] ); + $this->assertEquals( + $node['editorBlocks'][0]['attributes'], + [ + 'width' => '500', + 'height' => 500.0, + 'alt' => '', + 'id' => $this->attachment_id, + 'src' => 'http://mysite.local/wp-content/uploads/2023/05/online-programming-course-hero-section-bg.svg', + 'style' => null, + 'sizeSlug' => 'full', + 'linkClass' => null, + 'linkTarget' => null, + 'linkDestination' => 'none', + 'align' => null, + 'caption' => '', + 'cssClassName' => 'wp-block-image size-full is-resized', + ] + ); } } diff --git a/tests/unit/CoreTableTest.php b/tests/unit/CoreTableTest.php index 0f3f38f0..1e9847d0 100644 --- a/tests/unit/CoreTableTest.php +++ b/tests/unit/CoreTableTest.php @@ -3,15 +3,16 @@ namespace WPGraphQL\ContentBlocks\Unit; final class CoreTableTest extends PluginTestCase { - public $instance; + public $instance; public $post_id; public function setUp(): void { parent::setUp(); + global $wpdb; $this->post_id = wp_insert_post( - array( + [ 'post_title' => 'Post Title', 'post_content' => preg_replace( '/\s+/', @@ -28,46 +29,53 @@ public function setUp(): void { ) ), 'post_status' => 'publish', - ) + ] ); } public function tearDown(): void { parent::tearDown(); + wp_delete_post( $this->post_id, true ); } public function test_retrieve_core_table_attribute_fields() { - $this->markTestSkipped('must be revisited since the test is failing on the CI for an unknown reason'); + $this->markTestSkipped( 'must be revisited since the test is failing on the CI for an unknown reason' ); $query = ' - fragment CoreTableBlockFragment on CoreTable { - attributes { - caption - align - anchor + fragment CoreTableBlockFragment on CoreTable { + attributes { + caption + align + anchor + } } - } - query GetPosts { - posts(first: 1) { - nodes { - editorBlocks { - name - ...CoreTableBlockFragment + query GetPosts { + posts(first: 1) { + nodes { + editorBlocks { + name + ...CoreTableBlockFragment + } + } } - } } - } '; - $actual = graphql( array( 'query' => $query ) ); + + $actual = graphql( [ 'query' => $query ] ); + $node = $actual['data']['posts']['nodes'][0]; + $this->assertEquals( $node['editorBlocks'][0]['name'], 'core/table' ); // 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]['attributes'], [ - 'caption' => "Caption", - 'align' => null, - 'anchor' => null - ]); + $this->assertEquals( + $node['editorBlocks'][0]['attributes'], + [ + 'caption' => 'Caption', + 'align' => null, + 'anchor' => null, + ] + ); } } diff --git a/tests/unit/CoreVideoTest.php b/tests/unit/CoreVideoTest.php index 22653a0e..e32162f6 100644 --- a/tests/unit/CoreVideoTest.php +++ b/tests/unit/CoreVideoTest.php @@ -3,94 +3,99 @@ namespace WPGraphQL\ContentBlocks\Unit; final class CoreVideoTest extends PluginTestCase { - public $instance; + public $instance; public $post_id; - - public function setUp(): void { + + 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( ' - -
- - ' + +
+ + ' ) ), 'post_status' => 'publish', - ) + ] ); } - public function tearDown(): void { + public function tearDown(): void { // your tear down methods here parent::tearDown(); + wp_delete_post( $this->post_id, true ); } - public function test_retrieve_core_video_attributes() { - $this->markTestSkipped('must be revisited since the test is failing on the CI for an unknown reason'); - $query = ' - fragment CoreVideoBlockFragment on CoreVideo { - attributes { - align - anchor - autoplay - tracks - muted - caption - preload - src - playsInline - controls - loop - poster - id - } - } - - query GetPosts { - posts(first: 1) { - nodes { - databaseId - editorBlocks { - name - ...CoreVideoBlockFragment + public function test_retrieve_core_video_attributes() { + $this->markTestSkipped( 'must be revisited since the test is failing on the CI for an unknown reason' ); + $query = ' + fragment CoreVideoBlockFragment on CoreVideo { + attributes { + align + anchor + autoplay + tracks + muted + caption + preload + src + playsInline + controls + loop + poster + id + } + } + + query GetPosts { + posts(first: 1) { + nodes { + databaseId + editorBlocks { + name + ...CoreVideoBlockFragment + } + } } - } } - } '; - $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. $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/video' ); - $this->assertEquals( $node['editorBlocks'][0]['attributes'], [ - 'align' => null, - 'anchor' => null, - 'autoplay' => true, - 'tracks' => [], - 'muted' => null, - 'caption' => null, - 'preload' => 'auto', - 'src' => 'http://mysite.local/wp-content/uploads/2023/07/pexels_videos_1860684-1440p.mp4', - 'playsInline' => true, - 'controls' => true, - 'loop' => true, - 'poster' => 'http://mysite.local/wp-content/uploads/2023/05/pexels-egor-komarov-14420089-scaled.jpg', - 'id' => 1636.0, - ]); + $this->assertEquals( + $node['editorBlocks'][0]['attributes'], + [ + 'align' => null, + 'anchor' => null, + 'autoplay' => true, + 'tracks' => [], + 'muted' => null, + 'caption' => null, + 'preload' => 'auto', + 'src' => 'http://mysite.local/wp-content/uploads/2023/07/pexels_videos_1860684-1440p.mp4', + 'playsInline' => true, + 'controls' => true, + 'loop' => true, + 'poster' => 'http://mysite.local/wp-content/uploads/2023/05/pexels-egor-komarov-14420089-scaled.jpg', + 'id' => 1636.0, + ] + ); } } diff --git a/tests/unit/DOMHelpersTest.php b/tests/unit/DOMHelpersTest.php index f507de07..9313540c 100644 --- a/tests/unit/DOMHelpersTest.php +++ b/tests/unit/DOMHelpersTest.php @@ -2,21 +2,21 @@ namespace WPGraphQL\ContentBlocks\Unit; -use \WPGraphQL\ContentBlocks\Utilities\DOMHelpers; +use WPGraphQL\ContentBlocks\Utilities\DOMHelpers; final class DOMHelpersTest extends PluginTestCase { public function testParseAttribute(): void { - $html = '

Bar

'; - $html2 = 'Content 1 + $html = '

Bar

'; + $html2 = 'Content 1 Content 2'; - $html3 = '
LeftRight
'; + $html3 = '
LeftRight
'; $no_existent_selector = '#foo'; - $id_selector = '#foo-id'; - $class_selector = '.foo-class'; - $element_selector = 'p'; - $data_attribute = 'data'; - $class_attribute = 'class'; - $id_attribute = 'id'; + $id_selector = '#foo-id'; + $class_selector = '.foo-class'; + $element_selector = 'p'; + $data_attribute = 'data'; + $class_attribute = 'class'; + $id_attribute = 'id'; // $html $this->assertNull( DOMHelpers::parse_attribute( '', $no_existent_selector, $data_attribute ) ); @@ -44,11 +44,11 @@ public function testParseAttribute(): void { } public function testParseHTML(): void { - $html = '

Bar

'; + $html = '

Bar

'; $no_existent_selector = '#foo'; - $id_selector = '#foo-id'; - $class_selector = '.foo-class'; - $element_selector = 'p'; + $id_selector = '#foo-id'; + $class_selector = '.foo-class'; + $element_selector = 'p'; $this->assertNull( DOMHelpers::parse_html( '', $no_existent_selector ) ); $this->assertEmpty( DOMHelpers::parse_html( $html, $no_existent_selector ) ); @@ -59,8 +59,8 @@ public function testParseHTML(): void { } public function testGetElementsFromHTML(): void { - $html = '

First paragraph

My div

Second paragraph

'; - $element_selector = 'p'; + $html = '

First paragraph

My div

Second paragraph

'; + $element_selector = 'p'; $no_existent_selector = 'span'; $this->assertNull( DOMHelpers::get_elements_from_html( '', $no_existent_selector ) ); @@ -71,9 +71,9 @@ public function testGetElementsFromHTML(): void { public function getTextFromSelector(): void { $html = '

First paragraph

My div

Second paragraph

'; - $blockquote_element = 'blockquote'; - $p_element = 'p'; - $div_element = 'div'; + $blockquote_element = 'blockquote'; + $p_element = 'p'; + $div_element = 'div'; $no_existent_selector = 'span'; // getTextFromSelector should get all text (even descendents) according to "textContent" diff --git a/tests/unit/EditorBlockInterfaceTest.php b/tests/unit/EditorBlockInterfaceTest.php index 54fff2c7..23290f52 100644 --- a/tests/unit/EditorBlockInterfaceTest.php +++ b/tests/unit/EditorBlockInterfaceTest.php @@ -2,10 +2,9 @@ namespace WPGraphQL\ContentBlocks\Unit; -use \WPGraphQL\ContentBlocks\Type\InterfaceType\EditorBlockInterface; +use WPGraphQL\ContentBlocks\Type\InterfaceType\EditorBlockInterface; final class EditorBlockInterfaceTest extends PluginTestCase { - public function setUp(): void { parent::setUp(); @@ -25,12 +24,12 @@ public function tearDown(): void { * @covers EditorBlockInterface->get_block */ public function test_get_block() { - $block_exists = array( + $block_exists = [ 'blockName' => 'core/paragraph', - ); - $block_does_not_exist = array( + ]; + $block_does_not_exist = [ 'blockName' => 'core/block_does_not_exist', - ); + ]; $this->assertNull( EditorBlockInterface::get_block( $block_does_not_exist ) ); $this->assertNotNull( EditorBlockInterface::get_block( $block_exists ) ); @@ -41,7 +40,7 @@ public function test_get_block() { */ public function test_register_type() { $queryNodeWithEditorBlocksMeta = ' - query NodeWithEditorBlocksMeta { + query NodeWithEditorBlocksMeta { __type(name: "NodeWithEditorBlocks") { fields { name @@ -52,20 +51,20 @@ public function test_register_type() { // Verify NodeWithEditorBlocks fields registration $response = graphql( - array( + [ 'query' => $queryNodeWithEditorBlocksMeta, - 'variables' => array( + 'variables' => [ 'name' => 'NodeWithEditorBlocks', - ), - ) + ], + ] ); - $expected = array( - 'fields' => array( - array( + $expected = [ + 'fields' => [ + [ 'name' => 'editorBlocks', - ), - ), - ); + ], + ], + ]; $this->assertArrayHasKey( 'data', $response, json_encode( $response ) ); $this->assertEquals( $response['data']['__type'], $expected ); @@ -81,14 +80,14 @@ public function test_register_type() { // Verify ContentBlock fields registration $response = graphql( - array( + [ 'query' => $queryContentBlockMeta, - 'variables' => array( + 'variables' => [ 'name' => 'EditorBlock', - ), - ) + ], + ] ); - $expected = array( + $expected = [ 'apiVersion', 'blockEditorCategoryName', 'cssClassNames', @@ -98,10 +97,10 @@ public function test_register_type() { 'clientId', 'parentClientId', 'renderedHtml', - ); + ]; $this->assertArrayHasKey( 'data', $response, json_encode( $response ) ); $actual = array_map( - function ( $val ) { + static function ( $val ) { return $val['name']; }, $response['data']['__type']['fields'] diff --git a/tests/unit/PluginTestCase.php b/tests/unit/PluginTestCase.php index 2027962a..2efea2ea 100644 --- a/tests/unit/PluginTestCase.php +++ b/tests/unit/PluginTestCase.php @@ -2,21 +2,22 @@ namespace WPGraphQL\ContentBlocks\Unit; -use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; use Brain\Monkey; +use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; abstract class PluginTestCase extends \WP_UnitTestCase { - // Adds Mockery expectations to the PHPUnit assertions count. use MockeryPHPUnitIntegration; public function setUp(): void { parent::setUp(); + Monkey\setUp(); } public function tearDown(): void { Monkey\tearDown(); + parent::tearDown(); } } diff --git a/tests/unit/PostTypeBlockInterfaceTest.php b/tests/unit/PostTypeBlockInterfaceTest.php index 66b71cff..cceb2a25 100644 --- a/tests/unit/PostTypeBlockInterfaceTest.php +++ b/tests/unit/PostTypeBlockInterfaceTest.php @@ -2,10 +2,11 @@ namespace WPGraphQL\ContentBlocks\Unit; -use \WPGraphQL\ContentBlocks\Type\InterfaceType\PostTypeBlockInterface; +use WPGraphQL\ContentBlocks\Type\InterfaceType\PostTypeBlockInterface; final class PostTypeBlockInterfaceTest extends PluginTestCase { public $instance; + public function setUp(): void { parent::setUp(); @@ -20,6 +21,7 @@ public function setUp(): void { public function tearDown(): void { // your tear down methods here \WPGraphQL::clear_schema(); + parent::tearDown(); } @@ -27,57 +29,57 @@ public function tearDown(): void { * @covers PostTypeBlockInterface->register_type */ public function test_register_type() { - $this->instance::register_type( 'post', array(), \WPGraphQL::get_type_registry() ); + $this->instance::register_type( 'post', [], \WPGraphQL::get_type_registry() ); // Verify NodeWithPostEditorBlocks fields registration $queryNodeWithPostEditorBlocksMeta = ' query NodeWithPostEditorBlocksMeta { - __type(name: "NodeWithPostEditorBlocks") { - fields { - name - } - } - } + __type(name: "NodeWithPostEditorBlocks") { + fields { + name + } + } + } '; $response = graphql( - array( + [ 'query' => $queryNodeWithPostEditorBlocksMeta, - 'variables' => array( + 'variables' => [ 'name' => 'NodeWithPostEditorBlocks', - ), - ) + ], + ] ); - $expected = array( - 'fields' => array( - array( + $expected = [ + 'fields' => [ + [ 'name' => 'editorBlocks', - ), - ), - ); + ], + ], + ]; $this->assertArrayHasKey( 'data', $response, json_encode( $response ) ); $this->assertEquals( $response['data']['__type'], $expected ); // Verify PostEditorBlock fields registration $queryContentBlockMeta = ' query ContentBlockMeta { - __type(name: "PostEditorBlock") { - fields { - name - } - } + __type(name: "PostEditorBlock") { + fields { + name + } + } } '; // Verify ContentBlock fields registration $response = graphql( - array( + [ 'query' => $queryContentBlockMeta, - 'variables' => array( + 'variables' => [ 'name' => 'PostEditorBlock', - ), - ) + ], + ] ); - $expected = array( + $expected = [ 'apiVersion', 'blockEditorCategoryName', 'cssClassNames', @@ -87,10 +89,10 @@ public function test_register_type() { 'clientId', 'parentClientId', 'renderedHtml', - ); + ]; $this->assertArrayHasKey( 'data', $response, json_encode( $response ) ); $actual = array_map( - function ( $val ) { + static function ( $val ) { return $val['name']; }, $response['data']['__type']['fields'] diff --git a/tests/unit/RegistryTestCase.php b/tests/unit/RegistryTestCase.php index d26ad58a..41232379 100644 --- a/tests/unit/RegistryTestCase.php +++ b/tests/unit/RegistryTestCase.php @@ -2,7 +2,7 @@ namespace WPGraphQL\ContentBlocks\Unit; -use \WPGraphQL\ContentBlocks\Registry\Registry; +use WPGraphQL\ContentBlocks\Registry\Registry; final class RegistryTestCase extends PluginTestCase { /** @@ -25,6 +25,7 @@ public function setUp(): void { public function tearDown(): void { // your tear down methods here \WPGraphQL::clear_schema(); + parent::tearDown(); } @@ -48,17 +49,17 @@ interfaces { // Verify the response contains what we put in cache $response = graphql( - array( + [ 'query' => $query, - 'variables' => array( + 'variables' => [ 'name' => 'Post', - ), - ) + ], + ] ); - $contains_interface = array( + $contains_interface = [ 'name' => 'NodeWithEditorBlocks', 'description' => 'Node that has content blocks associated with it', - ); + ]; $this->assertArrayHasKey( 'data', $response, json_encode( $response ) ); $this->assertNotEmpty( $response['data']['__type']['interfaces'] ); $this->assertTrue( in_array( $contains_interface, $response['data']['__type']['interfaces'] ) ); @@ -69,7 +70,7 @@ interfaces { * no additional interfaces are included in them. */ public function test_no_additional_interfaces_on_block_editor_disabled_block_types() { - add_filter('use_block_editor_for_post_type', '__return_false'); + add_filter( 'use_block_editor_for_post_type', '__return_false' ); $query = ' query GetType($name:String!) { __type(name: $name) { @@ -83,21 +84,21 @@ interfaces { $this->instance->init(); // Verify the response contains what we put in cache - $response = graphql( - array( + $response = graphql( + [ 'query' => $query, - 'variables' => array( + 'variables' => [ 'name' => 'Post', - ), - ) - ); - $not_included = array( - 'name' => 'NodeWithEditorBlocks', + ], + ] ); + $not_included = [ + 'name' => 'NodeWithEditorBlocks', + ]; $this->assertArrayHasKey( 'data', $response, json_encode( $response ) ); $this->assertNotEmpty( $response['data']['__type']['interfaces'] ); $this->assertNotContains( $not_included, $response['data']['__type']['interfaces'] ); - remove_filter('use_block_editor_for_post_type', '__return_false'); + remove_filter( 'use_block_editor_for_post_type', '__return_false' ); } /** @@ -107,12 +108,12 @@ interfaces { public function test_add_block_fields_to_schema_with_get_allowed_block_types() { add_filter( 'allowed_block_types_all', - function ( $allowed_blocks, $editor_context ) { + static function ( $allowed_blocks, $editor_context ) { if ( isset( $editor_context->post ) && $editor_context->post instanceof \WP_Post && 'post' === $editor_context->post->post_type ) { - return array( + return [ 'core/image', 'core/paragraph', - ); + ]; } return $allowed_blocks; }, @@ -140,46 +141,46 @@ interfaces { // Verify Post meta $response = graphql( - array( + [ 'query' => $query, - 'variables' => array( + 'variables' => [ 'name' => 'Post', - ), - ) + ], + ] ); - $contains_interface = array( + $contains_interface = [ 'name' => 'NodeWithPostEditorBlocks', 'description' => 'Node that has Post content blocks associated with it', - ); + ]; $this->assertArrayHasKey( 'data', $response, json_encode( $response ) ); $this->assertNotEmpty( $response['data']['__type']['interfaces'] ); $this->assertTrue( in_array( $contains_interface, $response['data']['__type']['interfaces'] ) ); // Verify PostEditorBlock meta $response = graphql( - array( + [ 'query' => $query, - 'variables' => array( + 'variables' => [ 'name' => 'PostEditorBlock', - ), - ) + ], + ] ); - $contains_interface = array( + $contains_interface = [ 'name' => 'EditorBlock', 'description' => 'Blocks that can be edited to create content and layouts', - ); - $contains_detail = array( + ]; + $contains_detail = [ 'name' => 'PostEditorBlock', 'description' => '', - ); - $contains_possible_types = array( - array( + ]; + $contains_possible_types = [ + [ 'name' => 'CoreImage', - ), - array( + ], + [ 'name' => 'CoreParagraph', - ), - ); + ], + ]; $this->assertArrayHasKey( 'data', $response, json_encode( $response ) ); $this->assertNotEmpty( $response['data']['__type']['interfaces'] ); @@ -189,21 +190,21 @@ interfaces { // Verify NodeWithPostEditorBlocks meta $response = graphql( - array( + [ 'query' => $query, - 'variables' => array( + 'variables' => [ 'name' => 'NodeWithPostEditorBlocks', - ), - ) + ], + ] ); - $contains_interface = array( + $contains_interface = [ 'name' => 'NodeWithEditorBlocks', 'description' => 'Node that has content blocks associated with it', - ); - $contains_detail = array( + ]; + $contains_detail = [ 'name' => 'NodeWithPostEditorBlocks', 'description' => 'Node that has post content blocks associated with it', - ); + ]; $this->assertArrayHasKey( 'data', $response, json_encode( $response ) ); $this->assertNotEmpty( $response['data']['__type']['interfaces'] ); diff --git a/tests/unit/TraverseHelpersTest.php b/tests/unit/TraverseHelpersTest.php index ec710462..7b9589fc 100644 --- a/tests/unit/TraverseHelpersTest.php +++ b/tests/unit/TraverseHelpersTest.php @@ -2,84 +2,88 @@ namespace WPGraphQL\ContentBlocks\Unit; -use PHPUnit\Framework\TestCase; use WPGraphQL\ContentBlocks\Utilities\TraverseHelpers; class TraverseHelpersTest extends PluginTestCase { - public $post_id; - public function setUp(): void { + public $post_id; + + 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( ' - -

Test

- ' + +

Test

+ + ' ) ), 'post_status' => 'publish', - ) + ] ); } public function tearDown(): void { // your tear down methods here parent::tearDown(); + wp_delete_post( $this->post_id, true ); } + public function testTraverseBlocks() { // Sample blocks data - $blocks = [ - [ - 'blockName' => 'core/group', - 'attrs' => [], - 'innerBlocks' => [ - [ - 'blockName' => 'core/block', - 'attrs' => [ 'ref' => $this->post_id ], - 'innerBlocks' => [] - ] - ] + $blocks = [ + [ + 'blockName' => 'core/group', + 'attrs' => [], + 'innerBlocks' => [ + [ + 'blockName' => 'core/block', + 'attrs' => [ 'ref' => $this->post_id ], + 'innerBlocks' => [], + ], + ], + ], + [ + 'blockName' => 'core/block', + 'attrs' => [ 'ref' => $this->post_id ], + 'innerBlocks' => [], ], - [ - 'blockName' => 'core/block', - 'attrs' => [ 'ref' => $this->post_id ], - 'innerBlocks' => [] - ] ]; // Expected result after replacing reusable blocks - $expected = [ - [ - 'blockName' => 'core/group', - 'attrs' => [], - 'innerBlocks' => [ - [ - 'blockName' => 'core/paragraph', - 'attrs' => [], - 'innerBlocks' => [], - 'innerHTML' => '

Test

', - 'innerContent' => [ 0 => '

Test

'] - ] - ] + $expected = [ + [ + 'blockName' => 'core/group', + 'attrs' => [], + 'innerBlocks' => [ + [ + 'blockName' => 'core/paragraph', + 'attrs' => [], + 'innerBlocks' => [], + 'innerHTML' => '

Test

', + 'innerContent' => [ 0 => '

Test

' ], + ], + ], + ], + [ + 'blockName' => 'core/paragraph', + 'attrs' => [], + 'innerBlocks' => [], + 'innerHTML' => '

Test

', + 'innerContent' => [ 0 => '

Test

' ], ], - [ - 'blockName' => 'core/paragraph', - 'attrs' => [], - 'innerBlocks' => [], - 'innerHTML' => '

Test

', - 'innerContent' => [ 0 => '

Test

'] - ] ]; TraverseHelpers::traverse_blocks( $blocks, [ TraverseHelpers::class, 'replace_reusable_blocks' ], 0, PHP_INT_MAX ); $this->assertEquals( $expected, $blocks ); } -} \ No newline at end of file +} diff --git a/tests/unit/UpdateCalbacksTest.php b/tests/unit/UpdateCalbacksTest.php new file mode 100644 index 00000000..48df9352 --- /dev/null +++ b/tests/unit/UpdateCalbacksTest.php @@ -0,0 +1,22 @@ + true, 'graphql_single_name' => 'faq', 'graphql_plural_name' => 'faqs', - 'supports' => array( 'title', 'author', 'thumbnail' ), + 'supports' => [ 'title', 'author', 'thumbnail' ], 'public' => true, - ) + ] ); register_post_type( 'blocks_enabled', - array( + [ 'show_in_graphql' => true, 'graphql_single_name' => 'blocksEnabled', 'graphql_plural_name' => 'blocksEnabled', - 'supports' => array( 'title', 'editor', 'author', 'thumbnail' ), + 'supports' => [ 'title', 'editor', 'author', 'thumbnail' ], 'public' => true, 'show_in_rest' => true, - ) + ] ); register_post_type( 'blocks_disabled', - array( + [ 'show_in_graphql' => true, 'graphql_single_name' => 'blocksDisabled', 'graphql_plural_name' => 'blocksDisabled', - 'supports' => array( 'title', 'editor', 'author', 'thumbnail' ), + 'supports' => [ 'title', 'editor', 'author', 'thumbnail' ], 'public' => true, 'show_in_rest' => false, // post types that support the editor but do not show in rest will be prevented from using the Block editor - ) + ] ); \WPGraphQL::clear_schema(); @@ -50,8 +51,10 @@ public function tearDown(): void { unregister_post_type( 'blocks_enabled' ); unregister_post_type( 'blocks_disabled' ); \WPGraphQL::clear_schema(); + parent::tearDown(); } + /** * @covers WPHelpers::get_supported_post_types */