Skip to content

Commit

Permalink
Merge pull request #77 from alleyinteractive/feature/9/caching-404s-r…
Browse files Browse the repository at this point in the history
…equires-ssl

The "Full-Page Caching for 404s" feature requires ssl
  • Loading branch information
renatonascalves authored Feb 28, 2024
2 parents 8017c6a + 370860d commit 006f900
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ public function boot(): void {
* Only boot feature if external object cache is being used.
*
* We don't want to store the cached 404 page in the database.
*
* And only boot feature if the site is using SSL.
*/
if ( ! (bool) wp_using_ext_object_cache() ) {
if ( ! (bool) wp_using_ext_object_cache() || ! is_ssl() ) {
return;
}

Expand Down Expand Up @@ -116,6 +118,7 @@ public static function action__template_redirect(): void {

echo self::get_cached_response_with_headers(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

// If we're testing, don't exit, die instead.
if ( \defined( 'MANTLE_IS_TESTING' ) && MANTLE_IS_TESTING ) {
wp_die( '', '', [ 'response' => 404 ] );
}
Expand Down
27 changes: 25 additions & 2 deletions tests/alley/wp/alleyvate/features/test-full-page-cache-404.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ protected function setUp(): void {

$this->prevent_stray_requests();

// Turn SSL on.
$_SERVER['HTTPS'] = 'on';

$this->feature = new Full_Page_Cache_404();
}

Expand All @@ -53,7 +56,28 @@ public function tearDown(): void {
}

/**
* Test that the feature is disabled if the object cache is not in use.
* Test that the feature is disabled if SSL is off.
*/
public function test_feature_is_disabled_if_ssl_is_off(): void {
$this->assertTrue( is_ssl() );

$_SERVER['HTTPS'] = 'off';

$this->assertFalse( is_ssl() );

$this->feature->boot();

$response = $this->get( '/this-is-a-404-page' );
$response->assertStatus( 404 );

$this->assertFalse(
wp_next_scheduled( 'alleyvate_404_cache_single' ),
'Cron job to generate cached 404 page is scheduled and should not be.'
);
}

/**
* Test the feature is disabled if the object cache is not in use.
*/
public function test_feature_is_disabled_if_object_cache_is_not_in_use(): void {

Expand All @@ -71,7 +95,6 @@ public function test_feature_is_disabled_if_object_cache_is_not_in_use(): void {
$this->assertFalse( (bool) wp_using_ext_object_cache() );

$response = $this->get( '/this-is-a-404-page' );
$response->assertDontSee( $this->feature::prepare_response( $this->get_404_html() ) );
$response->assertStatus( 404 );

$this->assertFalse(
Expand Down

0 comments on commit 006f900

Please sign in to comment.