Files
wp-recache/tests/test-helpers.php
kevin.bataille 65657276f6 feat: rejected-cookie cache exclusions for WooCommerce cart
Add wp_recache_rejected_cookies option (defaults: woocommerce_items_in_cart,
wp_woocommerce_session_*, wp-postpass, comment_author_*, wordpress_*) that
bypasses page cache when any listed cookie is present. Both the WP-level
check (wp_recache_should_exclude) and the standalone drop-in apply the
same exact/prefix matching logic. Editing the option regenerates the
drop-in and purges all cache.
2026-09-13 15:43:20 +02:00

161 lines
6.3 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_has_rejected_cookie_returns_true_for_exact_match() {
wp_set_current_user(0);
update_option('wp_recache_rejected_cookies', ['wp-postpass']);
$_COOKIE = ['wp-postpass' => 'abc123'];
$this->assertTrue(wp_recache_has_rejected_cookie());
$_COOKIE = [];
}
public function test_wp_recache_has_rejected_cookie_returns_true_for_prefix_match() {
wp_set_current_user(0);
update_option('wp_recache_rejected_cookies', ['comment_author_*']);
$_COOKIE = ['comment_author_12345' => 'test'];
$this->assertTrue(wp_recache_has_rejected_cookie());
$_COOKIE = [];
}
public function test_wp_recache_has_rejected_cookie_returns_false_when_no_match() {
wp_set_current_user(0);
update_option('wp_recache_rejected_cookies', ['wp-postpass']);
$_COOKIE = ['unrelated_cookie' => 'value'];
$this->assertFalse(wp_recache_has_rejected_cookie());
$_COOKIE = [];
}
public function test_wp_recache_has_rejected_cookie_returns_false_when_empty_cookies() {
wp_set_current_user(0);
update_option('wp_recache_rejected_cookies', ['wp-postpass']);
$_COOKIE = [];
$this->assertFalse(wp_recache_has_rejected_cookie());
}
public function test_wp_recache_should_exclude_returns_true_for_rejected_cookie() {
wp_set_current_user(0);
update_option('wp_recache_rejected_cookies', ['woocommerce_items_in_cart']);
$_COOKIE = ['woocommerce_items_in_cart' => 'yes'];
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/test-page/';
unset($_SERVER['HTTP_USER_AGENT']);
$this->assertTrue(wp_recache_should_exclude());
$_COOKIE = [];
}
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');
}
}