new storyline functions

This commit is contained in:
John Bintz 2009-11-18 20:03:14 -05:00
parent 8d57acb09d
commit b2410a2678
3 changed files with 66 additions and 2 deletions

View File

@ -142,6 +142,7 @@ class ComicPressStoryline {
function _get_field($field, $id) {
if (isset($this->_structure)) {
$id = $this->_ensure_numeric_category($id);
if (isset($this->_structure[$id])) {
if (isset($this->_structure[$id][$field])) {
return $this->_structure[$id][$field];
@ -157,8 +158,9 @@ class ComicPressStoryline {
function next($id) { return $this->_get_field('next', $id); }
// @codeCoverageIgnoreEnd
function valid($id) {
if (isset($this->_structure[$id])) {
function valid($id) {
$id = $this->_ensure_numeric_category($id);
if (isset($this->_structure[$id])) {
return array_keys($this->_structure[$id]);
}
return false;

View File

@ -172,4 +172,33 @@ function SL() {
$storyline = new ComicPressStoryline();
$storyline->read_from_options();
return $storyline->_structure;
}
function SC($which, $relative_to = null) {
global $post;
$storyline = new ComicPressStoryline();
$storyline->read_from_options();
if (is_null($relative_to)) {
if (is_object($post)) {
if (isset($post->ID)) {
$categories = wp_get_post_categories($post->ID);
if (is_array($categories)) {
$relative_to = reset($categories);
}
}
}
}
if (!is_null($relative_to)) {
if ($result = $storyline->_get_field($which, $relative_to)) {
$category = get_category($result);
if (!empty($category)) {
return $category;
}
}
}
return false;
}

View File

@ -188,4 +188,37 @@ class FunctionsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($s->_structure, SL());
}
function providerTestSC() {
return array(
array('next', 1, 2),
array('next', null, 3),
array('next', 4, false),
array('test', 4, false),
);
}
/**
* @dataProvider providerTestSC
*/
function testSC($which, $relative_to, $expected_result) {
global $post;
$post = (object)array('ID' => 1);
wp_set_post_categories(1, array(2));
$s = new ComicPressStoryline();
$s->set_flattened_storyline('0/1,0/2,0/2/3,0/2/4');
for ($i = 1; $i <= 4; ++$i) {
add_category($i, (object)array('slug' => 'test-' . $i));
}
$result = SC($which, $relative_to);
if ($expected_result === false) {
$this->assertTrue(false === $result);
} else {
$this->assertEquals($expected_result, $result->term_id);
}
}
}