Skip to content

Commit

Permalink
[BUGS-6639] site health checks (#39)
Browse files Browse the repository at this point in the history
* remove site health tests that aren't relevant to pantheon envs

* add object cache checks to site health

* update with else error

* lint

* add site-health.php

* yellow doesn't exist

* define the test in the result

* add tests for the site_health_modifications filter
this really only tests that a filter does filtering things, but interacting directly with the WP_Site_Health class does not display these tests, either, so mocking it is as good as any other integration test

* add tests for the object_cache site status tests

* lint

* return early rather than if/else
  • Loading branch information
jazzsequence authored Apr 23, 2024
1 parent f904a8e commit ed7252a
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 0 deletions.
129 changes: 129 additions & 0 deletions inc/site-health.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/**
* Pantheon Site Health Modifications
*
* @package pantheon
*/

namespace Pantheon\Site_Health;

// If on Pantheon...
if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) ) {
add_filter( 'site_status_tests', __NAMESPACE__ . '\\site_health_mods' );
add_filter( 'site_status_tests', __NAMESPACE__ . '\\object_cache_tests' );
}

/**
* Modify the Site Health tests.
*
* @param array $tests The Site Health tests.
* @return array
*/
function site_health_mods( $tests ) {
// Remove checks that aren't relevant to Pantheon environments.
unset( $tests['direct']['update_temp_backup_writable'] );
unset( $tests['direct']['available_updates_disk_space'] );
unset( $tests['async']['background_updates'] );
return $tests;
}

/**
* Add object cache tests.
*
* @param array $tests The Site Health tests.
* @return array
*/
function object_cache_tests( $tests ) {
$tests['direct']['object_cache'] = [
'label' => __( 'Object Cache', 'pantheon' ),
'test' => 'test_object_cache',
];

return $tests;
}

/**
* Check for object cache and object cache plugins.
*
* @return array
*/
function test_object_cache() {
if ( ! isset( $_ENV['CACHE_HOST'] ) ) {
$result = [
'label' => __( 'Redis Object Cache', 'pantheon' ),
'status' => 'critical',
'badge' => [
'label' => __( 'Performance', 'pantheon' ),
'color' => 'red',
],
'description' => sprintf(
'<p>%s</p>',
__( 'Redis object cache is not active for your site.', 'pantheon' )
),
'test' => 'object_cache',
];

return $result;
}

$wp_redis_active = is_plugin_active( 'wp-redis/wp-redis.php' );
$ocp_active = is_plugin_active( 'object-cache-pro/object-cache-pro.php' );

if ( $wp_redis_active ) {
$result = [
'label' => __( 'WP Redis Active', 'pantheon' ),
'status' => 'recommended',
'badge' => [
'label' => __( 'Performance', 'pantheon' ),
'color' => 'orange',
],
'description' => sprintf(
'<p>%s</p><p>%s</p>',
__( 'WP Redis is active for your site. We recommend using Object Cache Pro.', 'pantheon' ),
// Translators: %s is a URL to the Pantheon documentation to install Object Cache Pro.
sprintf( __( 'Visit our <a href="%s">documentation site</a> to learn how.', 'pantheon' ), 'https://docs.pantheon.io/object-cache/wordpress' )
),
'test' => 'object_cache',
];

return $result;
}

if ( $ocp_active ) {
$result = [
'label' => __( 'Object Cache Pro Active', 'pantheon' ),
'status' => 'good',
'badge' => [
'label' => __( 'Performance', 'pantheon' ),
'color' => 'green',
],
'description' => sprintf(
'<p>%s</p><p>%s</p>',
__( 'Object Cache Pro is active for your site.', 'pantheon' ),
// Translators: %s is a URL to the Object Cache Pro documentation.
sprintf( __( 'Visit the <a href="%s">Object Cache Pro</a> documentation to learn more.', 'pantheon' ), 'https://objectcache.pro/docs' )
),
'test' => 'object_cache',
];

return $result;
}

$result = [
'label' => __( 'No Object Cache Plugin Active', 'pantheon' ),
'status' => 'critical',
'badge' => [
'label' => __( 'Performance', 'pantheon' ),
'color' => 'red',
],
'description' => sprintf(
'<p>%s</p><p>%s</p>',
__( 'Redis object cache is active for your site but you have no object cache plugin installed. We recommend using Object Cache Pro.', 'pantheon' ),
// Translators: %s is a URL to the Pantheon documentation to install Object Cache Pro.
sprintf( __( 'Visit our <a href="%s">documentation site</a> to learn how to install it.', 'pantheon' ), 'https://docs.pantheon.io/object-cache/wordpress' )
),
'test' => 'object_cache',
];

return $result;
}
1 change: 1 addition & 0 deletions pantheon.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) ) {
require_once 'inc/functions.php';
require_once 'inc/pantheon-page-cache.php';
require_once 'inc/site-health.php';
if ( ! defined( 'DISABLE_PANTHEON_UPDATE_NOTICES' ) || ! DISABLE_PANTHEON_UPDATE_NOTICES ) {
require_once 'inc/pantheon-updates.php';
}
Expand Down
92 changes: 92 additions & 0 deletions tests/phpunit/test-site-health.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Pantheon Site Health page Tests
*
* @package pantheon
*/

/**
* Pantheon Site Health page Test Case
*/
class Test_Site_Health extends WP_UnitTestCase {
/**
* The original active plugins.
*
* Used to restore the original active plugins after the test.
*
* @var array
*/
private $original_active_plugins;

public function setUp(): void {
parent::setUp();
$this->original_active_plugins = get_option( 'active_plugins' );
add_filter( 'site_status_tests', '\\Pantheon\\Site_Health\\site_health_mods' );
add_filter( 'site_status_tests', '\\Pantheon\\Site_Health\\object_cache_tests' );
}

public function tearDown(): void {
parent::tearDown();
update_option( 'active_plugins', $this->original_active_plugins );
}

private function set_active_plugin( $plugin ) {
update_option( 'active_plugins', $plugin );
wp_cache_delete( 'plugins', 'plugins' );
}

public function test_site_health_mods() {
// Mock array to represent the structure passed to the filter.
$mock_tests = [
'direct' => [
'update_temp_backup_writable' => [],
'available_updates_disk_space' => [],
],
'async' => [
'background_updates' => [],
],
];

$result = apply_filters( 'site_status_tests', $mock_tests );

$this->assertArrayNotHasKey( 'update_temp_backup_writable', $result['direct'] );
$this->assertArrayNotHasKey( 'available_updates_disk_space', $result['direct'] );
$this->assertArrayNotHasKey( 'background_updates', $result['async'] );
}

public function test_object_cache_no_redis() {
$result = Pantheon\Site_Health\test_object_cache();

$this->assertEquals( 'critical', $result['status'] );
$this->assertStringContainsString( 'Redis object cache is not active', $result['description'] );
}

public function test_object_cache_with_redis_no_plugin() {
$_ENV['CACHE_HOST'] = 'cacheserver'; // Ensure CACHE_HOST is set.

$result = Pantheon\Site_Health\test_object_cache();

$this->assertEquals( 'critical', $result['status'] );
$this->assertStringContainsString( 'Redis object cache is active for your site but you have no object cache plugin installed.', $result['description'] );
}

public function test_object_cache_with_wpredis_active() {
$_ENV['CACHE_HOST'] = 'cacheserver'; // Ensure CACHE_HOST is set.
$this->set_active_plugin( 'wp-redis/wp-redis.php' );

$result = Pantheon\Site_Health\test_object_cache();

$this->assertEquals( 'recommended', $result['status'] );
$this->assertStringContainsString( 'WP Redis is active for your site. We recommend using Object Cache Pro.', $result['description'] );
}

public function test_object_cache_with_ocp_active() {
$_ENV['CACHE_HOST'] = 'cacheserver'; // Ensure CACHE_HOST is set.
$this->set_active_plugin( 'object-cache-pro/object-cache-pro.php' );

$result = Pantheon\Site_Health\test_object_cache();

$this->assertEquals( 'good', $result['status'] );
$this->assertStringContainsString( 'Object Cache Pro is active for your site.', $result['description'] );
}
}

0 comments on commit ed7252a

Please sign in to comment.