From 77899ff346c3be87be5c6370b537a1fda5b1fd64 Mon Sep 17 00:00:00 2001 From: Ashutosh Gautam Date: Wed, 18 Sep 2024 20:55:08 +0530 Subject: [PATCH] feat: Add CoreCodeTest for Code Block --- tests/unit/CoreCodeTest.php | 138 ++++++++++++++++++++++++++++++++++++ 1 file changed, 138 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..e107c23a --- /dev/null +++ b/tests/unit/CoreCodeTest.php @@ -0,0 +1,138 @@ +post_id = wp_insert_post( + [ + 'post_title' => 'Post with Code', + 'post_content' => '', + 'post_status' => 'publish', + ] + ); + + \WPGraphQL::clear_schema(); + } + + public function tearDown(): void { + parent::tearDown(); + + wp_delete_post( $this->post_id, true ); + + \WPGraphQL::clear_schema(); + } + + public function query(): string { + return ' + fragment CoreCodeBlockFragment on CoreCode { + attributes { + content + cssClassName + fontSize + style + } + } + query Post( $id: ID! ) { + post(id: $id, idType: DATABASE_ID) { + databaseId + editorBlocks { + name + ...CoreCodeBlockFragment + } + } + } + '; + } + + public function test_retrieve_core_code_basic_attributes() { + $block_content = ' + +
function helloWorld() {
+  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('function helloWorld() { + console.log("Hello, World!"); +}', $attributes['content']); + $this->assertEquals('wp-block-code', $attributes['cssClassName']); + } + + public function test_retrieve_core_code_with_custom_font_size() { + $block_content = ' + +
var x = 10;
+ + '; + + 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('var x = 10;', $attributes['content']); + $this->assertEquals('large', $attributes['fontSize']); + } + + public function test_retrieve_core_code_with_custom_styles() { + $block_content = ' + +
const y = 20;
+ + '; + + 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('const y = 20;', $attributes['content']); + + $style = json_decode($attributes['style'], true); + $this->assertEquals('#f0f0f0', $style['color']['background']); + $this->assertEquals('#333333', $style['color']['text']); + $this->assertEquals('16px', $style['typography']['fontSize']); + } +}