Skip to content

Commit

Permalink
[BUGS-8085] Use filtered value when sending cache control headers (#45)
Browse files Browse the repository at this point in the history
* use the filter when sending cache control headers

* lint

* add a test for the max-age value(s)

* add better messaging for failed tests
  • Loading branch information
jazzsequence authored May 15, 2024
1 parent 023714b commit e8b3c51
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
3 changes: 2 additions & 1 deletion inc/pantheon-page-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ public function view_settings_page() {
*/
private function get_cache_control_header_value() {
if ( ! is_admin() && ! is_user_logged_in() ) {
$ttl = absint( $this->options['default_ttl'] );
// Use the filtered default max-age if applicable.
$ttl = apply_filters( 'pantheon_cache_default_max_age', absint( $this->options['default_ttl'] ) );
if ( $ttl < 60 && isset( $_ENV['PANTHEON_ENVIRONMENT'] ) && 'live' === $_ENV['PANTHEON_ENVIRONMENT'] ) {
$ttl = 60;
}
Expand Down
49 changes: 49 additions & 0 deletions tests/phpunit/test-page-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,53 @@ public function test_pantheon_cache_default_max_age_filter() {
// Remove the filter.
remove_all_filters( 'pantheon_cache_default_max_age' );
}

/**
* Helper function to get the max-age from the Cache-Control header of a REST API response.
*
* We're using a REST response here because it is a publicly accessible method that adds the Cache Control value from get_cache_control_header_value() to the response it returns.
*
* @return int The max-age value.
*/
private function get_max_age_from_rest_dispatch() {
// Set up a rest request.
$response = new WP_REST_Response();

// We're using the rest_post_dispatch callback here because it's a publicly accessible method that adds Cache Control to the response it returns.
$api_response = $this->pantheon_cache->filter_rest_post_dispatch_send_cache_control( $response );
$headers = $api_response->get_headers();
$cache_control_header_values = explode( ', ', $headers['Cache-Control'] );
$max_age = (int) str_replace( 'max-age=', '', $cache_control_header_values[1] );

return $max_age;
}

/**
* Test the Cache-Control header max-age.
*/
public function test_cache_control_headers() {
$max_age = $this->get_max_age_from_rest_dispatch();

// Assert the default max-age.
$this->assertEquals(
WEEK_IN_SECONDS,
$max_age,
sprintf( 'Expected max-age to be the default value (%1$d) but got %2$d', WEEK_IN_SECONDS, $max_age )
);

// Filter the default max-age to 120 seconds.
add_filter( 'pantheon_cache_default_max_age', function () {
return 120;
} );

// Get the max-age after the filter.
$max_age = $this->get_max_age_from_rest_dispatch();

// Assert the filtered max-age.
$this->assertEquals(
120,
$max_age,
sprintf( 'Expected max-age to be the filtered value (%1$d) but got %2$d', 120, $max_age )
);
}
}

0 comments on commit e8b3c51

Please sign in to comment.