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)
74 lines
2.0 KiB
PHP
74 lines
2.0 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();
|
|
|
|
$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();
|
|
|
|
$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));
|
|
}
|
|
}
|