Skip to content

Commit

Permalink
Update feed if the feed metadata files are updated
Browse files Browse the repository at this point in the history
description.txt, image.jpg, etc
  • Loading branch information
ben-xo committed Dec 29, 2021
1 parent d7d9d7e commit fa9524f
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 0 deletions.
29 changes: 29 additions & 0 deletions dir2cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,34 @@ public function update_mtime_if_dir2cast_or_settings_modified()
$this->podcast->updateMaxMtime(filemtime(INI_FILE), INI_FILE);
}

public function update_mtime_if_metadata_files_modified()
{
// Ensure that the cache is invalidated if we have updated any of non-episode files used for feed metadata
$metadata_files = array(
'description.txt',
'itunes_summary.txt',
'itunes_subtitle.txt',
'image.jpg',
'image.png',
'itunes_image.jpg',
'itunes_image.png',
);
foreach($metadata_files as $file)
{
$filepath = rtrim(MP3_DIR, '/') . '/' . $file;
if(!file_exists($filepath))
{
$filepath = DIR2CAST_BASE . '/' . $file;
}
if(!file_exists($filepath))
{
continue;
}

$this->podcast->updateMaxMtime(filemtime($filepath), $filepath);
}
}

public function init()
{
$podcast = $this->podcast;
Expand Down Expand Up @@ -2045,6 +2073,7 @@ function main($args)
$dispatcher->uncache_if_output_file();
if(!defined('IGNORE_DIR2CAST_MTIME'))
$dispatcher->update_mtime_if_dir2cast_or_settings_modified();
$dispatcher->update_mtime_if_metadata_files_modified();
$dispatcher->init();
return $dispatcher->output(); // returns exit code
}
Expand Down
159 changes: 159 additions & 0 deletions test/Caching_updates_to_feed_metadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php declare(strict_types=1);

use PHPUnit\Framework\TestCase;

class Caching_updates_to_feed_metadataTest extends TestCase
{
public $file = 'out.xml';
public $output = '';
public $returncode = 0;

public $content = '';

public function setUp(): void
{
prepare_testing_dir();

if(is_link('dir2cast.php')) {
// replace symlink with a real file for this test, if it isn't already.
// It will generally be a symlink if we're doing code coverage (i.e. not on Windows)
$source_file = readlink('dir2cast.php');
unlink('dir2cast.php');
copy($source_file, 'dir2cast.php');
}

if(file_exists('dir2cast.ini')) {
// interferes with this test.
unlink('dir2cast.ini');
}

exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime', $this->output, $this->returncode);
$this->content = file_get_contents($this->file);

age_dir_by('.', 86400);
}

protected function _update_feed()
{
$cached_output_files = glob('./temp/*.xml');

clearstatcache();
$old_mtime = filemtime($cached_output_files[0]);

exec('php dir2cast.php --output=out.xml --dont-uncache --ignore-dir2cast-mtime --min-file-age=0 --debug', $debug_out);
// print(implode("\n", $debug_out));

clearstatcache();
$cached_output_files = glob('./temp/*.xml');
$new_mtime = filemtime($cached_output_files[0]);

// cache file should be refreshed
$this->assertGreaterThan($old_mtime, $new_mtime);

$new_content = file_get_contents($this->file);

return $new_content;
}

public function test_update_to_description_txt_invalidates_cache(): void
{
file_put_contents('description.txt', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/Magic Word/', $this->content));
$this->assertEquals(1, preg_match('/Magic Word/', $new_content));
}

public function test_update_to_itunes_summary_invalidates_cache(): void
{
file_put_contents('itunes_summary.txt', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/Magic Word/', $this->content));
$this->assertEquals(1, preg_match('/Magic Word/', $new_content));
}

public function test_update_to_itunes_subtitle_invalidates_cache(): void
{
file_put_contents('itunes_subtitle.txt', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/Magic Word/', $this->content));
$this->assertEquals(1, preg_match('/Magic Word/', $new_content));
}

public function test_update_to_image_jpg_invalidates_cache(): void
{
file_put_contents('image.jpg', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/image\.jpg/', $this->content));
$this->assertEquals(1, preg_match('/image\.jpg/', $new_content));
}

public function test_update_to_image_png_invalidates_cache(): void
{
file_put_contents('image.png', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/image\.png/', $this->content));
$this->assertEquals(1, preg_match('/image\.png/', $new_content));
}

public function test_update_to_itunes_image_jpg_invalidates_cache(): void
{
file_put_contents('itunes_image.jpg', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/itunes_image\.jpg/', $this->content));
$this->assertEquals(1, preg_match('/itunes_image\.jpg/', $new_content));
}

public function test_update_to_itunes_image_png_invalidates_cache(): void
{
file_put_contents('itunes_image.png', 'Magic Word');
$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);
$this->assertEquals(0, preg_match('/itunes_image\.png/', $this->content));
$this->assertEquals(1, preg_match('/itunes_image\.png/', $new_content));
}

public function test_image_ignored_if_ancient(): void
{
file_put_contents('image.jpg', 'Magic Word');
touch('image.jpg', time() - 86400 - 60);

$new_content = $this->_update_feed();
$this->assertEquals($this->content, $new_content);

$this->assertEquals(0, preg_match('/image\.png/', $this->content));
$this->assertEquals(0, preg_match('/image\.jpg/', $this->content));

// an update to png was detected, but jpg still outranks png if both exist
$this->assertEquals(0, preg_match('/image\.png/', $new_content));
$this->assertEquals(0, preg_match('/image\.jpg/', $new_content));
}

public function test_png_ignored_if_jpg_exists(): void
{
file_put_contents('image.png', 'Magic Word');
file_put_contents('image.jpg', 'Magic Word');
touch('image.jpg', time() - 86400 - 60);

$new_content = $this->_update_feed();
$this->assertNotEquals($this->content, $new_content);

$this->assertEquals(0, preg_match('/image\.png/', $this->content));
$this->assertEquals(0, preg_match('/image\.jpg/', $this->content));

// an update to png was detected, but jpg still outranks png if both exist
$this->assertEquals(0, preg_match('/image\.png/', $new_content));
$this->assertEquals(1, preg_match('/image\.jpg/', $new_content));
}

public function tearDown(): void
{
unlink($this->file);
chdir('..');
}
}

0 comments on commit fa9524f

Please sign in to comment.