|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WPGraphQL\ContentBlocks\Unit; |
| 4 | + |
| 5 | +final class CorePostTermsTest extends PluginTestCase { |
| 6 | + |
| 7 | + /** |
| 8 | + * The ID of the post created for the test. |
| 9 | + * |
| 10 | + * @var int |
| 11 | + */ |
| 12 | + public $post_id; |
| 13 | + |
| 14 | + public function setUp(): void { |
| 15 | + parent::setUp(); |
| 16 | + |
| 17 | + $this->post_id = wp_insert_post( |
| 18 | + [ |
| 19 | + 'post_title' => 'Test Post', |
| 20 | + 'post_content' => '', |
| 21 | + 'post_status' => 'publish', |
| 22 | + ] |
| 23 | + ); |
| 24 | + |
| 25 | + \WPGraphQL::clear_schema(); |
| 26 | + } |
| 27 | + |
| 28 | + public function tearDown(): void { |
| 29 | + wp_delete_post($this->post_id, true); |
| 30 | + \WPGraphQL::clear_schema(); |
| 31 | + |
| 32 | + parent::tearDown(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Get the GraphQL query for the CorePostTerms block. |
| 37 | + */ |
| 38 | + public function query(): string { |
| 39 | + return ' |
| 40 | + query TestPostTerms($id: ID!) { |
| 41 | + post(id: $id, idType: DATABASE_ID) { |
| 42 | + databaseId |
| 43 | + editorBlocks { |
| 44 | + __typename |
| 45 | + ... on CorePostTerms { |
| 46 | + prefix |
| 47 | + suffix |
| 48 | + term |
| 49 | + terms { |
| 50 | + __typename |
| 51 | + nodes { |
| 52 | + id |
| 53 | + name |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + '; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Test that the CorePostTerms block retrieves attributes and terms correctly. |
| 65 | + */ |
| 66 | + public function test_retrieve_core_post_terms(): void { |
| 67 | + $block_content = '<!-- wp:post-terms {"prefix":"Before","suffix":"After","term":"category"} /-->'; |
| 68 | + |
| 69 | + wp_update_post( |
| 70 | + [ |
| 71 | + 'ID' => $this->post_id, |
| 72 | + 'post_content' => $block_content, |
| 73 | + ] |
| 74 | + ); |
| 75 | + |
| 76 | + $variables = [ 'id' => $this->post_id ]; |
| 77 | + $query = $this->query(); |
| 78 | + $actual = graphql(compact('query', 'variables')); |
| 79 | + |
| 80 | + $node = $actual['data']['post']; |
| 81 | + |
| 82 | + $this->assertEquals($this->post_id, $node['databaseId']); |
| 83 | + $this->assertArrayHasKey('editorBlocks', $node); |
| 84 | + $this->assertCount(1, $node['editorBlocks']); |
| 85 | + |
| 86 | + $block = $node['editorBlocks'][0]; |
| 87 | + |
| 88 | + $this->assertEquals('CorePostTerms', $block['__typename']); |
| 89 | + $this->assertEquals('Before', $block['prefix']); |
| 90 | + $this->assertEquals('After', $block['suffix']); |
| 91 | + $this->assertEquals('category', $block['term']); |
| 92 | + |
| 93 | + $this->assertArrayHasKey('terms', $block); |
| 94 | + $this->assertArrayHasKey('nodes', $block['terms']); |
| 95 | + $this->assertIsArray($block['terms']['nodes']); |
| 96 | + |
| 97 | + $this->assertEquals('CorePostTermsToTermNodeConnection', $block['terms']['__typename']); |
| 98 | + } |
| 99 | +} |
0 commit comments