in the middle of getting navigation cleaned up

This commit is contained in:
John Bintz 2009-10-20 20:40:16 -04:00
parent 1db0f884f3
commit b657209d29
9 changed files with 110 additions and 637 deletions

View File

@ -207,7 +207,7 @@ class ComicPressAddonCore extends ComicPressAddon {
*/ */
function show_media($override_post, $method, $format) { function show_media($override_post, $method, $format) {
global $post; global $post;
$post_to_use = (is_null($override_post)) ? $this->comicpress->get_last_comic() : $post; $post_to_use = $post;
switch ($this->comicpress->comicpress_options['comic_space']) { switch ($this->comicpress->comicpress_options['comic_space']) {
case "comic_only": case "comic_only":

View File

@ -53,10 +53,6 @@ class ComicPress {
*/ */
function init() { function init() {
$this->load(); $this->load();
$this->get_all_category_objects_by_id();
$this->flatten_categories();
$this->separate_categories();
$this->sort_comic_categories();
add_action('wp_head', array(&$this, 'wp_head')); add_action('wp_head', array(&$this, 'wp_head'));
add_filter('comicpress_nav', array(&$this, 'comicpress_nav'), 10, 2); add_filter('comicpress_nav', array(&$this, 'comicpress_nav'), 10, 2);
@ -164,250 +160,6 @@ class ComicPress {
return '<div class="partial-helper">' . str_replace(get_template_directory() . DIRECTORY_SEPARATOR, '', $target) . '</div>' . $content; return '<div class="partial-helper">' . str_replace(get_template_directory() . DIRECTORY_SEPARATOR, '', $target) . '</div>' . $content;
} }
/**
* Flatten all WP categories into nodes like 0/3/5.
* @tested
*/
function flatten_categories() {
$this->category_tree = array();
foreach (array_keys($this->categories_by_id) as $category_id) {
$this->category_tree[] = $this->categories_by_id[$category_id]->parent . '/' . $category_id;
}
do {
$all_ok = true;
for ($i = 0; $i < count($this->category_tree); ++$i) {
$current_parts = explode("/", $this->category_tree[$i]);
if (reset($current_parts) != 0) {
$all_ok = false;
for ($j = 0; $j < count($this->category_tree); ++$j) {
$j_parts = explode("/", $this->category_tree[$j]);
if (end($j_parts) == reset($current_parts)) {
$this->category_tree[$i] = implode("/", array_merge($j_parts, array_slice($current_parts, 1)));
break;
}
}
}
}
} while (!$all_ok);
return $this->category_tree;
}
/**
* Separate categories into comics and non-comics categories.
* @tested
*/
function separate_categories() {
$comic_categories = array();
$non_comic_categories = array();
foreach ($this->category_tree as $node) {
$parts = split("/", $node);
if ($parts[1] == $this->comicpress_options['comic_category_id']) {
$comic_categories[] = $node;
} else {
$non_comic_categories[] = $node;
}
}
$this->category_tree = $comic_categories;
$this->non_comic_categories = $non_comic_categories;
}
/**
* Sort the category tree, adding in new categories in the order as necessary.
* @tested
*/
function sort_comic_categories() {
if (is_array($this->comicpress_options['category_order'])) {
$new_order = array();
foreach ($this->comicpress_options['category_order'] as $node) {
if (in_array($node, $this->category_tree)) {
$new_order[] = $node;
}
}
foreach ($this->category_tree as $node) {
if (!in_array($node, $this->comicpress_options['category_order'])) {
$new_order[] = $node;
}
}
$this->category_tree = $new_order;;
}
return $this->category_tree;
}
/**
* Turn the list of categories into a hash table of category objects.
*/
function get_all_category_objects_by_id() {
if (empty($this->categories_by_id)) {
$this->categories_by_id = array();
foreach (get_categories("hide_empty=0") as $category_object) {
$this->categories_by_id[$category_object->term_id] = $category_object;
}
}
return $this->categories_by_id;
}
/**
* Turn the tree of comics categories into a string to be fed into wp_query functions.
* @tested
*/
function get_all_comic_categories_as_cat_string() {
if (empty($this->all_comic_categories_as_string)) {
$categories = array();
foreach ($this->category_tree as $node) {
$categories[] = end(explode("/", $node));
}
$this->all_comic_categories_as_string = implode(",", $categories);
}
return $this->all_comic_categories_as_string;
}
/**
* Return true if the current post is in the comics category or a child category.
* @tested
*/
function in_comic_category($post_id = null) {
global $post;
$post_id_to_use = !is_null($post_id) ? $post_id : $post->ID;
$categories = wp_get_post_categories($post_id_to_use);
if (is_array($categories)) {
foreach ($this->category_tree as $node) {
if (in_array(end(explode("/", $node)), $categories)) {
return true;
}
}
}
return false;
}
/**
* Get the comics necessary to build the navigation.
* @tested
*/
function get_nav_comics($override_category_id = null) {
global $post;
$category = is_numeric($override_category_id) ? $override_category_id : $this->comicpress_options['comic_category_id'];
$comic_posts = array();
foreach (array('first', 'last', 'previous', 'next') as $which) {
$comic_posts[$which] = $this->{"get_${which}_comic"}($category);
}
$comic_posts['show_first'] = (trim($post->ID) != trim($comic_posts['first']->ID));
$comic_posts['show_previous'] = (!empty($comic_posts['previous']) && (trim($comic_posts['first']->ID) != trim($comic_posts['previous']->ID)));
$comic_posts['show_next'] = (!empty($comic_posts['next']) && (trim($comic_posts['last']->ID) != trim($comic_posts['next']->ID)));
$comic_posts['show_last'] = (trim($post->ID) != trim($comic_posts['last']->ID));
if ($this->needs_storyline_nav()) {
$comic_posts = array_merge($comic_posts, $this->get_storyline_nav_comics());
}
return $comic_posts;
}
function get_storyline_nav_comics() {
$comic_posts = array('prior' => false, 'upcoming' => false);
foreach ($this->get_sorted_post_categories() as $category_id) {
$prev_next_categories = $this->get_previous_next_categories($category_id);
foreach ($prev_next_categories as $master_id => $cat_list) {
foreach ($cat_list as $which => $id) {
switch ($which) {
case "previous":
$terminal_post = $this->get_last_comic($id);
$which_field = "prior";
break;
case "next":
$terminal_post = $this->get_first_comic($id);
$which_field = "upcoming";
break;
}
if (is_object($terminal_post)) {
$comic_posts[$which_field] = $terminal_post;
$comic_posts["show_${which_field}"] =true;
}
if (count($terminal_post) == 2) { break; }
}
}
}
return $comic_posts;
}
/**
* Get the comic post adjacent to the current comic.
*/
function get_adjacent_comic($category, $next = false, $override_post = null) {
global $wp_query, $post;
$temp = $wp_query->is_single;
$wp_query->is_single = true;
if (!is_null($override_post)) {
$temp_post = $post;
$post = $override_post;
}
$categories_to_exclude = $this->get_leaves_of_tree($this->non_comic_categories);
if (!is_null($category)) {
$categories_to_exclude = $this->exclude_all_but_provided_categories($category);
}
$result = get_adjacent_post(false, implode(" and ", $categories_to_exclude), !$next);
$wp_query->is_single = $temp;
if (!is_null($override_post)) {
$post = $temp_post;
}
return empty($result) ? false : $result;
}
/**
* Given a category ID or an array of category IDs, create an exclusion string that will
* filter out every category but the provided ones.
*/
function get_string_to_exclude_all_but_provided_categories($category) {
return implode(",", $this->exclude_all_but_provided_categories($category));
}
/**
* Exclude every category but the given one.
*/
function exclude_all_but_provided_categories($category) {
$category_ids = array_keys($this->get_all_category_objects_by_id());
if (!is_array($category)) { $category = array($category); }
return array_diff($category_ids, $category);
}
/**
* Gets the leaves of a ComicPress node tree (branches look like "0/4/5").
*/
function get_leaves_of_tree($tree) {
$leaves = array();
foreach ($tree as $branch) { $leaves[] = end(explode("/", $branch)); }
return $leaves;
}
/**
* Get a new WP_Query object.
*/
function _new_wp_query() {
return new WP_Query();
}
/** /**
* Get the path to a partial. * Get the path to a partial.
* @param array $partials The partials to search for in each path. * @param array $partials The partials to search for in each path.

View File

@ -12,10 +12,31 @@ class ComicPressNavigation {
function get_post_nav($post) { function get_post_nav($post) {
$nav = array(); $nav = array();
// post // 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
if ($root_category = $this->_storyline->root_category) {
foreach (array('first', 'last') as $field) {
$nav[$field] = $this->_dbi->{"get_${field}_comic"}($root_category);
}
}
if ($category = $this->_storyline->get_valid_post_category($post->ID)) {
// storyline previous/next
foreach (array('previous', 'next') as $field) {
$nav["storyline-${field}"] = $this->_dbi->{"get_${field}_comic"}($category, $post);
}
// adjacent storyline nodes
if (is_array($valid = $this->_storyline->valid($category))) {
foreach ($valid as $field) {
$nav["storyline-chapter-${field}"] = $this->_dbi->get_first_comic($this->_storyline->{$field}($category));
}
}
}
} }
} }

View File

@ -3,7 +3,7 @@
require_once('ComicPressDBInterface.inc'); require_once('ComicPressDBInterface.inc');
class ComicPressStoryline { class ComicPressStoryline {
var $_structure; var $_structure, $root_category;
/** /**
* Create a searchable structure from a node list. * Create a searchable structure from a node list.
@ -14,6 +14,7 @@ class ComicPressStoryline {
$new_structure = array(); $new_structure = array();
$parent = null; $parent = null;
$all_leaves = array(); $all_leaves = array();
$this->root_category = false;
$adjacents_by_parent = array(); $adjacents_by_parent = array();
@ -42,6 +43,8 @@ class ComicPressStoryline {
$adjacents_by_parent[$parent][] = $leaf; $adjacents_by_parent[$parent][] = $leaf;
$data['parent'] = $parent; $data['parent'] = $parent;
} else {
$this->root_category = $leaf;
} }
$new_structure[$leaf] = $data; $new_structure[$leaf] = $data;
@ -51,18 +54,8 @@ class ComicPressStoryline {
} }
} }
if ($is_valid) { if ($is_valid) {
foreach ($adjacents_by_parent as $parent => $adjacents) {
for ($i = 0; $i < count($adjacents); ++$i) {
foreach (array('previous' => -1, 'next' => 1) as $type => $dir) {
if (isset($adjacents[$i + $dir])) {
$new_structure[$adjacents[$i]][$type] = $adjacents[$i + $dir];
}
}
}
}
for ($i = 0; $i < count($all_leaves); ++$i) { for ($i = 0; $i < count($all_leaves); ++$i) {
foreach (array('prior' => -1, 'upcoming' => 1) as $type => $dir) { foreach (array('previous' => -1, 'next' => 1) as $type => $dir) {
if (isset($all_leaves[$i + $dir])) { if (isset($all_leaves[$i + $dir])) {
$new_structure[$all_leaves[$i]][$type] = $all_leaves[$i + $dir]; $new_structure[$all_leaves[$i]][$type] = $all_leaves[$i + $dir];
} }
@ -89,8 +82,6 @@ class ComicPressStoryline {
function parent($id) { return $this->_get_field('parent', $id); } function parent($id) { return $this->_get_field('parent', $id); }
function previous($id) { return $this->_get_field('previous', $id); } function previous($id) { return $this->_get_field('previous', $id); }
function next($id) { return $this->_get_field('next', $id); } function next($id) { return $this->_get_field('next', $id); }
function upcoming($id) { return $this->_get_field('upcoming', $id); }
function prior($id) { return $this->_get_field('prior', $id); }
function valid($id) { function valid($id) {
if (isset($this->_structure[$id])) { if (isset($this->_structure[$id])) {
return array_keys($this->_structure[$id]); return array_keys($this->_structure[$id]);

View File

@ -16,7 +16,7 @@ function __comicpress_init() {
$comicpress = new ComicPress(); $comicpress = new ComicPress();
$comicpress->init(); $comicpress->init();
$addons = array(); $addons = array();
if (is_dir($addons_dir = (dirname(__FILE__) . '/addons'))) { if (is_dir($addons_dir = (dirname(__FILE__) . '/addons'))) {
$entries = glob($addons_dir . '/*'); $entries = glob($addons_dir . '/*');
@ -29,11 +29,11 @@ function __comicpress_init() {
$classname = "ComicPressAddon${classname}"; $classname = "ComicPressAddon${classname}";
if (class_exists($classname)) { if (class_exists($classname)) {
$addon =& new $classname(); $addon =& new $classname();
if ( if (
$comicpress->comicpress_options['addons'][$addon->name] || $comicpress->comicpress_options['addons'][$addon->name] ||
$addon->is_addon_manager $addon->is_addon_manager
) { ) {
$addon->init(&$comicpress); $addon->init(&$comicpress);
if (current_user_can('edit_posts')) { if (current_user_can('edit_posts')) {
if (is_array($_REQUEST['cp'])) { if (is_array($_REQUEST['cp'])) {
@ -51,7 +51,7 @@ function __comicpress_init() {
} else { } else {
add_action('wp_head', array(&$addon, 'display_messages')); add_action('wp_head', array(&$addon, 'display_messages'));
} }
} }
} }
$addons[] = $addon; $addons[] = $addon;
} }
@ -59,10 +59,10 @@ function __comicpress_init() {
} }
} }
} }
} }
foreach ($addons as $addon) { foreach ($addons as $addon) {
if ($addon->is_addon_manager) { $addon->all_addons =& $addons; break; } if ($addon->is_addon_manager) { $addon->all_addons =& $addons; break; }
} }
$layouts = $comicpress->get_layout_choices(); $layouts = $comicpress->get_layout_choices();
@ -119,8 +119,6 @@ function include_partial($partials = '') {
function in_comic_category() { function in_comic_category() {
global $post, $comicpress; global $post, $comicpress;
return $comicpress->in_comic_category($post->ID);
} }
/** /**

View File

@ -1,9 +1,14 @@
<?php <?php
global $comicpress, $post, $nav_comics; global $comicpress, $post, $nav_comics;
$dbi = ComicPressDBInterface::get_instance();
var_dump($dbi->get_terminal_post_in_category(3, true));
var_dump($dbi->get_terminal_post_in_category(3, false));
exit(0);
comicpress_init(); comicpress_init();
$nav_comics = $comicpress->get_nav_comics(); $nav_comics = array();
$t = $post; $t = $post;
$post = $nav_comics['last']; $post = $nav_comics['last'];
setup_postdata($post); setup_postdata($post);

View File

@ -14,17 +14,32 @@ class ComicPressNavigationTest extends PHPUnit_Framework_TestCase {
} }
function testGetPostNav() { function testGetPostNav() {
$dbi = $this->getMock('ComicPressDBInterface', array('get_previous_comic', 'get_next_comic')); $dbi = $this->getMock('ComicPressDBInterface', array('get_previous_comic', 'get_next_comic', 'get_first_comic', 'get_last_comic'));
$storyline = new ComicPressStoryline();
$storyline->root_category = 1;
$storyline->_structure = array(
'1' => array('next' => 2),
'2' => array('previous' => 1, 'next' => 3),
'3' => array('previous' => 2)
);
wp_insert_post(array('ID' => 1)); wp_insert_post(array('ID' => 1));
$post = get_post(1); $post = get_post(1);
wp_set_post_categories(1, array(1)); wp_set_post_categories(1, array(2));
$dbi->expects($this->once())->method('get_previous_comic')->with(null, $post); $dbi->expects($this->at(0))->method('get_previous_comic')->with(null, $post);
$dbi->expects($this->once())->method('get_next_comic')->with(null, $post); $dbi->expects($this->at(1))->method('get_next_comic')->with(null, $post);
$dbi->expects($this->at(2))->method('get_first_comic')->with(1);
$dbi->expects($this->at(3))->method('get_last_comic')->with(1);
$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(6))->method('get_first_comic')->with(1);
$dbi->expects($this->at(7))->method('get_first_comic')->with(3);
$this->nav->_dbi = $dbi; $this->nav->_dbi = $dbi;
$this->nav->_storyline = $storyline;
$this->nav->get_post_nav($post); $this->nav->get_post_nav($post);
} }

View File

@ -14,60 +14,69 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
function providerTestCreateStorylineStructure() { function providerTestCreateStorylineStructure() {
return array( return array(
array( array(
false,
false, false,
false false
), ),
array( array(
array('0'), array('0'),
false,
false false
), ),
array( array(
array('1'), array('1'),
false,
false false
), ),
array( array(
array(array(0,1)), array(array(0,1)),
false,
false false
), ),
array( array(
array('0/1'), array('0/1'),
array('1' => array()) array('1' => array()),
1
), ),
array( array(
array('0/1', '0/1/2'), array('0/1', '0/1/2'),
array('1' => array('upcoming' => 2), '2' => array('parent' => 1, 'prior' => 1)) array('1' => array('next' => 2), '2' => array('parent' => 1, 'previous' => 1)),
1
), ),
array( array(
array('0/1', '0/1/2', '0/1/3'), array('0/1', '0/1/2', '0/1/3'),
array( array(
'1' => array('upcoming' => 2), '1' => array('next' => 2),
'2' => array('parent' => 1, 'next' => 3, 'prior' => 1, 'upcoming' => 3), '2' => array('parent' => 1, 'previous' => 1, 'next' => 3),
'3' => array('parent' => 1, 'previous' => 2, 'prior' => 2), '3' => array('parent' => 1, 'previous' => 2),
) ),
1
), ),
array( array(
array('0/1', '0/1/2', '0/1/2/3', '0/1/2/4', '0/1/5'), array('0/1', '0/1/2', '0/1/2/3', '0/1/2/4', '0/1/5'),
array( array(
'1' => array('upcoming' => 2), '1' => array('next' => 2),
'2' => array('parent' => 1, 'next' => 5, 'upcoming' => 3, 'prior' => 1), '2' => array('parent' => 1, 'next' => 3, 'previous' => 1),
'3' => array('parent' => 2, 'next' => 4, 'upcoming' => 4, 'prior' => 2), '3' => array('parent' => 2, 'next' => 4, 'previous' => 2),
'4' => array('parent' => 2, 'previous' => 3, 'upcoming' => 5, 'prior' => 3), '4' => array('parent' => 2, 'next' => 5, 'previous' => 3),
'5' => array('parent' => 1, 'previous' => 2, 'prior' => 4), '5' => array('parent' => 1, 'previous' => 4),
) ),
1
), ),
array( array(
array('0/1', '0/1/2', '0/1/2/3', '0/1/2/4', '0/1/5', '0/1/5/6', '0/1/5/7', '0/1/5/8', '0/1/9'), array('0/1', '0/1/2', '0/1/2/3', '0/1/2/4', '0/1/5', '0/1/5/6', '0/1/5/7', '0/1/5/8', '0/1/9'),
array( array(
'1' => array('upcoming' => 2), '1' => array('next' => 2),
'2' => array('parent' => 1, 'next' => 5, 'upcoming' => 3, 'prior' => 1), '2' => array('parent' => 1, 'next' => 3, 'previous' => 1),
'3' => array('parent' => 2, 'next' => 4, 'upcoming' => 4, 'prior' => 2), '3' => array('parent' => 2, 'next' => 4, 'previous' => 2),
'4' => array('parent' => 2, 'previous' => 3, 'upcoming' => 5, 'prior' => 3), '4' => array('parent' => 2, 'next' => 5, 'previous' => 3),
'5' => array('parent' => 1, 'previous' => 2, 'next' => 9, 'upcoming' => 6, 'prior' => 4), '5' => array('parent' => 1, 'next' => 6, 'previous' => 4),
'6' => array('parent' => 5, 'next' => 7, 'upcoming' => 7, 'prior' => 5), '6' => array('parent' => 5, 'next' => 7, 'previous' => 5),
'7' => array('parent' => 5, 'previous' => 6, 'next' => 8, 'upcoming' => 8, 'prior' => 6), '7' => array('parent' => 5, 'next' => 8, 'previous' => 6),
'8' => array('parent' => 5, 'previous' => 7, 'upcoming' => 9, 'prior' => 7), '8' => array('parent' => 5, 'next' => 9, 'previous' => 7),
'9' => array('parent' => 1, 'previous' => 5, 'prior' => 8), '9' => array('parent' => 1, 'previous' => 8),
) ),
1
), ),
); );
} }
@ -75,23 +84,19 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
/** /**
* @dataProvider providerTestCreateStorylineStructure * @dataProvider providerTestCreateStorylineStructure
*/ */
function testCreateStorylineStructure($input, $expected_structure) { function testCreateStorylineStructure($input, $expected_structure, $expected_root_category) {
$this->assertEquals(is_array($expected_structure), $this->css->create_structure($input)); $this->assertEquals(is_array($expected_structure), $this->css->create_structure($input));
$this->assertEquals($expected_structure, $this->css->_structure); $this->assertEquals($expected_structure, $this->css->_structure);
$this->assertEquals($expected_root_category, $this->css->root_category);
} }
function providerTestGetFields() { function providerTestGetFields() {
return array( return array(
array('parent', 1, false), array('parent', 1, false),
array('parent', 2, 1), array('parent', 2, 1),
array('next', 2, 3),
array('next', 3, 4), array('next', 3, 4),
array('next', 4, false), array('valid', 1, array('next')),
array('previous', 4, 3),
array('previous', 3, false),
array('previous', 2, false),
array('upcoming', 2, 3),
array('upcoming', 3, 4),
array('valid', 1, array('upcoming')),
array('valid', 6, false), array('valid', 6, false),
); );
} }
@ -101,10 +106,10 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
*/ */
function testGetFields($field, $category, $expected_value) { function testGetFields($field, $category, $expected_value) {
$this->css->_structure = array( $this->css->_structure = array(
'1' => array('upcoming' => 2), '1' => array('next' => 2),
'2' => array('parent' => 1, 'prior' => 1, 'upcoming' => 3), '2' => array('parent' => 1, 'previous' => 1, 'next' => 3),
'3' => array('parent' => 2, 'next' => 4, 'upcoming' => 4, 'prior' => 2), '3' => array('parent' => 2, 'next' => 4, 'previous' => 2),
'4' => array('parent' => 2, 'previous' => 3, 'prior' => 3) '4' => array('parent' => 2, 'previous' => 3)
); );
$this->assertEquals($expected_value, $this->css->{$field}($category)); $this->assertEquals($expected_value, $this->css->{$field}($category));
@ -112,11 +117,11 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
function providerTestGetValidNav() { function providerTestGetValidNav() {
return array( return array(
array(array(1), array('upcoming')), array(array(1), array('next')),
array(array(1,2), false), array(array(1,2), false),
array(array(1,4), array('upcoming')), array(array(1,4), array('next')),
array(array(2), array('prior', 'upcoming', 'next')), array(array(2), array('previous', 'next')),
array(array(3), array('prior', 'previous')), array(array(3), array('previous')),
); );
} }
@ -127,9 +132,9 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
wp_set_post_categories(1, $post_categories); wp_set_post_categories(1, $post_categories);
$this->css->_structure = array( $this->css->_structure = array(
'1' => array('upcoming' => 2), '1' => array('next' => 2),
'2' => array('prior' => 1, 'upcoming' => 3, 'next' => 3), '2' => array('previous' => 1, 'next' => 3),
'3' => array('prior' => 2, 'previous' => 2) '3' => array('previous' => 2)
); );
$this->assertEquals($expected_navigation, $this->css->get_valid_nav(1)); $this->assertEquals($expected_navigation, $this->css->get_valid_nav(1));

View File

@ -13,320 +13,6 @@ class ComicPressTest extends PHPUnit_Framework_TestCase {
$this->cp = new ComicPress(); $this->cp = new ComicPress();
} }
function testFlattenCategories() {
$cp = $this->getMock('ComicPress', array('get_all_category_objects_by_id'));
$cp->categories_by_id = array(
'1' => (object)array(
'term_id' => 1,
'parent' => 0
),
'2' => (object)array(
'term_id' => 2,
'parent' => 1
),
'3' => (object)array(
'term_id' => 3,
'parent' => 0
)
);
$this->assertEquals(array('0/1', '0/1/2', '0/3'), $cp->flatten_categories());
}
function testSeparateCategories() {
$cp = $this->getMock('ComicPress', array('flatten_categories'));
$cp->category_tree = array('0/1', '0/1/2', '0/3');
$cp->comicpress_options['comic_category_id'] = 1;
$cp->separate_categories();
$this->assertEquals(array('0/1', '0/1/2'), $cp->category_tree);
$this->assertEquals(array('0/3'), $cp->non_comic_categories);
}
function providerSortComicCategories() {
return array(
array(false, array('0/1', '0/2'), array('0/1', '0/2')),
array(array('0/2', '0/1'), array('0/1', '0/2', '0/3'), array('0/2', '0/1', '0/3'))
);
}
/**
* @dataProvider providerSortComicCategories
*/
function testSortComicCategories($category_order, $category_tree, $expected_tree) {
$this->cp->comicpress_options['category_order'] = $category_order;
$this->cp->category_tree = $category_tree;
$this->assertEquals($expected_tree, $this->cp->sort_comic_categories());
}
function testGetAllComicCategoriesAsCatString() {
$this->cp->category_tree = array("0/1", "0/2", "0/3");
$this->assertEquals("1,2,3", $this->cp->get_all_comic_categories_as_cat_string());
}
function providerTestInComicCategory() {
return array(
array(array(1), false),
array(array(2), true)
);
}
/**
* @dataProvider providerTestInComicCategory
*/
function testInComicCategory($post_categories, $is_in_category) {
$this->cp->category_tree = array('0/2');
wp_set_post_categories(1, $post_categories);
$this->assertEquals($is_in_category, $this->cp->in_comic_category(1));
}
function providerTestGetNavComics() {
return array(
array(
array(
'first' => 1,
'previous' => 2,
'next' => 4,
'last' => 5
),
3,
array(
'first' => true,
'previous' => true,
'next' => true,
'last' => true
)
),
array(
array(
'first' => 1,
'previous' => false,
'next' => false,
'last' => 1
),
1,
array(
'first' => false,
'previous' => false,
'next' => false,
'last' => false
)
),
array(
array(
'first' => 1,
'previous' => false,
'next' => 3,
'last' => 3
),
1,
array(
'first' => false,
'previous' => false,
'next' => false,
'last' => true
)
),
array(
array(
'first' => 1,
'previous' => 1,
'next' => false,
'last' => 3
),
3,
array(
'first' => true,
'previous' => false,
'next' => false,
'last' => false
)
)
);
}
/**
* @dataProvider providerTestGetNavComics
*/
function testGetNavComics($nav_comics, $given_post, $expected_shows) {
global $post;
$cp = $this->getMock('ComicPress', array('get_first_comic', 'get_last_comic', 'get_previous_comic', 'get_next_comic'));
foreach ($nav_comics as $key => $result) {
$return = (is_numeric($result)) ? (object)array('ID' => $result) : false;
$cp->expects($this->once())->method("get_${key}_comic")->will($this->returnValue($return));
}
$post = (is_numeric($given_post)) ? (object)array('ID' => $given_post) : false;
$comic_posts = $cp->get_nav_comics();
foreach ($expected_shows as $show => $expected) {
$this->assertEquals($expected, $comic_posts["show_${show}"], $show);
}
}
function providerTestGetNavStorylineEnabled() {
return array(
array(
array('10'),
array(
'10' => array(
'10' => array('previous' => '9', 'next' => '11')
)
),
array(
'9' => true,
'11' => true
),
array(
'prior' => true,
'upcoming' => true
)
),
array(
array('10', '20'),
array(
'10' => array(
'10' => array('previous' => '7', 'next' => '3')
),
'20' => array(
'20' => array('previous' => '9', 'next' => '11')
),
),
array(
'9' => true,
'11' => true
),
array(
'prior' => true,
'upcoming' => true
)
),
array(
array('10'),
array(
'10' => array(
'10' => array('previous' => '7', 'next' => '3')
),
),
array(
'15' => true,
'20' => true
),
array(
'prior' => false,
'upcoming' => false
)
),
array(
array('10'),
array(
'10' => array(
'7' => array('previous' => '15', 'next' => '20')
),
),
array(
'15' => true,
'20' => true
),
array(
'prior' => true,
'upcoming' => true
)
),
);
}
/**
* @dataProvider providerTestGetNavStorylineEnabled
*/
function testGetNavComicsStorylineEnabled($post_categories, $previous_next_categories, $terminal_comics, $expected_results) {
$cp = $this->getMock('ComicPress', array('get_sorted_post_categories', 'get_previous_next_categories', 'get_last_comic', 'get_first_comic'));
$cp->expects($this->once())->method('get_sorted_post_categories')->will($this->returnValue($post_categories));
$cp->expects($this->any())->method('get_previous_next_categories')->will($this->returnCallback(array(&$this, 'callbackGetPreviousNextCategories')));
$cp->expects($this->any())->method('get_first_comic')->will($this->returnCallback(array(&$this, 'callbackGetTerminalComic')));
$cp->expects($this->any())->method('get_last_comic')->will($this->returnCallback(array(&$this, 'callbackGetTerminalComic')));
$this->_previous_next_categories = $previous_next_categories;
$this->_terminal_comics = $terminal_comics;
$result = $cp->get_storyline_nav_comics();
foreach ($expected_results as $field => $value) {
$this->assertEquals($value, $result["show_${field}"]);
}
}
function callbackGetPreviousNextCategories($id) {
return $this->_previous_next_categories[$id];
}
function callbackGetTerminalComic($id) {
return isset($this->_terminal_comics[$id]) ? (object)array() : false;
}
function providerTestGetPreviousNextCategories() {
return array(
array(
array(
'0/1'
),
1,
array()
),
array(
array(
'0/1',
'0/1/2'
),
2,
array()
),
array(
array(
'0/1',
'0/1/2',
'0/1/3',
),
2,
array(
'1' => array('next' => 3)
)
),
array(
array(
'0/1',
'0/1/6',
'0/1/2',
'0/1/2/4',
'0/1/2/5',
'0/1/3',
),
5,
array(
'2' => array('previous' => 4),
'1' => array('previous' => 6, 'next' => 3),
)
),
);
}
/**
* @dataProvider providerTestGetPreviousNextCategories
*/
function testGetPreviousNextCategories($category_tree, $current_category, $expected_prev_nexts) {
$this->cp->category_tree = $category_tree;
$this->assertEquals($expected_prev_nexts, $this->cp->get_previous_next_categories($current_category));
}
function providerTestGetLayoutChoices() { function providerTestGetLayoutChoices() {
return array( return array(
array( array(