From 3c3c7dffbcb3a28c40f47c2a44c069c0d2777d49 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Thu, 11 Jun 2009 09:09:13 -0400 Subject: [PATCH 1/8] remove caption as an option --- classes/DailyImageWidget.php | 13 +++---------- hubblesite-styles.css | 2 +- test/DailyImageWidgetTest.php | 7 ------- 3 files changed, 4 insertions(+), 18 deletions(-) diff --git a/classes/DailyImageWidget.php b/classes/DailyImageWidget.php index f9d17e9..fd911fd 100644 --- a/classes/DailyImageWidget.php +++ b/classes/DailyImageWidget.php @@ -14,11 +14,10 @@ class DailyImageWidget { $this->has_simplexml = class_exists('SimpleXMLElement'); - $this->_valid_column_names = array('title', 'caption', 'date', 'image_url', 'gallery_url', 'credits'); + $this->_valid_column_names = array('title', 'date', 'image_url', 'gallery_url', 'credits'); $this->_valid_options = array( "image" => __("Daily Image", "hubblesite-daily-image-widget"), "title" => __("Image Title", "hubblesite-daily-image-widget"), - "caption" => __("Image Caption", "hubblesite-daily-image-widget"), "credits" => __("Credits", "hubblesite-daily-image-widget"), "styles" => __("HubbleSite Styles", "hubblesite-daily-image-widget"), ); @@ -113,7 +112,7 @@ class DailyImageWidget { if (in_array("image", $options)) { echo ''; - echo '' . $this->data['title'] . ''; + echo '' . $this->data['title'] . ''; echo ''; } @@ -123,12 +122,6 @@ class DailyImageWidget { echo ''; } - if (in_array("caption", $options)) { - echo '
'; - echo $this->data['caption']; - echo '
'; - } - if (in_array("credits", $options)) { echo '
'; echo $this->data['credits']; @@ -254,4 +247,4 @@ function the_hubblesite_daily_image_widget() { $diw->render(); } -?> \ No newline at end of file +?> diff --git a/hubblesite-styles.css b/hubblesite-styles.css index ba7bca7..61efba4 100644 --- a/hubblesite-styles.css +++ b/hubblesite-styles.css @@ -25,4 +25,4 @@ p#hubblesite-daily-image-header { color: #666; margin: 0; padding: 0; -} \ No newline at end of file +} diff --git a/test/DailyImageWidgetTest.php b/test/DailyImageWidgetTest.php index b182e13..74e461f 100644 --- a/test/DailyImageWidgetTest.php +++ b/test/DailyImageWidgetTest.php @@ -15,7 +15,6 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { $this->sample_data = array( 'title' => 'title', - 'caption' => 'caption', 'date' => '12345', 'image_url' => 'image_url', 'gallery_url' => 'gallery_url', @@ -77,12 +76,6 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { '//style[@type="text/css"]' => true ) ), - array( - "caption", - array( - '//div/div[@id="hubblesite-daily-image-caption"]' => 'caption' - ) - ), array( "credits", array( From 14a971af2db8d3c9a44e18254e44a9ba14d6a7f1 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 24 Jun 2009 14:51:53 -0400 Subject: [PATCH 2/8] tweak colors --- hubblesite-styles.css | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/hubblesite-styles.css b/hubblesite-styles.css index 61efba4..0f9afdd 100644 --- a/hubblesite-styles.css +++ b/hubblesite-styles.css @@ -1,17 +1,21 @@ div#hubblesite-daily-image { text-align: center; - border: 1px solid #67201D; - background-color: #F0EEDD; + border: 1px solid #6B7595; + background-color: #191D28; padding: 10px; } +div#hubblesite-daily-image a { + color: #D1D184 +} + div#hubblesite-daily-image a:hover { color: #00d2d3 } div#hubblesite-daily-image img { margin: 5px 0; - border: 1px solid #67201D; + border: 1px solid #6B7595; } div#hubblesite-daily-image a:hover img { @@ -22,7 +26,11 @@ p#hubblesite-daily-image-header { text-align: center; font-size: 12px; font-weight: bold; - color: #666; + color: #9FA6BA; margin: 0; padding: 0; } + +div#hubblesite-daily-image-credits { + color: #CCCCCC +} \ No newline at end of file From 13da78251423ff2b90f857cf4448597b529bf6f5 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 24 Jun 2009 17:28:14 -0400 Subject: [PATCH 3/8] rip out non-WP styled code and fix widow issue --- classes/DailyImageWidget.php | 32 +++++++++++++++----------------- test/DailyImageWidgetTest.php | 35 +++++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/classes/DailyImageWidget.php b/classes/DailyImageWidget.php index fd911fd..a4193db 100644 --- a/classes/DailyImageWidget.php +++ b/classes/DailyImageWidget.php @@ -4,8 +4,7 @@ class DailyImageWidget { function DailyImageWidget($skip_load_data = false) { $this->default_display_options = array( 'title', - 'image', - 'styles' + 'image' ); $this->_cache_time = 86400; @@ -18,8 +17,7 @@ class DailyImageWidget { $this->_valid_options = array( "image" => __("Daily Image", "hubblesite-daily-image-widget"), "title" => __("Image Title", "hubblesite-daily-image-widget"), - "credits" => __("Credits", "hubblesite-daily-image-widget"), - "styles" => __("HubbleSite Styles", "hubblesite-daily-image-widget"), + "credits" => __("Credits", "hubblesite-daily-image-widget") ); add_action('init', array($this, "_init")); @@ -103,13 +101,15 @@ class DailyImageWidget { /** * Render the widget. */ - function render() { + function render($args) { if (!empty($this->data) && is_array($this->data)) { + extract($args); $options = $this->get_display_options(); - echo '
'; - echo '

HubbleSite Daily Image

'; - + echo $before_widget; + echo $before_title; + echo "HubbleSite Daily Image"; + echo $after_title; if (in_array("image", $options)) { echo ''; echo '' . $this->data['title'] . ''; @@ -118,22 +118,16 @@ class DailyImageWidget { if (in_array("title", $options)) { echo ''; - echo $this->data['title']; + echo $this->_fix_widows($this->data['title']); echo ''; } if (in_array("credits", $options)) { echo '
'; - echo $this->data['credits']; + echo $this->_fix_widows($this->data['credits']); echo '
'; } - echo '
'; - - if (in_array("styles", $options)) { - echo ""; - } + echo $after_widget; } } @@ -240,6 +234,10 @@ class DailyImageWidget { } return false; } + + function _fix_widows($text) { + return preg_replace("#([^\ ]+)\ ([^\ \>]+)($|

|)#", '\1 \2\3', $text); + } } function the_hubblesite_daily_image_widget() { diff --git a/test/DailyImageWidgetTest.php b/test/DailyImageWidgetTest.php index 74e461f..3cbed05 100644 --- a/test/DailyImageWidgetTest.php +++ b/test/DailyImageWidgetTest.php @@ -59,27 +59,21 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { array( "image", array( - '//div[@id="hubblesite-daily-image"]' => true, - '//div/a[@href="gallery_url" and @title="title"]' => true, - '//div/a/img[@src="image_url" and @alt="title"]' => true, + '//div[@id="hubblesite-daily-image"]' => false, + '//a[@href="gallery_url" and @title="title"]' => true, + '//a/img[@src="image_url" and @alt="title"]' => true, ) ), array( "title", array( - '//div/a[@href="gallery_url" and @id="hubblesite-daily-image-title"]' => "title" - ) - ), - array( - "styles", - array( - '//style[@type="text/css"]' => true + '//a[@href="gallery_url" and @id="hubblesite-daily-image-title"]' => "title" ) ), array( "credits", array( - '//div/div[@id="hubblesite-daily-image-credits"]' => 'credits' + '//div[@id="hubblesite-daily-image-credits"]' => 'credits' ) ) ); @@ -92,7 +86,12 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { update_option('hubblesite-daily-image-options', $option_string); ob_start(); - $this->diw->render(); + $this->diw->render(array( + 'before_widget' => "", + 'after_widget' => "", + 'before_title' => "", + 'after_title' => "" + )); $result = ob_get_clean(); $this->assertTrue(!empty($result)); @@ -105,8 +104,8 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { function providerTestGetDisplayOptions() { return array( - array("", array("title", "image", "styles")), - array("meow", array("title", "image", "styles")), + array("", array("title", "image")), + array("meow", array("title", "image")), array("title", array("title")), array("title,image", array("title", "image")), array("title,meow", array("title")) @@ -302,6 +301,14 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { $this->assertTrue($diw->_load_data()); $this->assertTrue(is_array(get_option('hubblesite-daily-image-cache'))); } + + function testWidowProtection() { + $this->assertEquals("this is fixed", $this->diw->_fix_widows("this is fixed")); + $this->assertEquals("

this is fixed

", $this->diw->_fix_widows("

this is fixed

")); + $this->assertEquals("this is fixed", $this->diw->_fix_widows("this is fixed")); + $this->assertEquals("word", $this->diw->_fix_widows("word")); + $this->assertEquals("

this is fixed

Also fixed

", $this->diw->_fix_widows("

this is fixed

Also fixed

")); + } } ?> \ No newline at end of file From cb4c7cd68e2041904227dd14e74ebd79eaf13d38 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 24 Jun 2009 17:50:33 -0400 Subject: [PATCH 4/8] add documentation --- classes/DailyImageWidget.php | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/classes/DailyImageWidget.php b/classes/DailyImageWidget.php index a4193db..f5fc529 100644 --- a/classes/DailyImageWidget.php +++ b/classes/DailyImageWidget.php @@ -1,6 +1,14 @@ default_display_options = array( 'title', @@ -31,6 +39,9 @@ class DailyImageWidget { } } + /** + * WordPress init hook. + */ 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")); @@ -39,6 +50,9 @@ class DailyImageWidget { $this->get_display_options(); } + /** + * Display a warning if the connection failed. + */ function _connection_warning() { echo "
"; _e("HubbleSite Daily Image Widget was unable to retrieve new data from HubbleSite.", "hubblesite-daily-image-widget"); @@ -46,10 +60,20 @@ class DailyImageWidget { echo "
"; } + /** + * Wrapper around a remote data call for unit testing purposes. + * @return string The data from the remote source. + */ function _get_from_data_source() { return @file_get_contents($this->data_source); } + /** + * Load the remote data into the object. + * This will try to pull from cache and, if necessary, retrieve and parse the XML from the + * remote server. If any of this fails, returns false. + * @return boolean True if data could be loaded, false otherwise. + */ function _load_data() { if (($result = $this->_get_cached_data()) === false) { if (($xml_text = $this->_get_from_data_source()) !== false) { @@ -64,6 +88,9 @@ class DailyImageWidget { } } + /** + * Handle updating the widget options. + */ function handle_post() { if (isset($_POST['hubblesite']['_wpnonce'])) { if (wp_verify_nonce($_POST['hubblesite']['_wpnonce'], 'hubble')) { @@ -100,6 +127,7 @@ class DailyImageWidget { /** * Render the widget. + * @param array $args The theme's widget layout arguments. */ function render($args) { if (!empty($this->data) && is_array($this->data)) { @@ -131,6 +159,9 @@ class DailyImageWidget { } } + /** + * Render the widget admin UI. + */ function render_ui() { echo ""; echo "

"; @@ -148,6 +179,9 @@ class DailyImageWidget { /** * Parse a string of XML from the HubbleSite Daily Gallery Image feed. + * This will try to use SimpleXML if vailable. If not, will fall back on Expat. + * @param string $xml_text The text to parse. + * @return array|boolean The retrieved data, or false on failure. */ function parse_xml($xml_text) { if ($this->has_simplexml) { @@ -195,10 +229,16 @@ class DailyImageWidget { return $this->data; } + /** + * Expat start element handler. + */ function _start_element_handler($parser, $name, $attributes) { $this->_character_data = ""; } + /** + * Expat end element handler. + */ function _end_element_handler($parser, $name) { $name = strtolower($name); if (in_array($name, $this->_valid_column_names)) { @@ -208,10 +248,17 @@ class DailyImageWidget { $this->_character_data = ""; } + /** + * Expat character data handler. + */ function _character_data_handler($parser, $data) { $this->_character_data .= $data; } + /** + * Retrieve the cached data from WP Options. + * @return array|boolean The cached data or false upon failure. + */ function _get_cached_data() { $result = get_option('hubblesite-daily-image-cache'); @@ -235,6 +282,11 @@ class DailyImageWidget { return false; } + /** + * Try to ensure that no words in a paragraph or link are widowed. + * @param string $text The text to process. + * @return string The processed text. + */ function _fix_widows($text) { return preg_replace("#([^\ ]+)\ ([^\ \>]+)($|

|)#", '\1 \2\3', $text); } From 33ba3f3a421782ab057793fdf7556563f10196d7 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 24 Jun 2009 17:50:49 -0400 Subject: [PATCH 5/8] switch to better option handling --- classes/DailyImageWidget.php | 26 +++++++++++--------------- test/DailyImageWidgetTest.php | 4 ++-- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/classes/DailyImageWidget.php b/classes/DailyImageWidget.php index f5fc529..1ebfda1 100644 --- a/classes/DailyImageWidget.php +++ b/classes/DailyImageWidget.php @@ -260,22 +260,18 @@ class DailyImageWidget { * @return array|boolean The cached data or false upon failure. */ function _get_cached_data() { - $result = get_option('hubblesite-daily-image-cache'); - - if (is_string($result)) { - if (($data = @unserialize($result)) !== false) { - list($timestamp, $cached_data) = $data; + if (($result = get_option('hubblesite-daily-image-cache')) !== false) { + list($timestamp, $cached_data) = $result; + + if (($timestamp + $this->_cache_time) > time()) { + $is_valid = true; + foreach ($this->_valid_column_names as $field) { + if (!isset($cached_data[$field])) { $is_valid = false; break; } + } - if (($timestamp + $this->_cache_time) > time()) { - $is_valid = true; - foreach ($this->_valid_column_names as $field) { - if (!isset($cached_data[$field])) { $is_valid = false; break; } - } - - if ($is_valid) { - $this->data = $cached_data; - return $cached_data; - } + if ($is_valid) { + $this->data = $cached_data; + return $cached_data; } } } diff --git a/test/DailyImageWidgetTest.php b/test/DailyImageWidgetTest.php index 3cbed05..2f503cb 100644 --- a/test/DailyImageWidgetTest.php +++ b/test/DailyImageWidgetTest.php @@ -256,11 +256,11 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { function testGetCachedData() { $test_time = time() + 86500; - update_option('hubblesite-daily-image-cache', serialize(array($test_time, $this->sample_data))); + update_option('hubblesite-daily-image-cache', array($test_time, $this->sample_data)); $this->assertEquals($this->sample_data, $this->diw->_get_cached_data()); $test_time = time() - 86500; - update_option('hubblesite-daily-image-cache', serialize(array($test_time, $this->sample_data))); + update_option('hubblesite-daily-image-cache', array($test_time, $this->sample_data)); $this->assertEquals(false, $this->diw->_get_cached_data()); update_option('hubblesite-daily-image-cache', null); From 1a48521ba9189db250d2a38a13ded435cc12e38d Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 29 Jun 2009 11:34:47 -0400 Subject: [PATCH 6/8] refactor test --- test/DailyImageWidgetTest.php | 51 +++++++++++++++-------------------- 1 file changed, 22 insertions(+), 29 deletions(-) diff --git a/test/DailyImageWidgetTest.php b/test/DailyImageWidgetTest.php index 2f503cb..07c7796 100644 --- a/test/DailyImageWidgetTest.php +++ b/test/DailyImageWidgetTest.php @@ -267,39 +267,32 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { $this->assertEquals(false, $this->diw->_get_cached_data()); } - function testLoadData() { + function providerTestLoadData() { + return array( + array(true, null, null, true), + array(false, false, null, false), + array(false, true, false, false), + array(false, true, true, true) + ); + } + + /** + * @dataProvider providerTestLoadData + */ + function testLoadData($get_cached_data, $get_from_data_source, $parse_xml_result, $expected_return) { $diw = $this->getMock('DailyImageWidget', array('_get_from_data_source', '_get_cached_data', 'parse_xml')); - $diw->expects($this->once())->method('_get_cached_data')->will($this->returnValue(false)); - $diw->expects($this->once())->method('_get_from_data_source')->will($this->returnValue(false)); - _reset_wp(); - $this->assertFalse($diw->_load_data()); - $this->assertFalse(is_array(get_option('hubblesite-daily-image-cache'))); - - $diw = $this->getMock('DailyImageWidget', array('_get_from_data_source', '_get_cached_data', 'parse_xml')); - $diw->expects($this->once())->method('_get_cached_data')->will($this->returnValue(true)); - _reset_wp(); + $diw->expects($this->once())->method('_get_cached_data')->will($this->returnValue($get_cached_data)); + if ($get_cached_data == false) { + $diw->expects($this->once())->method('_get_from_data_source')->will($this->returnValue($get_from_data_source)); + if ($get_from_data_source) { + $diw->expects($this->once())->method('parse_xml')->will($this->returnValue($parse_xml_result)); + } + } - $this->assertTrue($diw->_load_data()); - $this->assertFalse(is_array(get_option('hubblesite-daily-image-cache'))); - - $diw = $this->getMock('DailyImageWidget', array('_get_from_data_source', '_get_cached_data', 'parse_xml')); - $diw->expects($this->once())->method('_get_cached_data')->will($this->returnValue(false)); - $diw->expects($this->once())->method('_get_from_data_source')->will($this->returnValue(true)); - $diw->expects($this->once())->method('parse_xml')->will($this->returnValue(false)); - _reset_wp(); + $this->assertEquals($expected_return, $diw->_load_data()); - $this->assertFalse($diw->_load_data()); - $this->assertFalse(is_array(get_option('hubblesite-daily-image-cache'))); - - $diw = $this->getMock('DailyImageWidget', array('_get_from_data_source', '_get_cached_data', 'parse_xml')); - $diw->expects($this->once())->method('_get_cached_data')->will($this->returnValue(false)); - $diw->expects($this->once())->method('_get_from_data_source')->will($this->returnValue(true)); - $diw->expects($this->once())->method('parse_xml')->will($this->returnValue(true)); - _reset_wp(); - - $this->assertTrue($diw->_load_data()); - $this->assertTrue(is_array(get_option('hubblesite-daily-image-cache'))); + $this->assertEquals($parse_xml_result, is_array(get_option('hubblesite-daily-image-cache'))); } function testWidowProtection() { From 756f90e6cf3b1a06211206b413d54e238095a001 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 29 Jun 2009 11:42:57 -0400 Subject: [PATCH 7/8] refactor test --- test/DailyImageWidgetTest.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/test/DailyImageWidgetTest.php b/test/DailyImageWidgetTest.php index 07c7796..425ff02 100644 --- a/test/DailyImageWidgetTest.php +++ b/test/DailyImageWidgetTest.php @@ -295,12 +295,22 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { $this->assertEquals($parse_xml_result, is_array(get_option('hubblesite-daily-image-cache'))); } - function testWidowProtection() { - $this->assertEquals("this is fixed", $this->diw->_fix_widows("this is fixed")); - $this->assertEquals("

this is fixed

", $this->diw->_fix_widows("

this is fixed

")); - $this->assertEquals("this is fixed", $this->diw->_fix_widows("this is fixed")); - $this->assertEquals("word", $this->diw->_fix_widows("word")); - $this->assertEquals("

this is fixed

Also fixed

", $this->diw->_fix_widows("

this is fixed

Also fixed

")); + + function providerTestWidowProtection() { + return array( + array("this is fixed", "this is fixed"), + array("

this is fixed

" ,"

this is fixed

"), + array("this is fixed", "this is fixed"), + array("word", "word"), + array("

this is fixed

Also fixed

", '

this is fixed

Also fixed

') + ); + } + + /** + * @dataProvider providerTestWidowProtection + */ + function testWidowProtection($source, $result) { + $this->assertEquals($result, $this->diw->_fix_widows($source)); } } From cbc6346ba60c9a31f4acacdd99576dc003677845 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 29 Jun 2009 12:50:55 -0400 Subject: [PATCH 8/8] refactor test --- test/DailyImageWidgetTest.php | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/test/DailyImageWidgetTest.php b/test/DailyImageWidgetTest.php index 425ff02..bba3ed8 100644 --- a/test/DailyImageWidgetTest.php +++ b/test/DailyImageWidgetTest.php @@ -254,17 +254,25 @@ class DailyImageWidgetTest extends PHPUnit_Framework_TestCase { } } - function testGetCachedData() { - $test_time = time() + 86500; - update_option('hubblesite-daily-image-cache', array($test_time, $this->sample_data)); - $this->assertEquals($this->sample_data, $this->diw->_get_cached_data()); - - $test_time = time() - 86500; - update_option('hubblesite-daily-image-cache', array($test_time, $this->sample_data)); - $this->assertEquals(false, $this->diw->_get_cached_data()); - - update_option('hubblesite-daily-image-cache', null); - $this->assertEquals(false, $this->diw->_get_cached_data()); + function providerTestGetCachedData() { + return array( + array(time() + 86500, true), + array(time() - 86500, false), + array(null, false) + ); + } + + /** + * @dataProvider providerTestGetCachedData + */ + function testGetCachedData($test_time, $has_sample_data) { + if (!is_null($test_time)) { + update_option('hubblesite-daily-image-cache', array($test_time, $this->sample_data)); + } else { + update_option('hubblesite-daily-image-cache', null); + } + + $this->assertEquals($has_sample_data ? $this->sample_data : false, $this->diw->_get_cached_data()); } function providerTestLoadData() {