From ce0559649c191d462959a1bc698c0e9b7e198ae1 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Mon, 29 Jun 2009 07:05:41 -0400 Subject: [PATCH] fix update option error --- classes/PluginWonderful.php | 37 ++++++++++++++++++++++-------------- test/PluginWonderfulTest.php | 15 ++++++++++----- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/classes/PluginWonderful.php b/classes/PluginWonderful.php index 2c41d82..cb62ced 100644 --- a/classes/PluginWonderful.php +++ b/classes/PluginWonderful.php @@ -273,33 +273,42 @@ class PluginWonderful { } function handle_action_change_memberid() { - update_option('plugin-wonderful-memberid', ""); - if (trim($_POST['memberid'])) { - if (trim($_POST['memberid']) === (string)(int)$_POST['memberid']) { - 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']); - break; - case $this->message_types['CANT_PARSE']: - $this->messages[] = __("Unable to parse publisher data from Project Wonderful.", 'plugin-wonderful'); - break; - case $this->message_types['CANT_READ']: - $this->messages[] = __("Unable to read publisher data from Project Wonderful.", 'plugin-wonderful'); - break; + $original_member_id = get_option('plugin-wonderful-memberid'); + $trimmed_post_memberid = trim($_POST['memberid']); + if ($trimmed_post_memberid) { + if ($trimmed_post_memberid === (string)(int)$trimmed_post_memberid) { + if ($original_member_id !== $trimmed_post_memberid) { + update_option('plugin-wonderful-memberid', (int)$trimmed_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']); + break; + case $this->message_types['CANT_PARSE']: + $this->messages[] = __("Unable to parse publisher data from Project Wonderful.", 'plugin-wonderful'); + 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'); $this->publisher_info = false; + update_option('plugin-wonderful-memberid', ""); } } else { $this->messages[] = __("Existing adbox information removed.", 'plugin-wonderful'); $this->publisher_info = false; + update_option('plugin-wonderful-memberid', ""); } foreach (array('use-standardcode', 'enable-body-copy-embedding') as $field) { update_option("plugin-wonderful-${field}", isset($_POST[$field]) ? "1" : "0"); } + + if (count($this->messages) == 0) { + $this->messages[] = __("Options updated.", 'plugin-wonderful'); + } } function _render_adbox($adboxid, $center = false) { diff --git a/test/PluginWonderfulTest.php b/test/PluginWonderfulTest.php index d227ded..6dd5226 100644 --- a/test/PluginWonderfulTest.php +++ b/test/PluginWonderfulTest.php @@ -393,18 +393,20 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { function providerTestHandleActionChangeMemberID() { return array( - array("", false), - array("1.5", false), - array("a", false), - array("1", true) + array("", "", false, true), + array("", "1.5", false, true), + array("", "a", false, true), + array("", "1", true, false), + array("1", "1", false, false) ); } /** * @dataProvider providerTestHandleActionChangeMemberID */ - function testHandleActionChangeMemberID($member_id, $is_downloaded) { + function testHandleActionChangeMemberID($original_member_id, $member_id, $is_downloaded, $member_id_blank) { $_POST['memberid'] = $member_id; + update_option('plugin-wonderful-memberid', $original_member_id); $pw = $this->getMock('PluginWonderful', array("_download_project_wonderful_data")); if ($is_downloaded) { @@ -414,6 +416,9 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase { } $pw->handle_action_change_memberid(); + + $result = get_option('plugin-wonderful-memberid'); + $this->assertEquals($member_id_blank, empty($result)); } function providerTestRenderAdbox() {