|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @group block-supports |
| 4 | + * |
| 5 | + * @covers ::wp_mark_auto_generate_control_attributes |
| 6 | + */ |
| 7 | +class Tests_Block_Supports_Auto_Register extends WP_UnitTestCase { |
| 8 | + |
| 9 | + /** |
| 10 | + * Tests that attributes are marked when auto_register is enabled. |
| 11 | + * |
| 12 | + * @ticket 64639 |
| 13 | + */ |
| 14 | + public function test_marks_attributes_with_auto_register_flag() { |
| 15 | + $settings = array( |
| 16 | + 'supports' => array( 'auto_register' => true ), |
| 17 | + 'attributes' => array( |
| 18 | + 'title' => array( 'type' => 'string' ), |
| 19 | + 'count' => array( 'type' => 'integer' ), |
| 20 | + ), |
| 21 | + ); |
| 22 | + |
| 23 | + $result = wp_mark_auto_generate_control_attributes( $settings, 'test/block' ); |
| 24 | + |
| 25 | + $this->assertTrue( $result['attributes']['title']['autoGenerateControl'] ); |
| 26 | + $this->assertTrue( $result['attributes']['count']['autoGenerateControl'] ); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Tests that attributes are not marked without auto_register flag. |
| 31 | + * |
| 32 | + * @ticket 64639 |
| 33 | + */ |
| 34 | + public function test_does_not_mark_attributes_without_auto_register() { |
| 35 | + $settings = array( |
| 36 | + 'attributes' => array( |
| 37 | + 'title' => array( 'type' => 'string' ), |
| 38 | + ), |
| 39 | + ); |
| 40 | + |
| 41 | + $result = wp_mark_auto_generate_control_attributes( $settings, 'test/block' ); |
| 42 | + |
| 43 | + $this->assertArrayNotHasKey( 'autoGenerateControl', $result['attributes']['title'] ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Tests that attributes with source are excluded. |
| 48 | + * |
| 49 | + * @ticket 64639 |
| 50 | + */ |
| 51 | + public function test_excludes_attributes_with_source() { |
| 52 | + $settings = array( |
| 53 | + 'supports' => array( 'auto_register' => true ), |
| 54 | + 'attributes' => array( |
| 55 | + 'title' => array( 'type' => 'string' ), |
| 56 | + 'content' => array( |
| 57 | + 'type' => 'string', |
| 58 | + 'source' => 'html', |
| 59 | + ), |
| 60 | + ), |
| 61 | + ); |
| 62 | + |
| 63 | + $result = wp_mark_auto_generate_control_attributes( $settings, 'test/block' ); |
| 64 | + |
| 65 | + $this->assertTrue( $result['attributes']['title']['autoGenerateControl'] ); |
| 66 | + $this->assertArrayNotHasKey( 'autoGenerateControl', $result['attributes']['content'] ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Tests that attributes with role: local are excluded. |
| 71 | + * |
| 72 | + * Example: The 'blob' attribute in media blocks (image, video, file, audio) |
| 73 | + * stores a temporary blob URL during file upload. This is internal state |
| 74 | + * that shouldn't be shown in the inspector or saved to the database. |
| 75 | + * |
| 76 | + * @ticket 64639 |
| 77 | + */ |
| 78 | + public function test_excludes_attributes_with_role_local() { |
| 79 | + $settings = array( |
| 80 | + 'supports' => array( 'auto_register' => true ), |
| 81 | + 'attributes' => array( |
| 82 | + 'title' => array( 'type' => 'string' ), |
| 83 | + 'blob' => array( |
| 84 | + 'type' => 'string', |
| 85 | + 'role' => 'local', |
| 86 | + ), |
| 87 | + ), |
| 88 | + ); |
| 89 | + |
| 90 | + $result = wp_mark_auto_generate_control_attributes( $settings, 'test/block' ); |
| 91 | + |
| 92 | + $this->assertTrue( $result['attributes']['title']['autoGenerateControl'] ); |
| 93 | + $this->assertArrayNotHasKey( 'autoGenerateControl', $result['attributes']['blob'] ); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Tests that empty attributes are handled gracefully. |
| 98 | + */ |
| 99 | + public function test_handles_empty_attributes() { |
| 100 | + $settings = array( |
| 101 | + 'supports' => array( 'auto_register' => true ), |
| 102 | + ); |
| 103 | + |
| 104 | + $result = wp_mark_auto_generate_control_attributes( $settings, 'test/block' ); |
| 105 | + |
| 106 | + $this->assertSame( $settings, $result ); |
| 107 | + } |
| 108 | +} |
0 commit comments