From 1f428c786a8bca4e4d2f11b670871166ca6785b8 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 1 Dec 2009 21:37:28 -0500 Subject: [PATCH] filtering --- classes/ComicPressMediaHandling.inc | 50 ++++++++++++++++++++++- test/ComicPressMediaHandlingTest.php | 59 ++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 6 deletions(-) diff --git a/classes/ComicPressMediaHandling.inc b/classes/ComicPressMediaHandling.inc index 57e95d7..1071585 100644 --- a/classes/ComicPressMediaHandling.inc +++ b/classes/ComicPressMediaHandling.inc @@ -1,6 +1,7 @@ _convert_to_percent_filter($comic_filename_filters[$filter_to_use]); } } } @@ -34,7 +35,52 @@ class ComicPressMediaHandling { return $this->default_filter; } - function get_comic_path($source = 'comic', $override_post = null, $filter = 'default', $multi = false) { + function _convert_to_percent_filter($old) { + if (strpos(strtolower($old), '%wordpress%') !== 0) { + $old = str_replace('{date}', '%date-Y-m-d%', $old); + return $this->root_filter . $old; + } + return $old; + } + function _expand_filter($filter, $type_filter, $override_post = null) { + global $post; + $this->post_to_use = !is_null($override_post) ? $override_post : $post; + $this->type_filter = $type_filter; + + $result = preg_replace_callback('#%([a-z0-9-]+)%#i', array(&$this, '_expand_filter_callback'), $filter); + $result = str_replace('.', '\.', $result); + $result = str_replace('*', '.*', $result); + + unset($this->post_to_use); + unset($this->type_filter); + + return $result; + } + + function _abspath() { return ABSPATH; } + + function _expand_filter_callback($matches) { + $value = ''; + switch (strtolower($matches[1])) { + case 'wordpress': + $value = $this->_abspath(); + break; + case 'type-folder': + $value = $this->type_filter; + break; + default: + if (preg_match('#^date-(.*)$#', $matches[1], $date_matches) > 0) { + $value = date($date_matches[1], strtotime($this->post_to_use->post_date)); + break; + } + $value = $matches[0]; + break; + } + return $value; + } + + function get_comic_path($type = 'comic', $override_post = null, $filter = 'default', $multi = false) { + $filter = $this->get_filter($filter); } } diff --git a/test/ComicPressMediaHandlingTest.php b/test/ComicPressMediaHandlingTest.php index 603f53e..358c1c5 100644 --- a/test/ComicPressMediaHandlingTest.php +++ b/test/ComicPressMediaHandlingTest.php @@ -9,6 +9,10 @@ class ComicPressMediaHandlingTest extends PHPUnit_Framework_TestCase { function setUp() { _reset_wp(); $this->cpmh = new ComicPressMediaHandling(); + $this->default_filter = $this->cpmh->default_filter; + + vfsStreamWrapper::register(); + vfsStreamWrapper::setRoot(new vfsStreamDirectory('root')); } function testBundleGlobalVariables() { @@ -28,10 +32,12 @@ class ComicPressMediaHandlingTest extends PHPUnit_Framework_TestCase { } function providerTestGetFilter() { + $cpmh = new ComicPressMediaHandling(); + return array( - array(null, '%wordpress%/%type-folder%/%date-Y-m-d%*.*'), - array('fail', '%wordpress%/%type-folder%/%date-Y-m-d%*.*'), - array(array(), '%wordpress%/%type-folder%/%date-Y-m-d%*.*'), + array(null, $cpmh->default_filter), + array('fail', $cpmh->default_filter), + array(array(), $cpmh->default_filter), array('test', 'test') ); } @@ -44,6 +50,51 @@ class ComicPressMediaHandlingTest extends PHPUnit_Framework_TestCase { $comic_filename_filters['test'] = 'test'; - $this->assertEquals($expected_result, $this->cpmh->_get_filter($filter_to_use)); + $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'), + array('%wordpress%/%type-folder%/%date-Y-m-d%*.*', vfsStream::url('root') . '/comic/2009-01-01.*\..*'), + ); + } + + /** + * @dataProvider providerTestExpandFilter + */ + function testExpandFilter($filter, $expected_result) { + $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'))); } }