-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(latest-posts) Make latest-posts ssr categories handling more defensive #53659
(latest-posts) Make latest-posts ssr categories handling more defensive #53659
Conversation
…ncluding blank string)
Flaky tests detected in 197c98d. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5861602111
|
Does anyone know if there are any existing blocks that have unit tests for their SSR/PHP logic? I couldn't find any so far. |
I further debugged it and it seems the bug happens at save-time, meaning that by the time we get the error from this stacktrace:
...the block markup already has the faulty markup for the block (with a
Given the additional testing and troubleshooting, I arrived at the conclusion that this patch is the best solution for it now, and it seems to work fine, too (I tested in one of the sites in our multisite instance, and the latest posts started to render correctly, you can try to reproduce it by setting categories to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change makes sense to me. Would have been nice if we could add a unit test.
@tyxla Yep! I'm just not sure where/how to add it though. I didn't find any examples of unit-tests that test the backend of a block -- this would definitely not be a hard test to write, but not sure if GB has the infrastructure for PHP unit-tests inside the block directories? I only see JS tests there, e.g: https://github.com/WordPress/gutenberg/tree/trunk/packages/block-library/src/latest-posts/test. Happy to add it in a follow-up if someone could guide me here! |
Yup, I have the same observations. The only ones I found were CodeSniffer-based coding standards ones. It's definitely not worth adding the infrastructure just for that. |
What?
This is an attempt to fix #53648.
Why?
In some multisite instances (i.g Wordpress.com) data corruption somewhere else (we couldn't troubleshoot the origin yet, unfortunately) can cause the
categories
attribute passed over torender_block_core_latest_posts
to be blank (''
), this ends up causing a PHP warning:array_column() expects parameter 1 to be array, string given
And worst of all, ends up printing the warning in the HTML instead of the actual latest-posts block!
How?
By making the condition more defensive by making sure the assignment doesn't happen if
$attributes['categories
] isempty
. For some reason,categories
is added with anempty
string, while under normal circumstances, it should not be part of the$attributes
array at all if it's not set.Testing Instructions
Unfortunately, it's not a scenario that's trivial to reproduce. The only way is to force it by passing an
$attributes
array arg with an emptycategories
(set to''
) at the beginning of this method: https://github.com/WordPress/gutenberg/blame/trunk/packages/block-library/src/latest-posts/index.php#L36. I also didn't find unit tests for this block so this was the only way I could test it.