diff --git a/.changeset/khaki-forks-prove.md b/.changeset/khaki-forks-prove.md new file mode 100644 index 00000000..9e2e1c09 --- /dev/null +++ b/.changeset/khaki-forks-prove.md @@ -0,0 +1,71 @@ +--- +"@wpengine/wp-graphql-content-blocks": patch +--- + +Added CoreListItem block for WPGraphQL Content Blocks. + +```gql + query postQuery($id: ID!) { + post(id: $id, idType: DATABASE_ID, asPreview: false) { + title + editorBlocks(flat: false) { + name + ... on CoreList { + type + name + renderedHtml + innerBlocks { + ... on CoreListItem { + type + name + + renderedHtml + } + } + } + } + } + } +``` + +```json +{ + "data": { + "post": { + "title": "Hello world!", + "editorBlocks": [ + { + "name": "core/list", + "type": "CoreList", + "renderedHtml": "\n
    \n
  1. List item 1
  2. \n\n\n\n
  3. List item 2\n\n
  4. \n\n\n\n
  5. List item 3
  6. \n\n\n\n
  7. List item 4
  8. \n
\n", + "innerBlocks": [ + { + "type": "CoreListItem", + "name": "core/list-item", + "renderedHtml": "\n
  • List item 1
  • \n" + }, + { + "type": "CoreListItem", + "name": "core/list-item", + "renderedHtml": "\n
  • List item 2\n\n
  • \n" + }, + { + "type": "CoreListItem", + "name": "core/list-item", + "renderedHtml": "\n
  • List item 3
  • \n" + }, + { + "type": "CoreListItem", + "name": "core/list-item", + "renderedHtml": "\n
  • List item 4
  • \n" + } + ] + }, + { + "name": "core/paragraph" + } + ] + } + } +} +``` \ No newline at end of file diff --git a/includes/Blocks/CoreListItem.php b/includes/Blocks/CoreListItem.php new file mode 100644 index 00000000..58fed771 --- /dev/null +++ b/includes/Blocks/CoreListItem.php @@ -0,0 +1,27 @@ + [ + 'type' => 'string', + 'selector' => 'li', + 'source' => 'attribute', + 'attribute' => 'class', + ], + ]; +} diff --git a/tests/unit/CoreListTest.php b/tests/unit/CoreListTest.php index 36cf7fc0..59f96e11 100644 --- a/tests/unit/CoreListTest.php +++ b/tests/unit/CoreListTest.php @@ -2,6 +2,10 @@ namespace WPGraphQL\ContentBlocks\Unit; +/** + * @group block + * @group core-list + */ final class CoreListTest extends PluginTestCase { /** * The ID of the post created for the test. @@ -701,4 +705,123 @@ className $block['attributes'], ); } + + + public function test_retrieve_core_list_item_values(): void { + $block_content = << +
      +
    1. List item 1
    2. + + + +
    3. List item 2 + +
    4. + + + +
    5. List item 3
    6. + + + +
    7. List item 4
    8. +
    + +HTML; + // Set post content. + wp_update_post( + [ + 'ID' => $this->post_id, + 'post_content' => $block_content, + ] + ); + + $query = ' + query postQuery($id: ID!) { + post(id: $id, idType: DATABASE_ID, asPreview: false) { + title + editorBlocks(flat: false) { + name + ... on CoreList { + type + name + renderedHtml + innerBlocks { + ... on CoreListItem { + type + name + + renderedHtml + } + } + } + } + } + } + '; + + $variables = [ + 'id' => $this->post_id, + ]; + + $actual = graphql( compact( 'query', 'variables' ) ); + + $this->assertArrayNotHasKey( 'errors', $actual, 'There should not be any errors' ); + $this->assertArrayHasKey( 'data', $actual, 'The data key should be present' ); + $this->assertArrayHasKey( 'post', $actual['data'], 'The post key should be present' ); + + $editorBlocks = $actual['data']['post']['editorBlocks']; + $this->assertEquals( 1, count($editorBlocks)); + + $editorBlock = $editorBlocks[0]; + $this->assertEquals( 'core/list', $editorBlock['name'], 'The block name should be core/list' ); + $this->assertEquals( 'CoreList', $editorBlock['type'], 'The block type should be CoreList' ); + $this->assertArrayHasKey( 'renderedHtml', $editorBlock); + $this->assertArrayHasKey( 'innerBlocks', $editorBlock); + + $innerBlocks = $editorBlock['innerBlocks']; + $this->assertEquals( 4, count($innerBlocks)); + $firstBlock = $innerBlocks[0]; + $secondBlock = $innerBlocks[0]; + + /** + * No child list items + */ + $this->assertEquals('CoreListItem', $firstBlock['type'], 'The block type should be CoreListItem'); + $this->assertEquals('core/list-item', $firstBlock['name'], 'The block name should be core/list-item'); + $this->assertEquals('
  • List item 1
  • ', trim($firstBlock['renderedHtml']), 'The block should have valid HTML block'); + + + /** + * Child list items + */ + $html = <<List item 2 + + +HTML; + + $html = trim(preg_replace('/\s+/', ' ', $html)); + $renderedHtml = trim(preg_replace('/\s+/', ' ', $innerBlocks[1]['renderedHtml'] )); + $this->assertEquals($html, $renderedHtml, 'The block should have valid HTML block'); + + } }