test: standalone drop-in test and docker-based integration test
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
/**
|
||||
* Standalone test for the wp-recache-cache.php drop-in cookie logic.
|
||||
*
|
||||
* Run with plain PHP:
|
||||
* php tests/test-dropin.php
|
||||
*
|
||||
* The drop-in uses `return;` to skip cache. Since we can't intercept
|
||||
* `return` from an include, we generate a test variant that sets a
|
||||
* global flag instead. The flag mirrors the exact same branching logic
|
||||
* as the production drop-in.
|
||||
*/
|
||||
|
||||
$pass = 0;
|
||||
$fail = 0;
|
||||
|
||||
/**
|
||||
* Generate a test drop-in that sets $_wp_recache_exited instead of returning.
|
||||
* Uses identical cookie-matching logic to the real drop-in.
|
||||
*/
|
||||
function write_test_dropin(array $rejected_cookies): string {
|
||||
$cookies_php = var_export($rejected_cookies, true);
|
||||
|
||||
$content = '<?php
|
||||
$_wp_recache_exited = false;
|
||||
$rejected_cookies = ' . $cookies_php . ';
|
||||
|
||||
foreach ($rejected_cookies as $cookie) {
|
||||
$cookie = trim($cookie);
|
||||
if ($cookie === \'\') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (substr($cookie, -1) === \'*\') {
|
||||
$prefix = substr($cookie, 0, -1);
|
||||
foreach ($_COOKIE as $name => $value) {
|
||||
if (strpos($name, $prefix) === 0) {
|
||||
$_wp_recache_exited = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (isset($_COOKIE[$cookie])) {
|
||||
$_wp_recache_exited = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
';
|
||||
|
||||
$tmp = sys_get_temp_dir() . '/wp-recache-test-' . getmypid();
|
||||
if (!is_dir($tmp)) {
|
||||
mkdir($tmp, 0777, true);
|
||||
}
|
||||
$file = $tmp . '/dropin.php';
|
||||
file_put_contents($file, $content);
|
||||
return $file;
|
||||
}
|
||||
|
||||
function test(string $name, array $cookies, array $request, array $server, bool $expect_exited): void {
|
||||
global $pass, $fail;
|
||||
|
||||
$_COOKIE = $request;
|
||||
$_SERVER = array_merge([
|
||||
'REQUEST_METHOD' => 'GET',
|
||||
'QUERY_STRING' => '',
|
||||
'REQUEST_URI' => '/test/',
|
||||
'HTTP_HOST' => 'example.com',
|
||||
], $server);
|
||||
|
||||
$_wp_recache_exited = false;
|
||||
$file = write_test_dropin($cookies);
|
||||
include $file;
|
||||
@unlink($file);
|
||||
|
||||
if ($_wp_recache_exited !== $expect_exited) {
|
||||
echo " FAIL: $name\n";
|
||||
echo " expected " . ($expect_exited ? 'exited' : 'fell through') . ", got " . ($_wp_recache_exited ? 'exited' : 'fell through') . "\n";
|
||||
$fail++;
|
||||
} else {
|
||||
echo " PASS: $name\n";
|
||||
$pass++;
|
||||
}
|
||||
}
|
||||
|
||||
$defaults = [
|
||||
'woocommerce_items_in_cart',
|
||||
'wp_woocommerce_session_*',
|
||||
'wp-postpass',
|
||||
'comment_author_*',
|
||||
'wordpress_*',
|
||||
];
|
||||
|
||||
echo "=== Exact match ===\n";
|
||||
test('wp-postpass present → exits', $defaults, ['wp-postpass' => 'abc123'], [], true);
|
||||
test('woocommerce_items_in_cart present → exits', $defaults, ['woocommerce_items_in_cart' => 'yes'], [], true);
|
||||
|
||||
echo "\n=== Prefix match ===\n";
|
||||
test('comment_author_12345 matches comment_author_*', $defaults, ['comment_author_12345' => 'test'], [], true);
|
||||
test('wp_woocommerce_session_abc matches wp_woocommerce_session_*', $defaults, ['wp_woocommerce_session_abc123' => 'xyz'], [], true);
|
||||
test('wordpress_logged_in_xyz matches wordpress_*', $defaults, ['wordpress_logged_in_xyz' => '1'], [], true);
|
||||
|
||||
echo "\n=== No match → falls through ===\n";
|
||||
test('unrelated cookie → falls through', $defaults, ['session_id' => 'abc123'], [], false);
|
||||
test('empty cookies → falls through', $defaults, [], [], false);
|
||||
|
||||
echo "\n=== Edge cases ===\n";
|
||||
test('whitespace-only cookie name skipped', array_merge([' '], $defaults), ['wp-postpass' => 'val'], [], true);
|
||||
test('empty rejected list → falls through', [], ['anything' => '1'], [], false);
|
||||
|
||||
echo "\n=== POST method → exits before cookie check ===\n";
|
||||
test('POST request exits early', $defaults, ['wp-postpass' => 'val'], ['REQUEST_METHOD' => 'POST'], true);
|
||||
|
||||
echo "\n=== Query string → exits before cookie check ===\n";
|
||||
test('Query string present exits early', $defaults, ['wp-postpass' => 'val'], ['QUERY_STRING' => 'foo=bar'], true);
|
||||
|
||||
$tmp = sys_get_temp_dir() . '/wp-recache-test-' . getmypid();
|
||||
@rmdir($tmp);
|
||||
|
||||
echo "\n--- Results: $pass passed, $fail failed ---\n";
|
||||
exit($fail > 0 ? 1 : 0);
|
||||
Reference in New Issue
Block a user