diff --git a/classes/PostFixtures.inc b/classes/PostFixtures.inc index 7bc8452..dbd9f48 100644 --- a/classes/PostFixtures.inc +++ b/classes/PostFixtures.inc @@ -192,4 +192,31 @@ class PostFixtures { $this->remove_all_posts(); $this->remove_all_categories(); } + + function process_blog_options($options, $categories) { + $this->_category = $categories; + foreach ($options as $option => $value) { + $value = preg_replace_callback('#\$\{([^\}]+)\}#', array(&$this, '_process_blog_options_callback'), $value); + update_option($option, $value); + } + unset($this->_category); + } + + function _process_blog_options_callback($matches) { + $value = $matches[0]; + $parts = explode(':', $matches[1]); + if (count($parts) > 1) { + $source = array_shift($parts); + if (count($parts) == 1) { + $index = reset($parts); + if (isset($this->{"_${source}"})) { + if (isset($this->{"_${source}"}[$index])) { + $value = $this->{"_${source}"}[$index]; + } + } + } + } + + return $value; + } } diff --git a/test/PostFixturesTest.php b/test/PostFixturesTest.php index a1662f1..fbf78a9 100644 --- a/test/PostFixturesTest.php +++ b/test/PostFixturesTest.php @@ -74,12 +74,14 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase { return array( array( array( - array( - 'title' => 'test', - 'categories' => array('test1', 'test2'), - 'date' => '2009-01-01', - 'metadata' => array( - 'test' => 'test2' + 'posts' => array( + array( + 'title' => 'test', + 'categories' => array('test1', 'test2'), + 'date' => '2009-01-01', + 'metadata' => array( + 'test' => 'test2' + ) ) ) ), @@ -218,4 +220,38 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase { $pf->remove(); } + + function providerTestProcessBlogOptions() { + return array( + array( + array(), + array('test' => 'test') + ), + array( + array('test2' => 'test2'), + array('test' => 'test', 'test2' => 'test2') + ), + array( + array('test' => false), + array('test' => false) + ), + array( + array('test' => '${category:category}'), + array('test' => '1') + ), + ); + } + + /** + * @dataProvider providerTestProcessBlogOptions + */ + function testProcessBlogOptions($data, $expected_fields) { + update_option('test', 'test'); + + $this->pf->process_blog_options($data, array('category' => 1)); + + foreach ($expected_fields as $name => $value) { + $this->assertEquals($value, get_option($name)); + } + } }