From 4a0719c9546eee97eb13b9d9275c7ee75ca27bef Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 26 Jun 2009 06:53:10 -0400 Subject: [PATCH] finish widget form --- classes/PluginWonderful.php | 36 --------------- classes/PluginWonderfulWidget.php | 48 +++++++++++++++++++- test/PluginWonderfulTest.php | 59 ------------------------- test/PluginWonderfulWidgetTest.php | 71 ++++++++++++++++++++++++++++-- 4 files changed, 113 insertions(+), 101 deletions(-) diff --git a/classes/PluginWonderful.php b/classes/PluginWonderful.php index 98a5c18..9b23faf 100644 --- a/classes/PluginWonderful.php +++ b/classes/PluginWonderful.php @@ -18,8 +18,6 @@ class PluginWonderful { $this->_get_publisher_info(); $this->_update_database_version(); - $this->set_up_widgets(); - if (!empty($_POST)) { $this->handle_action(); } } } @@ -93,25 +91,6 @@ class PluginWonderful { if (!empty($result)) { echo $result; } } - function render_widget($adboxid) { - 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 ($adbox->center_widget == 1) { - $output = "
{$output}
"; - } - echo $output; - break; - } - } - } - } - function inject_ads_into_body_copy($body) { if ($this->publisher_info !== false) { if (get_option("plugin-wonderful-enable-body-copy-embedding") == 1) { @@ -125,21 +104,6 @@ class PluginWonderful { add_options_page('Plugin Wonderful', __("Plugin Wonderful", 'plugin-wonderful'), 5, __FILE__, array($this, "plugin_wonderful_main")); } - function render_widget_control($adboxid) { - if ($this->publisher_info !== false) { - foreach ($this->publisher_info->adboxes as $box) { - if ($box->adboxid == $adboxid) { - echo ''; - echo ''; - break; - } - } - } - } - function handle_activation() { $this->init(); $this->adboxes_client->initialize(); diff --git a/classes/PluginWonderfulWidget.php b/classes/PluginWonderfulWidget.php index e7708cb..59f159c 100644 --- a/classes/PluginWonderfulWidget.php +++ b/classes/PluginWonderfulWidget.php @@ -16,9 +16,53 @@ class PluginWonderfulWidget extends WP_Widget { function widget($args, $instance) { global $plugin_wonderful; - - $plugin_wonderful->render_widget($instance['adboxid']); + + 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; + } + } + } } + + function form($instance) { + global $plugin_wonderful; + + if ($plugin_wonderful->publisher_info !== false) { + echo '

'; + echo 'Select an adbox:'; + foreach ($plugin_wonderful->publisher_info->adboxes as $box) { + echo '"; + } + echo '

'; + + echo '

'; + echo ''; + echo '

'; + } + } } ?> \ No newline at end of file diff --git a/test/PluginWonderfulTest.php b/test/PluginWonderfulTest.php index e807699..8f56fc6 100644 --- a/test/PluginWonderfulTest.php +++ b/test/PluginWonderfulTest.php @@ -24,27 +24,6 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { $pw->handle_action(); } - function testRenderWidgetControl() { - _set_valid_nonce("plugin-wonderful", "12345"); - - $this->pw->publisher_info->adboxes = array( - (object)array('adboxid' => '123', - 'center_widget' => 0) - ); - - ob_start(); - $this->pw->render_widget_control('123'); - $source = ob_get_clean(); - - $this->assertTrue(($xml = _to_xml($source)) !== false); - - foreach (array( - '//input[@name="pw[_nonce]" and @value="12345"]' => true - ) as $xpath => $value) { - $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); - } - } - function testHandleActivation() { $pw = $this->getMock('PluginWonderful', array('init')); $pw->adboxes_client = $this->getMock('PWAdboxesClient', array('initialize')); @@ -179,44 +158,6 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { } } } - - 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) { - $test_ad = (object)array( - 'adboxid' => '123', - 'template_tag_id' => 'abc', - 'standardcode' => 'standard', - 'advancedcode' => 'advanced', - 'center_widget' => $center_widget - ); - - 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_widget($requested_adboxid); - $this->assertEquals($expected_result, ob_get_clean()); - } } ?> \ No newline at end of file diff --git a/test/PluginWonderfulWidgetTest.php b/test/PluginWonderfulWidgetTest.php index 59c4adc..8b24ef1 100644 --- a/test/PluginWonderfulWidgetTest.php +++ b/test/PluginWonderfulWidgetTest.php @@ -16,10 +16,73 @@ class PluginWonderfulWidgetTest extends PHPUnit_Framework_TestCase { $this->w = new PluginWonderfulWidget(); $this->assertEquals("Plugin Wonderful", $wp_test_expectations['wp_widgets']['plugin-wonderful']['name']); } - - function testWidget() { - - } + + 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) { + global $plugin_wonderful; + $plugin_wonderful = $this->getMock('PluginWonderful'); + + $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()); + } + + + function testRenderWidgetControl() { + global $plugin_wonderful; + $plugin_wonderful = $this->getMock('PluginWonderful'); + + $plugin_wonderful->publisher_info->adboxes = array( + (object)array('adboxid' => '123'), + (object)array('adboxid' => '234'), + (object)array('adboxid' => '345'), + ); + + ob_start(); + $this->w->form(array('adboxid' => '123', 'center' => 0)); + $source = ob_get_clean(); + + $this->assertTrue(($xml = _to_xml($source)) !== false); + + foreach (array( + '//input[@type="radio" and @name="' . $this->w->get_field_name('adboxid') . '" and @value="123" and @checked="checked"]' => true, + '//input[@type="radio" and @name="' . $this->w->get_field_name('adboxid') . '" and @value="234" and not(@checked="checked")]' => true, + '//input[@type="radio" and @name="' . $this->w->get_field_name('adboxid') . '" and @value="345" and not(@checked="checked")]' => true, + '//input[@type="checkbox" and @name="' . $this->w->get_field_name('center') . '" and @value="1" and not(@checked="checked")]' => true + ) as $xpath => $value) { + $this->assertTrue(_xpath_test($xml, $xpath, $value), $xpath); + } + } } ?> \ No newline at end of file