more factory reworking
This commit is contained in:
parent
57c83c74fb
commit
603f11fc3a
|
@ -5,6 +5,66 @@ require_once(dirname(__file__) . '/../ComicPressBackend.inc');
|
||||||
class ComicPressBackendFilesystem extends ComicPressBackend {
|
class ComicPressBackendFilesystem extends ComicPressBackend {
|
||||||
var $search_string = '';
|
var $search_string = '';
|
||||||
var $id, $files_by_type = array();
|
var $id, $files_by_type = array();
|
||||||
|
}
|
||||||
|
|
||||||
|
class ComicPressBackendFilesystemFactory {
|
||||||
|
function generate_from_id($id) {
|
||||||
|
if (preg_match('#^filesystem-([0-9]+)-(.*)$#', $id, $matches) > 0) {
|
||||||
|
list($all, $post_id, $root) = $matches;
|
||||||
|
|
||||||
|
if (($result = get_post_meta($post_id, 'backend_filesystem_files_by_type', true)) !== false) {
|
||||||
|
if (isset($result[$root])) {
|
||||||
|
$return = new ComicPressBackendFilesystem();
|
||||||
|
$return->id = $id;
|
||||||
|
$return->files_by_type = $result[$root];
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function generate_from_post($post) {
|
||||||
|
$return = array();
|
||||||
|
$comicpress = ComicPress::get_instance();
|
||||||
|
|
||||||
|
if (isset(
|
||||||
|
$comicpress->comicpress_options['backend_options'],
|
||||||
|
$comicpress->comicpress_options['backend_options']['filesystem'],
|
||||||
|
$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern']
|
||||||
|
)) {
|
||||||
|
$this->search_pattern = (string)$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern'];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($comicpress->comicpress_options['image_types'])) {
|
||||||
|
$files = array();
|
||||||
|
$all_patterns = array();
|
||||||
|
foreach (array_keys($comicpress->comicpress_options['image_types']) as $type) {
|
||||||
|
$patterns = $this->process_search_string($post, $type);
|
||||||
|
if (!empty($patterns)) {
|
||||||
|
$result = $this->find_matching_files($patterns);
|
||||||
|
if (!empty($result)) {
|
||||||
|
$files[$type] = $result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$all_patterns = array_merge($all_patterns, $patterns);
|
||||||
|
}
|
||||||
|
if (($filename_pattern = $this->has_common_filename_pattern($all_patterns)) !== false) {
|
||||||
|
if (!empty($files)) {
|
||||||
|
$grouped_by_root = $this->group_by_root($filename_pattern, $files);
|
||||||
|
update_post_meta($post->ID, 'backend_filesystem_files_by_type', $grouped_by_root);
|
||||||
|
foreach ($grouped_by_root as $root => $files_for_root) {
|
||||||
|
$fs = new ComicPressBackendFilesystem();
|
||||||
|
$fs->id = 'filesystem-' . $post->ID . '-' . $root;
|
||||||
|
$fs->files_by_type = $files_for_root;
|
||||||
|
$return[] = $fs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
// TODO Make this more generic (ex: date-Ymd calls _replace_date($post, $type, "Ymd"))
|
// TODO Make this more generic (ex: date-Ymd calls _replace_date($post, $type, "Ymd"))
|
||||||
function process_search_string($post, $type) {
|
function process_search_string($post, $type) {
|
||||||
|
@ -84,22 +144,6 @@ class ComicPressBackendFilesystem extends ComicPressBackend {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function get_regex_dirname($input) {
|
|
||||||
return dirname(ComicPressBackendFilesystem::resolve_regex_path($input));
|
|
||||||
}
|
|
||||||
|
|
||||||
function get_regex_filename($input) {
|
|
||||||
$input = preg_replace('#\\\(?![.])#', '/', $input);
|
|
||||||
$input = preg_replace('#^.*\/([^\/]+)$#', '$1', $input);
|
|
||||||
$input = preg_replace('#(?<![.])\*#', '.*', $input);
|
|
||||||
return $input;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolve_regex_path($input) {
|
|
||||||
$input = str_replace('\.', '.', $input);
|
|
||||||
$input = str_replace('\\', '/', $input);
|
|
||||||
return $input;
|
|
||||||
}
|
|
||||||
|
|
||||||
function find_matching_files($patterns) {
|
function find_matching_files($patterns) {
|
||||||
$matches = array();
|
$matches = array();
|
||||||
|
@ -123,46 +167,21 @@ class ComicPressBackendFilesystem extends ComicPressBackend {
|
||||||
return $matches;
|
return $matches;
|
||||||
}
|
}
|
||||||
|
|
||||||
function generate_from_post($post) {
|
function get_regex_dirname($input) {
|
||||||
$return = array();
|
return dirname($this->resolve_regex_path($input));
|
||||||
$comicpress = ComicPress::get_instance();
|
}
|
||||||
|
|
||||||
if (isset(
|
function get_regex_filename($input) {
|
||||||
$comicpress->comicpress_options['backend_options'],
|
$input = preg_replace('#\\\(?![.])#', '/', $input);
|
||||||
$comicpress->comicpress_options['backend_options']['filesystem'],
|
$input = preg_replace('#^.*\/([^\/]+)$#', '$1', $input);
|
||||||
$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern']
|
$input = preg_replace('#(?<![.])\*#', '.*', $input);
|
||||||
)) {
|
return $input;
|
||||||
$this->search_pattern = (string)$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern'];
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($comicpress->comicpress_options['image_types'])) {
|
function resolve_regex_path($input) {
|
||||||
$files = array();
|
$input = str_replace('\.', '.', $input);
|
||||||
$all_patterns = array();
|
$input = str_replace('\\', '/', $input);
|
||||||
foreach (array_keys($comicpress->comicpress_options['image_types']) as $type) {
|
return $input;
|
||||||
$patterns = $this->process_search_string($post, $type);
|
|
||||||
if (!empty($patterns)) {
|
|
||||||
$result = $this->find_matching_files($patterns);
|
|
||||||
if (!empty($result)) {
|
|
||||||
$files[$type] = $result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$all_patterns = array_merge($all_patterns, $patterns);
|
|
||||||
}
|
|
||||||
if (($filename_pattern = $this->has_common_filename_pattern($all_patterns)) !== false) {
|
|
||||||
if (!empty($files)) {
|
|
||||||
$grouped_by_root = $this->group_by_root($filename_pattern, $files);
|
|
||||||
update_post_meta($post->ID, 'backend_filesystem_files_by_type', $grouped_by_root);
|
|
||||||
foreach ($grouped_by_root as $root => $files_for_root) {
|
|
||||||
$fs = new ComicPressBackendFilesystem();
|
|
||||||
$fs->id = 'filesystem-' . $post->ID . '-' . $root;
|
|
||||||
$fs->files_by_type = $files_for_root;
|
|
||||||
$return[] = $fs;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function has_common_filename_pattern($patterns) {
|
function has_common_filename_pattern($patterns) {
|
||||||
|
@ -188,20 +207,4 @@ class ComicPressBackendFilesystem extends ComicPressBackend {
|
||||||
|
|
||||||
return $roots;
|
return $roots;
|
||||||
}
|
}
|
||||||
|
|
||||||
function generate_from_id($id) {
|
|
||||||
if (preg_match('#^filesystem-([0-9]+)-(.*)$#', $id, $matches) > 0) {
|
|
||||||
list($all, $post_id, $root) = $matches;
|
|
||||||
|
|
||||||
if (($result = get_post_meta($post_id, 'backend_filesystem_files_by_type', true)) !== false) {
|
|
||||||
if (isset($result[$root])) {
|
|
||||||
$return = new ComicPressBackendFilesystem();
|
|
||||||
$return->id = $id;
|
|
||||||
$return->files_by_type = $result[$root];
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('PHPUnit/Framework.php');
|
||||||
|
require_once('MockPress/mockpress.php');
|
||||||
|
require_once('backends/ComicPressBackendAttachment.inc');
|
||||||
|
|
||||||
|
class ComicPressBackendAttachmentFactoryTest extends PHPUnit_Framework_TestCase {
|
||||||
|
function setUp() {
|
||||||
|
_reset_wp();
|
||||||
|
|
||||||
|
$this->fa = new ComicPressBackendAttachmentFactory();
|
||||||
|
}
|
||||||
|
|
||||||
|
function providerTestGenerateFromPost() {
|
||||||
|
return array(
|
||||||
|
array(array(), array(), false),
|
||||||
|
array(array((object)array('ID' => 2)), array(), array()),
|
||||||
|
array(array((object)array('ID' => 2)), array('managed' => false), array()),
|
||||||
|
array(array((object)array('ID' => 2)), array('managed' => true), array('attachment-2')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerTestGenerateFromPost
|
||||||
|
*/
|
||||||
|
function testGenerateFromPost($get_children_response, $post_meta, $expected_ids) {
|
||||||
|
_set_get_children(array(
|
||||||
|
'post_parent' => 1,
|
||||||
|
'post_type' => 'attachment',
|
||||||
|
'post_mime_type' => 'image'
|
||||||
|
), $get_children_response);
|
||||||
|
|
||||||
|
update_post_meta(2, 'comicpress', $post_meta);
|
||||||
|
|
||||||
|
$results = $this->fa->generate_from_post((object)array('ID' => 1));
|
||||||
|
if ($expected_ids === false) {
|
||||||
|
$this->assertTrue(empty($results));
|
||||||
|
} else {
|
||||||
|
$this->assertEquals(count($expected_ids), count($results));
|
||||||
|
foreach ($results as $result) {
|
||||||
|
$this->assertTrue(in_array($result->id, $expected_ids));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function providerTestGenerateFromID() {
|
||||||
|
return array(
|
||||||
|
array(null, false, false),
|
||||||
|
array(1, false, false),
|
||||||
|
array('attachment-1', true, true),
|
||||||
|
array('attachment-1', false, false),
|
||||||
|
array('attachment-2', false, false),
|
||||||
|
array('attachment-3', false, false),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerTestGenerateFromID
|
||||||
|
*/
|
||||||
|
function testGenerateFromID($id, $is_managed, $is_successful) {
|
||||||
|
wp_insert_post(array('ID' => 1));
|
||||||
|
wp_insert_post(array('ID' => 3));
|
||||||
|
|
||||||
|
update_post_meta(1, 'comicpress', array('managed' => $is_managed));
|
||||||
|
|
||||||
|
if ($is_successful) {
|
||||||
|
$return = new ComicPressBackendAttachment((object)array('ID' => 1));
|
||||||
|
} else {
|
||||||
|
$return = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertEquals($return, $this->fa->generate_from_id($id));
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,258 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('PHPUnit/Framework.php');
|
||||||
|
require_once('MockPress/mockpress.php');
|
||||||
|
require_once('backends/ComicPressBackendFilesystem.inc');
|
||||||
|
require_once('ComicPress.inc');
|
||||||
|
require_once('vfsStream/vfsStream.php');
|
||||||
|
|
||||||
|
class ComicPressBackendFilesystemFactoryTest extends PHPUnit_Framework_TestCase {
|
||||||
|
function setUp() {
|
||||||
|
_reset_wp();
|
||||||
|
$this->fa = new ComicPressBackendFilesystemFactory();
|
||||||
|
|
||||||
|
vfsStreamWrapper::register();
|
||||||
|
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
|
||||||
|
}
|
||||||
|
|
||||||
|
function providerTestGenerateFromID() {
|
||||||
|
$valid_backend = new ComicPressBackendFilesystem();
|
||||||
|
$valid_backend->id = 'filesystem-1--test';
|
||||||
|
$valid_backend->files_by_type = array('comic' => 'comic-file');
|
||||||
|
|
||||||
|
return array(
|
||||||
|
array('blah', false),
|
||||||
|
array('filesystem-1', false),
|
||||||
|
array('filesystem-1--test', $valid_backend),
|
||||||
|
array('filesystem-1--test2', false),
|
||||||
|
array('filesystem-2--test', false),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerTestGenerateFromID
|
||||||
|
*/
|
||||||
|
function testGenerateFromID($id, $is_successful) {
|
||||||
|
wp_insert_post((object)array('ID' => 1));
|
||||||
|
|
||||||
|
update_post_meta(1, 'backend_filesystem_files_by_type', array('-test' => array('comic' => 'comic-file')));
|
||||||
|
|
||||||
|
if ($is_successful) {
|
||||||
|
$return = $is_successful;
|
||||||
|
} else {
|
||||||
|
$return = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertEquals($return, $this->fa->generate_from_id($id));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function testGenerateFromPost() {
|
||||||
|
$post = (object)array('ID' => 1);
|
||||||
|
|
||||||
|
$comicpress = ComicPress::get_instance();
|
||||||
|
$comicpress->comicpress_options['image_types'] = array(
|
||||||
|
'comic' => array(),
|
||||||
|
'rss' => array()
|
||||||
|
);
|
||||||
|
|
||||||
|
$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern'] = 'test';
|
||||||
|
|
||||||
|
$fs = $this->getMock('ComicPressBackendFilesystemFactory', array('process_search_string', 'find_matching_files', 'group_by_root', 'has_common_filename_pattern'));
|
||||||
|
|
||||||
|
$fs->expects($this->at(0))->method('process_search_string')->with($post, 'comic')->will($this->returnValue(array('comic')));
|
||||||
|
$fs->expects($this->at(1))->method('find_matching_files')->with(array('comic'))->will($this->returnValue(array('comic')));
|
||||||
|
$fs->expects($this->at(2))->method('process_search_string')->with($post, 'rss')->will($this->returnValue(array('rss')));
|
||||||
|
$fs->expects($this->at(3))->method('find_matching_files')->with(array('rss'))->will($this->returnValue(array('rss')));
|
||||||
|
$fs->expects($this->at(4))->method('has_common_filename_pattern')->with(array('comic', 'rss'))->will($this->returnValue('test'));
|
||||||
|
$fs->expects($this->at(5))->method('group_by_root')->with('test', array(
|
||||||
|
'comic' => array('comic'),
|
||||||
|
'rss' => array('rss')
|
||||||
|
))->will($this->returnValue(array(
|
||||||
|
'root' => array(
|
||||||
|
'comic' => 'comic',
|
||||||
|
'rss' => 'rss',
|
||||||
|
)
|
||||||
|
)));
|
||||||
|
|
||||||
|
$return = $fs->generate_from_post($post);
|
||||||
|
|
||||||
|
$this->assertEquals(1, count($return));
|
||||||
|
$this->assertEquals('filesystem-1-root', $return[0]->id);
|
||||||
|
$this->assertEquals(array(
|
||||||
|
'comic' => 'comic',
|
||||||
|
'rss' => 'rss'
|
||||||
|
), $return[0]->files_by_type);
|
||||||
|
|
||||||
|
$this->assertEquals(array(
|
||||||
|
'root' => array(
|
||||||
|
'comic' => 'comic',
|
||||||
|
'rss' => 'rss',
|
||||||
|
)
|
||||||
|
), get_post_meta(1, 'backend_filesystem_files_by_type', true));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function providerTestProcessSearchString() {
|
||||||
|
return array(
|
||||||
|
array('/comic/*.jpg', array('/comic/*.jpg')),
|
||||||
|
array('%wordpress%/comic/*.jpg', array('/wordpress/comic/*.jpg')),
|
||||||
|
array('%test%/comic/*.jpg', array('/comic/*.jpg')),
|
||||||
|
array('%wordpress%/%type%/*.jpg', array('/wordpress/comic/*.jpg')),
|
||||||
|
array('%wordpress%/comic/%y-m-d%*.jpg', array('/wordpress/comic/2009-01-01*.jpg')),
|
||||||
|
array('%wordpress%/comic/%ymd%*.jpg', array('/wordpress/comic/20090101*.jpg')),
|
||||||
|
array('%wordpress%/comic/%year%/%y-m-d%*.jpg', array('/wordpress/comic/2009/2009-01-01*.jpg')),
|
||||||
|
array(
|
||||||
|
'%wordpress%/comic/%categories%/%y-m-d%*.jpg',
|
||||||
|
array(
|
||||||
|
'/wordpress/comic/parent/child/2009-01-01*.jpg',
|
||||||
|
'/wordpress/comic/parent/2009-01-01*.jpg',
|
||||||
|
)
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'%wordpress%/comic/%categories%/%y-m-d%*.jpg',
|
||||||
|
array(
|
||||||
|
'/wordpress/comic//2009-01-01*.jpg',
|
||||||
|
),
|
||||||
|
2
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerTestProcessSearchString
|
||||||
|
*/
|
||||||
|
function testProcessSearchString($string, $expected_searches, $post_id_to_use = 1) {
|
||||||
|
$fs = $this->getMock('ComicPressBackendFilesystemFactory', array('_replace_wordpress'));
|
||||||
|
|
||||||
|
$fs->expects($this->any())->method('_replace_wordpress')->will($this->returnValue('/wordpress'));
|
||||||
|
|
||||||
|
$posts = array(
|
||||||
|
1 => (object)array('ID' => 1, 'post_date' => '2009-01-01'),
|
||||||
|
2 => (object)array('ID' => 2, 'post_date' => '2009-01-01'),
|
||||||
|
);
|
||||||
|
|
||||||
|
add_category(1, (object)array('slug' => 'parent', 'parent' => 0));
|
||||||
|
add_category(2, (object)array('slug' => 'child', 'parent' => 1));
|
||||||
|
add_category(4, (object)array('slug' => 'bad', 'parent' => 3));
|
||||||
|
|
||||||
|
wp_set_post_categories(1, array(2));
|
||||||
|
wp_set_post_categories(2, array(4));
|
||||||
|
|
||||||
|
$fs->search_string = $string;
|
||||||
|
|
||||||
|
$this->assertEquals($expected_searches, $fs->process_search_string($posts[$post_id_to_use], 'comic'));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function providerTestFindMatchingFiles() {
|
||||||
|
return array(
|
||||||
|
array(array('/blah'), array()),
|
||||||
|
array(array('/comic/2008-01-01.jpg'), array()),
|
||||||
|
array(array('/comic/2009-01-01.jpg'), array(vfsStream::url('root/comic/2009-01-01.jpg'))),
|
||||||
|
array(array('/comic/2009-01-01-test.jpg'), array(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->fa->find_matching_files(array(vfsStream::url('root/comic/2009-01-01*.jpg'))));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function providerTestHasCommonFilenamePattern() {
|
||||||
|
return array(
|
||||||
|
array(array('/test/*.jpg', '/test2/*.jpg'), '*.jpg'),
|
||||||
|
array(array('/test/*.jpg', '/test2/*.gif'), false)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerTestHasCommonFilenamePattern
|
||||||
|
*/
|
||||||
|
function testHasCommonFilenamePattern($patterns, $expected_result) {
|
||||||
|
$this->assertTrue($expected_result === $this->fa->has_common_filename_pattern($patterns));
|
||||||
|
}
|
||||||
|
|
||||||
|
function providerTestGroupByRoot() {
|
||||||
|
return array(
|
||||||
|
array(
|
||||||
|
'test*.jpg',
|
||||||
|
array('comic' => array('/test/test1.jpg', '/test/test2.jpg')),
|
||||||
|
array('1' => array('comic' => '/test/test1.jpg'), '2' => array('comic' => '/test/test2.jpg'))
|
||||||
|
),
|
||||||
|
array(
|
||||||
|
'2009-01-01*.jpg',
|
||||||
|
array(
|
||||||
|
'comic' => array('/comic/2009-01-01-01-yeah.jpg'),
|
||||||
|
'rss' => array('/rss/2009-01-01-01-yeah.jpg')
|
||||||
|
),
|
||||||
|
array('-01-yeah' => array('comic' => '/comic/2009-01-01-01-yeah.jpg', 'rss' => '/rss/2009-01-01-01-yeah.jpg'))
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerTestGroupByRoot
|
||||||
|
*/
|
||||||
|
function testGroupByRoot($pattern, $files, $expected_groupings) {
|
||||||
|
$this->assertEquals($expected_groupings, $this->fa->group_by_root($pattern, $files));
|
||||||
|
}
|
||||||
|
|
||||||
|
function providerTestResolveRegexPath() {
|
||||||
|
return array(
|
||||||
|
array('test', 'test'),
|
||||||
|
array('te\.st', 'te.st'),
|
||||||
|
array('te\st', 'te/st'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerTestResolveRegexPath
|
||||||
|
*/
|
||||||
|
function testResolveRegexPath($input, $expected_output) {
|
||||||
|
$this->assertEquals($expected_output, $this->fa->resolve_regex_path($input));
|
||||||
|
}
|
||||||
|
|
||||||
|
function providerTestGetRegexDirname() {
|
||||||
|
return array(
|
||||||
|
array('/test/test2', '/test')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerTestGetRegexDirname
|
||||||
|
*/
|
||||||
|
function testGetRegexDirname($input, $expected_output) {
|
||||||
|
$this->assertEquals($expected_output, $this->fa->get_regex_dirname($input));
|
||||||
|
}
|
||||||
|
|
||||||
|
function providerTestGetRegexFilename() {
|
||||||
|
return array(
|
||||||
|
array('/test/test2', 'test2'),
|
||||||
|
array('c:\test\test2', 'test2'),
|
||||||
|
array('/test/test2\.cat', 'test2\.cat'),
|
||||||
|
array('c:\test\test2\.cat', 'test2\.cat'),
|
||||||
|
array('C:/inetpub/a\.windows\.directory/comics/2009-11-24.*\..*', '2009-11-24.*\..*'),
|
||||||
|
array('c:\test\test2\.cat*', 'test2\.cat.*'),
|
||||||
|
array('c:\test\test2\.cat.*', 'test2\.cat.*'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerTestGetRegexFilename
|
||||||
|
*/
|
||||||
|
function testGetRegexFilename($input, $expected_output) {
|
||||||
|
$this->assertEquals($expected_output, $this->fa->get_regex_filename($input));
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,240 +15,5 @@ class ComicPressBackendFilesystemTest extends PHPUnit_Framework_TestCase {
|
||||||
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
|
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
|
||||||
}
|
}
|
||||||
|
|
||||||
function providerTestProcessSearchString() {
|
function testSomething() {}
|
||||||
return array(
|
|
||||||
array('/comic/*.jpg', array('/comic/*.jpg')),
|
|
||||||
array('%wordpress%/comic/*.jpg', array('/wordpress/comic/*.jpg')),
|
|
||||||
array('%test%/comic/*.jpg', array('/comic/*.jpg')),
|
|
||||||
array('%wordpress%/%type%/*.jpg', array('/wordpress/comic/*.jpg')),
|
|
||||||
array('%wordpress%/comic/%y-m-d%*.jpg', array('/wordpress/comic/2009-01-01*.jpg')),
|
|
||||||
array('%wordpress%/comic/%ymd%*.jpg', array('/wordpress/comic/20090101*.jpg')),
|
|
||||||
array('%wordpress%/comic/%year%/%y-m-d%*.jpg', array('/wordpress/comic/2009/2009-01-01*.jpg')),
|
|
||||||
array(
|
|
||||||
'%wordpress%/comic/%categories%/%y-m-d%*.jpg',
|
|
||||||
array(
|
|
||||||
'/wordpress/comic/parent/child/2009-01-01*.jpg',
|
|
||||||
'/wordpress/comic/parent/2009-01-01*.jpg',
|
|
||||||
)
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'%wordpress%/comic/%categories%/%y-m-d%*.jpg',
|
|
||||||
array(
|
|
||||||
'/wordpress/comic//2009-01-01*.jpg',
|
|
||||||
),
|
|
||||||
2
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerTestProcessSearchString
|
|
||||||
*/
|
|
||||||
function testProcessSearchString($string, $expected_searches, $post_id_to_use = 1) {
|
|
||||||
$fs = $this->getMock('ComicPressBackendFilesystem', array('_replace_wordpress'));
|
|
||||||
|
|
||||||
$fs->expects($this->any())->method('_replace_wordpress')->will($this->returnValue('/wordpress'));
|
|
||||||
|
|
||||||
$posts = array(
|
|
||||||
1 => (object)array('ID' => 1, 'post_date' => '2009-01-01'),
|
|
||||||
2 => (object)array('ID' => 2, 'post_date' => '2009-01-01'),
|
|
||||||
);
|
|
||||||
|
|
||||||
add_category(1, (object)array('slug' => 'parent', 'parent' => 0));
|
|
||||||
add_category(2, (object)array('slug' => 'child', 'parent' => 1));
|
|
||||||
add_category(4, (object)array('slug' => 'bad', 'parent' => 3));
|
|
||||||
|
|
||||||
wp_set_post_categories(1, array(2));
|
|
||||||
wp_set_post_categories(2, array(4));
|
|
||||||
|
|
||||||
$fs->search_string = $string;
|
|
||||||
|
|
||||||
$this->assertEquals($expected_searches, $fs->process_search_string($posts[$post_id_to_use], 'comic'));
|
|
||||||
}
|
|
||||||
|
|
||||||
function providerTestHasCommonFilenamePattern() {
|
|
||||||
return array(
|
|
||||||
array(array('/test/*.jpg', '/test2/*.jpg'), '*.jpg'),
|
|
||||||
array(array('/test/*.jpg', '/test2/*.gif'), false)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerTestHasCommonFilenamePattern
|
|
||||||
*/
|
|
||||||
function testHasCommonFilenamePattern($patterns, $expected_result) {
|
|
||||||
$this->assertTrue($expected_result === $this->fs->has_common_filename_pattern($patterns));
|
|
||||||
}
|
|
||||||
|
|
||||||
function providerTestFindMatchingFiles() {
|
|
||||||
return array(
|
|
||||||
array(array('/blah'), array()),
|
|
||||||
array(array('/comic/2008-01-01.jpg'), array()),
|
|
||||||
array(array('/comic/2009-01-01.jpg'), array(vfsStream::url('root/comic/2009-01-01.jpg'))),
|
|
||||||
array(array('/comic/2009-01-01-test.jpg'), array(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'))));
|
|
||||||
}
|
|
||||||
|
|
||||||
function testGenerateFromPost() {
|
|
||||||
$post = (object)array('ID' => 1);
|
|
||||||
|
|
||||||
$comicpress = ComicPress::get_instance();
|
|
||||||
$comicpress->comicpress_options['image_types'] = array(
|
|
||||||
'comic' => array(),
|
|
||||||
'rss' => array()
|
|
||||||
);
|
|
||||||
|
|
||||||
$comicpress->comicpress_options['backend_options']['filesystem']['search_pattern'] = 'test';
|
|
||||||
|
|
||||||
$fs = $this->getMock('ComicPressBackendFilesystem', array('process_search_string', 'find_matching_files', 'group_by_root', 'has_common_filename_pattern'));
|
|
||||||
|
|
||||||
$fs->expects($this->at(0))->method('process_search_string')->with($post, 'comic')->will($this->returnValue(array('comic')));
|
|
||||||
$fs->expects($this->at(1))->method('find_matching_files')->with(array('comic'))->will($this->returnValue(array('comic')));
|
|
||||||
$fs->expects($this->at(2))->method('process_search_string')->with($post, 'rss')->will($this->returnValue(array('rss')));
|
|
||||||
$fs->expects($this->at(3))->method('find_matching_files')->with(array('rss'))->will($this->returnValue(array('rss')));
|
|
||||||
$fs->expects($this->at(4))->method('has_common_filename_pattern')->with(array('comic', 'rss'))->will($this->returnValue('test'));
|
|
||||||
$fs->expects($this->at(5))->method('group_by_root')->with('test', array(
|
|
||||||
'comic' => array('comic'),
|
|
||||||
'rss' => array('rss')
|
|
||||||
))->will($this->returnValue(array(
|
|
||||||
'root' => array(
|
|
||||||
'comic' => 'comic',
|
|
||||||
'rss' => 'rss',
|
|
||||||
)
|
|
||||||
)));
|
|
||||||
|
|
||||||
$return = $fs->generate_from_post($post);
|
|
||||||
|
|
||||||
$this->assertEquals(1, count($return));
|
|
||||||
$this->assertEquals('filesystem-1-root', $return[0]->id);
|
|
||||||
$this->assertEquals(array(
|
|
||||||
'comic' => 'comic',
|
|
||||||
'rss' => 'rss'
|
|
||||||
), $return[0]->files_by_type);
|
|
||||||
|
|
||||||
$this->assertEquals(array(
|
|
||||||
'root' => array(
|
|
||||||
'comic' => 'comic',
|
|
||||||
'rss' => 'rss',
|
|
||||||
)
|
|
||||||
), get_post_meta(1, 'backend_filesystem_files_by_type', true));
|
|
||||||
}
|
|
||||||
|
|
||||||
function providerTestGroupByRoot() {
|
|
||||||
return array(
|
|
||||||
array(
|
|
||||||
'test*.jpg',
|
|
||||||
array('comic' => array('/test/test1.jpg', '/test/test2.jpg')),
|
|
||||||
array('1' => array('comic' => '/test/test1.jpg'), '2' => array('comic' => '/test/test2.jpg'))
|
|
||||||
),
|
|
||||||
array(
|
|
||||||
'2009-01-01*.jpg',
|
|
||||||
array(
|
|
||||||
'comic' => array('/comic/2009-01-01-01-yeah.jpg'),
|
|
||||||
'rss' => array('/rss/2009-01-01-01-yeah.jpg')
|
|
||||||
),
|
|
||||||
array('-01-yeah' => array('comic' => '/comic/2009-01-01-01-yeah.jpg', 'rss' => '/rss/2009-01-01-01-yeah.jpg'))
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerTestGroupByRoot
|
|
||||||
*/
|
|
||||||
function testGroupByRoot($pattern, $files, $expected_groupings) {
|
|
||||||
$this->assertEquals($expected_groupings, $this->fs->group_by_root($pattern, $files));
|
|
||||||
}
|
|
||||||
|
|
||||||
function providerTestGenerateFromID() {
|
|
||||||
$valid_backend = new ComicPressBackendFilesystem();
|
|
||||||
$valid_backend->id = 'filesystem-1--test';
|
|
||||||
$valid_backend->files_by_type = array('comic' => 'comic-file');
|
|
||||||
|
|
||||||
return array(
|
|
||||||
array('blah', false),
|
|
||||||
array('filesystem-1', false),
|
|
||||||
array('filesystem-1--test', $valid_backend),
|
|
||||||
array('filesystem-1--test2', false),
|
|
||||||
array('filesystem-2--test', false),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerTestGenerateFromID
|
|
||||||
*/
|
|
||||||
function testGenerateFromID($id, $is_successful) {
|
|
||||||
wp_insert_post((object)array('ID' => 1));
|
|
||||||
|
|
||||||
update_post_meta(1, 'backend_filesystem_files_by_type', array('-test' => array('comic' => 'comic-file')));
|
|
||||||
|
|
||||||
if ($is_successful) {
|
|
||||||
$return = $is_successful;
|
|
||||||
} else {
|
|
||||||
$return = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->assertEquals($return, ComicPressBackendFilesystem::generate_from_id($id));
|
|
||||||
}
|
|
||||||
|
|
||||||
function providerTestResolveRegexPath() {
|
|
||||||
return array(
|
|
||||||
array('test', 'test'),
|
|
||||||
array('te\.st', 'te.st'),
|
|
||||||
array('te\st', 'te/st'),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerTestResolveRegexPath
|
|
||||||
*/
|
|
||||||
function testResolveRegexPath($input, $expected_output) {
|
|
||||||
$this->assertEquals($expected_output, ComicPressBackendFilesystem::resolve_regex_path($input));
|
|
||||||
}
|
|
||||||
|
|
||||||
function providerTestGetRegexDirname() {
|
|
||||||
return array(
|
|
||||||
array('/test/test2', '/test')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerTestGetRegexDirname
|
|
||||||
*/
|
|
||||||
function testGetRegexDirname($input, $expected_output) {
|
|
||||||
$this->assertEquals($expected_output, ComicPressBackendFilesystem::get_regex_dirname($input));
|
|
||||||
}
|
|
||||||
|
|
||||||
function providerTestGetRegexFilename() {
|
|
||||||
return array(
|
|
||||||
array('/test/test2', 'test2'),
|
|
||||||
array('c:\test\test2', 'test2'),
|
|
||||||
array('/test/test2\.cat', 'test2\.cat'),
|
|
||||||
array('c:\test\test2\.cat', 'test2\.cat'),
|
|
||||||
array('C:/inetpub/a\.windows\.directory/comics/2009-11-24.*\..*', '2009-11-24.*\..*'),
|
|
||||||
array('c:\test\test2\.cat*', 'test2\.cat.*'),
|
|
||||||
array('c:\test\test2\.cat.*', 'test2\.cat.*'),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dataProvider providerTestGetRegexFilename
|
|
||||||
*/
|
|
||||||
function testGetRegexFilename($input, $expected_output) {
|
|
||||||
$this->assertEquals($expected_output, ComicPressBackendFilesystem::get_regex_filename($input));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue