Add first round of tests for plugin
This commit is contained in:
Родитель
78fef2c25a
Коммит
b12c628153
|
@ -1,9 +0,0 @@
|
|||
<?php
|
||||
|
||||
class SampleTest extends WP_UnitTestCase {
|
||||
|
||||
function test_sample() {
|
||||
// replace this with some actual testing code
|
||||
$this->assertTrue( true );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
<?php
|
||||
|
||||
class SW_Tests extends WP_UnitTestCase {
|
||||
|
||||
function setup() {
|
||||
parent::setup();
|
||||
|
||||
// Ensure the plugin is enabled
|
||||
update_option('wp_sw_cache_enabled', true);
|
||||
|
||||
// Set which files should be cached
|
||||
update_option('wp_sw_cache_files', array('style.css', 'screenshot.png'));
|
||||
}
|
||||
|
||||
function test_file_must_exist() {
|
||||
// Step 1: Set the hash
|
||||
$first_hash = get_option('wp_sw_cache_name');
|
||||
|
||||
// Step 2: Rename file so to mock that the file has been deleted
|
||||
rename(get_template_directory().'/style.css', get_template_directory().'/style.temp');
|
||||
|
||||
// Step 3: Get hash
|
||||
SW_Cache_Main::build_sw();
|
||||
$second_hash = get_option('wp_sw_cache_name');
|
||||
|
||||
// Success means the hash was updated because the file was deleted
|
||||
$this->assertTrue($first_hash != $second_hash);
|
||||
|
||||
// Cleanup: put the file back
|
||||
rename(get_template_directory().'/style.temp', get_template_directory().'/style.css');
|
||||
}
|
||||
|
||||
function test_unchanged_files_and_times_generates_same_hash() {
|
||||
// Step 1: Get hash
|
||||
SW_Cache_Main::build_sw();
|
||||
$first_hash = get_option('wp_sw_cache_name');
|
||||
|
||||
// Step 2: Do nothing, get the hash again
|
||||
SW_Cache_Main::build_sw();
|
||||
$second_hash = get_option('wp_sw_cache_name');
|
||||
|
||||
// Success means the hashes are the same because nothing has changed
|
||||
$this->assertTrue($first_hash === $second_hash);
|
||||
}
|
||||
|
||||
function test_file_changed_generates_new_hash() {
|
||||
// Step 1: Get hash
|
||||
SW_Cache_Main::build_sw();
|
||||
$first_hash = get_option('wp_sw_cache_name');
|
||||
|
||||
// Step 2: Update a file's contents to nudge the modified time
|
||||
$file_to_edit = get_template_directory().'/style.css';
|
||||
$original_content = file_get_contents($file_to_edit);
|
||||
file_put_contents($file_to_edit, 'blah blah');
|
||||
|
||||
// Step 3: Get the new hash
|
||||
SW_Cache_Main::build_sw();
|
||||
$second_hash = get_option('wp_sw_cache_name');
|
||||
|
||||
// Success means the hashes are different because a file has changed
|
||||
$this->assertTrue($first_hash !== $second_hash);
|
||||
}
|
||||
|
||||
function test_changed_file_list_generates_new_hash() {
|
||||
// Step 1: Get hash
|
||||
SW_Cache_Main::build_sw();
|
||||
$first_hash = get_option('wp_sw_cache_name');
|
||||
|
||||
// Step 2: Remove the last item from the list
|
||||
$files = get_option('wp_sw_cache_files');
|
||||
$last_file = array_pop($files);
|
||||
update_option('wp_sw_cache_files', $files);
|
||||
|
||||
// Step 3: Get the new hash
|
||||
SW_Cache_Main::build_sw();
|
||||
$second_hash = get_option('wp_sw_cache_name');
|
||||
|
||||
// Success means the hashes are different because a file has changed
|
||||
$this->assertTrue($first_hash !== $second_hash);
|
||||
|
||||
// Cleanup: Put the removed item back
|
||||
array_push($files, $last_file);
|
||||
update_option('wp_sw_cache_files', $files);
|
||||
}
|
||||
|
||||
function test_disabled_plugin_returns_nothing() {
|
||||
// Step 1: Disable the plugin
|
||||
update_option('wp_sw_cache_enabled', false);
|
||||
|
||||
// Step 2: Get the SW output, which should be nothing
|
||||
$sw_output = SW_Cache_Main::build_sw();
|
||||
|
||||
// Success is no output because the plugin is disabled
|
||||
$this->assertTrue($sw_output === '');
|
||||
|
||||
// Cleanup: re-enable the plugin
|
||||
update_option('wp_sw_cache_enabled', true);
|
||||
}
|
||||
}
|
|
@ -100,46 +100,6 @@ class SW_Cache_Admin {
|
|||
|
||||
?>
|
||||
|
||||
<style>
|
||||
.wp-sw-cache-suggested {
|
||||
background: lightgreen;
|
||||
}
|
||||
|
||||
.wp-sw-cache-suggest-file,
|
||||
.wp-sw-cache-toggle-all,
|
||||
.wp-sw-cache-clear-all {
|
||||
float: right;
|
||||
margin-left: 10px !important;
|
||||
}
|
||||
|
||||
.wp-sw-cache-file-list {
|
||||
max-height: 300px;
|
||||
background: #fefefe;
|
||||
border: 1px solid #ccc;
|
||||
padding: 10px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.wp-sw-cache-suggest-file span {
|
||||
font-size: smaller;
|
||||
color: #fc0;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.wp-sw-cache-file-size,
|
||||
.wp-sw-cache-file-recommended {
|
||||
font-size: smaller;
|
||||
color: #999;
|
||||
font-size: italic;
|
||||
display: inline-block;
|
||||
margin-left: 20px;
|
||||
}
|
||||
|
||||
.wp-sw-cache-file-recommended {
|
||||
color: green;
|
||||
}
|
||||
|
||||
</style>
|
||||
<div class="wrap">
|
||||
|
||||
<?php if($submitted) { ?>
|
||||
|
@ -150,7 +110,7 @@ class SW_Cache_Admin {
|
|||
|
||||
<h1><?php _e('WordPress Service Worker Cache', 'wpswcache'); ?> (<?php echo SW_Cache_Main::$cache_prefix; ?>)</h1>
|
||||
|
||||
<p><?php _e('WordPress Service Worker Cache is a ultility that harnesses the power of the <a href="https://serviceworke.rs" target="_blank">ServiceWorker API</a> to cache frequently used assets for the purposes of performance and offline viewing.'); ?></p>
|
||||
<p><?php _e('WordPress Service Worker Cache is a utility that harnesses the power of the <a href="https://serviceworke.rs" target="_blank">ServiceWorker API</a> to cache frequently used assets for the purposes of performance and offline viewing.'); ?></p>
|
||||
|
||||
<form method="post" action="">
|
||||
<input type="hidden" name="wpswcache_form_submitted" value="1">
|
||||
|
@ -267,6 +227,47 @@ class SW_Cache_Admin {
|
|||
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.wp-sw-cache-suggested {
|
||||
background: lightgreen;
|
||||
}
|
||||
|
||||
.wp-sw-cache-suggest-file,
|
||||
.wp-sw-cache-toggle-all,
|
||||
.wp-sw-cache-clear-all {
|
||||
float: right;
|
||||
margin-left: 10px !important;
|
||||
}
|
||||
|
||||
.wp-sw-cache-file-list {
|
||||
max-height: 300px;
|
||||
background: #fefefe;
|
||||
border: 1px solid #ccc;
|
||||
padding: 10px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.wp-sw-cache-suggest-file span {
|
||||
font-size: smaller;
|
||||
color: #fc0;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.wp-sw-cache-file-size,
|
||||
.wp-sw-cache-file-recommended {
|
||||
font-size: smaller;
|
||||
color: #999;
|
||||
font-size: italic;
|
||||
display: inline-block;
|
||||
margin-left: 20px;
|
||||
}
|
||||
|
||||
.wp-sw-cache-file-recommended {
|
||||
color: green;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
<script type="text/javascript">
|
||||
jQuery('.wp-sw-cache-suggest-file').on('click', function() {
|
||||
// TODO: More advanced logic
|
||||
|
|
|
@ -11,15 +11,17 @@ class SW_Cache_DB {
|
|||
if (!self::$instance) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public static function on_activate() {
|
||||
// Set default options.
|
||||
// For v1 we'll prompt the user enable the plugin manually upon activation
|
||||
update_option('wp_sw_cache_enabled', false);
|
||||
update_option('wp_sw_cache_files', array());
|
||||
// The "style.css" file is a standard WordPress file, so we can safely assume this exists
|
||||
update_option('wp_sw_cache_files', array('style.css'));
|
||||
// Create an initial SW version
|
||||
SW_Cache_Main::update_version();
|
||||
// Setting debug initially will help the user understand what the SW is doing via the console
|
||||
update_option('wp_sw_cache_debug', true);
|
||||
}
|
||||
|
||||
|
|
|
@ -22,14 +22,18 @@ class SW_Cache_Main {
|
|||
}
|
||||
}
|
||||
|
||||
public function update_version($name = '') {
|
||||
public static function update_version($name = '') {
|
||||
if(!$name) {
|
||||
$name = time();
|
||||
}
|
||||
update_option('wp_sw_cache_name', self::$cache_prefix.'-'.$name);
|
||||
}
|
||||
|
||||
public function write_sw() {
|
||||
public static function build_sw() {
|
||||
// Return nothing if the plugin is disabled
|
||||
if(!get_option('wp_sw_cache_enabled')) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$files = get_option('wp_sw_cache_files');
|
||||
if(!$files) {
|
||||
|
@ -42,7 +46,6 @@ class SW_Cache_Main {
|
|||
// Ensure that every file directed to be cached still exists
|
||||
foreach($files as $index=>$file) {
|
||||
$tfile = get_template_directory().'/'.$file;
|
||||
|
||||
if(file_exists($tfile)) {
|
||||
// Use file's last change time in name hash so the SW is updated if any file is updated
|
||||
$file_keys[get_template_directory_uri().'/'.$file] = filemtime($tfile);
|
||||
|
@ -59,7 +62,11 @@ class SW_Cache_Main {
|
|||
$contents = str_replace('$name', $name, $contents);
|
||||
$contents = str_replace('$files', json_encode(array_keys($file_keys)), $contents);
|
||||
$contents = str_replace('$debug', get_option('wp_sw_cache_debug') ? 'true' : 'false', $contents);
|
||||
echo $contents;
|
||||
return $contents;
|
||||
}
|
||||
|
||||
public function write_sw() {
|
||||
echo self::build_sw();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
/*
|
||||
Plugin Name: WP ServiceWorker Cache
|
||||
Plugin Name: WP Service Worker Cache
|
||||
Plugin URI: https://github.com/darkwing/wp-sw-cache
|
||||
Description: This WordPress plugin provides a method for caching theme assets via a service worker.
|
||||
Version: 0.2
|
||||
|
|
Загрузка…
Ссылка в новой задаче