diff --git a/classes/backends/ComicPressBackendFilesystem.inc b/classes/backends/ComicPressBackendFilesystem.inc index f65e07a..7581083 100644 --- a/classes/backends/ComicPressBackendFilesystem.inc +++ b/classes/backends/ComicPressBackendFilesystem.inc @@ -80,4 +80,25 @@ class ComicPressBackendFilesystem extends ComicPressBackend { } return false; } + + function find_matching_files($patterns) { + foreach ($patterns as $pattern) { + $dir = dirname($pattern); + if (is_dir($dir)) { + $pattern = str_replace('*', '.*', basename($pattern)); + if (($dh = opendir($dir)) !== false) { + while (($file = readdir($dh)) !== false) { + $target = $dir . '/' . $file; + if (is_file($target) || is_link($target)) { + if (preg_match('#' . $pattern. '#', $file) > 0) { + return $target; + } + } + } + closedir($dh); + } + } + } + return false; + } } \ No newline at end of file diff --git a/test/backends/ComicPressBackendFilesystemTest.php b/test/backends/ComicPressBackendFilesystemTest.php index 527908e..2e1e917 100644 --- a/test/backends/ComicPressBackendFilesystemTest.php +++ b/test/backends/ComicPressBackendFilesystemTest.php @@ -9,6 +9,9 @@ class ComicPressBackendFilesystemTest extends PHPUnit_Framework_TestCase { function setUp() { _reset_wp(); $this->fs = new ComicPressBackendFilesystem(); + + vfsStreamWrapper::register(); + vfsStreamWrapper::setRoot(new vfsStreamDirectory('root')); } function providerTestProcessSearchString() { @@ -60,4 +63,28 @@ class ComicPressBackendFilesystemTest extends PHPUnit_Framework_TestCase { $this->assertEquals($expected_searches, $fs->process_search_string($posts[$post_id_to_use], 'comic')); } + + function providerTestFindMatchingFiles() { + return array( + array(array('/blah'), false), + array(array('/comic/2008-01-01.jpg'), false), + array(array('/comic/2009-01-01.jpg'), vfsStream::url('root/comic/2009-01-01.jpg')), + array(array('/comic/2009-01-01-test.jpg'), vfsStream::url('root/comic/2009-01-01-test.jpg')), + ); + } + + /** + * @dataProvider providerTestFindMatchingFiles + */ + function testFindMatchingFiles($filesystem_layout, $expected_match) { + foreach ($filesystem_layout as $file) { + $parts = pathinfo($file); + mkdir(vfsStream::url("root{$parts['dirname']}"), 0666, true); + file_put_contents(vfsStream::url("root${file}"), 'test'); + } + + wp_set_post_categories(1, array(2)); + + $this->assertEquals($expected_match, $this->fs->find_matching_files(array(vfsStream::url('root/comic/2009-01-01*.jpg')))); + } } \ No newline at end of file