Files
wp-recache/tests/test-helpers.php
T
kevin.bataille d2be239c3f fix: address Phase 1 code review findings
- Serve cache via template_redirect so feeds/404/REST are excluded and
  query conditionals work
- Bypass cache on query strings; key files by md5(host+path) to avoid
  collisions and support multisite
- Write wp-content/wp-recache-cache.php drop-in server and set WP_CACHE
  in wp-config.php so advanced-cache.php actually runs
- Only remove advanced-cache.php on deactivation when it is ours
- Respect DONOTCACHEPAGE, non-200 responses; bots read but never warm
  cache
- Fix comment purge (comment_post_ID) and heartbeat purge ($data arg)
- Fix exclude URLs textarea parsing; purge all when exclusions change
- Enqueue admin purge script on every admin-bar page, not just settings
- Sync tests, add TESTING.md manual test guide
2026-09-13 13:44:30 +02:00

119 lines
4.5 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 . md5('example.com/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 . md5('example.com/') . '.html';
$this->assertEquals($expected, wp_recache_get_cache_path($url));
}
public function test_wp_recache_get_cache_path_no_collision_between_nested_urls() {
$a = wp_recache_get_cache_path('https://example.com/a/b/');
$b = wp_recache_get_cache_path('https://example.com/ab/');
$this->assertNotEquals($a, $b);
}
public function test_wp_recache_get_cache_path_no_collision_between_hosts() {
$a = wp_recache_get_cache_path('https://site1.example.com/');
$b = wp_recache_get_cache_path('https://site2.example.com/');
$this->assertNotEquals($a, $b);
}
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');
}
}