Make WordPress Core

Opened 18 months ago

Closed 18 months ago

Last modified 18 months ago

#57706 closed defect (bug) (fixed)

REST_Block_Type_Controller_Test::test_get_item_invalid() throws PHP Warning for invalid 'styles' data type

Reported by: hellofromtonya's profile hellofromTonya Owned by: hellofromtonya's profile hellofromTonya
Milestone: 6.2 Priority: normal
Severity: normal Version:
Component: Build/Test Tools Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description

The REST_Block_Type_Controller_Test::test_get_item_invalid() throws a PHP Warning due to the 'styles' setting not being an array data type.

Warning: foreach() argument must be of type array|object, string given in wp-includes/class-wp-theme-json.php on line 835

WP_Block_Type expects the 'styles' property to be an array. The code in WP_Theme_JSON (added in #57583 / [55172] expects the property to be an array as it is passed into a foreach(). But this test sets it to a string.

The PHP Warning alerts the developer of a problem which in this case is an incorrect data type when registering the block type.

This ticket proposes either checking for the PHP Warning OR setting the 'styles' property to an empty array() instead.

Change History (7)

This ticket was mentioned in PR #4068 on WordPress/wordpress-develop by @tanjimtc71.


18 months ago
#1

  • Keywords has-patch has-unit-tests added; needs-patch removed

Added fix for REST_Block_Type_Controller_Test::test_get_item_invalid() which was throwing a PHP warning due to an incorrect data type in the 'styles' property of the WP_Block_Type.

This PR Fixes a PHP Warning in the test caused by the 'styles' property not being an array. The proposed fix sets the 'styles' property to an empty array().

Could you please review the changes and let me know if anything needs to be updated or changed? I would really appreciate your feedback.
Thank you for taking the time to review this.

Trac ticket: https://core.trac.wordpress.org/ticket/57706

#2 @kapilpaul
18 months ago

PR looks good. Thank you @tanjimtc71.

#3 @davidbaumwald
18 months ago

#57717 was marked as a duplicate.

#4 @hellofromTonya
18 months ago

  • Keywords commit added
  • Owner set to hellofromTonya
  • Status changed from assigned to reviewing

Patch: https://github.com/WordPress/wordpress-develop/pull/4068

This patch does indeed resolve the PHP Warning in the test suites. Ready for commit. I'm prepping it now.

#5 @hellofromTonya
18 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 55342:

Build/Test Tools: Fix invalid styles data type PHP Warning in REST_Block_Type_Controller_Test::test_get_item_invalid().

Sets the styles block setting to an empty array in the REST_Block_Type_Controller_Test::test_get_item_invalid() test to resolve:

Warning: foreach() argument must be of type array|object, string given in wp-includes/class-wp-theme-json.php on line 835

Previously the test set the styles setting to a string value, which is incompatible. The problem surfaced after [55172] added a foreach to iterate the styles. The root cause was due to this test passing a string instead of an array.

Why?

WP_Block_Type styles property is documented as only accepting an array data type. At the time of this changeset, it does not validate before setting the property.

Code interacting with the property expects it to be an array data type. The PHP Warning alerts that of a doing it wrong when the incorrect data type is registered for the block type.

Follow-up to [55172], [48173].

Props tanjimtc71, hellofromTonya, kapilpaul.
Fixes #57706.

#7 @hellofromTonya
18 months ago

Thank you @tanjimtc71 for the PR and @kapilpaul for reviewing it.

Note: See TracTickets for help on using tickets.