blog options with category injection

This commit is contained in:
John Bintz 2009-11-23 08:16:55 -05:00
parent b41a580502
commit 78b5325a0e
2 changed files with 69 additions and 6 deletions

View File

@ -192,4 +192,31 @@ class PostFixtures {
$this->remove_all_posts(); $this->remove_all_posts();
$this->remove_all_categories(); $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;
}
} }

View File

@ -74,12 +74,14 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
return array( return array(
array( array(
array( array(
array( 'posts' => array(
'title' => 'test', array(
'categories' => array('test1', 'test2'), 'title' => 'test',
'date' => '2009-01-01', 'categories' => array('test1', 'test2'),
'metadata' => array( 'date' => '2009-01-01',
'test' => 'test2' 'metadata' => array(
'test' => 'test2'
)
) )
) )
), ),
@ -218,4 +220,38 @@ class PostFixturesTest extends PHPUnit_Framework_TestCase {
$pf->remove(); $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));
}
}
} }