fix FS#113

This commit is contained in:
John Bintz 2009-11-09 21:12:42 -05:00
parent 99939dac4c
commit 6c9d05f6af
2 changed files with 55 additions and 13 deletions

View File

@ -14,44 +14,50 @@ class ComicPressNavigation {
if (is_object($post)) { if (is_object($post)) {
if (isset($post->ID)) { if (isset($post->ID)) {
$cache_key = 'navigation-' . $post->ID; $cache_key = 'navigation-' . $post->ID;
if (($result = wp_cache_get($cache_key, 'comicpress')) !== false) { if (($result = wp_cache_get($cache_key, 'comicpress')) !== false) {
foreach ($result as $key => $post_id) { foreach ($result as $key => $post_id) {
$nev[$key] = get_post($post_id); $nev[$key] = get_post($post_id);
} }
} }
// global previous/next // global previous/next
foreach (array('previous', 'next') as $field) { foreach (array('previous', 'next') as $field) {
$nav[$field] = $this->_dbi->{"get_${field}_comic"}(null, $post); $nav[$field] = $this->_dbi->{"get_${field}_comic"}(null, $post);
} }
// global first/last // global first/last
foreach (array('first', 'last') as $field) { foreach (array('first', 'last') as $field) {
$nav[$field] = $this->_dbi->{"get_${field}_comic"}(null); $nav[$field] = $this->_dbi->{"get_${field}_comic"}(null);
} }
if ($category = $this->_storyline->get_valid_post_category($post->ID)) { if ($category = $this->_storyline->get_valid_post_category($post->ID)) {
// storyline previous/next // storyline previous/next
foreach (array('previous', 'next') as $field) { foreach (array('previous', 'next') as $field) {
$nav["storyline-${field}"] = $this->_dbi->{"get_${field}_comic"}($category, $post); $nav["storyline-${field}"] = $this->_dbi->{"get_${field}_comic"}($category, $post);
} }
// adjacent storyline nodes // adjacent storyline nodes
if (is_array($valid = $this->_storyline->valid($category))) { if (is_array($valid = $this->_storyline->valid($category))) {
foreach ($valid as $field) { foreach ($valid as $field) {
$nav["storyline-chapter-${field}"] = $this->_dbi->get_first_comic($this->_storyline->{$field}($category)); $all_adjacents = $this->_storyline->all_adjacent($category, $field);
foreach ($all_adjacents as $adjacent_category) {
$result = $this->_dbi->get_first_comic($adjacent_category);
if (!empty($result)) {
$nav["storyline-chapter-${field}"] = $result; break;
}
}
} }
} }
} }
$cache_data = array(); $cache_data = array();
foreach ($nav as $key => $output_post) { foreach ($nav as $key => $output_post) {
if (!empty($output_post)) { $cache_data[$key] = $output_post->ID; } if (!empty($output_post)) { $cache_data[$key] = $output_post->ID; }
} }
wp_cache_set($cache_key, $cache_data, 'comicpress'); wp_cache_set($cache_key, $cache_data, 'comicpress');
return $nav; return $nav;
} }
} }

View File

@ -12,7 +12,7 @@ class ComicPressNavigationTest extends PHPUnit_Framework_TestCase {
_reset_wp(); _reset_wp();
$this->nav = new ComicPressNavigation(); $this->nav = new ComicPressNavigation();
} }
function testGetPostNav() { function testGetPostNav() {
$dbi = $this->getMock('ComicPressDBInterface', array('get_previous_comic', 'get_next_comic', 'get_first_comic', 'get_last_comic')); $dbi = $this->getMock('ComicPressDBInterface', array('get_previous_comic', 'get_next_comic', 'get_first_comic', 'get_last_comic'));
$storyline = new ComicPressStoryline(); $storyline = new ComicPressStoryline();
@ -34,18 +34,54 @@ class ComicPressNavigationTest extends PHPUnit_Framework_TestCase {
$dbi->expects($this->at(3))->method('get_last_comic')->with(null); $dbi->expects($this->at(3))->method('get_last_comic')->with(null);
$dbi->expects($this->at(4))->method('get_previous_comic')->with(2, $post); $dbi->expects($this->at(4))->method('get_previous_comic')->with(2, $post);
$dbi->expects($this->at(5))->method('get_next_comic')->with(2, $post); $dbi->expects($this->at(5))->method('get_next_comic')->with(2, $post);
$dbi->expects($this->at(6))->method('get_first_comic')->with(1); $dbi->expects($this->at(6))->method('get_first_comic')->with(1)->will($this->returnValue((object)array('ID' => 1)));
$dbi->expects($this->at(7))->method('get_first_comic')->with(3); $dbi->expects($this->at(7))->method('get_first_comic')->with(3)->will($this->returnValue((object)array('ID' => 1)));
$this->nav->_dbi = $dbi; $this->nav->_dbi = $dbi;
$this->nav->_storyline = $storyline; $this->nav->_storyline = $storyline;
$this->assertFalse(wp_cache_get('navigation-1', 'comicpress')); $this->assertFalse(wp_cache_get('navigation-1', 'comicpress'));
$this->nav->get_post_nav($post); $this->nav->get_post_nav($post);
$this->assertTrue(wp_cache_get('navigation-1', 'comicpress') !== false); $this->assertTrue(wp_cache_get('navigation-1', 'comicpress') !== false);
} }
function testSkipEmptyCategories() {
$dbi = $this->getMock('ComicPressDBInterface', array('get_previous_comic', 'get_next_comic', 'get_first_comic', 'get_last_comic'));
$storyline = new ComicPressStoryline();
$storyline->_structure = array(
'1' => array('next' => 2),
'2' => array('previous' => 1, 'next' => 3),
'3' => array('previous' => 2)
);
wp_insert_post(array('ID' => 1));
$post = get_post(1);
wp_set_post_categories(1, array(1));
$dbi->expects($this->any())->method('get_first_comic')->will($this->returnCallback(array(&$this, 'callbackTestSkipEmptyCategories')));
$this->nav->_dbi = $dbi;
$this->nav->_storyline = $storyline;
$nav = $this->nav->get_post_nav($post);
$this->assertEquals(10, $nav['storyline-chapter-next']->ID);
}
function callbackTestSkipEmptyCategories($category_id) {
if (!is_null($category_id)) {
switch ($category_id) {
case 3: return (object)array('ID' => 10);
default: return false;
}
} else {
return (object)array('ID' => 1);
}
}
} }
?> ?>