This commit is contained in:
John Bintz 2010-01-24 20:35:26 -05:00
parent 2def8b9185
commit e7bb9f0766
2 changed files with 47 additions and 18 deletions

View File

@ -44,6 +44,17 @@ class ComicPressBookmarkWidget extends WP_Widget {
) )
) )
); );
$this->default_instance = array(
'title' => __('Bookmark This Page', 'comicpress'),
'mode' => array_shift(array_keys($this->text_fields))
);
foreach (array_values($this->text_fields) as $fields) {
foreach ($fields as $field => $info) {
$this->default_instance[$field] = $info['default'];
}
}
} }
function init() {} function init() {}
@ -69,17 +80,7 @@ class ComicPressBookmarkWidget extends WP_Widget {
// @codeCoverageIgnoreEnd // @codeCoverageIgnoreEnd
function form($instance) { function form($instance) {
$instance = (array)$instance; $instance = array_merge($this->default_instance, (array)$instance); ?>
$defaults = array('title' => '', 'mode' => 'three-button');
foreach (array_values($this->text_fields) as $field => $info) {
if (isset($info['default'])) {
$defaults[$field] = $info['default'];
}
}
$instance = array_merge($defaults, $instance); ?>
<div id="<?php echo $this->get_field_id('wrapper') ?>"> <div id="<?php echo $this->get_field_id('wrapper') ?>">
<p> <p>
<label><?php _e('Title', 'comicpress') ?><br /> <label><?php _e('Title', 'comicpress') ?><br />
@ -137,7 +138,7 @@ class ComicPressBookmarkWidget extends WP_Widget {
<?php } <?php }
function update($new_instance, $old_instance) { function update($new_instance, $old_instance) {
$instance = array(); $instance = $this->default_instance;
$all_text_fields = array('title'); $all_text_fields = array('title');
foreach ($this->text_fields as $type => $fields) { foreach ($this->text_fields as $type => $fields) {
@ -145,13 +146,15 @@ class ComicPressBookmarkWidget extends WP_Widget {
} }
foreach ($all_text_fields as $key) { foreach ($all_text_fields as $key) {
$instance[$key] = strip_tags($new_instance[$key]); if (isset($new_instance[$key])) {
$instance[$key] = strip_tags($new_instance[$key]);
}
} }
if (isset($this->text_fields[$new_instance['mode']])) { if (isset($new_instance['mode'])) {
$instance['mode'] = $new_instance['mode']; if (isset($this->text_fields[$new_instance['mode']])) {
} else { $instance['mode'] = $new_instance['mode'];
$instance['mode'] = array_shift(array_keys($this->text_fields)); }
} }
return $instance; return $instance;

View File

@ -15,7 +15,7 @@ class BookmarkWidgetTest extends PHPUnit_Framework_TestCase {
array(), array( array(), array(
'//input[contains(@name, "mode") and @value="three-button" and @checked]' => true, '//input[contains(@name, "mode") and @value="three-button" and @checked]' => true,
'//input[contains(@name, "mode") and @value="one-button" and not(@checked)]' => true, '//input[contains(@name, "mode") and @value="one-button" and not(@checked)]' => true,
'//input[contains(@name, "title") and @value=""]' => true, '//input[contains(@name, "title") and @value="Bookmark This Page"]' => true,
), ),
), ),
array( array(
@ -53,4 +53,30 @@ class BookmarkWidgetTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath);
} }
} }
function providerTestUpdate() {
return array(
array(
array(),
array()
),
array(
array('tag-page' => 'Test', 'title' => 'Test title', 'mode' => 'one-button'),
array('tag-page' => 'Test', 'title' => 'Test title', 'mode' => 'one-button')
),
array(
array('mode' => 'two-button'),
array('mode' => 'three-button')
),
);
}
/**
* @dataProvider providerTestUpdate
*/
function testUpdate($update_array, $expected_instance_merge) {
$w = new ComicPressBookmarkWidget();
$this->assertEquals(array_merge($w->default_instance, $expected_instance_merge), $w->update($update_array, array()));
}
} }