From b12c628153722bbb4495a690eda90dcbad9730c6 Mon Sep 17 00:00:00 2001 From: David Walsh Date: Thu, 25 Feb 2016 12:39:32 -0600 Subject: [PATCH] Add first round of tests for plugin --- tests/test-sample.php | 9 --- tests/test-sw.php | 99 +++++++++++++++++++++++++++++++ wp-sw-cache/wp-sw-cache-admin.php | 83 +++++++++++++------------- wp-sw-cache/wp-sw-cache-db.php | 8 ++- wp-sw-cache/wp-sw-cache-main.php | 15 +++-- wp-sw-cache/wp-sw-cache.php | 2 +- 6 files changed, 158 insertions(+), 58 deletions(-) delete mode 100755 tests/test-sample.php create mode 100755 tests/test-sw.php diff --git a/tests/test-sample.php b/tests/test-sample.php deleted file mode 100755 index 85e0251..0000000 --- a/tests/test-sample.php +++ /dev/null @@ -1,9 +0,0 @@ -assertTrue( true ); - } -} diff --git a/tests/test-sw.php b/tests/test-sw.php new file mode 100755 index 0000000..846f077 --- /dev/null +++ b/tests/test-sw.php @@ -0,0 +1,99 @@ +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); + } +} diff --git a/wp-sw-cache/wp-sw-cache-admin.php b/wp-sw-cache/wp-sw-cache-admin.php index 5368ffd..ac542d9 100644 --- a/wp-sw-cache/wp-sw-cache-admin.php +++ b/wp-sw-cache/wp-sw-cache-admin.php @@ -100,46 +100,6 @@ class SW_Cache_Admin { ?> -
@@ -150,7 +110,7 @@ class SW_Cache_Admin {

()

-

ServiceWorker API to cache frequently used assets for the purposes of performance and offline viewing.'); ?>

+

ServiceWorker API to cache frequently used assets for the purposes of performance and offline viewing.'); ?>

@@ -267,6 +227,47 @@ class SW_Cache_Admin {
+ +