start work on theme options page

This commit is contained in:
John Bintz 2009-07-04 12:08:02 -04:00
parent 8148765724
commit bc964971f7
3 changed files with 103 additions and 2 deletions

View File

@ -30,8 +30,15 @@ if (defined("CPM_DATE_FORMAT")) {
$comic_filename_filters = array(); $comic_filename_filters = array();
$comic_filename_filters['default'] = "{date}*.*"; $comic_filename_filters['default'] = "{date}*.*";
require_once(dirname(__FILE__) . '/options.php');
// load all of the comic & non-comic category information // load all of the comic & non-comic category information
add_action('init', 'get_all_comic_categories'); add_action('init', '__comicpress_init');
function __comicpress_init() {
get_all_comic_categories();
add_action('admin_init', '__comicpress_add_options_admin');
}
function get_first_comic() { function get_first_comic() {
return get_terminal_post_in_category(get_all_comic_categories_as_cat_string()); return get_terminal_post_in_category(get_all_comic_categories_as_cat_string());

36
options.php Normal file
View File

@ -0,0 +1,36 @@
<?php
class ComicPressOptionsAdmin {
function render_admin() {
$nonce = wp_create_nonce('comicpress');
echo '<div class="wrap">';
echo '<h2>ComicPress Config</h2>';
echo '<form method="post">';
echo '<input type="hidden" name="cp[_nonce]" value="' . $nonce . '" />';
echo '</form>';
echo '</div>';
}
function get_root_categories() {
$root_categories = array();
foreach (get_all_category_ids() as $id) {
$category = get_category($id);
if (!empty($category)) {
if ($category->parent == 0) {
$root_categories[] = $category;
}
}
}
return $root_categories;
}
}
$comicpress_options_admin = new ComicPressOptionsAdmin();
function __comicpress_add_options_admin() {
global $comicpress_options_admin;
add_theme_page(__("ComicPress Options", 'comicpress'), __('ComicPress Options', 'comicpress'), 'edit_themes', basename(__FILE__), array($comicpress_options_admin, 'render_admin'));
}
?>

58
test/OptionsPageTest.php Normal file
View File

@ -0,0 +1,58 @@
<?php
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
require_once(dirname(__FILE__) . '/../options.php');
class OptionsPageTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
$this->admin = new ComicPressOptionsAdmin();
}
function testShowOptionsPage() {
$nonce = wp_create_nonce('comicpress');
ob_start();
$this->admin->render_admin();
$source = ob_get_clean();
$this->assertTrue(($xml = _to_xml($source)) !== false);
foreach (array(
'//input[@name="cp[_nonce]" and @value="' . $nonce . '"]' => true
) as $xpath => $value) {
$this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath);
}
}
function providerTestGetRootComicCategories() {
return array(
array(array(), array()),
array(
array(
array('id' => 1, 'parent' => 0),
array('id' => 2, 'parent' => 1)
),
array(1)
)
);
}
/**
* @dataProvider providerTestGetRootComicCategories
*/
function testGetRootCategories($categories, $expected_result) {
foreach ($categories as $category) {
add_category($category['id'], (object)$category);
}
$result_ids = array();
foreach ($this->admin->get_root_categories() as $category) {
$result_ids[] = $category->term_id;
}
$this->assertEquals($expected_result, $result_ids);
}
}
?>