From 9ab7bbbfca394fc84da6eb4243de6807e0b92409 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 10 Jun 2009 16:44:19 -0400 Subject: [PATCH] wow it's working --- classes/DailyImageWidget.php | 25 ++++++++++-- test/DailyImageWidgetTest.php | 74 ++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 5 deletions(-) diff --git a/classes/DailyImageWidget.php b/classes/DailyImageWidget.php index 7ccc78b..61fca74 100644 --- a/classes/DailyImageWidget.php +++ b/classes/DailyImageWidget.php @@ -32,13 +32,14 @@ class DailyImageWidget { } else { $this->data = false; } - - $this->display_options = $this->get_display_options(); } function _init() { register_sidebar_widget(__("HubbleSite Daily Image", "hubblesite-daily-image-widget"), array($this, "render")); register_widget_control(__("HubbleSite Daily Image", "hubblesite-daily-image-widget"), array($this, "render_ui")); + + $this->handle_post(); + $this->get_display_options(); } function _connection_warning() { @@ -66,6 +67,21 @@ class DailyImageWidget { } } + function handle_post() { + if (isset($_POST['hubblesite']['_wpnonce'])) { + if (wp_verify_nonce($_POST['hubblesite']['_wpnonce'], 'hubble')) { + $options = array(); + foreach ($this->_valid_options as $option => $label) { + if (isset($_POST['hubblesite'][$option])) { + $options[] = $option; + } + } + $this->display_options = $options; + update_option('hubblesite-daily-image-options', implode(",", $this->display_options)); + } + } + } + /** * Get the list of display options from the WordPress options database. */ @@ -127,13 +143,14 @@ class DailyImageWidget { } function render_ui() { + echo ""; echo "

"; - _e("Show on Widget:", "hubblesite-daily-image-widget"); + _e("Show on Widget (must select at least one):", "hubblesite-daily-image-widget"); echo "

"; foreach ($this->_valid_options as $option => $label) { echo ""; echo "
"; diff --git a/test/DailyImageWidgetTest.php b/test/DailyImageWidgetTest.php index 7ce5ea4..b182e13 100644 --- a/test/DailyImageWidgetTest.php +++ b/test/DailyImageWidgetTest.php @@ -8,6 +8,8 @@ require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php'); class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { function setUp() { _reset_wp(); + wp_create_nonce("hubble"); + $_POST = array(); $this->diw = new DailyImageWidget(true); @@ -133,6 +135,70 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { $this->assertTrue(get_option('hubblesite-daily-image-options') !== false); } + function testCheckedOptions() { + $this->diw->display_options = array_keys($this->diw->_valid_options); + + ob_start(); + $this->diw->render_ui(); + $result = ob_get_clean(); + + $this->assertTrue(($xml = _to_xml($result, true)) !== false); + + foreach ($this->diw->display_options as $option) { + $this->assertTrue(_node_exists($xml, '//input[@name="hubblesite[' . $option . ']" and @checked="checked"]')); + } + } + + function providerTestUpdateOptions() { + $d = new DailyImageWidget(true); + $default_display_options = $d->default_display_options; + + return array( + array( + array(), + $default_display_options + ), + array( + array( + 'save-widgets' => "yes" + ), + $default_display_options + ), + array( + array( + 'hubblesite' => array( + '_wpnonce' => "~*NONCE*~" + ) + ), + $default_display_options + ), + array( + array( + 'hubblesite' => array( + '_wpnonce' => "~*NONCE*~", + 'credits' => "yes" + ) + ), + array("credits") + ), + ); + } + + /** + * @dataProvider providerTestUpdateOptions + */ + function testUpdateOptions($post, $result) { + $_POST = $post; + + if (isset($_POST['hubblesite']['_wpnonce'])) { + $_POST['hubblesite']['_wpnonce'] = _get_nonce('hubble'); + } + + $this->diw->handle_post(); + $this->diw->get_display_options(); + $this->assertEquals($result, $this->diw->display_options); + } + function providerTestParseBadXML() { return array( array(null), @@ -187,7 +253,13 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { foreach ($this->diw->_valid_options as $option => $label) { $xpath = "//label[contains(text(), '${label}')]"; $this->assertTrue(_xpath_test($xml, $xpath, true), $xpath); - } + } + + foreach (array( + '//input[@type="hidden" and @name="hubblesite[_wpnonce]"]' => true + ) as $xpath => $value) { + $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); + } } function testGetCachedData() {