fix a bunch of things

This commit is contained in:
John Bintz 2009-02-17 21:25:37 -05:00
parent 3a381e1d2f
commit 98c86a9afd
3 changed files with 94 additions and 19 deletions

View File

@ -14,6 +14,23 @@ class PWAdboxesClient {
$this->table_name = $wpdb->prefix . "pw_adboxes";
$this->table_exists = false;
$this->schema_info = array(
array('type', 'int', '1', "NOT NULL"),
array('adboxid', 'int', '11', "NOT NULL"),
array('sitename', 'char', '100', 'NOT NULL'),
array('adtype', 'char', '30', 'NOT NULL'),
array('url', 'char', '255', 'NOT NULL'),
array('dimensions', 'char', '10', 'NOT NULL'),
array('rating', 'char', '30', 'NOT NULL'),
array('category', 'char', '50', 'NOT NULL'),
array('description', 'text', '', 'NOT NULL'),
array('tags', 'text', '', 'NOT NULL'),
array('standardcode', 'text', '', 'NOT NULL'),
array('advancedcode', 'text', '', 'NOT NULL'),
array('template_tag_id', 'char', '30', ''),
array('in_rss_feed', 'int', '1', '')
);
}
/**
@ -23,23 +40,20 @@ class PWAdboxesClient {
global $wpdb;
if ($wpdb->get_var("SHOW TABLES LIKE {$this->table_name}") != $this->table_name) {
$sql = "CREATE TABLE {$this->table_name} (\n";
$statements = array();
foreach ($this->schema_info as $info) {
list($name, $type, $size, $extra) = $info;
$statement = "{$name} {$type}";
if (!empty($size)) { $statement .= "({$size})"; }
if (!empty($extra)) { $statement .= " {$extra}"; }
$statements[] = $statement;
}
$sql .= implode(",\n", $statements) . ");";
if (!$wpdb->is_mock) {
$sql = "CREATE TABLE {$this->table_name} (
type int(1) NOT NULL,
adboxid int(11) NOT NULL,
sitename char(100) NOT NULL,
adtype char(30) NOT NULL,
url char(255) NOT NULL,
dimensions char(10) NOT NULL,
rating char(30) NOT NULL,
category char(50) NOT NULL,
description text NOT NULL,
tags text NOT NULL,
standardcode text NOT NULL,
advancedcode text NOT NULL,
template_tag_id char(30),
in_rss_feed int(1)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
@ -84,14 +98,20 @@ class PWAdboxesClient {
}
}
foreach ((array)$box as $key => $value) {
foreach ($this->schema_info as $info) {
list($key, $column_type, $size, $extra) = $info;
if ($key !== "type") {
$columns[] = $key;
$value = $box->{$key};
if (!empty($size)) { $value = substr($value, 0, $size); }
$values[] = '"' . $wpdb->escape($value) . '"';
}
}
if (!$wpdb->query("INSERT INTO {$this->table_name} (" . implode(",", $columns) . ") VALUES (" . implode(",", $values) . ")")) {
$sql = "INSERT INTO {$this->table_name} (" . implode(",", $columns) . ") VALUES (" . implode(",", $values) . ")";
if (!$wpdb->query($sql)) {
return false;
}
}
@ -141,12 +161,24 @@ class PWAdboxesClient {
$wpdb->query("DELETE FROM {$this->table_name}");
}
function trim_field($field, $value) {
foreach ($this->schema_info as $info) {
list($key, $type, $size, $extra) = $info;
if ($key == $field) {
$value = substr($value, 0, $size); break;
}
}
return $value;
}
/**
* Set the template tag id for an advertisement.
*/
function set_template_tag($adboxid, $tag) {
global $wpdb;
$tag = $this->trim_field('template_tag_id', $tag);
$query = "UPDATE {$this->table_name} SET ";
$query .= "template_tag_id = '" . $wpdb->escape($tag) . "'";
$query .= " WHERE adboxid = '" . $wpdb->escape($adboxid) . "'";

View File

@ -26,6 +26,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
require_once('classes/PWAdboxesClient.php');
//require_once('FirePHPCore/fb.php');
define('PLUGIN_WONDERFUL_XML_URL', 'http://www.projectwonderful.com/xmlpublisherdata.php?publisher=%d');
define('PLUGIN_WONDERFUL_UPDATE_TIME', 60 * 60 * 12); // every 12 hours
@ -71,6 +72,7 @@ class PluginWonderful {
}
}
}
return $content;
}
function insert_activation_ad() {
@ -167,6 +169,9 @@ class PluginWonderful {
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;

View File

@ -23,7 +23,7 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
$wpdb->prefix = "wp_";
$wpdb->is_mock = true;
$wpdb->expects($this->once())->method('get_var')->with($this->equalTo("SHOW TABLES LIKE {$this->database_client->table_name}"));
$wpdb->expects($this->once())->method('get_var')->with($this->equalTo("SHOW TABLES LIKE {$this->database_client->table_name}"))->will($this->returnValue(array()));
$this->database_client->initialize();
}
@ -163,6 +163,35 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
$this->assertEquals($this->target_ad, $this->database_client->get_ad_by_template_tag(1, $test_tag));
}
function testDataTooLarge() {
global $wpdb;
$wpdb = $this->getMock('wpdb', array('escape', 'query', 'get_results'));
$wpdb->prefix = "wp_";
$ads = $this->getMock('PublisherInfo', array());
$ads->member_id = "1";
$ads->is_valid = true;
$large_sample_ad = array();
foreach (array('adboxid', 'sitename', 'url',
'dimensions', 'rating', 'category',
'description', 'tags', 'standardcode',
'advancedcode', 'adtype', 'template_tag_id',
'in_rss_feed') as $field) {
$large_sample_ad[$field] = $field . "-" . str_repeat("x", 300);
}
$ads->adboxes = array((object)$large_sample_ad);
$wpdb->expects($this->exactly(13))->method('escape')->will($this->returnCallback(array($this, 'postDataTooLargeCallback')));
$wpdb->expects($this->exactly(2))->method('query');
$wpdb->expects($this->exactly(1))->method('get_results');
$this->escape_count = 1;
$this->database_client->post_ads($ads, PW_ADBOXES_PROJECT_WONDERFUL);
}
function postAdsCallback($query) {
if (strpos($query, "DELETE") === 0) {
return $query == ("DELETE FROM {$this->database_client->table_name} WHERE type = " . PW_ADBOXES_PROJECT_WONDERFUL);
@ -171,6 +200,15 @@ class TestPWAdboxesClient extends PHPUnit_Framework_TestCase {
}
}
function postDataTooLargeCallback($query) {
$size = $this->database_client->schema_info[$this->escape_count][2];
if (!empty($size)) {
$this->assertTrue(strlen($query) <= $size);
}
$this->escape_count++;
}
function postAdsFilterCallback($query) {
if (strpos($query, "INSERT") === 0) {
return count(explode("type", $query)) === 3;