working on cleaning up plugin wonderful class

This commit is contained in:
John Bintz 2009-06-25 21:40:41 -04:00
parent 1c97eee3e9
commit 7a662ac5c3
4 changed files with 187 additions and 28 deletions

View File

@ -2,29 +2,21 @@
class PluginWonderful {
var $messages, $adboxes_client, $publisher_info, $member_id;
var $widget_prefix = "plugin-wonderful";
function PluginWonderful() {}
function _retrieve_url($url) {
return @file_get_contents($url);
}
function init() {
if (empty($this->adboxes_client)) {
$this->messages = array();
$this->adboxes_client = new PWAdboxesClient();
$this->publisher_info = false;
if ($member_id = get_option('plugin-wonderful-memberid')) {
$this->publisher_info = $this->adboxes_client->get_ads($member_id);
if ((get_option('plugin-wonderful-last-update') + PLUGIN_WONDERFUL_UPDATE_TIME) < time()) {
if (($result = file_get_contents(sprintf(PLUGIN_WONDERFUL_XML_URL, (int)get_option('plugin-wonderful-memberid')))) !== false) {
$this->publisher_info = new PublisherInfo();
if ($this->publisher_info->parse($result)) {
$this->adboxes_client->post_ads($this->publisher_info);
update_option('plugin-wonderful-last-update', time());
}
}
}
}
$this->_get_publisher_info();
$result = get_option('plugin-wonderful-database-version');
if (empty($result) || ($result < PLUGIN_WONDERFUL_DATABASE_VERSION)) {
if ($this->adboxes_client->initialize(true)) {
@ -40,6 +32,35 @@ class PluginWonderful {
}
}
function _get_new_publisher_info_object() {
return new PublisherInfo();
}
function _get_publisher_info() {
$this->publisher_info = false;
$member_id = get_option('plugin-wonderful-memberid');
if (is_numeric($member_id)) {
$member_id = (int)$member_id;
$this->publisher_info = $this->adboxes_client->get_ads($member_id);
$last_update = get_option('plugin-wonderful-last-update') ;
if (!is_numeric($last_update)) { $last_update = 0; }
$last_update = (int)$last_update;
if (($last_update + PLUGIN_WONDERFUL_UPDATE_TIME) < time()) {
if (($result = $this->_retrieve_url(sprintf(PLUGIN_WONDERFUL_XML_URL, (int)get_option('plugin-wonderful-memberid')))) !== false) {
$this->publisher_info = $this->_get_new_publisher_info_object();
if ($this->publisher_info->parse($result)) {
$this->adboxes_client->post_ads($this->publisher_info);
update_option('plugin-wonderful-last-update', time());
}
}
}
}
return $this->publisher_info;
}
function insert_rss_feed_ads($content) {
if (is_feed()) {
if ($this->publisher_info !== false) {
@ -55,6 +76,15 @@ class PluginWonderful {
return $content;
}
function get_ad_widget_ordering($adbox_id) {
if (($result = get_option('plugin-wonderful-sidebar-adboxes')) !== false) {
foreach ($result as $position => $id) {
if ($id == $adbox_id) { return $position; }
}
}
return null;
}
function insert_activation_ad() {
$result = get_option('plugin-wonderful-activate-ad-code');
if (!empty($result)) { echo $result; }
@ -92,18 +122,6 @@ class PluginWonderful {
add_options_page('Plugin Wonderful', __("Plugin Wonderful", 'plugin-wonderful'), 5, __FILE__, array($this, "plugin_wonderful_main"));
}
function set_up_widgets() {
if ($this->publisher_info !== false) {
if (($widgets = $this->publisher_info->get_sidebar_widget_info()) !== false) {
foreach ($widgets as $widget_info) {
extract($widget_info);
wp_register_sidebar_widget($id, $name, array($this, 'render_widget'), "", $options['adboxid']);
register_widget_control($id, array($this, 'render_widget_control'), null, null, $options['adboxid']);
}
}
}
}
function render_widget_control($adboxid) {
if ($this->publisher_info !== false) {
foreach ($this->publisher_info->adboxes as $box) {

View File

@ -0,0 +1,24 @@
<?php
class PluginWonderfulWidget extends WP_Widget {
function PluginWonderfulWidget() {
$widget_options = array(
'classname' => 'plugin-wonderful',
'description' => __('A widget for adding your Project Wonderful advertisements', 'plugin-wonderful')
);
$control_options = array(
'id_base' => 'plugin-wonderful'
);
$this->WP_Widget('plugin-wonderful', __('Plugin Wonderful', 'plugin-wonderful'), $widget_options, $control_options);
}
function widget($args, $instance) {
extract($args);
}
}
?>

View File

@ -2,12 +2,14 @@
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../classes/PluginWonderful.php');
require_once(dirname(__FILE__) . '/../classes/PublisherInfo.php');
require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
class PluginWonderfulTest extends PHPUnit_Framework_TestCase {
function setUp() {
$this->pw = new PluginWonderful();
$_POST = array();
_reset_wp();
}
function testSaveWidgetsIsCalled() {
@ -49,6 +51,96 @@ class PluginWonderfulTest extends PHPUnit_Framework_TestCase {
$pw->handle_activation();
}
function providerTestGetPubliserInfo() {
return array(
array(
array(
'plugin-wonderful-memberid' => "",
),
false,
false,
false
),
array(
array(
'plugin-wonderful-memberid' => 1,
'plugin-wonderful-last-update' => time()
),
false,
false,
"~*test*~"
),
array(
array(
'plugin-wonderful-memberid' => 1,
'plugin-wonderful-last-update' => 0
),
false,
false,
"~*test*~"
),
array(
array(
'plugin-wonderful-memberid' => 1,
'plugin-wonderful-last-update' => 0
),
true,
false,
"~*test-xml*~"
),
array(
array(
'plugin-wonderful-memberid' => 1,
'plugin-wonderful-last-update' => 0
),
true,
true,
"~*test-xml*~"
)
);
}
/**
* @dataProvider providerTestGetPubliserInfo
*/
function testGetPublisherInfo($options, $retrieve_url_return, $parse_success, $expected_result) {
foreach ($options as $key => $value) { update_option($key, $value); }
$pw = $this->getMock('PluginWonderful', array('_retrieve_url', '_get_new_publisher_info_object'));
$pw->adboxes_client = $this->getMock('PWAdboxesClient', array('get_ads', 'post_ads'));
$test_publisher_info = $this->getMock('PublisherInfo');
$test_xml_publisher_info = $this->getMock('PublisherInfo', array('parse'));
if (is_numeric($options['plugin-wonderful-memberid'])) {
$pw->adboxes_client->expects($this->once())->method('get_ads')->will($this->returnValue($test_publisher_info));
if (($options['plugin-wonderful-last-update'] + PLUGIN_WONDERFUL_UPDATE_TIME) < time()) {
$pw->expects($this->once())->method('_retrieve_url')->will($this->returnValue($retrieve_url_return));
if ($retrieve_url_return) {
$pw->expects($this->once())->method('_get_new_publisher_info_object')->will($this->returnValue($test_xml_publisher_info));
$test_xml_publisher_info->expects($this->once())->method('parse')->will($this->returnValue($parse_success));
if ($parse_success) {
$pw->adboxes_client->expects($this->once())->method('post_ads');
} else {
$pw->adboxes_client->expects($this->never())->method('post_ads');
}
} else {
$pw->expects($this->never())->method('_get_new_publisher_info_object');
}
} else {
$pw->expects($this->never())->method('_retrieve_url');
}
}
if ($expected_result == "~*test*~") { $expected_result = $test_publisher_info; }
if ($expected_result == "~*test-xml*~") { $expected_result = $test_xml_publisher_info; }
$this->assertEquals($expected_result, $pw->_get_publisher_info());
}
}
?>

View File

@ -0,0 +1,25 @@
<?php
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__) . '/../../mockpress/mockpress.php');
require_once(dirname(__FILE__) . '/../classes/PluginWonderfulWidget.php');
class PluginWonderfulWidgetTest extends PHPUnit_Framework_TestCase {
function setUp() {
_reset_wp();
$this->w = new PluginWonderfulWidget();
}
function testInitialize() {
global $wp_test_expectations;
$this->w = new PluginWonderfulWidget();
$this->assertEquals("Plugin Wonderful", $wp_test_expectations['wp_widgets']['plugin-wonderful']['name']);
}
function testWidget() {
}
}
?>