[wp-trac] [WordPress Trac] #52592: PHP warning when the label property is missing from register_block_style

WordPress Trac noreply at wordpress.org
Sun Sep 8 14:51:40 UTC 2024


#52592: PHP warning when the label property is missing from register_block_style
----------------------------------------------------+---------------------
 Reporter:  poena                                   |       Owner:  (none)
     Type:  defect (bug)                            |      Status:  new
 Priority:  normal                                  |   Milestone:  6.7
Component:  Editor                                  |     Version:  5.3
 Severity:  normal                                  |  Resolution:
 Keywords:  has-patch needs-testing has-unit-tests  |     Focuses:
----------------------------------------------------+---------------------
Changes (by Rahmohn):

 * keywords:  has-patch needs-testing needs-unit-tests => has-patch needs-
     testing has-unit-tests


Comment:

 Hi @poena

 I've added a test covering the missing/invalid label and the
 `_doing_it_wrong` trigger. Also, I've refactored a bit the test you added:

 {{{#!php
 <?php
 /**
  * Should accept valid string style label.
  * The registered style should have the same label.
  *
  * @ticket 52592
  *
  * @covers WP_Block_Styles_Registry::register
  * @covers WP_Block_Styles_Registry::is_registered
  * @covers WP_Block_Styles_Registry::get_registered_styles_for_block
  */
 public function test_register_block_style_with_string_style_label() {
         $name             = 'core/paragraph';
         $style_properties = array(
                 'name'  => 'fancy',
                 'label' => 'Fancy',
         );
         $result           = $this->registry->register( $name,
 $style_properties );

         $this->assertTrue( $result, 'The block style should be registered
 when the label is a valid string.' );
         $this->assertTrue(
                 $this->registry->is_registered( $name, 'fancy' ),
                 'The block type should have the block style registered
 when the label is valid.'
         );
         $this->assertEquals(
                 $style_properties['label'],
                 $this->registry->get_registered_styles_for_block( $name
 )['fancy']['label'],
                 'The registered block style should have the same label.'
         );
 }

 /**
  * Should not accept invalid style label.
  *
  * @ticket 52592
  *
  * @covers WP_Block_Styles_Registry::register
  * @covers WP_Block_Styles_Registry::is_registered
  */
 public function test_register_block_style_with_invalid_style_label() {
         $name             = 'core/paragraph';
         $style_properties = array(
                 'name' => 'fancy',
         );

         $this->setExpectedIncorrectUsage(
 'WP_Block_Styles_Registry::register' );

         $result           = $this->registry->register( $name,
 $style_properties );
         $this->assertFalse( $result, 'The block style should not be
 registered when the label property is missing.' );
         $this->assertFalse(
                 $this->registry->is_registered( $name, 'fancy' ),
                 'The block type should not have the block style registered
 when the label property is missing.'
         );

         $style_properties['label'] = ['Fancy'];

         $result           = $this->registry->register( $name,
 $style_properties );
         $this->assertFalse( $result, 'The block style should not be
 registered when the label property is not a string.' );
         $this->assertFalse(
                 $this->registry->is_registered( $name, 'fancy' ),
                 'The block type should not have the block style registered
 when the label property is not a string.'
         );
 }
 }}}


 I am attaching the diff file but I can create a new PR with your and my
 changes. Just let me know.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/52592#comment:8>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list