diff --git a/tests/unit/CoreParagraphTest.php b/tests/unit/CoreParagraphTest.php new file mode 100644 index 00000000..eaf43bc4 --- /dev/null +++ b/tests/unit/CoreParagraphTest.php @@ -0,0 +1,336 @@ +post_id = wp_insert_post( + [ + 'post_title' => 'Post with Paragraph', + 'post_content' => '', + 'post_status' => 'publish', + ] + ); + + \WPGraphQL::clear_schema(); + } + + /** + * Tear down the test environment. + */ + public function tearDown(): void { + parent::tearDown(); + + wp_delete_post( $this->post_id, true ); + + \WPGraphQL::clear_schema(); + } + + /** + * Provide the GraphQL query for testing. + * + * @return string The GraphQL query. + */ + public function query(): string { + return ' + fragment CoreParagraphBlockFragment on CoreParagraph { + attributes { + align + anchor + backgroundColor + className + content + cssClassName + dropCap + direction + fontFamily + fontSize + gradient + lock + metadata + placeholder + style + textColor + } + } + query Post( $id: ID! ) { + post(id: $id, idType: DATABASE_ID) { + databaseId + editorBlocks { + name + ...CoreParagraphBlockFragment + } + } + } + '; + } + + /** + * Test the retrieval of core/paragraph block attributes. + * + * Attributes covered: + * - align + * - backgroundColor + * - textColor + * - fontSize + * - fontFamily + * - content + * - cssClassName + * + * @return void + */ + public function test_retrieve_core_paragraph_attributes() { + $block_content = ' + +

This is a test paragraph with various attributes.

+ + '; + + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $actual = graphql( + [ + 'query' => $this->query(), + 'variables' => [ 'id' => $this->post_id ], + ] + ); + + $this->assertArrayNotHasKey( 'errors', $actual ); + $this->assertArrayHasKey( 'data', $actual ); + $this->assertArrayHasKey( 'post', $actual['data'] ); + + $block = $actual['data']['post']['editorBlocks'][0]; + $attributes = $block['attributes']; + + $this->assertEquals( 'core/paragraph', $block['name'] ); + $this->assertEquals( + [ + 'align' => 'center', + 'anchor' => null, + 'backgroundColor' => 'pale-cyan-blue', + 'className' => null, + 'content' => 'This is a test paragraph with various attributes.', + 'cssClassName' => 'has-text-align-center has-vivid-red-color has-pale-cyan-blue-background-color has-text-color has-background has-large-font-size has-helvetica-arial-font-family', + 'dropCap' => false, + 'direction' => null, + 'fontFamily' => 'helvetica-arial', + 'fontSize' => 'large', + 'gradient' => null, + 'lock' => null, + 'metadata' => null, + 'placeholder' => null, + 'style' => null, + 'textColor' => 'vivid-red', + ], + $attributes + ); + } + + /** + * Test retrieval of core/paragraph block with drop cap and custom styles. + * + * Attributes covered: + * - dropCap + * - style (typography and spacing) + * - content + * - cssClassName + * + * @return void + */ + public function test_retrieve_core_paragraph_with_drop_cap_and_custom_styles() { + $block_content = ' + +

This is a paragraph with drop cap and custom styles.

+ + '; + + 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' => 'This is a paragraph with drop cap and custom styles.', + 'cssClassName' => 'has-drop-cap', + 'dropCap' => true, + 'direction' => null, + 'fontFamily' => null, + 'fontSize' => null, + 'gradient' => null, + 'lock' => null, + 'metadata' => null, + 'placeholder' => null, + 'style' => wp_json_encode( + [ + 'typography' => [ + 'lineHeight' => '2', + 'textTransform' => 'uppercase', + ], + 'spacing' => [ + 'padding' => [ + 'top' => '20px', + 'right' => '20px', + 'bottom' => '20px', + 'left' => '20px', + ], + ], + ] + ), + 'textColor' => null, + ], + $attributes + ); + } + + /** + * Test retrieval of core/paragraph block with direction and gradient. + * + * Attributes covered: + * - align + * - direction + * - gradient + * - content + * - cssClassName + * + * @return void + */ + public function test_retrieve_core_paragraph_with_direction_and_gradient() { + $block_content = ' + +

This is a right-aligned RTL paragraph with a gradient background.

+ + '; + + 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' => 'right', + 'anchor' => null, + 'backgroundColor' => null, + 'className' => null, + 'content' => 'This is a right-aligned RTL paragraph with a gradient background.', + 'cssClassName' => 'has-text-align-right has-vivid-cyan-blue-to-vivid-purple-gradient-background', + 'dropCap' => false, + 'direction' => 'rtl', + 'fontFamily' => null, + 'fontSize' => null, + 'gradient' => 'vivid-cyan-blue-to-vivid-purple', + 'lock' => null, + 'metadata' => null, + 'placeholder' => null, + 'style' => null, + 'textColor' => null, + ], + $attributes + ); + } + + /** + * Test retrieval of core/paragraph block with additional attributes. + * + * Attributes covered: + * - anchor + * - className + * - lock + * - metadata + * - placeholder + * + * @return void + */ + public function test_retrieve_core_paragraph_with_additional_attributes() { + $block_content = ' + +

This is a paragraph with additional attributes.

+ + '; + + 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' => 'test-anchor', + 'backgroundColor' => null, + 'className' => 'custom-class', + 'content' => 'This is a paragraph with additional attributes.', + 'cssClassName' => 'custom-class', + 'dropCap' => false, + 'direction' => null, + 'fontFamily' => null, + 'fontSize' => null, + 'gradient' => null, + 'lock' => '{"move":true,"remove":true}', + 'metadata' => '{"someKey":"someValue"}', + 'placeholder' => 'Type here...', + 'style' => null, + 'textColor' => null, + ], + $attributes + ); + } +}