-
Notifications
You must be signed in to change notification settings - Fork 128
Prevent effective asset cache headers audit from running on local/development environments #2035
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
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Hi @westonruter, Test fails because it is not able to mimic the |
Hi @hbhalodia, I think removing the entire test would be the only solution, as |
Hi @b1ink0, I have removed the tests for now. Thank You, |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #2035 +/- ##
==========================================
- Coverage 68.06% 68.02% -0.05%
==========================================
Files 92 92
Lines 7626 7627 +1
==========================================
- Hits 5191 5188 -3
- Misses 2435 2439 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@b1ink0 @hbhalodia What about adding |
Hi @westonruter, I can try that but how can I set 2 different values for the same constant? like we need to first test for local that key should not exists, and then again set the constant to production and check the key should exists. So that we can cover all the scenarios? I checked this - performance/plugins/optimization-detective/tests/test-uninstall.php Lines 18 to 20 in 05444d4
But still finding it complex on how to accomodate both scenarios? OR, if I am thinking correctly, Would add the normal test case with |
@westonruter This could only be overridden by modifying the The only other option I can think of is using the If we go with this, then if you use For me, I mostly use LocalWP for development, so I have manually set the If we go with patchdiff --git a/plugins/performance-lab/includes/site-health/effective-asset-cache-headers/hooks.php b/plugins/performance-lab/includes/site-health/effective-asset-cache-headers/hooks.php
index cc083eee..07de32f1 100644
--- a/plugins/performance-lab/includes/site-health/effective-asset-cache-headers/hooks.php
+++ b/plugins/performance-lab/includes/site-health/effective-asset-cache-headers/hooks.php
@@ -32,7 +32,7 @@ function perflab_effective_asset_cache_headers_add_test( array $tests ): array {
*
* GH Issue: https://github.com/WordPress/performance/issues/2031
*/
- if ( in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) {
+ if ( in_array( wp_get_development_mode(), array( 'plugin' ), true ) ) {
unset( $tests['direct']['effective_asset_cache_headers'] );
}
diff --git a/plugins/performance-lab/tests/includes/site-health/effective-asset-cache-headers/test-effective-asset-cache-headers.php b/plugins/performance-lab/tests/includes/site-health/effective-asset-cache-headers/test-effective-asset-cache-headers.php
index b35cd092..9b104180 100644
--- a/plugins/performance-lab/tests/includes/site-health/effective-asset-cache-headers/test-effective-asset-cache-headers.php
+++ b/plugins/performance-lab/tests/includes/site-health/effective-asset-cache-headers/test-effective-asset-cache-headers.php
@@ -31,16 +31,47 @@ class Test_Effective_Asset_Cache_Headers extends WP_UnitTestCase {
/**
* Test that the effective caching headers test is added to the site health tests.
*
+ * @runInSeparateProcess
+ * @preserveGlobalState disabled
+ *
+ * @dataProvider data_provider_test_perflab_effective_asset_cache_headers_add_test
* @covers ::perflab_effective_asset_cache_headers_add_test
+ *
+ * @param string $wp_development_mode The development mode to test against. Can be 'plugin' or '' (non-development mode).
*/
- public function test_perflab_effective_asset_cache_headers_add_test(): void {
+ public function test_perflab_effective_asset_cache_headers_add_test( string $wp_development_mode ): void {
+ if ( ! defined( 'WP_RUN_CORE_TESTS' ) ) {
+ define( 'WP_RUN_CORE_TESTS', 'YES' );
+ }
+
+ $GLOBALS['_wp_tests_development_mode'] = $wp_development_mode;
+
$tests = array(
'direct' => array(),
);
$tests = perflab_effective_asset_cache_headers_add_test( $tests );
- $this->assertArrayNotHasKey( 'effective_asset_cache_headers', $tests['direct'] );
+ if ( 'plugin' === $wp_development_mode ) {
+ // If in development mode, the test should not be added.
+ $this->assertArrayNotHasKey( 'effective_asset_cache_headers', $tests['direct'] );
+ } else {
+ $this->assertArrayHasKey( 'effective_asset_cache_headers', $tests['direct'] );
+ $this->assertEquals( 'Effective Caching Headers', $tests['direct']['effective_asset_cache_headers']['label'] );
+ $this->assertEquals( 'perflab_effective_asset_cache_headers_assets_test', $tests['direct']['effective_asset_cache_headers']['test'] );
+ }
+ }
+
+ /**
+ * Data provider for test_perflab_effective_asset_cache_headers_add_test.
+ *
+ * @return array<array<string>> Data provider.
+ */
+ public function data_provider_test_perflab_effective_asset_cache_headers_add_test(): array {
+ return array(
+ array( 'non_development_mode' => '' ),
+ array( 'development_mode' => 'plugin' ),
+ );
}
/**
|
Oh, I wasn't aware the constant was already defined up front. That does complicate things. Would it be feasible to add runkit as a dependency for running this test and if so utilize https://www.php.net/manual/en/function.runkit7-constant-remove.php to remove the constant? If runkit isn't available then the test could be skipped. |
Hi @westonruter, Adding There are a few technical challenges:
While For now, I’d lean toward removing the test or using |
return $tests; | ||
} | ||
add_filter( 'site_status_tests', 'perflab_effective_asset_cache_headers_add_test' ); | ||
add_filter( 'site_status_tests', 'perflab_effective_asset_cache_headers_add_test', 100 ); |
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.
Why the change to priority 100?
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.
Need to make sure it would run after all the filters, I thought it would be filter somewhere else as well, but priority 100 can be removed though.
@@ -26,6 +26,16 @@ function perflab_effective_asset_cache_headers_add_test( array $tests ): array { | |||
'label' => __( 'Effective Caching Headers', 'performance-lab' ), | |||
'test' => 'perflab_effective_asset_cache_headers_assets_test', | |||
); | |||
|
|||
/** |
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.
/** | |
/* |
* Static assets are expected to not have effective cache headers in non-production environments. | ||
* | ||
* GH Issue: https://github.com/WordPress/performance/issues/2031 | ||
*/ | ||
if ( in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) { | ||
unset( $tests['direct']['effective_asset_cache_headers'] ); | ||
} |
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.
Instead of unsetting something that was set, why not just short-circuit the function to avoid setting it in the first place, or rather wrap the setting with the if
statement, so the entire function body can be:
if ( ! in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) {
$tests['direct']['effective_asset_cache_headers'] = array(
'label' => __( 'Effective Caching Headers', 'performance-lab' ),
'test' => 'perflab_effective_asset_cache_headers_assets_test',
);
}
return $tests;
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.
Yeah, it can be work and even more instead of wrapping it in if
, we can bail early like,
/*
* Bail early.
*/
if ( in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) {
return $tests;
}
$tests['direct']['effective_asset_cache_headers'] = array(
'label' => __( 'Effective Caching Headers', 'performance-lab' ),
'test' => 'perflab_effective_asset_cache_headers_assets_test',
);
return $tests;
@b1ink0 Thanks for that feedback. So removing |
Only the test that checks whether the Site Health test was added needs to be removed. The other tests are not affected by this change, as it calls the functions directly to test them and doesn't rely on the filter. |
if ( in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) { | ||
return $tests; | ||
} | ||
|
||
$tests['direct']['effective_asset_cache_headers'] = array( | ||
'label' => __( 'Effective Caching Headers', 'performance-lab' ), | ||
'test' => 'perflab_effective_asset_cache_headers_assets_test', | ||
); | ||
|
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.
I think this is slightly better as it means there is only one return $tests
:
if ( in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) { | |
return $tests; | |
} | |
$tests['direct']['effective_asset_cache_headers'] = array( | |
'label' => __( 'Effective Caching Headers', 'performance-lab' ), | |
'test' => 'perflab_effective_asset_cache_headers_assets_test', | |
); | |
if ( ! in_array( wp_get_environment_type(), array( 'local', 'development' ), true ) ) { | |
$tests['direct']['effective_asset_cache_headers'] = array( | |
'label' => __( 'Effective Caching Headers', 'performance-lab' ), | |
'test' => 'perflab_effective_asset_cache_headers_assets_test', | |
); | |
} |
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.
Okay, would update this to use only 1 return statement.
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.
This is now fixed and function is updated.
Thank You,
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.
Thanks for working on this!
Summary
Fixes #2031
Relevant technical choices
site_status_tests
filter to unset theeffective_asset_cache_headers
for local and development env.wp_get_environment_type
to support for 2 different scenarios.