HEX
Server: Apache/2.4.66 (Debian)
System: Linux 6dfabc3b2241 6.8.0-71-generic #71-Ubuntu SMP PREEMPT_DYNAMIC Tue Jul 22 16:52:38 UTC 2025 x86_64
User: (1000)
PHP: 8.3.30
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/themes/custom-theme/tests/ResolveBlockTest.php
<?php

use PHPUnit\Framework\TestCase;

class ResolveBlockTest extends TestCase
{
    protected function setUp(): void
    {
        $GLOBALS['_mock_wp_get_attachment_image_src'] = null;
        $GLOBALS['_mock_get_post_meta'] = null;
        $GLOBALS['_mock_get_field'] = null;
        $GLOBALS['_mock_wp_get_attachment_image_url'] = null;
        $GLOBALS['_mock_wp_get_attachment_metadata'] = null;
        $GLOBALS['_mock_wp_get_attachment_url'] = null;
    }

    public function testEmptyBlockNameWithoutInnerHTML(): void
    {
        $block = ['blockName' => null, 'innerHTML' => ''];
        $result = headless_resolve_block($block);

        $this->assertSame([], $result);
    }

    public function testEmptyBlockNameWithInnerHTML(): void
    {
        $block = ['blockName' => null, 'innerHTML' => '<p>Hello</p>'];
        $result = headless_resolve_block($block);

        $this->assertCount(1, $result);
        $this->assertSame('core/html', $result[0]['name']);
        $this->assertSame('<p>Hello</p>', $result[0]['attributes']['content']);
    }

    public function testEmptyBlockNameWithWhitespaceOnlyInnerHTML(): void
    {
        $block = ['blockName' => null, 'innerHTML' => '   '];
        $result = headless_resolve_block($block);

        $this->assertSame([], $result);
    }

    public function testCoreBlockWithInnerHTML(): void
    {
        $block = [
            'blockName'   => 'core/paragraph',
            'attrs'       => [],
            'innerHTML'   => '<p>Some text</p>',
            'innerBlocks' => [],
        ];
        $result = headless_resolve_block($block);

        $this->assertCount(1, $result);
        $this->assertSame('core/paragraph', $result[0]['name']);
        $this->assertSame('<p>Some text</p>', $result[0]['attributes']['content']);
    }

    public function testCoreBlockEmptyAttrsReturnsStdClass(): void
    {
        $block = [
            'blockName'   => 'core/spacer',
            'attrs'       => [],
            'innerHTML'   => '',
            'innerBlocks' => [],
        ];
        $result = headless_resolve_block($block);

        $this->assertCount(1, $result);
        $this->assertInstanceOf(\stdClass::class, $result[0]['attributes']);
    }

    public function testAcfBlockTextFieldsPassThrough(): void
    {
        $block = [
            'blockName'   => 'acf/hero',
            'attrs'       => [
                'data' => [
                    'hero_heading'      => 'Hello',
                    '_hero_heading'     => 'field_hero_heading',
                    'hero_description'  => 'World',
                    '_hero_description' => 'field_hero_description',
                ],
            ],
            'innerHTML'   => '',
            'innerBlocks' => [],
        ];
        $result = headless_resolve_block($block);

        $this->assertCount(1, $result);
        $this->assertSame('acf/hero', $result[0]['name']);
        $this->assertSame('Hello', $result[0]['attributes']['hero_heading']);
        $this->assertSame('World', $result[0]['attributes']['hero_description']);
        $this->assertArrayNotHasKey('_hero_heading', $result[0]['attributes']);
        $this->assertArrayNotHasKey('_hero_description', $result[0]['attributes']);
    }

    public function testAcfBlockImageResolution(): void
    {
        $GLOBALS['_mock_wp_get_attachment_image_src'] = function ($id, $size) {
            return ['http://example.com/image.jpg', 1200, 800, false];
        };
        $GLOBALS['_mock_get_post_meta'] = function ($post_id, $key, $single) {
            return 'Alt text';
        };

        $block = [
            'blockName'   => 'acf/hero',
            'attrs'       => [
                'data' => [
                    'hero_image'  => '42',
                    '_hero_image' => 'field_hero_image',
                ],
            ],
            'innerHTML'   => '',
            'innerBlocks' => [],
        ];
        $result = headless_resolve_block($block);

        $expected = [
            'url'    => 'http://example.com/image.jpg',
            'width'  => 1200,
            'height' => 800,
            'alt'    => 'Alt text',
        ];
        $this->assertSame($expected, $result[0]['attributes']['hero_image']);
    }

    public function testAcfBlockNoDataReturnsStdClass(): void
    {
        $block = [
            'blockName'   => 'acf/hero',
            'attrs'       => [],
            'innerHTML'   => '',
            'innerBlocks' => [],
        ];
        $result = headless_resolve_block($block);

        $this->assertCount(1, $result);
        $this->assertInstanceOf(\stdClass::class, $result[0]['attributes']);
    }

    public function testInnerBlocksFlattenedOneLevel(): void
    {
        $block = [
            'blockName'   => 'core/quote',
            'attrs'       => [],
            'innerHTML'   => '<blockquote>Quote</blockquote>',
            'innerBlocks' => [
                [
                    'blockName'   => 'core/paragraph',
                    'attrs'       => [],
                    'innerHTML'   => '<p>Inner text</p>',
                    'innerBlocks' => [],
                ],
            ],
        ];
        $result = headless_resolve_block($block);

        $this->assertCount(2, $result);
        $this->assertSame('core/quote', $result[0]['name']);
        $this->assertSame('core/paragraph', $result[1]['name']);
        $this->assertSame('<p>Inner text</p>', $result[1]['attributes']['content']);
    }

    public function testInnerBlocksFlattenedDeeply(): void
    {
        $block = [
            'blockName'   => 'core/group',
            'attrs'       => [],
            'innerHTML'   => '<div>Group</div>',
            'innerBlocks' => [
                [
                    'blockName'   => 'core/quote',
                    'attrs'       => [],
                    'innerHTML'   => '<blockquote>Quote</blockquote>',
                    'innerBlocks' => [
                        [
                            'blockName'   => 'core/paragraph',
                            'attrs'       => [],
                            'innerHTML'   => '<p>Deep</p>',
                            'innerBlocks' => [],
                        ],
                    ],
                ],
            ],
        ];
        $result = headless_resolve_block($block);

        $this->assertCount(3, $result);
        $this->assertSame('core/group', $result[0]['name']);
        $this->assertSame('core/quote', $result[1]['name']);
        $this->assertSame('core/paragraph', $result[2]['name']);
        $this->assertSame('<p>Deep</p>', $result[2]['attributes']['content']);
    }
}