- 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
84 lines
2.4 KiB
PHP
84 lines
2.4 KiB
PHP
<?php
|
|
/**
|
|
* Tests for WPCache functionality.
|
|
*
|
|
* @package WP_Recache
|
|
*/
|
|
|
|
class WP_Recache_WPCache_Test extends WP_UnitTestCase {
|
|
|
|
public function test_cache_creates_file_on_first_request() {
|
|
$cache = new WP_Recache_WPCache();
|
|
|
|
$_SERVER['HTTP_HOST'] = 'example.com';
|
|
$_SERVER['REQUEST_URI'] = '/test-page/';
|
|
$_SERVER['HTTPS'] = 'off';
|
|
unset($_SERVER['HTTP_USER_AGENT']);
|
|
|
|
$url = 'https://example.com/test-page/';
|
|
$path = wp_recache_get_cache_path($url);
|
|
|
|
wp_mkdir_p(dirname($path));
|
|
|
|
$output = '<html><body>Test content</body></html>';
|
|
$result = $cache->cache_output($output);
|
|
|
|
$this->assertEquals($output, $result);
|
|
$this->assertFileExists($path);
|
|
$this->assertEquals($output, file_get_contents($path));
|
|
}
|
|
|
|
public function test_purge_post_cache_removes_file() {
|
|
$cache = new WP_Recache_WPCache();
|
|
|
|
$post_id = $this->factory()->post->create();
|
|
$url = get_permalink($post_id);
|
|
$path = wp_recache_get_cache_path($url);
|
|
|
|
wp_mkdir_p(dirname($path));
|
|
file_put_contents($path, 'cached content');
|
|
|
|
$this->assertFileExists($path);
|
|
|
|
$cache->purge_post_cache($post_id);
|
|
|
|
$this->assertFileDoesNotExist($path);
|
|
}
|
|
|
|
public function test_get_current_url_returns_correct_url() {
|
|
$cache = new WP_Recache_WPCache();
|
|
|
|
$_SERVER['HTTP_HOST'] = 'example.com';
|
|
$_SERVER['REQUEST_URI'] = '/test-page/';
|
|
$_SERVER['HTTPS'] = 'off';
|
|
|
|
$reflection = new ReflectionClass($cache);
|
|
$method = $reflection->getMethod('get_current_url');
|
|
$method->setAccessible(true);
|
|
|
|
$url = $method->invoke($cache);
|
|
|
|
$this->assertEquals('http://example.com/test-page/', $url);
|
|
}
|
|
|
|
public function test_cache_file_is_created_with_correct_permissions() {
|
|
$cache = new WP_Recache_WPCache();
|
|
|
|
$_SERVER['HTTP_HOST'] = 'example.com';
|
|
$_SERVER['REQUEST_URI'] = '/test-page/';
|
|
$_SERVER['HTTPS'] = 'off';
|
|
unset($_SERVER['HTTP_USER_AGENT']);
|
|
|
|
$url = 'https://example.com/test-page/';
|
|
$path = wp_recache_get_cache_path($url);
|
|
|
|
wp_mkdir_p(dirname($path));
|
|
|
|
$output = '<html><body>Test content</body></html>';
|
|
$cache->cache_output($output);
|
|
|
|
$this->assertFileExists($path);
|
|
$this->assertTrue(is_readable($path));
|
|
}
|
|
}
|