|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WPGraphQL\ContentBlocks\Unit; |
| 4 | + |
| 5 | +final class CoreSeparatorTest extends PluginTestCase { |
| 6 | + /** |
| 7 | + * The ID of the post created for the test. |
| 8 | + * |
| 9 | + * @var int |
| 10 | + */ |
| 11 | + public $post_id; |
| 12 | + |
| 13 | + public function setUp(): void { |
| 14 | + parent::setUp(); |
| 15 | + |
| 16 | + $this->post_id = wp_insert_post( |
| 17 | + [ |
| 18 | + 'post_title' => 'Post with Separator', |
| 19 | + 'post_content' => '', |
| 20 | + 'post_status' => 'publish', |
| 21 | + ] |
| 22 | + ); |
| 23 | + |
| 24 | + \WPGraphQL::clear_schema(); |
| 25 | + } |
| 26 | + |
| 27 | + public function tearDown(): void { |
| 28 | + parent::tearDown(); |
| 29 | + |
| 30 | + wp_delete_post( $this->post_id, true ); |
| 31 | + |
| 32 | + \WPGraphQL::clear_schema(); |
| 33 | + } |
| 34 | + |
| 35 | + public function query(): string { |
| 36 | + return ' |
| 37 | + fragment CoreSeparatorBlockFragment on CoreSeparator { |
| 38 | + attributes { |
| 39 | + align |
| 40 | + anchor |
| 41 | + backgroundColor |
| 42 | + className |
| 43 | + cssClassName |
| 44 | + gradient |
| 45 | + lock |
| 46 | + # metadata |
| 47 | + opacity |
| 48 | + style |
| 49 | + } |
| 50 | + } |
| 51 | +
|
| 52 | + query Post( $id: ID! ) { |
| 53 | + post(id: $id, idType: DATABASE_ID) { |
| 54 | + databaseId |
| 55 | + editorBlocks { |
| 56 | + apiVersion |
| 57 | + blockEditorCategoryName |
| 58 | + clientId |
| 59 | + cssClassNames |
| 60 | + innerBlocks { |
| 61 | + name |
| 62 | + } |
| 63 | + isDynamic |
| 64 | + name |
| 65 | + parentClientId |
| 66 | + renderedHtml |
| 67 | + ...CoreSeparatorBlockFragment |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + '; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test the retrieval of core/separator block fields and attributes. |
| 76 | + */ |
| 77 | + public function test_retrieve_core_separator_attribute_fields(): void { |
| 78 | + $block_content = ' |
| 79 | + <!-- wp:separator {"lock":{"move":true,"remove":true},"align":"wide","className":"is-style-dots","style":{"color":{"gradient":"linear-gradient(135deg,rgb(6,147,227) 1%,rgb(155,81,224) 100%)"}}} --> |
| 80 | + <hr class="wp-block-separator alignwide has-alpha-channel-opacity"/> |
| 81 | + <!-- /wp:separator --> |
| 82 | + '; |
| 83 | + |
| 84 | + // Set post content. |
| 85 | + wp_update_post( |
| 86 | + [ |
| 87 | + 'ID' => $this->post_id, |
| 88 | + 'post_content' => $block_content, |
| 89 | + ] |
| 90 | + ); |
| 91 | + |
| 92 | + $query = $this->query(); |
| 93 | + $variables = [ |
| 94 | + 'id' => $this->post_id, |
| 95 | + ]; |
| 96 | + |
| 97 | + $actual = graphql( compact( 'query', 'variables' ) ); |
| 98 | + |
| 99 | + $this->assertArrayNotHasKey( 'errors', $actual, 'There should not be any errors' ); |
| 100 | + $this->assertArrayHasKey( 'data', $actual, 'The data key should be present' ); |
| 101 | + $this->assertArrayHasKey( 'post', $actual['data'], 'The post key should be present' ); |
| 102 | + $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); |
| 103 | + $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); |
| 104 | + |
| 105 | + $block = $actual['data']['post']['editorBlocks'][0]; |
| 106 | + |
| 107 | + // Verify the block data. |
| 108 | + $this->assertNotEmpty( $block['apiVersion'], 'The apiVersion should be present' ); |
| 109 | + $this->assertEquals( 'design', $block['blockEditorCategoryName'], 'The blockEditorCategoryName should be text' ); |
| 110 | + $this->assertNotEmpty( $block['clientId'], 'The clientId should be present' ); |
| 111 | + $this->assertNotEmpty( $block['cssClassNames'], 'There should be cssClassNames' ); |
| 112 | + $this->assertEmpty( $block['innerBlocks'], 'There should be no inner blocks' ); |
| 113 | + $this->assertEquals( 'core/separator', $block['name'], 'The block name should be core/separator' ); |
| 114 | + $this->assertEmpty( $block['parentClientId'], 'There should be no parentClientId' ); |
| 115 | + $this->assertNotEmpty( $block['renderedHtml'], 'The renderedHtml should be present' ); |
| 116 | + |
| 117 | + // Verify the attributes. |
| 118 | + $this->assertEquals( |
| 119 | + [ |
| 120 | + 'align' => 'wide', |
| 121 | + 'anchor' => null, |
| 122 | + 'backgroundColor' => null, |
| 123 | + 'className' => 'is-style-dots', |
| 124 | + 'cssClassName' => 'wp-block-separator alignwide has-alpha-channel-opacity', |
| 125 | + 'gradient' => null, |
| 126 | + 'opacity' => 'alpha-channel', |
| 127 | + 'style' => wp_json_encode( |
| 128 | + [ |
| 129 | + 'color' => [ |
| 130 | + 'gradient' => 'linear-gradient(135deg,rgb(6,147,227) 1%,rgb(155,81,224) 100%)', |
| 131 | + ], |
| 132 | + ] |
| 133 | + ), |
| 134 | + 'lock' => wp_json_encode( |
| 135 | + [ |
| 136 | + 'move' => true, |
| 137 | + 'remove' => true, |
| 138 | + ] |
| 139 | + ), |
| 140 | + ], |
| 141 | + $block['attributes'], |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Tests additional CoreSeparatorAttributes values. |
| 147 | + * |
| 148 | + * Covers: `anchor`, `backgroundColor`, and `gradient`. |
| 149 | + */ |
| 150 | + public function test_retrieve_core_separator_attributes(): void { |
| 151 | + $block_content = ' |
| 152 | + <!-- wp:separator {"gradient":"gradient-10","backgroundColor":"accent-4"} --> |
| 153 | + <hr class="wp-block-separator has-text-color has-accent-4-color has-alpha-channel-opacity has-accent-4-background-color has-background" id="test-anchor"/> |
| 154 | + <!-- /wp:separator --> |
| 155 | + '; |
| 156 | + |
| 157 | + // Set post content. |
| 158 | + wp_update_post( |
| 159 | + [ |
| 160 | + 'ID' => $this->post_id, |
| 161 | + 'post_content' => $block_content, |
| 162 | + ] |
| 163 | + ); |
| 164 | + |
| 165 | + $query = $this->query(); |
| 166 | + $variables = [ |
| 167 | + 'id' => $this->post_id, |
| 168 | + ]; |
| 169 | + |
| 170 | + $actual = graphql( compact( 'query', 'variables' ) ); |
| 171 | + |
| 172 | + $this->assertArrayNotHasKey( 'errors', $actual, 'There should not be any errors' ); |
| 173 | + $this->assertArrayHasKey( 'data', $actual, 'The data key should be present' ); |
| 174 | + $this->assertArrayHasKey( 'post', $actual['data'], 'The post key should be present' ); |
| 175 | + $this->assertEquals( $this->post_id, $actual['data']['post']['databaseId'], 'The post ID should match' ); |
| 176 | + $this->assertEquals( 1, count( $actual['data']['post']['editorBlocks'] ) ); |
| 177 | + $this->assertEquals( 'core/separator', $actual['data']['post']['editorBlocks'][0]['name'], 'The block name should be core/separator' ); |
| 178 | + |
| 179 | + // Verify the attributes. |
| 180 | + $this->assertEquals( |
| 181 | + [ |
| 182 | + 'align' => null, |
| 183 | + 'anchor' => 'test-anchor', // Previously untested. |
| 184 | + 'backgroundColor' => 'accent-4', // Previously untested. |
| 185 | + 'className' => null, |
| 186 | + 'cssClassName' => 'wp-block-separator has-text-color has-accent-4-color has-alpha-channel-opacity has-accent-4-background-color has-background', |
| 187 | + 'gradient' => 'gradient-10', // Previously untested. |
| 188 | + 'opacity' => 'alpha-channel', |
| 189 | + 'style' => null, |
| 190 | + 'lock' => null, |
| 191 | + ], |
| 192 | + $actual['data']['post']['editorBlocks'][0]['attributes'], |
| 193 | + ); |
| 194 | + } |
| 195 | +} |
0 commit comments