comicpress-2.8/test/ComicPressMediaHandlingTest...

179 lines
5.7 KiB
PHP
Raw Normal View History

2009-12-02 02:07:10 +00:00
<?php
require_once('PHPUnit/Framework.php');
require_once('MockPress/mockpress.php');
require_once('vfsStream/vfsStream.php');
require_once(dirname(__FILE__) . '/../classes/ComicPressMediaHandling.inc');
2009-12-02 03:13:21 +00:00
require_once(dirname(__FILE__) . '/../functions/wpmu.php');
2009-12-02 02:07:10 +00:00
class ComicPressMediaHandlingTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
$this->cpmh = new ComicPressMediaHandling();
2009-12-02 02:37:28 +00:00
$this->default_filter = $this->cpmh->default_filter;
vfsStreamWrapper::register();
vfsStreamWrapper::setRoot(new vfsStreamDirectory('root'));
2009-12-02 02:07:10 +00:00
}
function testBundleGlobalVariables() {
global $comic_folder, $archive_comic_folder, $rss_comic_folder, $mini_comic_folder;
$comic_folder = 'comic';
$archive_comic_folder = 'archive';
$rss_comic_folder = 'rss';
$mini_comic_folder = 'mini';
$this->assertEquals(array(
'comic' => 'comic',
'archive' => 'archive',
'rss' => 'rss',
'mini' => 'mini'
), $this->cpmh->_bundle_global_variables());
}
2009-12-02 02:14:54 +00:00
function providerTestGetFilter() {
2009-12-02 02:37:28 +00:00
$cpmh = new ComicPressMediaHandling();
2009-12-02 02:14:54 +00:00
return array(
2009-12-02 02:37:28 +00:00
array(null, $cpmh->default_filter),
array('fail', $cpmh->default_filter),
array(array(), $cpmh->default_filter),
2009-12-02 02:14:54 +00:00
array('test', 'test')
);
}
/**
* @dataProvider providerTestGetFilter
*/
function testGetFilter($filter_to_use, $expected_result) {
global $comic_filename_filters;
$comic_filename_filters['test'] = 'test';
2009-12-02 02:37:28 +00:00
$cpmh = $this->getMock('ComicPressMediaHandling', array('_convert_to_percent_filter'));
if ($expected_result !== $cpmh->default_filter) {
$cpmh->expects($this->once())->method('_convert_to_percent_filter')->with($expected_result)->will($this->returnValue($expected_result));
} else {
$cpmh->expects($this->never())->method('_convert_to_percent_filter');
}
$this->assertEquals($expected_result, $cpmh->_get_filter($filter_to_use));
}
function providerTestConvertToPercentFilter() {
return array(
array('', '%wordpress%/%type-folder%/'),
array('{date}', '%wordpress%/%type-folder%/%date-Y-m-d%'),
array('%wordpress%/%type-folder%/{date}', '%wordpress%/%type-folder%/{date}'),
);
}
/**
* @dataProvider providerTestConvertToPercentFilter
*/
function testConvertToPercentFilter($old_filter, $new_filter) {
$this->assertEquals($new_filter, $this->cpmh->_convert_to_percent_filter($old_filter));
}
function providerTestExpandFilter() {
return array(
array('', ''),
array('%test%', '%test%'),
array('%wordpress%', vfsStream::url('root')),
array('%wordpress%%wordpress%', vfsStream::url('root') . vfsStream::url('root')),
array('%test test%', '%test test%'),
array('%wordpress%/%type-folder%', vfsStream::url('root') . '/comic'),
array('%date-Y%', '2009'),
2009-12-02 03:13:21 +00:00
array('%wordpress%/%type-folder%/%date-Y-m-d%*.*', vfsStream::url('root') . '/comic/2009-01-01.*\..*')
2009-12-02 02:37:28 +00:00
);
}
/**
* @dataProvider providerTestExpandFilter
*/
2009-12-02 03:13:21 +00:00
function testExpandFilter($filter, $expected_result) {
2009-12-02 02:37:28 +00:00
$cpmh = $this->getMock('ComicPressMediaHandling', array('_abspath'));
$cpmh->expects($this->any())->method('_abspath')->will($this->returnValue(vfsStream::url('root')));
$this->assertEquals($expected_result, $cpmh->_expand_filter($filter, 'comic', (object)array('ID' => 1, 'post_date' => '2009-01-01 15:00:00')));
2009-12-02 02:14:54 +00:00
}
2009-12-02 02:53:46 +00:00
2009-12-02 03:13:21 +00:00
function providerTestExpandFilterWPMUCallback() {
return array(
2009-12-02 03:27:54 +00:00
array('', '', 'original'),
array('', 'new', 'original'),
array('1', 'new', 'new'),
2009-12-02 03:13:21 +00:00
);
}
/**
* @dataProvider providerTestExpandFilterWPMUCallback
*/
2009-12-02 03:27:54 +00:00
function testExpandFilterWPMUCallback($_wpmu_version, $option_value, $expected_result) {
global $wpmu_version;
$wpmu_version = $_wpmu_version;
2009-12-02 03:13:21 +00:00
update_option('upload_path', $option_value);
2009-12-02 03:27:54 +00:00
$this->assertEquals($expected_result, _comicpress_expand_filter_callback('original', array('all', 'wordpress')));
2009-12-02 03:13:21 +00:00
}
2009-12-02 02:53:46 +00:00
function providerTestReadDirectory() {
return array(
array(vfsStream::url('root2/.*'), false),
array(vfsStream::url('root/.*'), array('2009-01-01.jpg', '2009-01-02.jpg', '2009-02-02-two.jpg', '2008-01-01.jpg')),
array(vfsStream::url('root/2009.*'), array('2009-01-01.jpg', '2009-01-02.jpg', '2009-02-02-two.jpg')),
array(vfsStream::url('root/2009-01.*'), array('2009-01-01.jpg', '2009-01-02.jpg')),
array(vfsStream::url('root/2009-01-01.*'), array('2009-01-01.jpg')),
);
}
/**
* @dataProvider providerTestReadDirectory
*/
function testReadDirectory($pattern, $expected_results) {
foreach (array('2009-01-01.jpg', '2009-01-02.jpg', '2009-02-02-two.jpg', '2008-01-01.jpg') as $file) {
file_put_contents(vfsStream::url("root/${file}"), 'file');
}
if (is_array($expected_results)) {
foreach ($expected_results as &$result) { $result = vfsStream::url("root/${result}"); }
}
$this->assertEquals($expected_results, $this->cpmh->_read_directory($pattern));
}
2009-12-02 03:13:21 +00:00
function providerTestResolveRegexPath() {
return array(
array('test', 'test'),
array('te\.st', 'te.st')
);
}
/**
* @dataProvider providerTestResolveRegexPath
*/
function testResolveRegexPath($input, $expected_output) {
$this->assertEquals($expected_output, $this->cpmh->_resolve_regex_path($input));
}
2009-12-02 03:13:21 +00:00
function providerTestPreHandleComicPathResults() {
return array(
array('', '', false),
array('1', '', 'comic/one'),
array('1', 'comic', 'files/one'),
);
}
/**
* @dataProvider providerTestPreHandleComicPathResults
*/
function testPreHandleComicPathResults($_wpmu_version, $upload_path, $expected_result) {
global $wpmu_version, $comic_folder;
$wpmu_version = $_wpmu_version;
$comic_folder = 'comic';
update_option('upload_path', $upload_path);
$this->assertEquals($expected_result, _comicpress_pre_handle_comic_path_results(false, array('one/one', 'two/two', 'three/three'), 'comic', (object)array('ID' => 1)));
}
2009-12-02 02:07:10 +00:00
}