From ea50cf0a27d4be862bece95cf85c4f1c6b6bfc81 Mon Sep 17 00:00:00 2001 From: Ashutosh Gautam Date: Tue, 24 Sep 2024 16:05:30 +0530 Subject: [PATCH] feat: Add tests for Code Block --- tests/unit/CoreCodeTest.php | 264 ++++++++++++++++++++++++++++++++++++ 1 file changed, 264 insertions(+) create mode 100644 tests/unit/CoreCodeTest.php diff --git a/tests/unit/CoreCodeTest.php b/tests/unit/CoreCodeTest.php new file mode 100644 index 00000000..3666fec8 --- /dev/null +++ b/tests/unit/CoreCodeTest.php @@ -0,0 +1,264 @@ +post_id = wp_insert_post( + [ + 'post_title' => 'Post with Code Block', + '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 CoreCodeBlockFragment on CoreCode { + attributes { + align + anchor + backgroundColor + borderColor + className + content + cssClassName + fontFamily + fontSize + gradient + lock + metadata + style + textColor + } + } + query Post( $id: ID! ) { + post(id: $id, idType: DATABASE_ID) { + databaseId + editorBlocks { + name + ...CoreCodeBlockFragment + } + } + } + '; + } + + /** + * Test the retrieval of core/code block attributes. + * + * Attributes covered: + * - content + * - cssClassName + * - backgroundColor + * - textColor + * - fontSize + * - fontFamily + * + * @return void + */ + public function test_retrieve_core_code_attributes() { + $block_content = ' + +
function hello_world() {
+    console.log("Hello, World!");
+}
+ + '; + + 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( 'core/code', $block['name'] ); + $this->assertEquals( + [ + 'align' => null, + 'anchor' => null, + 'backgroundColor' => 'pale-cyan-blue', + 'borderColor' => null, + 'className' => null, + 'content' => "function hello_world() {\n console.log(\"Hello, World!\");\n}", + 'cssClassName' => 'wp-block-code has-vivid-red-color has-pale-cyan-blue-background-color has-text-color has-background has-large-font-size has-monospace-font-family', + 'fontFamily' => 'monospace', + 'fontSize' => 'large', + 'gradient' => null, + 'lock' => null, + 'metadata' => null, + 'style' => null, + 'textColor' => 'vivid-red', + ], + $attributes + ); + } + + /** + * Test retrieval of core/code block with custom styles and border color. + * + * Attributes covered: + * - borderColor + * - style + * - className + * - align + * + * @return void + */ + public function test_retrieve_core_code_with_custom_styles() { + $block_content = ' + +
const greeting = "Hello, styled code!";
+ + '; + + 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' => null, + 'borderColor' => 'vivid-cyan-blue', + 'className' => 'custom-class', + 'content' => 'const greeting = "Hello, styled code!";', + 'cssClassName' => 'wp-block-code alignwide custom-class has-border-color has-vivid-cyan-blue-border-color', + 'fontFamily' => null, + 'fontSize' => null, + 'gradient' => null, + 'lock' => null, + 'metadata' => null, + 'style' => wp_json_encode( + [ + 'border' => [ + 'width' => '2px', + ], + 'spacing' => [ + 'padding' => [ + 'top' => '20px', + 'right' => '20px', + 'bottom' => '20px', + 'left' => '20px', + ], + ], + ] + ), + 'textColor' => null, + ], + $attributes + ); + } + + /** + * Test retrieval of core/code block with gradient and additional attributes. + * + * Attributes covered: + * - gradient + * - anchor + * - lock + * - metadata + * + * @return void + */ + public function test_retrieve_core_code_with_gradient_and_additional_attributes() { + $block_content = ' + +
console.log("Gradient and locked code block");
+ + '; + + 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, + 'borderColor' => null, + 'className' => null, + 'content' => 'console.log("Gradient and locked code block");', + 'cssClassName' => 'wp-block-code has-vivid-cyan-blue-to-vivid-purple-gradient-background', + 'fontFamily' => null, + 'fontSize' => null, + 'gradient' => 'vivid-cyan-blue-to-vivid-purple', + 'lock' => '{"move":true,"remove":true}', + 'metadata' => '{"someKey":"someValue"}', + 'style' => null, + 'textColor' => null, + ], + $attributes + ); + } +}