Skip to content

Commit

Permalink
Refactor wp-404-caching plugin and update tests
Browse files Browse the repository at this point in the history
This commit includes modifications to the wp-404-caching plugin and its corresponding tests. The unnecessary typecasting to boolean in various test conditions has been removed. Also, the name of the scheduled task has been corrected from 'wp_404_caching_single' to 'wp_404_cache_single' in the tests. Lastly, the URI in the test html codes has been updated to '/wp-404-caching/404-template-generator'.
  • Loading branch information
attackant committed Mar 12, 2024
1 parent cd729d6 commit 87e9eb7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
38 changes: 19 additions & 19 deletions tests/Feature/FullPageCache404Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ public function test_feature_is_disabled_if_ssl_is_off(): void {
*/
public function test_feature_is_disabled_if_object_cache_is_not_in_use(): void {

if ( ! (bool) wp_using_ext_object_cache() ) {
if ( ! wp_using_ext_object_cache() ) {
$this->markTestSkipped( 'This test requires that an external object cache is in use.' );
}

$this->assertTrue( (bool) wp_using_ext_object_cache() );
$this->assertTrue( wp_using_ext_object_cache() );

// Disable the object cache.
wp_using_ext_object_cache( false );

$this->feature->boot();

$this->assertFalse( (bool) wp_using_ext_object_cache() );
$this->assertFalse( wp_using_ext_object_cache() );

$response = $this->get( '/this-is-a-404-page' );
$response->assertStatus( 404 );
Expand All @@ -106,15 +106,15 @@ public function test_feature_is_disabled_if_object_cache_is_not_in_use(): void {
// Re-enable the object cache.
wp_using_ext_object_cache( true );

$this->assertTrue( (bool) wp_using_ext_object_cache() );
$this->assertTrue( wp_using_ext_object_cache() );
}

/**
* Test full page cache 404.
*/
public function test_full_page_cache_404_returns_cache(): void {

if ( ! (bool) wp_using_ext_object_cache() ) {
if ( ! wp_using_ext_object_cache() ) {
$this->markTestSkipped( 'This test requires that an external object cache is in use.' );
}

Expand All @@ -126,7 +126,7 @@ public function test_full_page_cache_404_returns_cache(): void {
$response->assertNoContent( 404 );

// Expect cron job to be scheduled.
$this->assertTrue( wp_next_scheduled( 'wp_404_caching_single' ) > 0 );
$this->assertTrue( wp_next_scheduled( 'wp_404_cache_single' ) > 0 );

add_action( 'template_redirect', [ $this, 'set_404_cache' ], 0 );

Expand All @@ -145,7 +145,7 @@ public function test_full_page_cache_404_returns_cache(): void {
*/
public function test_full_page_cache_404_does_not_return_cache_for_logged_in_user(): void {

if ( ! (bool) wp_using_ext_object_cache() ) {
if ( ! wp_using_ext_object_cache() ) {
$this->markTestSkipped( 'This test requires that an external object cache is in use.' );
}

Expand All @@ -157,7 +157,7 @@ public function test_full_page_cache_404_does_not_return_cache_for_logged_in_use
$response->assertNoContent( 404 );

// Expect cron job to be scheduled.
$this->assertTrue( wp_next_scheduled( 'wp_404_caching_single' ) > 0 );
$this->assertTrue( wp_next_scheduled( 'wp_404_cache_single' ) > 0 );

add_action( 'template_redirect', [ $this, 'set_404_cache' ], 0 );

Expand All @@ -177,7 +177,7 @@ public function test_full_page_cache_404_does_not_return_cache_for_logged_in_use
*/
public function test_full_page_cache_404_does_not_return_cache_for_generator_uri(): void {

if ( ! (bool) wp_using_ext_object_cache() ) {
if ( ! wp_using_ext_object_cache() ) {
$this->markTestSkipped( 'This test requires that an external object cache is in use.' );
}

Expand All @@ -187,7 +187,7 @@ public function test_full_page_cache_404_does_not_return_cache_for_generator_uri
$response->assertNoContent( 404 );

// Hit the generator URI to populate the cache.
$response = $this->get( '/wp-alleyvate/404-template-generator/?generate=1&uri=1' );
$response = $this->get( '/wp-404-caching/404-template-generator/?generate=1&uri=1' );
$response->assertDontSee( $this->feature::prepare_response( $this->get_404_html() ) );
$response->assertStatus( 404 );

Expand All @@ -206,7 +206,7 @@ public function test_full_page_cache_404_does_not_return_cache_for_generator_uri
*/
public function test_full_page_cache_not_returned_for_non_404(): void {

if ( ! (bool) wp_using_ext_object_cache() ) {
if ( ! wp_using_ext_object_cache() ) {
$this->markTestSkipped( 'This test requires that an external object cache is in use.' );
}

Expand All @@ -219,7 +219,7 @@ public function test_full_page_cache_not_returned_for_non_404(): void {
$response->assertSee( 'Hello World' );

$this->assertFalse(
wp_next_scheduled( 'wp_404_caching_single' ),
wp_next_scheduled( 'wp_404_cache_single' ),
'Cron job to generate cached 404 page is scheduled and should not be.'
);
}
Expand Down Expand Up @@ -259,7 +259,7 @@ public function test_full_page_cache_prepare_content(): void {
*/
public function test_full_page_cache_404_cron(): void {

if ( ! (bool) wp_using_ext_object_cache() ) {
if ( ! wp_using_ext_object_cache() ) {
$this->markTestSkipped( 'This test requires that an external object cache is in use.' );
}

Expand All @@ -274,13 +274,13 @@ public function test_full_page_cache_404_cron(): void {
$response->assertNoContent( 404 );

// Expect cron job to be scheduled.
$this->assertTrue( wp_next_scheduled( 'wp_404_caching_single' ) > 0 );
$this->assertTrue( wp_next_scheduled( 'wp_404_cache_single' ) > 0 );

// Run the cron job.
do_action( 'wp_404_caching' );
do_action( 'wp_404_cache' );

// This is an hourly cron job, so we expect it to be scheduled again.
$this->assertTrue( wp_next_scheduled( 'wp_404_caching_single' ) > 0 );
$this->assertTrue( wp_next_scheduled( 'wp_404_cache_single' ) > 0 );
}

/**
Expand All @@ -303,16 +303,16 @@ private function get_404_html(): string {
<title>404 Not Found</title>
<script type="text/javascript">
window.dataLayer = window.dataLayer || [];
dataLayer.push({"pagename":"\/wp-alleyvate\/404-template-generator\/?generate=1&uri=1"});
dataLayer.push({"pagename":"\/wp-404-caching\/404-template-generator\/?generate=1&uri=1"});
</script>
</head>
<body>
<h1>404 Not Found</h1>
<p>The <a href="/wp-alleyvate/404-template-generator/?generate=1&#038;uri=1">requested URL</a> was not found on this server.</p>
<p>The <a href="/wp-404-caching/404-template-generator/?generate=1&#038;uri=1">requested URL</a> was not found on this server.</p>
<p>This test includes different ways the URI may be output in the content. Above shows the use of esc_url and
wp_json_encode.</p>
<p>So that we can do content aware replacement of the URI for security and analytics reporting.</p>
<p>esc_html would output: /wp-alleyvate/404-template-generator/?generate=1&amp;uri=1</p>
<p>esc_html would output: /wp-404-caching/404-template-generator/?generate=1&amp;uri=1</p>
</body>
</html>
HTML;
Expand Down
2 changes: 1 addition & 1 deletion wp-404-caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: WP 404 Caching
* Plugin URI: https://github.com/alleyinteractive/wp-404-caching
* Description: Full Page Cache for WordPress 404s
* Version: 0.0.0
* Version: 0.0.1
* Author: Alley
* Author URI: https://github.com/alleyinteractive/wp-404-caching
* Requires at least: 6.4
Expand Down

0 comments on commit 87e9eb7

Please sign in to comment.