refactor and set up testing for adbox settings changes

This commit is contained in:
John Bintz 2009-06-28 22:32:17 -04:00
parent fdd4bfe3ac
commit bd2ce48b0d
2 changed files with 129 additions and 31 deletions

View File

@ -173,8 +173,13 @@ class PluginWonderful {
} }
function handle_action_change_adbox_settings() { function handle_action_change_adbox_settings() {
if ($member_id = get_option('plugin-wonderful-memberid')) { $member_id = get_option('plugin-wonderful-memberid');
if (isset($_POST['template_tag_id']) && is_array($_POST['template_tag_id'])) { if (is_numeric($member_id)) {
$changes = array(
'template_tag_id' => array(),
'in_rss_feed' => array()
);
if (is_array($this->publisher_info->adboxes)) { if (is_array($this->publisher_info->adboxes)) {
$new_boxes = array(); $new_boxes = array();
foreach ($this->publisher_info->adboxes as $box) { foreach ($this->publisher_info->adboxes as $box) {
@ -189,43 +194,44 @@ class PluginWonderful {
if (!empty($tag) && ($prior_value != $tag)) { if (!empty($tag) && ($prior_value != $tag)) {
$this->messages[] = sprintf(__('Template tag identifier for ad <strong>%1$s</strong> set to <strong>%2$s</strong>.', 'plugin-wonderful'), $box->adboxid, $tag); $this->messages[] = sprintf(__('Template tag identifier for ad <strong>%1$s</strong> set to <strong>%2$s</strong>.', 'plugin-wonderful'), $box->adboxid, $tag);
$changes['template_tag_id'][$box->adboxid] = "set";
} else { } else {
if (!empty($prior_value) && empty($tag)) { if (!empty($prior_value) && empty($tag)) {
$this->messages[] = sprintf(__('Template tag identifier for ad <strong>%s</strong> removed.', 'plugin-wonderful'), $box->adboxid); $this->messages[] = sprintf(__('Template tag identifier for ad <strong>%s</strong> removed.', 'plugin-wonderful'), $box->adboxid);
$changes['template_tag_id'][$box->adboxid] = "removed";
} }
} }
} }
$new_boxes[] = $box;
}
$this->publisher_info->adboxes = $new_boxes;
}
}
if (is_array($this->publisher_info->adboxes)) { if (!empty($_POST['in_rss_feed'][$box->adboxid])) {
$new_boxes = array();
foreach ($this->publisher_info->adboxes as $box) {
if (isset($_POST['in_rss_feed'][$box->adboxid])) {
$this->adboxes_client->set_rss_feed_usage($box->adboxid, true); $this->adboxes_client->set_rss_feed_usage($box->adboxid, true);
if ($box->in_rss_feed == 0) { if ($box->in_rss_feed == 0) {
$this->messages[] = sprintf(__('RSS feed usage for ad <strong>%1$s</strong> enabled.', 'plugin-wonderful'), $box->adboxid); $this->messages[] = sprintf(__('RSS feed usage for ad <strong>%1$s</strong> enabled.', 'plugin-wonderful'), $box->adboxid);
$changes['in_rss_feed'][$box->adboxid] = "enabled";
} }
$box->in_rss_feed = "1"; $box->in_rss_feed = "1";
} else { } else {
$this->adboxes_client->set_rss_feed_usage($box->adboxid, false); $this->adboxes_client->set_rss_feed_usage($box->adboxid, false);
if ($box->in_rss_feed == 1) { if ($box->in_rss_feed == 1) {
$this->messages[] = sprintf(__('RSS feed usage for ad <strong>%1$s</strong> disabled.', 'plugin-wonderful'), $box->adboxid); $this->messages[] = sprintf(__('RSS feed usage for ad <strong>%1$s</strong> disabled.', 'plugin-wonderful'), $box->adboxid);
$changes['in_rss_feed'][$box->adboxid] = "disabled";
} }
$box->in_rss_feed = "0"; $box->in_rss_feed = "0";
} }
$new_boxes[] = $box; $new_boxes[] = $box;
} }
$this->publisher_info->adboxes = $new_boxes; $this->publisher_info->adboxes = $new_boxes;
} }
} else {
return null;
} }
if (count($this->messages) == 0) { if (count($this->messages) == 0) {
$this->messages[] = __("No changes to adboxes were made.", 'plugin-wonderful'); $this->messages[] = __("No changes to adboxes were made.", 'plugin-wonderful');
} }
return $changes;
} }
function handle_action_rebuild_database() { function handle_action_rebuild_database() {

View File

@ -293,8 +293,100 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase {
$pw->handle_action(); $pw->handle_action();
} }
function testHandleActionChangeAdboxSettings() { function providerTestHandleActionChangeAdboxSettings() {
$this->markTestIncomplete(); 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() { function testHandleActionRebuildDatabase() {