From fad9b40b0d875ae8cebd217b8083696080d1a7aa Mon Sep 17 00:00:00 2001 From: Ben XO <75862+ben-xo@users.noreply.github.com> Date: Wed, 25 May 2022 08:53:23 +0100 Subject: [PATCH] Update tests to trick content hashing --- test/Cached_Dir_PodcastTest.php | 37 ++++++++++++++++++++----- test/CachingTest.php | 18 ++++++------ test/Dir_PodcastTest.php | 3 +- test/Dir_Podcast_RecursiveTest.php | 1 - test/Locking_Cached_Dir_PodcastTest.php | 3 +- 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/test/Cached_Dir_PodcastTest.php b/test/Cached_Dir_PodcastTest.php index 38aace3..c3a6b21 100644 --- a/test/Cached_Dir_PodcastTest.php +++ b/test/Cached_Dir_PodcastTest.php @@ -9,6 +9,7 @@ public static function setUpBeforeClass(): void { Dir_PodcastTest::setUpBeforeClass(); Cached_Dir_Podcast::$MIN_CACHE_TIME = 5; + Cached_Dir_Podcast::$DEBUG = false; } public function setUp(): void @@ -17,9 +18,10 @@ public function setUp(): void mkdir('temp'); } - public function newPodcast() + public function newPodcast($offset=0) { $podcast = new Cached_Dir_Podcast('.', './temp'); + $podcast->setClockOffset($offset); $podcast->init(); return $podcast; } @@ -49,7 +51,7 @@ public function test_uses_generated_cache_file_if_min_time_not_elapsed_yet() // this should be ignored file_put_contents('extra.mp3', 'new data'); - $mp2 = $this->newPodcast(); + $mp2 = $this->newPodcast(2); $content2 = $mp2->generate(); // should not pick up extra.mp3 as the cache file isn't old enough @@ -71,7 +73,7 @@ public function test_does_not_use_generated_cache_file_if_min_time_has_elapsed_a // this should be considered file_put_contents('extra.mp3', 'new data'); - $mp2 = $this->newPodcast(); + $mp2 = $this->newPodcast(10); $content2 = $mp2->generate(); // should pick up extra.mp3 as the cache file is older than the min, and there's new content @@ -79,6 +81,28 @@ public function test_does_not_use_generated_cache_file_if_min_time_has_elapsed_a $this->assertEquals(1, preg_match('/extra\.mp3/', $content2)); } + public function test_does_not_use_generated_cache_file_if_min_time_has_elapsed_and_theres_additional_old_content() + { + $this->createTestItems(); + age_dir_by('.', 3600); + + $mp = $this->newPodcast(); + $content = $mp->generate(); + unset($mp); // release lock, in sub tests + + age_dir_by('.', 10); + + // this should be considered + file_put_contents('extra.mp3', 'new data'); + touch('extra.mp3', time() - 86400); + + $mp2 = $this->newPodcast(10); + $content2 = $mp2->generate(); + + // should pick up extra.mp3 as the cache file is older than the min, and there's new content + $this->assertNotEquals($content, $content2); + $this->assertEquals(1, preg_match('/extra\.mp3/', $content2)); + } public function test_renews_cache_if_old_but_not_stale() { @@ -91,8 +115,7 @@ public function test_renews_cache_if_old_but_not_stale() age_dir_by('.', 3600); - $mp2 = $this->newPodcast(); - + $mp2 = $this->newPodcast(3600); $content2 = $mp2->generate(); // should have used cache file anyway @@ -126,7 +149,7 @@ public function test_lastBuildDate_is_valid_whether_served_from_cache_or_not() age_dir_by('.', 3600); - $mp2 = $this->newPodcast(); + $mp2 = $this->newPodcast(3600); $this->assertTrue($mp2->isCached()); $mp2->generate(); clearstatcache(); @@ -138,7 +161,7 @@ public function test_lastBuildDate_is_valid_whether_served_from_cache_or_not() clearstatcache(); age_dir_by('.', 3600); sleep(1); // not much choice here! - $mp3 = $this->newPodcast(); + $mp3 = $this->newPodcast(3600+3600); $mp3->generate(); clearstatcache(); diff --git a/test/CachingTest.php b/test/CachingTest.php index 65e37a9..4a7db1f 100644 --- a/test/CachingTest.php +++ b/test/CachingTest.php @@ -43,7 +43,7 @@ public function test_default_empty_podcast_doesnt_regenerate_in_first_MIN_CACHE_ clearstatcache(); $cached_mtime_before = filemtime($cached_output_files[0]); - exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime', $new_output, $this->returncode); + exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime --clock-offset=7', $new_output, $this->returncode); clearstatcache(); $cached_output_files = glob(temp_xml_glob()); @@ -71,7 +71,7 @@ public function test_default_empty_podcast_renews_cache_file_mtime_after_MIN_CAC clearstatcache(); $cached_mtime_before = filemtime($cached_output_files[0]); - exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime', $new_output, $this->returncode); + exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime --clock-offset=7', $new_output, $this->returncode); clearstatcache(); $cached_output_files = glob(temp_xml_glob()); @@ -104,7 +104,7 @@ public function test_default_empty_podcast_doesnt_regenerate_before_MIN_CACHE_TI clearstatcache(); $cached_mtime_before = filemtime($cached_output_files[0]); - exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime', $new_output, $this->returncode); + exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime --clock-offset=7', $new_output, $this->returncode); clearstatcache(); $cached_output_files = glob(temp_xml_glob()); @@ -138,7 +138,7 @@ public function test_default_empty_podcast_regenerates_after_MIN_CACHE_TIME_with clearstatcache(); $cached_mtime_before = filemtime($cached_output_files[0]); - exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime', $new_output, $this->returncode); + exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime --clock-offset=90', $new_output, $this->returncode); clearstatcache(); $cached_output_files = glob(temp_xml_glob()); @@ -168,7 +168,7 @@ public function test_default_empty_podcast_obeys_minimum_cache_time_not_elapsed( // --dont-uncache: tells dir2cast not use the default caching rules, not ignore them due to CLI // --min-file-age=0 : tells dir2cast to include files that are brand new - exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=0 --ignore-dir2cast-mtime', $this->output, $this->returncode); + exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=0 --ignore-dir2cast-mtime --clock-offset=2', $this->output, $this->returncode); $new_content = file_get_contents($this->file); $this->assertEquals($this->content, $new_content); @@ -184,7 +184,7 @@ public function test_default_empty_podcast_obeys_minimum_cache_time_elapsed(): v // --dont-uncache: tells dir2cast not use the default caching rules, not ignore them due to CLI // --min-file-age=0 : tells dir2cast to include files that are brand new - exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=0 --ignore-dir2cast-mtime', $this->output, $this->returncode); + exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=0 --ignore-dir2cast-mtime --clock-offset=3600', $this->output, $this->returncode); $new_content = file_get_contents($this->file); $this->assertNotEquals($this->content, $new_content); @@ -198,7 +198,7 @@ public function test_default_empty_podcast_uncaches_without_dont_uncache_even_if // too new to bust the cache, but cli runner uncaches anyway file_put_contents('empty.mp3', 'test'); - exec('php dir2cast.php --output=out.xml --min-file-age=0 --ignore-dir2cast-mtime', $this->output, $this->returncode); + exec('php dir2cast.php --output=out.xml --min-file-age=0 --ignore-dir2cast-mtime --clock-offset=2', $this->output, $this->returncode); $new_content = file_get_contents($this->file); $this->assertNotEquals($this->content, $new_content); @@ -217,7 +217,7 @@ public function test_expired_podcast_is_regenerated(): void file_put_contents('empty.mp3', 'test'); touch('empty.mp3', time()-3600); // busts cache as older than min-file-age - exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=30 --ignore-dir2cast-mtime'); + exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=30 --ignore-dir2cast-mtime --clock-offset=86400'); $new_content = file_get_contents($this->file); // should have empty.mp3 $this->assertNotEquals($this->content, $new_content); $this->assertEquals(1, preg_match('/empty\.mp3/', $new_content)); @@ -246,7 +246,7 @@ public function test_too_new_file_not_included_in_podcast(): void // (which it shouldn't be!) sleep(1); - exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=30 --ignore-dir2cast-mtime'); + exec('php dir2cast.php --output=out.xml --dont-uncache --min-file-age=30 --ignore-dir2cast-mtime --clock-offset=86400'); $new_content = file_get_contents($this->file); // should not have empty.mp3 diff --git a/test/Dir_PodcastTest.php b/test/Dir_PodcastTest.php index 4a086f7..533fa2a 100644 --- a/test/Dir_PodcastTest.php +++ b/test/Dir_PodcastTest.php @@ -17,6 +17,7 @@ public function setUp(): void parent::setUp(); Dir_Podcast::$RECURSIVE_DIRECTORY_ITERATOR = false; Dir_Podcast::$ITEM_COUNT = 10; + Dir_Podcast::$DEBUG = false; } public function newPodcast() @@ -110,7 +111,6 @@ public function test_generating_twice_doesnt_rescan() public function test_regenerates_if_metadata_files_added() { - Dir_Podcast::$DEBUG = false; Media_RSS_Item::$DESCRIPTION_SOURCE = 'summary'; $filemtime = $this->createTestItems(); age_dir_by('.', 200); @@ -204,7 +204,6 @@ protected function delete_test_files() public function tearDown(): void { - Dir_Podcast::$DEBUG = false; Media_RSS_Item::$DESCRIPTION_SOURCE = 'comment'; $this->delete_test_files(); parent::tearDown(); diff --git a/test/Dir_Podcast_RecursiveTest.php b/test/Dir_Podcast_RecursiveTest.php index 52e8b9d..6f39dbb 100644 --- a/test/Dir_Podcast_RecursiveTest.php +++ b/test/Dir_Podcast_RecursiveTest.php @@ -78,7 +78,6 @@ protected function delete_test_files() public function test_regenerates_if_metadata_files_added() { - Dir_Podcast::$DEBUG = false; Media_RSS_Item::$DESCRIPTION_SOURCE = 'summary'; $filemtime = $this->createTestItems(); age_dir_by('.', 200); diff --git a/test/Locking_Cached_Dir_PodcastTest.php b/test/Locking_Cached_Dir_PodcastTest.php index ca4edf1..7336c98 100644 --- a/test/Locking_Cached_Dir_PodcastTest.php +++ b/test/Locking_Cached_Dir_PodcastTest.php @@ -10,9 +10,10 @@ public static function setUpBeforeClass(): void Cached_Dir_PodcastTest::setUpBeforeClass(); } - public function newPodcast() + public function newPodcast($offset=0) { $podcast = new Locking_Cached_Dir_Podcast('.', './temp'); + $podcast->setClockOffset($offset); $podcast->init(); return $podcast; }