|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test the block visibility block support. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage Block Supports |
| 7 | + * @since 6.9.0 |
| 8 | + * |
| 9 | + * @group block-supports |
| 10 | + * |
| 11 | + * @covers ::wp_render_block_visibility_support |
| 12 | + */ |
| 13 | +class Tests_Block_Supports_Block_Visibility extends WP_UnitTestCase { |
| 14 | + /** |
| 15 | + * @var string|null |
| 16 | + */ |
| 17 | + private $test_block_name; |
| 18 | + |
| 19 | + public function set_up() { |
| 20 | + parent::set_up(); |
| 21 | + $this->test_block_name = null; |
| 22 | + } |
| 23 | + |
| 24 | + public function tear_down() { |
| 25 | + unregister_block_type( $this->test_block_name ); |
| 26 | + $this->test_block_name = null; |
| 27 | + parent::tear_down(); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Registers a new block for testing block visibility support. |
| 32 | + * |
| 33 | + * @param string $block_name Name for the test block. |
| 34 | + * @param array $supports Array defining block support configuration. |
| 35 | + * |
| 36 | + * @return WP_Block_Type The block type for the newly registered test block. |
| 37 | + */ |
| 38 | + private function register_visibility_block_with_support( $block_name, $supports = array() ) { |
| 39 | + $this->test_block_name = $block_name; |
| 40 | + register_block_type( |
| 41 | + $this->test_block_name, |
| 42 | + array( |
| 43 | + 'api_version' => 3, |
| 44 | + 'attributes' => array( |
| 45 | + 'metadata' => array( |
| 46 | + 'type' => 'object', |
| 47 | + ), |
| 48 | + ), |
| 49 | + 'supports' => $supports, |
| 50 | + ) |
| 51 | + ); |
| 52 | + $registry = WP_Block_Type_Registry::get_instance(); |
| 53 | + |
| 54 | + return $registry->get_registered( $this->test_block_name ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Tests that block visibility support renders empty string when block is hidden |
| 59 | + * and blockVisibility support is opted in. |
| 60 | + * |
| 61 | + * @ticket 64061 |
| 62 | + */ |
| 63 | + public function test_block_visibility_support_hides_block_when_visibility_false() { |
| 64 | + $block_type = $this->register_visibility_block_with_support( |
| 65 | + 'test/visibility-block', |
| 66 | + array( 'blockVisibility' => true ) |
| 67 | + ); |
| 68 | + |
| 69 | + $block_content = '<p>This is a test block.</p>'; |
| 70 | + $block = array( |
| 71 | + 'blockName' => 'test/visibility-block', |
| 72 | + 'attrs' => array( |
| 73 | + 'metadata' => array( |
| 74 | + 'blockVisibility' => false, |
| 75 | + ), |
| 76 | + ), |
| 77 | + ); |
| 78 | + |
| 79 | + $result = wp_render_block_visibility_support( $block_content, $block ); |
| 80 | + |
| 81 | + $this->assertSame( '', $result, 'Block content should be empty when blockVisibility is false and support is opted in.' ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Tests that block visibility support renders block normally when visibility is false |
| 86 | + * but blockVisibility support is not opted in. |
| 87 | + * |
| 88 | + * @ticket 64061 |
| 89 | + */ |
| 90 | + public function test_block_visibility_support_shows_block_when_support_not_opted_in() { |
| 91 | + $block_type = $this->register_visibility_block_with_support( |
| 92 | + 'test/visibility-block', |
| 93 | + array( 'blockVisibility' => false ) |
| 94 | + ); |
| 95 | + |
| 96 | + $block_content = '<p>This is a test block.</p>'; |
| 97 | + $block = array( |
| 98 | + 'blockName' => 'test/visibility-block', |
| 99 | + 'attrs' => array( |
| 100 | + 'metadata' => array( |
| 101 | + 'blockVisibility' => false, |
| 102 | + ), |
| 103 | + ), |
| 104 | + ); |
| 105 | + |
| 106 | + $result = wp_render_block_visibility_support( $block_content, $block ); |
| 107 | + |
| 108 | + $this->assertSame( $block_content, $result, 'Block content should remain unchanged when blockVisibility support is not opted in.' ); |
| 109 | + } |
| 110 | +} |
0 commit comments