From bd2ce48b0d33272826e346a310ef4a2d35d37aae Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sun, 28 Jun 2009 22:32:17 -0400 Subject: [PATCH] refactor and set up testing for adbox settings changes --- classes/PluginWonderful.php | 64 +++++++++++++----------- test/PluginWonderfulTest.php | 96 +++++++++++++++++++++++++++++++++++- 2 files changed, 129 insertions(+), 31 deletions(-) diff --git a/classes/PluginWonderful.php b/classes/PluginWonderful.php index 79cf1af..bbeb2be 100644 --- a/classes/PluginWonderful.php +++ b/classes/PluginWonderful.php @@ -173,59 +173,65 @@ class PluginWonderful { } function handle_action_change_adbox_settings() { - if ($member_id = get_option('plugin-wonderful-memberid')) { - if (isset($_POST['template_tag_id']) && is_array($_POST['template_tag_id'])) { - if (is_array($this->publisher_info->adboxes)) { - $new_boxes = array(); - foreach ($this->publisher_info->adboxes as $box) { - if (isset($_POST['template_tag_id'][$box->adboxid])) { - $tag = $_POST['template_tag_id'][$box->adboxid]; - $prior_value = $box->template_tag_id; - - $tag = $this->adboxes_client->trim_field('template_tag_id', $tag); - - $this->adboxes_client->set_template_tag($box->adboxid, $tag); - $box->template_tag_id = $tag; - - if (!empty($tag) && ($prior_value != $tag)) { - $this->messages[] = sprintf(__('Template tag identifier for ad %1$s set to %2$s.', 'plugin-wonderful'), $box->adboxid, $tag); - } else { - if (!empty($prior_value) && empty($tag)) { - $this->messages[] = sprintf(__('Template tag identifier for ad %s removed.', 'plugin-wonderful'), $box->adboxid); - } - } - } - $new_boxes[] = $box; - } - $this->publisher_info->adboxes = $new_boxes; - } - } - + $member_id = get_option('plugin-wonderful-memberid'); + if (is_numeric($member_id)) { + $changes = array( + 'template_tag_id' => array(), + 'in_rss_feed' => array() + ); + if (is_array($this->publisher_info->adboxes)) { $new_boxes = array(); foreach ($this->publisher_info->adboxes as $box) { - if (isset($_POST['in_rss_feed'][$box->adboxid])) { + if (isset($_POST['template_tag_id'][$box->adboxid])) { + $tag = $_POST['template_tag_id'][$box->adboxid]; + $prior_value = $box->template_tag_id; + + $tag = $this->adboxes_client->trim_field('template_tag_id', $tag); + + $this->adboxes_client->set_template_tag($box->adboxid, $tag); + $box->template_tag_id = $tag; + + if (!empty($tag) && ($prior_value != $tag)) { + $this->messages[] = sprintf(__('Template tag identifier for ad %1$s set to %2$s.', 'plugin-wonderful'), $box->adboxid, $tag); + $changes['template_tag_id'][$box->adboxid] = "set"; + } else { + if (!empty($prior_value) && empty($tag)) { + $this->messages[] = sprintf(__('Template tag identifier for ad %s removed.', 'plugin-wonderful'), $box->adboxid); + $changes['template_tag_id'][$box->adboxid] = "removed"; + } + } + } + + if (!empty($_POST['in_rss_feed'][$box->adboxid])) { $this->adboxes_client->set_rss_feed_usage($box->adboxid, true); if ($box->in_rss_feed == 0) { $this->messages[] = sprintf(__('RSS feed usage for ad %1$s enabled.', 'plugin-wonderful'), $box->adboxid); + $changes['in_rss_feed'][$box->adboxid] = "enabled"; } $box->in_rss_feed = "1"; } else { $this->adboxes_client->set_rss_feed_usage($box->adboxid, false); if ($box->in_rss_feed == 1) { $this->messages[] = sprintf(__('RSS feed usage for ad %1$s disabled.', 'plugin-wonderful'), $box->adboxid); + $changes['in_rss_feed'][$box->adboxid] = "disabled"; } $box->in_rss_feed = "0"; } + $new_boxes[] = $box; } + $this->publisher_info->adboxes = $new_boxes; } + } else { + return null; } if (count($this->messages) == 0) { $this->messages[] = __("No changes to adboxes were made.", 'plugin-wonderful'); } + return $changes; } function handle_action_rebuild_database() { diff --git a/test/PluginWonderfulTest.php b/test/PluginWonderfulTest.php index 65b760c..785be47 100644 --- a/test/PluginWonderfulTest.php +++ b/test/PluginWonderfulTest.php @@ -293,8 +293,100 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { $pw->handle_action(); } - function testHandleActionChangeAdboxSettings() { - $this->markTestIncomplete(); + function providerTestHandleActionChangeAdboxSettings() { + return array( + array(false), + array(true), + ); + } + + /** + * @dataProvider providerTestHandleActionChangeAdboxSettings + */ + function testHandleActionChangeAdboxSettings($member_id_valid) { + $pw = new PluginWonderful(); + + if ($member_id_valid) { + update_option('plugin-wonderful-memberid', '1'); + } + + if ($member_id_valid) { + foreach (array(false, true) as $had_template_tag_id) { + foreach (array("null", "no", "yes", "remove") as $new_template_tag_id) { + foreach (array(false, true) as $was_in_rss_feed) { + foreach (array("null", "no", "yes") as $now_in_rss_feed) { + $pw->publisher_info = (object)array( + 'adboxes' => array( + (object)array( + 'adboxid' => '123', + 'template_tag_id' => ($had_template_tag_id ? "test" : ""), + 'in_rss_feed' => ($was_in_rss_feed ? "1" : "0") + ) + ) + ); + + $pw->adboxes_client = $this->getMock('PWAdboxesClient', array('trim_field', 'set_template_tag', 'set_rss_feed_usage')); + + $_POST['template_tag_id'] = array(); + + switch ($new_template_tag_id) { + case "no": $_POST['template_tag_id']['123'] = "test"; break; + case "yes": $_POST['template_tag_id']['123'] = "test2"; break; + case "remove": $_POST['template_tag_id']['123'] = ""; break; + } + + if ($new_template_tag_id !== "null") { + $pw->adboxes_client->expects($this->once())->method('trim_field')->with('template_tag_id', $_POST['template_tag_id']['123'])->will($this->returnValue($_POST['template_tag_id']['123'])); + $pw->adboxes_client->expects($this->once())->method('set_template_tag')->with('123', $_POST['template_tag_id']['123']); + } + + $_POST['in_rss_feed'] = array(); + + switch ($now_in_rss_feed) { + case "no": unset($_POST['in_rss_feed']['123']); break; + case "yes": $_POST['in_rss_feed']['123'] = "1"; break; + } + + $result = $pw->handle_action_change_adbox_settings(); + + if (isset($_POST['template_tag_id']['123'])) { + $this->assertEquals($_POST['template_tag_id']['123'], $pw->publisher_info->adboxes[0]->template_tag_id); + } + + if (isset($_POST['in_rss_feed']['123'])) { + $this->assertEquals($_POST['in_rss_feed']['123'], $pw->publisher_info->adboxes[0]->in_rss_feed); + } + + switch ($new_template_tag_id) { + case "yes": + $this->assertEquals("set", $result['template_tag_id']['123']); + break; + case "remove": + if ($had_template_tag_id) { + $this->assertEquals("removed", $result['template_tag_id']['123']); + } + break; + } + + switch ($now_in_rss_feed) { + case "no": + if ($was_in_rss_feed) { + $this->assertEquals("disabled", $result['in_rss_feed']['123']); + } + break; + case "yes": + if (!$was_in_rss_feed) { + $this->assertEquals("enabled", $result['in_rss_feed']['123']); + } + break; + } + } + } + } + } + } else { + $this->assertTrue(is_null($result)); + } } function testHandleActionRebuildDatabase() {