rebuild database and refactor some code
This commit is contained in:
parent
bd2ce48b0d
commit
2f9ab57bbd
@ -6,6 +6,12 @@ class PluginWonderful {
|
|||||||
var $messages, $adboxes_client, $publisher_info, $member_id;
|
var $messages, $adboxes_client, $publisher_info, $member_id;
|
||||||
var $widget_prefix = "plugin-wonderful";
|
var $widget_prefix = "plugin-wonderful";
|
||||||
|
|
||||||
|
var $message_types = array(
|
||||||
|
'CANT_READ' => "can't read",
|
||||||
|
'CANT_PARSE' => "can't parse",
|
||||||
|
'DOWNLOADED' => "downloaded"
|
||||||
|
);
|
||||||
|
|
||||||
function PluginWonderful() {}
|
function PluginWonderful() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -234,25 +240,40 @@ class PluginWonderful {
|
|||||||
return $changes;
|
return $changes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _download_project_wonderful_data($member_id) {
|
||||||
|
if (($result = $this->_retrieve_url(sprintf(PLUGIN_WONDERFUL_XML_URL, $member_id))) !== false) {
|
||||||
|
$this->publisher_info = $this->_get_new_publisher_info_object();
|
||||||
|
if ($this->publisher_info->parse($result)) {
|
||||||
|
$this->adboxes_client->post_ads($this->publisher_info);
|
||||||
|
return $this->message_types['DOWNLOADED'];
|
||||||
|
} else {
|
||||||
|
$this->publisher_info = false;
|
||||||
|
return $this->message_types['CANT_PARSE'];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$this->publisher_info = false;
|
||||||
|
return $this->message_types['CANT_READ'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function handle_action_rebuild_database() {
|
function handle_action_rebuild_database() {
|
||||||
$this->adboxes_client->destroy();
|
$this->adboxes_client->destroy();
|
||||||
$this->adboxes_client->initialize();
|
$this->adboxes_client->initialize();
|
||||||
|
|
||||||
$this->messages[] = __("Adbox database destroyed and rebuilt.", 'plugin-wonderful');
|
$this->messages[] = __("Adbox database destroyed and rebuilt.", 'plugin-wonderful');
|
||||||
|
|
||||||
if (get_option('plugin-wonderful-memberid') != "") {
|
$result = get_option('plugin-wonderful-memberid');
|
||||||
if (($result = file_get_contents(sprintf(PLUGIN_WONDERFUL_XML_URL, (int)get_option('plugin-wonderful-memberid')))) !== false) {
|
if (!empty($result)) {
|
||||||
$this->publisher_info = new PublisherInfo();
|
switch ($this->_download_project_wonderful_data($result)) {
|
||||||
if ($this->publisher_info->parse($result)) {
|
case $this->message_types['DOWNLOADED']:
|
||||||
$this->adboxes_client->post_ads($this->publisher_info);
|
$this->messages[] = __('Adbox information redownloaded.', 'plugin-wonderful');
|
||||||
$this->messages[] = sprintf(__('Adbox information redownloaded.', 'plugin-wonderful'), (int)$_POST['memberid']);
|
break;
|
||||||
} else {
|
case $this->message_types['CANT_PARSE']:
|
||||||
$this->messages[] = __("Unable to parse publisher data from Project Wonderful.", 'plugin-wonderful');
|
$this->messages[] = __("Unable to parse publisher data from Project Wonderful.", 'plugin-wonderful');
|
||||||
$this->publisher_info = false;
|
break;
|
||||||
}
|
case $this->message_types['CANT_READ']:
|
||||||
} else {
|
$this->messages[] = __("Unable to read publisher data from Project Wonderful.", 'plugin-wonderful');
|
||||||
$this->messages[] = __("Unable to read publisher data from Project Wonderful.", 'plugin-wonderful');
|
break;
|
||||||
$this->publisher_info = false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -389,8 +389,55 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function testHandleActionRebuildDatabase() {
|
function providerTestDownloadProjectWonderfulData() {
|
||||||
$this->markTestIncomplete();
|
return array(
|
||||||
|
array(false, false, "can't read"),
|
||||||
|
array(true, false, "can't parse"),
|
||||||
|
array(true, true, "downloaded"),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerTestDownloadProjectWonderfulData
|
||||||
|
*/
|
||||||
|
function testDownloadProjectWonderfulData($did_download_data, $did_parse_data, $expected_result) {
|
||||||
|
$pw = $this->getMock('PluginWonderful', array('_retrieve_url', '_get_new_publisher_info_object'));
|
||||||
|
|
||||||
|
$pw->expects($this->once())->method('_retrieve_url')->will($this->returnValue($did_download_data));
|
||||||
|
if ($did_download_data) {
|
||||||
|
$publisher_info = $this->getMock('PublisherInfo');
|
||||||
|
$publisher_info->expects($this->once())->method('parse')->will($this->returnValue($did_parse_data));
|
||||||
|
|
||||||
|
$pw->expects($this->once())->method('_get_new_publisher_info_object')->will($this->returnValue($publisher_info));
|
||||||
|
|
||||||
|
if ($did_parse_data) {
|
||||||
|
$pw->adboxes_client = $this->getMock('PWAdboxesClient', array('post_ads'));
|
||||||
|
$pw->adboxes_client->expects($this->once())->method('post_ads');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->assertEquals($expected_result, $pw->_download_project_wonderful_data('123'));
|
||||||
|
}
|
||||||
|
|
||||||
|
function providerTestHandleActionRebuildDatabase() {
|
||||||
|
return array(
|
||||||
|
array(""), array(1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider providerTestHandleActionRebuildDatabase
|
||||||
|
*/
|
||||||
|
function testHandleActionRebuildDatabase($member_id) {
|
||||||
|
$pw = $this->getMock('PluginWonderful', array('_download_project_wonderful_data'));
|
||||||
|
$pw->adboxes_client = $this->getMock('PWAdboxesClient', array('destroy', 'initialize'));
|
||||||
|
|
||||||
|
update_option('plugin-wonderful-memberid', $member_id);
|
||||||
|
if (!empty($member_id)) {
|
||||||
|
$pw->expects($this->once())->method("_download_project_wonderful_data")->with($member_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
$pw->handle_action_rebuild_database();
|
||||||
}
|
}
|
||||||
|
|
||||||
function testHandleActionChangeMemberID() {
|
function testHandleActionChangeMemberID() {
|
||||||
|
Loading…
Reference in New Issue
Block a user