Skip to content
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

Make layout supports compatible with enhanced pagination #5528

Conversation

luisherranz
Copy link
Member

This is a backport PR for WordPress 6.4 that includes the following PHP Gutenberg changes:

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


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link
Contributor

@tellthemachines tellthemachines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! The code change itself LGTM; my question is whether the layout file is the best home for the wp_incremental_id_per_prefix function. Might we be using it elsewhere in the future?

Also might be good to have a unit test for the function, similar to wp_unique_id here.

I pushed a fix for the failing PHP tests that were still expecting the old classnames.

Copy link
Contributor

@costdev costdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @luisherranz and for the ping @tellthemachines!

I've left some thoughts in this review.

src/wp-includes/block-supports/layout.php Outdated Show resolved Hide resolved
src/wp-includes/block-supports/layout.php Outdated Show resolved Hide resolved
src/wp-includes/block-supports/layout.php Outdated Show resolved Hide resolved
src/wp-includes/block-supports/layout.php Outdated Show resolved Hide resolved
tests/phpunit/tests/blocks/render.php Outdated Show resolved Hide resolved
src/wp-includes/block-supports/layout.php Outdated Show resolved Hide resolved
@cbravobernal
Copy link
Contributor

Hi there!

I applied all changes requested. Thanks for the review @tellthemachines @costdev

Copy link
Contributor

@costdev costdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates @luisherranz!

I've left a note about a minor alignment issue, and an example of how we can leverage a data provider to ensure all tests run even if one fails.

tests/phpunit/tests/functions/wpUniquePrefixedId.php Outdated Show resolved Hide resolved
src/wp-includes/block-supports/layout.php Outdated Show resolved Hide resolved
@cbravobernal
Copy link
Contributor

Code updated with changes requested. Thanks for your reviews @tellthemachines , @costdev ! 🙇

@luisherranz
Copy link
Member Author

This looks much better now. Thanks, @c4rl0sbr4v0 and @costdev! 🙏

Co-authored-by: Tonya Mork <tonya.mork@automattic.com>
@cbravobernal
Copy link
Contributor

cbravobernal commented Oct 23, 2023

== Patch Report

Patch tested: https://patch-diff.githubusercontent.com/raw/WordPress/wordpress-develop/pull/5528.diff

How to read this report:
🐞 <= Bug occurs.
✅ <= Behavior is ''expected''.
❌ <= Behavior is ''NOT expected''.

==== Environment

  • OS: macOS 14.0
  • Web Server: Nginx
  • macOS 14.0
  • PHP 8.2.11
  • 6.4-RC1-56475-src
  • Firefox 118.0.2

==== Steps to Reproduce

  1. With WordPress 6.4 installed
  2. Create 7 posts, configure to have 3 per page.
  3. On the site editor, in home page, TT4, add pagination to the Query Loop, set it to inherit the template settings, activate "Enhanced Pagination feature). Make sure your layout is three posts in a row.
  4. 🐞 Check that on page three, the last post layout is broken (full width instead of 1/3)

==== Steps to Test Patch

  1. 🛠 Apply [https://patch-diff.githubusercontent.com/raw/Make layout supports compatible with enhanced pagination #5528.diff the patch].
  2. Refresh the home page.
  3. ✅ Check that the layout is correct when navigating to the last page.
  4. ✅ Check that the navigation happened without a page reload.

==== Test Results

  • ✅ Issue is reproducible.
  • ✅ Issue resolved with patch.

==== Screenshots
Before patch:
Screenshot 2023-10-23 at 17 58 31

After patch:
Screenshot 2023-10-23 at 17 58 13

$id_counters[ $prefix ] = 0;
}

return $prefix . (string) ++$id_counters[ $prefix ];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I find the incrementing inside the return to be a bit harder to read. I wonder if it might be better to increment and then return.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's consistent with wp_unique_id(). That said, I thought the same thing : separate the increment from the string build/return.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can refactor both in 6.5, as it is code quality. I can leave the PR ready after the RC, pointing trunk branch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what makes it less readable as compared to wp_unique_id() is the array lookup. To help, split the increment in 11f1cac.

tests/phpunit/tests/functions/wpUniquePrefixedId.php Outdated Show resolved Hide resolved
src/wp-includes/functions.php Show resolved Hide resolved
src/wp-includes/functions.php Outdated Show resolved Hide resolved
The wp_unique_prefixed_id() function uses a function scoped static variable for memoization. The incremented value may be different between tests, especially over time when more tests are added.

Running the tests in separate process improves the stability of these tests.
'prefix as (string) "1"' => array(
'prefix' => '1',
'expected' => array( '11', '12' ),
),
Copy link
Contributor

@costdev costdev Oct 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As wp_unique_prefixed_id() is a general function not specific to block names, it's possible that an ID may contain . and a number of other characters. Reference.

It's also possible that the "id" in question does not link directly to the HTML id attribute`.

Coverage should also be added for a variety of possible "id" values, such as (string) "0.0", etc. to protect behavior as this general function could be used to generate unique IDs for a variety of purposes/accepted formats.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure that can be added, though would be part of the prefix, and not the incremented integer value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @costdev, commit 7a32e26 adds 2 more happy path datasets:

  • One for '.'
  • Another for the prefix with a block name.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Tonya!

If there are any other potential "gotcha" values that a potential future refactor could regress, we should consider those too. The non-string dataset looks good for "gotcha" values, though I think we should complete the scalar datasets with (float) 0.0 and (float) 1.0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@costdev 28cbcad adds a double dataset.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@costdev here's the added coverage we talked about in Make/Core slack DM 5151440.

* @param string $expected_message Expected notice message.
* @param string $expected_value Expected unique ID.
*/
public function test_should_raise_notice_and_use_empty_string_prefix_when_nonstring_given( $non_string_prefix, $expected_message, $expected_value ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be a post commit cleanup, but If this test is ever run before test_should_create_unique_prefixed_ids then they will fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the order of the tests within the class does currently matter. The tests in this class runs in a separate process to avoid a situation where the static$id_counters already has values in it.

To handle the test run order within the class, could change that to run each test in a separate process. By doing so, the static $id_counters will always be reset to an empty array when each test starts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit 5aac204 runs each test dataset in a separate process to avoid test order concerns,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also tweaked the unhappy path tests to ensure the IDs remain unique f4f99fc

How? By allowing each dataset to determine how many unique IDs to generate.

Why? Each of these non-string prefix datasets would have the same ID, as the prefix is omitted (empty string). One more check just to make sure.

To avoid test order, changes to run each dataset in a separate process, which each test starts with the static `$id_counters` equal to an empty array.
Unhappy path tests have non-string prefixes, which will be changed to the default empty string.

To further check uniqueness within the dataset's expected results, the unhappy path test now allows each dataset to specify how many unique IDs to generate for its tests.
@hellofromtonya
Copy link
Contributor

@aaronjorbin @costdev all feedback is now addressed. Can you re-review please?

THis test guards against a future regression should the defensive guard ever change.

Props @costdev.
Copy link
Contributor

@costdev costdev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates @luisherranz and @hellofromtonya! LGTM! 👍

Copy link
Contributor

@hellofromtonya hellofromtonya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 should be ready for commit

@ockham
Copy link
Contributor

ockham commented Oct 24, 2023

Thank you everyone! Luis has asked me to commit this, which I'll promptly do 😊

@ockham
Copy link
Contributor

ockham commented Oct 24, 2023

Committed to Core trunk in [56994] and to the 6.4 branch in [56995].

@ockham ockham closed this Oct 24, 2023
@luisherranz
Copy link
Member Author

Thank you all for your help getting this to the finish line! @ockham, @c4rl0sbr4v0, @hellofromtonya, @aaronjorbin and @costdev 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants