Implement the core page caching functionality and admin settings interface for WP Recache plugin. This includes: - Plugin bootstrap with activation/deactivation handlers - Admin settings page with tabbed interface (Cache, Optimization, Advanced) - Admin bar integration for quick cache purging - Core page cache using output buffering - Cache file management with mobile device separation - Cache purge on post update/comment - Default URL exclusions (cart, checkout, account) - Cache statistics display - PHPUnit test suite setup - Docker-based development environment - PHPStan configuration for static analysis References: - Closes #1 (Core Page Cache) - Closes #8 (Admin Settings UI)
119 lines
4.4 KiB
PHP
119 lines
4.4 KiB
PHP
<?php
|
|
/**
|
|
* Tests for Helpers functions.
|
|
*
|
|
* @package WP_Recache
|
|
*/
|
|
|
|
class WP_Recache_Helpers_Test extends WP_UnitTestCase {
|
|
|
|
public function test_wp_recache_is_cache_enabled_returns_true_by_default() {
|
|
update_option('wp_recache_cache_enabled', true);
|
|
$this->assertTrue(wp_recache_is_cache_enabled());
|
|
}
|
|
|
|
public function test_wp_recache_is_cache_enabled_returns_false_when_disabled() {
|
|
update_option('wp_recache_cache_enabled', false);
|
|
$this->assertFalse(wp_recache_is_cache_enabled());
|
|
}
|
|
|
|
public function test_wp_recache_get_option_returns_default_when_not_set() {
|
|
delete_option('wp_recache_test_option');
|
|
$this->assertEquals('default', wp_recache_get_option('test_option', 'default'));
|
|
}
|
|
|
|
public function test_wp_recache_get_option_returns_stored_value() {
|
|
update_option('wp_recache_test_option', 'stored');
|
|
$this->assertEquals('stored', wp_recache_get_option('test_option', 'default'));
|
|
}
|
|
|
|
public function test_wp_recache_set_option_stores_value() {
|
|
wp_recache_set_option('test_option', 'new_value');
|
|
$this->assertEquals('new_value', get_option('wp_recache_test_option'));
|
|
}
|
|
|
|
public function test_wp_recache_get_cache_path_returns_correct_path() {
|
|
$url = 'https://example.com/test-page/';
|
|
$expected = WP_RECACHE_CACHE_PATH . 'test-page.html';
|
|
$this->assertEquals($expected, wp_recache_get_cache_path($url));
|
|
}
|
|
|
|
public function test_wp_recache_get_cache_path_handles_root_url() {
|
|
$url = 'https://example.com/';
|
|
$expected = WP_RECACHE_CACHE_PATH . 'index.html';
|
|
$this->assertEquals($expected, wp_recache_get_cache_path($url));
|
|
}
|
|
|
|
public function test_wp_recache_get_cache_key_returns_md5_hash() {
|
|
$url = 'https://example.com/test-page/';
|
|
$expected = md5('/test-page/');
|
|
$this->assertEquals($expected, wp_recache_get_cache_key($url));
|
|
}
|
|
|
|
public function test_wp_recache_get_cache_key_handles_root_url() {
|
|
$url = 'https://example.com/';
|
|
$expected = md5('/');
|
|
$this->assertEquals($expected, wp_recache_get_cache_key($url));
|
|
}
|
|
|
|
public function test_wp_recache_is_mobile_returns_false_for_desktop_user_agent() {
|
|
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36';
|
|
$this->assertFalse(wp_recache_is_mobile());
|
|
}
|
|
|
|
public function test_wp_recache_is_mobile_returns_true_for_mobile_user_agent() {
|
|
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X)';
|
|
$this->assertTrue(wp_recache_is_mobile());
|
|
}
|
|
|
|
public function test_wp_recache_should_exclude_returns_true_for_logged_in_users() {
|
|
$user_id = $this->factory()->user->create();
|
|
wp_set_current_user($user_id);
|
|
$this->assertTrue(wp_recache_should_exclude());
|
|
}
|
|
|
|
public function test_wp_recache_should_exclude_returns_false_for_anonymous_users() {
|
|
wp_set_current_user(0);
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
$_SERVER['REQUEST_URI'] = '/test-page/';
|
|
unset($_SERVER['HTTP_USER_AGENT']);
|
|
$this->assertFalse(wp_recache_should_exclude());
|
|
}
|
|
|
|
public function test_wp_recache_should_exclude_returns_true_for_excluded_urls() {
|
|
wp_set_current_user(0);
|
|
$_SERVER['REQUEST_METHOD'] = 'GET';
|
|
$_SERVER['REQUEST_URI'] = '/cart/';
|
|
unset($_SERVER['HTTP_USER_AGENT']);
|
|
$this->assertTrue(wp_recache_should_exclude());
|
|
}
|
|
|
|
public function test_wp_recache_delete_cache_removes_file() {
|
|
$url = 'https://example.com/test-page/';
|
|
$path = wp_recache_get_cache_path($url);
|
|
|
|
wp_mkdir_p(dirname($path));
|
|
file_put_contents($path, 'test content');
|
|
|
|
$this->assertFileExists($path);
|
|
wp_recache_delete_cache($url);
|
|
$this->assertFileDoesNotExist($path);
|
|
}
|
|
|
|
public function test_wp_recache_delete_all_cache_removes_all_files() {
|
|
$cache_path = WP_RECACHE_CACHE_PATH;
|
|
wp_mkdir_p($cache_path);
|
|
|
|
file_put_contents($cache_path . 'test1.html', 'content1');
|
|
file_put_contents($cache_path . 'test2.html', 'content2');
|
|
|
|
$this->assertFileExists($cache_path . 'test1.html');
|
|
$this->assertFileExists($cache_path . 'test2.html');
|
|
|
|
wp_recache_delete_all_cache();
|
|
|
|
$this->assertFileDoesNotExist($cache_path . 'test1.html');
|
|
$this->assertFileDoesNotExist($cache_path . 'test2.html');
|
|
}
|
|
}
|