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

Fix phpunit failures #51950

Merged
merged 3 commits into from
Jun 27, 2023
Merged

Conversation

noisysocks
Copy link
Member

@noisysocks noisysocks commented Jun 27, 2023

Fix the phpunit failures in trunk.

All but one of the failures were from Gutenberg phpunit tests that one way or the other call into Core's global styles and settings functionality e.g. wp_get_global_settings. These functions will cache their results unless WP_DEVELOPMENT_MODE is set to 'theme' or if Core detects that the Core unit tests are running via the WP_RUN_CORE_TESTS flag. Since Gutenberg is testing Core functionality here, it is de facto performing the role of Core unit tests, and so the fix is to set WP_RUN_CORE_TESTS. See WordPress/wordpress-develop@4a16702#r119747488.

edit: Nope! All but one of the failures are to do with wp_theme_has_theme_json caching its return value unless WP_RUN_CORE_TESTS is set. See #51950 (comment).

One failure (test_get_template_hierarchy) is due to an issue where a test taxonomy is registered in wpSetUpBeforeClass and not set_up. This causes issues because the switch_theme in set_up will clobber the taxonomies. I note that when these tests were ported to Core this was addressed and we're not using wpSetUpBeforeClass there.

A lot of these tests, I think, don't belong in Gutenberg anymore. They should have been removed when the functionality that they test were moved from Gutenberg to Core. It doesn't make sense to have tests in Gutenberg that test Core functionality. Alternatively, the changes that were made to the PHP test code before being merged to Core should have been brought back to the plugin. For example note how different Tests_Block_Template_Utils looks in Core versus in Gutenberg.

Copy link
Member

@ramonjd ramonjd 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 help on this one!

phpunit/bootstrap.php Show resolved Hide resolved
noisysocks referenced this pull request in WordPress/wordpress-develop Jun 27, 2023
…specific development mode.

In recent releases, WordPress core added several instances of cache usage around specific files. While those caches are safe to use in a production context, in development certain nuances apply for whether or not those caches make sense to use. Initially, `WP_DEBUG` was used as a temporary workaround, but it was clear that a more granular method to signify a specific development mode was required: For example, caches around `theme.json` should be disabled when working on a theme as otherwise it would disrupt the theme developer's workflow, but when working on a plugin or WordPress core, this consideration does not apply.

This changeset introduces a `WP_DEVELOPMENT_MODE` constant which, for now, can be set to either "core", "plugin", "theme", or an empty string, the latter of which means no development mode, which is also the default. A new function `wp_get_development_mode()` is the recommended way to retrieve that configuration value.

With the new function available, this changeset replaces all existing instances of the aforementioned `WP_DEBUG` workaround to use `wp_get_development_mode()` with a more specific check.

Props azaozz, sergeybiryukov, peterwilsoncc, spacedmonkey.
Fixes #57487.


git-svn-id: https://develop.svn.wordpress.org/trunk@56042 602fd350-edb4-49c9-b593-d223f7449a82
@ramonjd ramonjd added the [Type] Build Tooling Issues or PRs related to build tooling label Jun 27, 2023
@felixarntz
Copy link
Member

felixarntz commented Jun 27, 2023

@noisysocks

These functions will cache their results unless WP_DEVELOPMENT_MODE is set to 'theme' or if Core detects that the Core unit tests are running via the WP_RUN_CORE_TESTS flag.

Hmm, the second part is not correct I think. The only intention of the WP_RUN_CORE_TESTS check in WordPress/wordpress-develop@4a16702#diff-132ca3b683e71f40cf375043fae509e3a541090f428f462e7b68a46480ea83bcR282 is to facilitate overriding the value of the WP_DEVELOPMENT_MODE constant in tests.

As far as I understand, the failing Gutenberg tests assume that the logic is not being cached, which isn't guaranteed. I agree those tests would preferably only live in core, but I would suggest one of the two following workarounds:

  • Clear the cache in the relevant tests between calls to the function so there will never be a cache hit.
  • Set WP_RUN_CORE_TESTS and then adjust the relevant tests to set the $_wp_tests_development_mode global to 'theme', which is the way that core tests handle this for the case where the logic shouldn't be cached.

I'm a bit confused by the change of just setting WP_RUN_CORE_TESTS to true - that alone shouldn't fix the test failures 🤔

@ramonjd
Copy link
Member

ramonjd commented Jun 27, 2023

Clear the cache in the relevant tests between calls to the function so there will never be a cache hit.

Thanks for the advice @felixarntz

I've made a note to return to this after WP 6.3 Beta 1 (which is soon 😄 )

In the meantime, this patch would unblock a lot of work in the next few days.

@noisysocks do you think we could merge this in the meantime?

@noisysocks
Copy link
Member Author

Thanks for the extra context @felixarntz! You're right, re-reading wp_get_development_mode I can see that WP_RUN_CORE_TESTS shouldn't have any affect. And yet it does... 🤔

I'll try one of your suggestions. We can hold off on merging this unless an urgent need comes up.

@noisysocks
Copy link
Member Author

noisysocks commented Jun 27, 2023

Ah. Setting WP_RUN_CORE_TESTS fixes the tests because of this check in wp_theme_has_theme_json:

https://github.com/WordPress/wordpress-develop/blob/12ed5ccb0a61cf684682a94e9b9c02dd11bb7d75/src/wp-includes/global-styles-and-settings.php#L405

The tests in Gutenberg are written assuming that WP_DEBUG is enough to stop wp_theme_has_theme_json from caching its result:

if ( ! WP_DEBUG && is_int( $theme_has_support ) ) {

It's not anything to do with wp_get_development_mode.

@noisysocks
Copy link
Member Author

Note also that wp_theme_has_theme_json uses a static variable as the cache and not wp_cache. The unit tests therefore can't manually reset the cache.

I think setting WP_RUN_CORE_TESTS is all that we can do until these tests are removed.

@ramonjd
Copy link
Member

ramonjd commented Jun 27, 2023

I think setting WP_RUN_CORE_TESTS is all that we can do until these tests are removed.

👍

Maybe I'm overthinking it, but say we remove the tests and also the new constant.

What about future additions to say, WP_Theme_Json_Gutenberg, that the plugin will make, experimental or destined for a future Core release. Some of these might need to call wp_theme_has_theme_json() and also require test coverage.

I guess Gutenberg could do what it always does for compatibility, e.g., create a correspsonding, but altered, function gutenbeg_theme_has_theme_json() ?

@noisysocks
Copy link
Member Author

Hmm yeah good flag. We can do what you suggest or, maybe better, update Core's implementation of wp_theme_has_theme_json to cache the return value in a way that is reset by wp_clean_theme_json_cache.

@ramonjd
Copy link
Member

ramonjd commented Jun 27, 2023

update Core's implementation of wp_theme_has_theme_json to cache the return value in a way that is reset by wp_clean_theme_json_cache.

I like the cut of your gib

@noisysocks noisysocks merged commit 40aa280 into trunk Jun 27, 2023
50 checks passed
@noisysocks noisysocks deleted the fix/phpunit-failures-due-to-development-mode branch June 27, 2023 05:02
@github-actions github-actions bot added this to the Gutenberg 16.2 milestone Jun 27, 2023
@felixarntz
Copy link
Member

@noisysocks @ramonjd Thank you for the additional digging, based on the further context you provided the fix here LGTM.

Hmm yeah good flag. We can do what you suggest or, maybe better, update Core's implementation of wp_theme_has_theme_json to cache the return value in a way that is reset by wp_clean_theme_json_cache.

Big +1 from me, would you mind opening a Trac ticket for this?

@ramonjd
Copy link
Member

ramonjd commented Jun 28, 2023

Ticket here: https://core.trac.wordpress.org/ticket/58650

Please edit/expand if I've got the details wrong

🙇

ramonjd pushed a commit that referenced this pull request Jun 28, 2023
* Fix phpunit failures

* Add comment

* Update comment with actual reason this fix works
tellthemachines added a commit that referenced this pull request Jun 28, 2023
* Restore sidebar in focus mode on Pattern click through in Browse Mode Library (#51897)

Brings back #51492

* [Command center]: Add preferences and keyboard shortcuts commands (#51862)

* [Command center]: Add preferences and keyboard shortcuts commands

* update labels

* [Site Editor]: Fix `library` command path (#51837)

* Updating social link attributes (#51997)

* Try: Update template titles (#51428)

* Update template titles

* Fix typo

Co-authored-by: Alex Stine <alex.stine@yourtechadvisors.com>

* Revert Index rename

* "front page" -> "homepage"

* Update 404

Page make more sense given the template appears in the Pages panel too.

* Front Page

* home title + description

* Revert Home name change, and move compat files

* separate code for wp versions

* update tests

---------

Co-authored-by: Alex Stine <alex.stine@yourtechadvisors.com>
Co-authored-by: ntsekouras <ntsekouras@outlook.com>

* Update text color (#51965)

* Modal: Add small top padding to the content so that avoid cutting off the visible outline when hovering items (#51829)

* Site Editor: Fix focus cutoff in add template modal

* Add padding-top to the modal content

* Remove unnecessary padding-top

* Remove unnecessary padding-top

* Update changelog

* Revert top padding from block patterns list

* Revert top padding from block patterns list

* Remove unnecessary changes

* Remove unnecessary changes

* Add CSS inline comment

* Change padding metrics

* Rest API: rename navigation fallback classes from WP_ to Gutenberg_ (#51959)

* The `WP_REST_Navigation_Fallback_Controller` class has been committed to core and therefore results in a naming conflict and unit test failures.
Ideally `WP_REST_Navigation_Fallback_Controller` should have been named `WP_REST_Navigation_Fallback_Controller_Gutenberg` and extended `WP_REST_Navigation_Fallback_Controller`.
But we can conditionally load the file instead.

* Renamed WP_Classic_To_Block_Menu_Converter to Gutenberg_Classic_To_Block_Menu_Converter
Load WP_REST_Navigation_Fallback_Controller dependencies in load.php

* Renamed all 6.3 classes to have the Gutenberg_ prefix. This should avoid compat errors and hopefully some confusion later.

* Also rename test files for completeness

* Updated deprecation notices to refer to Gutenberg classes

* Fix phpunit failures (#51950)

* Fix phpunit failures

* Add comment

* Update comment with actual reason this fix works

---------

Co-authored-by: Dave Smith <getdavemail@gmail.com>
Co-authored-by: Nik Tsekouras <ntsekouras@outlook.com>
Co-authored-by: Ramon <ramonjd@users.noreply.github.com>
Co-authored-by: James Koster <james@jameskoster.co.uk>
Co-authored-by: Alex Stine <alex.stine@yourtechadvisors.com>
Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com>
Co-authored-by: Robert Anderson <robert@noisysocks.com>
glendaviesnz pushed a commit that referenced this pull request Jul 3, 2023
* Fix phpunit failures

* Add comment

* Update comment with actual reason this fix works
glendaviesnz added a commit that referenced this pull request Jul 3, 2023
…#52229)

* Patterns: Fix setting of sync status for fully synced patterns (#51952)
* Add check for sync status of fully to account for bug in 16.1 release
* Fix phpunit failures (#51950)
* Fix phpunit failures
* Performance tests: configure as a production environment (#52016)

---------

Co-authored-by: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com>
Co-authored-by: Robert Anderson <robert@noisysocks.com>
Co-authored-by: André <583546+oandregal@users.noreply.github.com>
sethrubenstein pushed a commit to pewresearch/gutenberg that referenced this pull request Jul 13, 2023
* Fix phpunit failures

* Add comment

* Update comment with actual reason this fix works
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Build Tooling Issues or PRs related to build tooling
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants