wow it's working

This commit is contained in:
John Bintz 2009-06-10 16:44:19 -04:00
parent 0097bc65dc
commit 9ab7bbbfca
2 changed files with 94 additions and 5 deletions

View File

@ -32,13 +32,14 @@ class DailyImageWidget {
} else { } else {
$this->data = false; $this->data = false;
} }
$this->display_options = $this->get_display_options();
} }
function _init() { function _init() {
register_sidebar_widget(__("HubbleSite Daily Image", "hubblesite-daily-image-widget"), array($this, "render")); 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")); register_widget_control(__("HubbleSite Daily Image", "hubblesite-daily-image-widget"), array($this, "render_ui"));
$this->handle_post();
$this->get_display_options();
} }
function _connection_warning() { 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. * Get the list of display options from the WordPress options database.
*/ */
@ -127,13 +143,14 @@ class DailyImageWidget {
} }
function render_ui() { function render_ui() {
echo "<input type=\"hidden\" name=\"hubblesite[_wpnonce]\" value=\"" . wp_create_nonce('hubble') . "\" />";
echo "<p>"; echo "<p>";
_e("Show on Widget:", "hubblesite-daily-image-widget"); _e("Show on Widget <em>(must select at least one)</em>:", "hubblesite-daily-image-widget");
echo "</p>"; echo "</p>";
foreach ($this->_valid_options as $option => $label) { foreach ($this->_valid_options as $option => $label) {
echo "<label>"; echo "<label>";
echo "<input type=\"checkbox\" name=\"hubblesite[${option}]\" />"; echo "<input type=\"checkbox\" name=\"hubblesite[${option}]\" " . (in_array($option, $this->display_options) ? "checked=\"checked\"" : "") . "/> ";
echo $label; echo $label;
echo "</label>"; echo "</label>";
echo "<br />"; echo "<br />";

View File

@ -8,6 +8,8 @@ require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
function setUp() { function setUp() {
_reset_wp(); _reset_wp();
wp_create_nonce("hubble");
$_POST = array();
$this->diw = new DailyImageWidget(true); $this->diw = new DailyImageWidget(true);
@ -133,6 +135,70 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(get_option('hubblesite-daily-image-options') !== false); $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() { function providerTestParseBadXML() {
return array( return array(
array(null), array(null),
@ -188,6 +254,12 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase {
$xpath = "//label[contains(text(), '${label}')]"; $xpath = "//label[contains(text(), '${label}')]";
$this->assertTrue(_xpath_test($xml, $xpath, true), $xpath); $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() { function testGetCachedData() {