From 8abc13692d89f7cc0d48a0ab0a9d88eaa77553ed Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 29 Jun 2009 06:57:05 -0400 Subject: [PATCH] finish all tests, move widget rendering back to PW --- classes/PluginWonderful.php | 49 ++++++++++++------- classes/PluginWonderfulWidget.php | 22 ++------- test/PluginWonderfulTest.php | 78 +++++++++++++++++++++++++----- test/PluginWonderfulWidgetTest.php | 43 ++-------------- 4 files changed, 104 insertions(+), 88 deletions(-) diff --git a/classes/PluginWonderful.php b/classes/PluginWonderful.php index 828d178..2c41d82 100644 --- a/classes/PluginWonderful.php +++ b/classes/PluginWonderful.php @@ -273,33 +273,27 @@ class PluginWonderful { } function handle_action_change_memberid() { + update_option('plugin-wonderful-memberid', ""); if (trim($_POST['memberid'])) { if (trim($_POST['memberid']) === (string)(int)$_POST['memberid']) { - if (($result = file_get_contents(sprintf(PLUGIN_WONDERFUL_XML_URL, (int)$_POST['memberid']))) !== false) { - $this->publisher_info = new PublisherInfo(); - if ($this->publisher_info->parse($result)) { - update_option('plugin-wonderful-memberid', (int)$_POST['memberid']); - $this->adboxes_client->post_ads($this->publisher_info); + update_option('plugin-wonderful-memberid', (int)$_POST['memberid']); + switch ($this->_download_project_wonderful_data((int)$_POST['memberid'])) { + case $this->message_types['DOWNLOADED']: $this->messages[] = sprintf(__('Member number changed to %s and adbox information redownloaded.', 'plugin-wonderful'), (int)$_POST['memberid']); - } else { + break; + case $this->message_types['CANT_PARSE']: $this->messages[] = __("Unable to parse publisher data from Project Wonderful.", 'plugin-wonderful'); - update_option('plugin-wonderful-memberid', ""); - $this->publisher_info = false; - } - } else { - $this->messages[] = __("Unable to read publisher data from Project Wonderful.", 'plugin-wonderful'); - update_option('plugin-wonderful-memberid', ""); - $this->publisher_info = false; + break; + case $this->message_types['CANT_READ']: + $this->messages[] = __("Unable to read publisher data from Project Wonderful.", 'plugin-wonderful'); + break; } } else { $this->messages[] = __("Member numbers need to be numeric.", 'plugin-wonderful'); - update_option('plugin-wonderful-memberid', ""); $this->publisher_info = false; } } else { $this->messages[] = __("Existing adbox information removed.", 'plugin-wonderful'); - update_option('plugin-wonderful-memberid', ""); - $this->publisher_info = false; } @@ -307,11 +301,30 @@ class PluginWonderful { update_option("plugin-wonderful-${field}", isset($_POST[$field]) ? "1" : "0"); } } + + function _render_adbox($adboxid, $center = false) { + if ($this->publisher_info !== false) { + foreach ($this->publisher_info->adboxes as $adbox) { + if (($adbox->adboxid == $adboxid) || ($adbox->template_tag_id == $adboxid)) { + if (get_option("plugin-wonderful-use-standardcode") == 1) { + $output = $adbox->standardcode; + } else { + $output = $adbox->advancedcode; + } + if ($center == 1) { + $output = "
{$output}
"; + } + echo $output; + break; + } + } + } + } } function the_project_wonderful_ad($adboxid) { - $w = new PluginWonderfulWidget(); - $w->widget(array(), array('adboxid' => $adboxid)); + global $plugin_wonderful; + $plugin_wonderful->_render_adbox($adboxid); } ?> diff --git a/classes/PluginWonderfulWidget.php b/classes/PluginWonderfulWidget.php index f2fcb8e..42d9436 100644 --- a/classes/PluginWonderfulWidget.php +++ b/classes/PluginWonderfulWidget.php @@ -15,25 +15,9 @@ class PluginWonderfulWidget extends WP_Widget { } function widget($args, $instance) { - global $plugin_wonderful; - - if ($plugin_wonderful->publisher_info !== false) { - foreach ($plugin_wonderful->publisher_info->adboxes as $adbox) { - if (($adbox->adboxid == $instance['adboxid']) || ($adbox->template_tag_id == $instance['adboxid'])) { - if (get_option("plugin-wonderful-use-standardcode") == 1) { - $output = $adbox->standardcode; - } else { - $output = $adbox->advancedcode; - } - if ($instance['center'] == 1) { - $output = "
{$output}
"; - } - echo $output; - break; - } - } - } - } + global $plugin_wonderful; + $plugin_wonderful->_render_adbox($instance['adboxid'], $instance['center']); + } function form($instance) { global $plugin_wonderful; diff --git a/test/PluginWonderfulTest.php b/test/PluginWonderfulTest.php index 1073df7..d227ded 100644 --- a/test/PluginWonderfulTest.php +++ b/test/PluginWonderfulTest.php @@ -111,18 +111,10 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { function testTemplateTag() { global $plugin_wonderful; - - $plugin_wonderful = $this->getMock('PluginWonderful'); - - $plugin_wonderful->publisher_info = (object)array( - 'adboxes' => array( - (object)array('adboxid' => '123', 'advancedcode' => "test", 'standardcode' => "not-test") - ) - ); - - ob_start(); + + $plugin_wonderful = $this->getMock('PluginWonderful', array('_render_adbox')); + $plugin_wonderful->expects($this->once())->method('_render_adbox'); the_project_wonderful_ad('123'); - $this->assertEquals("test", ob_get_clean()); } function providerInsertAdsIntoRSS() { @@ -399,8 +391,68 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { $pw->handle_action_rebuild_database(); } - function testHandleActionChangeMemberID() { - $this->markTestIncomplete(); + function providerTestHandleActionChangeMemberID() { + return array( + array("", false), + array("1.5", false), + array("a", false), + array("1", true) + ); + } + + /** + * @dataProvider providerTestHandleActionChangeMemberID + */ + function testHandleActionChangeMemberID($member_id, $is_downloaded) { + $_POST['memberid'] = $member_id; + + $pw = $this->getMock('PluginWonderful', array("_download_project_wonderful_data")); + if ($is_downloaded) { + $pw->expects($this->once())->method('_download_project_wonderful_data'); + } else { + $pw->expects($this->never())->method('_download_project_wonderful_data'); + } + + $pw->handle_action_change_memberid(); + } + + function providerTestRenderAdbox() { + return array( + array(false, null, null, null, ""), + array(true, null, null, null, ""), + array(true, "123", 0, null, "advanced"), + array(true, "123", 1, null, "standard"), + array(true, "abc", 1, null, "standard"), + array(true, "abc", 1, 1, "
standard
") + ); + } + + /** + * @dataProvider providerTestRenderAdbox + */ + function testRenderAdbox($has_publisher_info, $requested_adboxid, $use_standardcode, $center_widget, $expected_result) { + global $plugin_wonderful; + + $test_ad = (object)array( + 'adboxid' => '123', + 'template_tag_id' => 'abc', + 'standardcode' => 'standard', + 'advancedcode' => 'advanced' + ); + + if ($has_publisher_info) { + $this->pw->publisher_info = (object)array( + 'adboxes' => array($test_ad) + ); + + update_option("plugin-wonderful-use-standardcode", $use_standardcode); + } else { + $this->pw->publisher_info = false; + } + + ob_start(); + $this->pw->_render_adbox($requested_adboxid, $center_widget); + $this->assertEquals($expected_result, ob_get_clean()); } } diff --git a/test/PluginWonderfulWidgetTest.php b/test/PluginWonderfulWidgetTest.php index 220db9d..d3e3896 100644 --- a/test/PluginWonderfulWidgetTest.php +++ b/test/PluginWonderfulWidgetTest.php @@ -16,48 +16,15 @@ class PluginWonderfulWidgetTest extends PHPUnit_Framework_TestCase { $this->w = new PluginWonderfulWidget(); $this->assertEquals("Plugin Wonderful", $wp_test_expectations['wp_widgets']['plugin-wonderful']['name']); } - - function providerTestRenderWidget() { - return array( - array(false, null, null, null, ""), - array(true, null, null, null, ""), - array(true, "123", 0, null, "advanced"), - array(true, "123", 1, null, "standard"), - array(true, "abc", 1, null, "standard"), - array(true, "abc", 1, 1, "
standard
") - ); - } - - /** - * @dataProvider providerTestRenderWidget - */ - function testRenderWidget($has_publisher_info, $requested_adboxid, $use_standardcode, $center_widget, $expected_result) { + + function testRenderWidget() { global $plugin_wonderful; - $plugin_wonderful = $this->getMock('PluginWonderful'); + $plugin_wonderful = $this->getMock('PluginWonderful', array('_render_adbox')); + $plugin_wonderful->expects($this->once())->method('_render_adbox'); - $test_ad = (object)array( - 'adboxid' => '123', - 'template_tag_id' => 'abc', - 'standardcode' => 'standard', - 'advancedcode' => 'advanced' - ); - - if ($has_publisher_info) { - $plugin_wonderful->publisher_info = (object)array( - 'adboxes' => array($test_ad) - ); - - update_option("plugin-wonderful-use-standardcode", $use_standardcode); - } else { - $plugin_wonderful->publisher_info = false; - } - - ob_start(); - $this->w->widget(array(), array('adboxid' => $requested_adboxid, 'center' => $center_widget)); - $this->assertEquals($expected_result, ob_get_clean()); + $this->w->widget(array(), array()); } - function testRenderWidgetControl() { global $plugin_wonderful; $plugin_wonderful = $this->getMock('PluginWonderful');