From bc2bc475d48ed3518d929a9bda4f56056e30e744 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 24 Feb 2010 21:29:41 -0500 Subject: [PATCH] database and multi-post retrieval fixes --- classes/ComicPressDBInterface.inc | 4 ++-- classes/ComicPressTagBuilder.inc | 21 +++++++++++++++++---- test/ComicPressTagBuilderTest.php | 24 ++++++++++++++++++++---- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/classes/ComicPressDBInterface.inc b/classes/ComicPressDBInterface.inc index ebb68cd..fb3f665 100644 --- a/classes/ComicPressDBInterface.inc +++ b/classes/ComicPressDBInterface.inc @@ -101,6 +101,7 @@ class ComicPressDBInterface { $op = ($next ? '>' : '<'); $order = ($next ? 'ASC' : 'DESC'); + $cats = implode(',', $categories); $query = $wpdb->prepare("SELECT p.* FROM $wpdb->posts AS p INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id @@ -109,10 +110,9 @@ class ComicPressDBInterface { AND p.post_type = 'post' AND p.post_status = 'publish' AND tt.taxonomy = 'category' - AND tt.term_id IN (%s) + AND tt.term_id IN (${cats}) ORDER BY p.post_date ${order} LIMIT %d", $post_to_use->post_date, - implode(',', $categories), $count); $query_key = 'comicpress_adjacent_post_' . md5($query); diff --git a/classes/ComicPressTagBuilder.inc b/classes/ComicPressTagBuilder.inc index af4b1fa..06b2a6f 100644 --- a/classes/ComicPressTagBuilder.inc +++ b/classes/ComicPressTagBuilder.inc @@ -110,8 +110,11 @@ class ComicPressTagBuilder { $this->parent_post = $parent_post; } - public function _setup_postdata($post) { - setup_postdata($post); + public function _setup_postdata($p) { + global $post; + + $post = $p; + setup_postdata($p); } public function _new_comicpresscomicpost($post) { @@ -194,9 +197,19 @@ class ComicPressTagBuilder { if ($count < 2) { $count = false; } + $result = call_user_func(array($this->dbi, "get_${method}_post"), $this->storyline->build_from_restrictions($this->restrictions), $this->parent_post, $count); - if (is_array($result)) { - return $result; + + if ($count > 1) { + if (is_array($result)) { + return $result; + } else { + if (is_object($result)) { + return array($result); + } else { + return array(); + } + } } else { $this->post = $result; } diff --git a/test/ComicPressTagBuilderTest.php b/test/ComicPressTagBuilderTest.php index 2ee7480..fc17bd4 100644 --- a/test/ComicPressTagBuilderTest.php +++ b/test/ComicPressTagBuilderTest.php @@ -79,7 +79,23 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase { ), array('get_next_post', array(1), $p, 3), false, - true + array('test') + ), + array( + array( + array('next', 3) + ), + array('get_next_post', array(1), $p, 3), + false, + (object)array('test' => 'test2') + ), + array( + array( + array('next', 3) + ), + array('get_next_post', array(1), $p, 3), + false, + "string" ), array( array( @@ -94,7 +110,7 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase { /** * @dataProvider providerTestBuilder */ - function testStorylineBuilder($instructions, $expected_dbi_call, $expects_setup_postdata = false, $returns_array = false) { + function testStorylineBuilder($instructions, $expected_dbi_call, $expects_setup_postdata = false, $returns_override = null) { global $post, $wp_test_expectations; $post = (object)array('ID' => 1); @@ -110,8 +126,8 @@ class ComicPressTagBuilderTest extends PHPUnit_Framework_TestCase { call_user_func(array($expectation, 'will'), $this->returnValue('new-post')); } - if ($returns_array) { - call_user_func(array($expectation, 'will'), $this->returnValue(array('new-post'))); + if (!empty($returns_override)) { + call_user_func(array($expectation, 'will'), $this->returnValue($returns_override)); } $core = new ComicPressTagBuilderFactory($dbi);