more storyline tests

This commit is contained in:
John Bintz 2009-10-18 17:59:40 -04:00
parent 1075712fbd
commit dfa4560df4
5 changed files with 179 additions and 47 deletions

View File

@ -290,34 +290,6 @@ class ComicPress {
return false; return false;
} }
/**
* Find the terminal post in a specific category.
*/
function get_terminal_post_in_category($category_id = null, $first = true) {
$category_id = is_null($category_id) ? $this->comicpress_options['comic_category_id'] : $category_id;
$sort_order = $first ? "asc" : "desc";
$terminal_comic_query =$this->_new_wp_query();
$terminal_comic_query->query("showposts=1&order=${sort_order}&cat=${category_id}&status=publish");
if ($terminal_comic_query->have_posts()) {
return reset($terminal_comic_query->posts);
}
return false;
}
/**
* Get the first comic in a category.
*/
function get_first_comic($category_id = null) {
return $this->get_terminal_post_in_category($category_id);
}
/**
* Get the last comic in a category.
*/
function get_last_comic($category_id = null) {
return $this->get_terminal_post_in_category($category_id, false);
}
/** /**
* Get the comics necessary to build the navigation. * Get the comics necessary to build the navigation.
* @tested * @tested

View File

@ -0,0 +1,37 @@
<?php
class ComicPressNavigation {
function init($post, $storyline) {
$this->post = $post;
$this->storyline = $storyline;
}
/**
* Find the terminal post in a specific category.
*/
function get_terminal_post_in_category($category_id, $first = true) {
$sort_order = $first ? "asc" : "desc";
$terminal_comic_query = new WP_Query();
$terminal_comic_query->query("showposts=1&order=${sort_order}&cat=${category_id}&status=publish");
if ($terminal_comic_query->have_posts()) {
return reset($terminal_comic_query->posts);
}
return false;
}
/**
* Get the first comic in a category.
*/
function get_first_in_category($category_id) {
return $this->get_terminal_post_in_category($category_id);
}
/**
* Get the last comic in a category.
*/
function get_last_in_category($category_id) {
return $this->get_terminal_post_in_category($category_id, false);
}
}
?>

View File

@ -3,10 +3,15 @@
class ComicPressStoryline { class ComicPressStoryline {
var $_structure; var $_structure;
/**
* Create a searchable structure from a node list.
* @param array $structure The structure to process.
* @return boolean True if the structure was valid.
*/
function create_structure($structure) { function create_structure($structure) {
$new_structure = array(); $new_structure = array();
$parent = null; $parent = null;
$previous = null; $all_leaves = array();
$adjacents_by_parent = array(); $adjacents_by_parent = array();
@ -24,6 +29,7 @@ class ComicPressStoryline {
} else { } else {
$data = array(); $data = array();
$leaf = end($parts); $leaf = end($parts);
$all_leaves[] = $leaf;
if (count($parts) > 2) { if (count($parts) > 2) {
$parent = $parts[count($parts) - 2]; $parent = $parts[count($parts) - 2];
@ -52,12 +58,56 @@ class ComicPressStoryline {
} }
} }
} }
for ($i = 0; $i < count($all_leaves); ++$i) {
foreach (array('prior' => -1, 'upcoming' => 1) as $type => $dir) {
if (isset($all_leaves[$i + $dir])) {
$new_structure[$all_leaves[$i]][$type] = $all_leaves[$i + $dir];
}
}
}
$this->_structure = $new_structure; $this->_structure = $new_structure;
} }
} }
return is_array($this->_structure); return is_array($this->_structure);
} }
function _get_field($field, $id) {
if (isset($this->_structure)) {
if (isset($this->_structure[$id])) {
if (isset($this->_structure[$id][$field])) {
return $this->_structure[$id][$field];
}
}
}
return false;
}
function parent($id) { return $this->_get_field('parent', $id); }
function previous($id) { return $this->_get_field('previous', $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) {
if (isset($this->_structure[$id])) {
return array_keys($this->_structure[$id]);
}
return false;
}
function get_valid_storyline_nav($post_id) {
$data = false;
foreach (wp_get_post_categories($post_id) as $category) {
if ($result = $this->valid($category)) {
if ($data) { return false; }
$data = $result;
}
}
return $data;
}
} }
?> ?>

View File

@ -0,0 +1,18 @@
<?php
require_once('MockPress/mockpress.php');
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../classes/ComicPressNavigation.inc');
class ComicPressNavigationTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
$this->nav = new ComicPressNavigation();
}
function testSomething() {
$this->markTestIncomplete();
}
}
?>

View File

@ -35,38 +35,38 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
), ),
array( array(
array('0/1', '0/1/2'), array('0/1', '0/1/2'),
array('1' => array(), '2' => array('parent' => 1)) array('1' => array('upcoming' => 2), '2' => array('parent' => 1, 'prior' => 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(), '1' => array('upcoming' => 2),
'2' => array('parent' => 1, 'next' => 3), '2' => array('parent' => 1, 'next' => 3, 'prior' => 1, 'upcoming' => 3),
'3' => array('parent' => 1, 'previous' => 2), '3' => array('parent' => 1, 'previous' => 2, 'prior' => 2),
) )
), ),
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(), '1' => array('upcoming' => 2),
'2' => array('parent' => 1, 'next' => 5), '2' => array('parent' => 1, 'next' => 5, 'upcoming' => 3, 'prior' => 1),
'3' => array('parent' => 2, 'next' => 4), '3' => array('parent' => 2, 'next' => 4, 'upcoming' => 4, 'prior' => 2),
'4' => array('parent' => 2, 'previous' => 3), '4' => array('parent' => 2, 'previous' => 3, 'upcoming' => 5, 'prior' => 3),
'5' => array('parent' => 1, 'previous' => 2), '5' => array('parent' => 1, 'previous' => 2, 'prior' => 4),
) )
), ),
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(), '1' => array('upcoming' => 2),
'2' => array('parent' => 1, 'next' => 5), '2' => array('parent' => 1, 'next' => 5, 'upcoming' => 3, 'prior' => 1),
'3' => array('parent' => 2, 'next' => 4), '3' => array('parent' => 2, 'next' => 4, 'upcoming' => 4, 'prior' => 2),
'4' => array('parent' => 2, 'previous' => 3), '4' => array('parent' => 2, 'previous' => 3, 'upcoming' => 5, 'prior' => 3),
'5' => array('parent' => 1, 'previous' => 2, 'next' => 9), '5' => array('parent' => 1, 'previous' => 2, 'next' => 9, 'upcoming' => 6, 'prior' => 4),
'6' => array('parent' => 5, 'next' => 7), '6' => array('parent' => 5, 'next' => 7, 'upcoming' => 7, 'prior' => 5),
'7' => array('parent' => 5, 'previous' => 6, 'next' => 8), '7' => array('parent' => 5, 'previous' => 6, 'next' => 8, 'upcoming' => 8, 'prior' => 6),
'8' => array('parent' => 5, 'previous' => 7), '8' => array('parent' => 5, 'previous' => 7, 'upcoming' => 9, 'prior' => 7),
'9' => array('parent' => 1, 'previous' => 5), '9' => array('parent' => 1, 'previous' => 5, 'prior' => 8),
) )
), ),
); );
@ -79,6 +79,61 @@ class ComicPressStorylineTest extends PHPUnit_Framework_TestCase {
$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);
} }
function providerTestGetFields() {
return array(
array('parent', 1, false),
array('parent', 2, 1),
array('next', 3, 4),
array('next', 4, false),
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),
);
}
/**
* @dataProvider providerTestGetFields
*/
function testGetFields($field, $category, $expected_value) {
$this->css->_structure = array(
'1' => array('upcoming' => 2),
'2' => array('parent' => 1, 'prior' => 1, 'upcoming' => 3),
'3' => array('parent' => 2, 'next' => 4, 'upcoming' => 4, 'prior' => 2),
'4' => array('parent' => 2, 'previous' => 3, 'prior' => 3)
);
$this->assertEquals($expected_value, $this->css->{$field}($category));
}
function providerTestGetValidStorylineNav() {
return array(
array(array(1), array('upcoming')),
array(array(1,2), false),
array(array(1,4), array('upcoming')),
array(array(2), array('prior', 'upcoming', 'next')),
array(array(3), array('prior', 'previous')),
);
}
/**
* @dataProvider providerTestGetValidStorylineNav
*/
function testGetValidStorylineNav($post_categories, $expected_navigation) {
wp_set_post_categories(1, $post_categories);
$this->css->_structure = array(
'1' => array('upcoming' => 2),
'2' => array('prior' => 1, 'upcoming' => 3, 'next' => 3),
'3' => array('prior' => 2, 'previous' => 2)
);
$this->assertEquals($expected_navigation, $this->css->get_valid_storyline_nav(1));
}
} }
?> ?>