more tests, some cleanup

Added tests for the FQL code.
Cleaned up some of the graph stuff.
Added some things that don't work.
Object type is definitely broken--there needs to be a test for it.
This commit is contained in:
Ben Liyanage 2014-10-13 16:07:20 -04:00
parent 86846d9ff4
commit d17c71611f
5 changed files with 176 additions and 52 deletions

View file

@ -18,6 +18,7 @@ FBPost:
*/
require_once("addon/fbpost/fbpost.php");
require_once("include/items.php");
define('FBSYNC_DEFAULT_POLL_INTERVAL', 5); // given in minutes
@ -205,8 +206,7 @@ function fbsync_expire($a,$b) {
$r = q("DELETE FROM `item` WHERE `deleted` AND `network` = '%s'", dbesc(NETWORK_FACEBOOK));
require_once("include/items.php");
logger('fbsync_expire: expire_start');
$r = q("SELECT * FROM `pconfig` WHERE `cat` = 'fbsync' AND `k` = 'sync' AND `v` = '1' ORDER BY RAND()");
@ -236,7 +236,7 @@ function fbsync_createpost($a, $uid, $contacts, $applications, $post, $create_us
dbesc('fb::'.$post->post_id)
);
if(count($r))
return;
return 1;
$postarray = array();
$postarray['gravity'] = 0;
@ -310,10 +310,10 @@ function fbsync_createpost($a, $uid, $contacts, $applications, $post, $create_us
if ($contact_id == -1) {
logger('fbsync_createpost: Contact is blocked. Post not imported '.print_r($post, true), LOGGER_DEBUG);
return;
return 2;
} elseif (($contact_id <= 0) AND !$create_user) {
logger('fbsync_createpost: No matching contact found. Post not imported '.print_r($post, true), LOGGER_DEBUG);
return;
return 3;
} elseif ($contact_id == 0) {
// This case should never happen
logger('fbsync_createpost: No matching contact found. Using own id. (Should never happen) '.print_r($post, true), LOGGER_DEBUG);
@ -377,7 +377,7 @@ function fbsync_createpost($a, $uid, $contacts, $applications, $post, $create_us
$quote = $post->attachment->caption;
if ($quote.$post->attachment->href.$content.$postarray["body"] == "")
return;
return 3;
if (isset($post->attachment->media) AND (($type == "") OR ($type == "link"))) {
foreach ($post->attachment->media AS $media) {
@ -439,7 +439,7 @@ function fbsync_createpost($a, $uid, $contacts, $applications, $post, $create_us
$postarray["body"] = trim($postarray["body"]);
if (trim($postarray["body"]) == "")
return;
return 4;
if ($prebody != "")
$postarray["body"] = $prebody.$postarray["body"]."[/share]";
@ -459,6 +459,7 @@ function fbsync_createpost($a, $uid, $contacts, $applications, $post, $create_us
$item = item_store($postarray);
logger('fbsync_createpost: User ' . $uid . ' posted feed item '.$item, LOGGER_DEBUG);
return 0;
}
function fbsync_createcomment($a, $uid, $self_id, $self, $user, $contacts, $applications, $comment) {
@ -994,8 +995,6 @@ function fbsync_fetchfeed($a, $uid, $self_id, $last_updated) {
$access_token = get_pconfig($uid,'facebook','access_token');
$do_likes = get_config('fbsync', 'do_likes');
require_once('include/items.php');
//if ($last_updated == "")
$last_updated = 0;
@ -1012,9 +1011,9 @@ function fbsync_fetchfeed($a, $uid, $self_id, $last_updated) {
$fql["likes"] = "SELECT post_id, user_id FROM like WHERE post_id IN (SELECT post_id FROM #posts)";
$fql["profiles"] .= " OR id IN (SELECT user_id FROM #likes)";
}
$url = "https://graph.facebook.com/fql?q=".urlencode(json_encode($fql))."&access_token=".$access_token;
$url = "https://graph.facebook.com/fql?q=".urlencode(json_encode($fql))."&access_token=".$access_token;
echo $url;
$feed = fetch_url($url);
$data = json_decode($feed);
@ -1092,11 +1091,14 @@ function fbsync_processfeed($data, $self, $a, $uid, $self_id, $user, $last_updat
}
unset($applications);
foreach ($posts AS $post) {
if ($post->updated_time > $last_updated)
$last_updated = $post->updated_time;
fbsync_createpost($a, $uid, $contacts, $applications, $post, $create_user);
$result = fbsync_createpost($a, $uid, $contacts, $applications, $post, $create_user);
echo $result;
}
foreach ($comments AS $comment) {

View file

@ -0,0 +1,9 @@
<?php
require_once("./addon/fbsync/object/Facebook.php");
Class Facebook_Graph21 extends Facebook
{
}
?>

View file

@ -24,7 +24,10 @@ Class Facebook_Graph21 extends Facebook
dbesc('fb::'.$post->id)
);
if(count($r))
{
logger('fbsync_createpost: skipping post $post->id--already exists', LOGGER_DEBUG);
return;
}
$postarray = array();
$postarray['gravity'] = 0;
@ -64,7 +67,8 @@ Class Facebook_Graph21 extends Facebook
$postarray['contact-id'] = 1;
//Set Object Type
if (!isset($post->attachment[0]->type))
//TODO: This code is broken.
if (!isset($post->attachment[0]->type)) //This is never set since its from the FQL dataset.
{
//Default Object Type
$postarray['object-type'] = ACTIVITY_OBJ_NOTE; // default value - is maybe changed later when media is attached
@ -79,14 +83,19 @@ Class Facebook_Graph21 extends Facebook
if ($type == "rich")
$type = "link";
*/
/*
echo "type: " . $postarray['object-type'];
die();
*/
//TODO: Body needs more testing, and has some more fringe cases.
$postarray["body"] = $this->AssembleBody($post->name, $post->link, $post->description, $post->picture); //"This is the body. [quote]This is the quote.[/quote]";
$postarray["body"] = $this->AssembleBody($post->name, $post->link, $post->description, $post->picture, $postarray['object-type']);
//TODO: Do tags
$postarray["tag"] = "This is the tag";
$postarray['app'] = ($post->application->name == "" ? "Facebook" : $post->application->name);
$postarray['app'] = ($post->application->name == "Links" ? "Facebook" : $post->application->name);
if(isset($post->privacy) && $post->privacy->value !== '') {
$postarray['private'] = 1;
@ -99,17 +108,16 @@ Class Facebook_Graph21 extends Facebook
return $postarray;
}
function AssembleBody($Title, $Href, $Body, $Picture)
function AssembleBody($Title, $Href, $Body, $Picture, $ObjectType)
{
//TODO: Need to do prebody code still.
//TODO: Need to add class (aka type) code
/*
$postarray["body"] = (isset($post->message) ? escape_tags($post->message) : '');
$msgdata = fbsync_convertmsg($a, $postarray["body"]);
$postarray["body"] = $msgdata["body"];
$postarray["tag"] = $msgdata["tags"];
*/
$content = "";
if ($Picture != "")
@ -181,32 +189,21 @@ Class Facebook_Graph21 extends Facebook
}
}
}
if ($type == "link")
$postarray["object-type"] = ACTIVITY_OBJ_BOOKMARK;
if ($content)
$postarray["body"] .= "\n";
if ($type)
$postarray["body"] .= "[class=type-".$type."]";
if ($content)
$postarray["body"] .= trim($content);
if ($quote)
$postarray["body"] .= "\n[quote]".trim($quote)."[/quote]";
if ($type)
$postarray["body"] .= "[/class]";
$postarray["body"] = trim($postarray["body"]);
if (trim($postarray["body"]) == "")
return;
if ($prebody != "")
$postarray["body"] = $prebody.$postarray["body"]."[/share]";
*/
$content = '[class="type-'. $ObjectType . '"]' . $content . '[/class]';
/*
TODO:
* What is the wall-to-wall system setting supposed to do? What is the "Share" syntax used for?
This code does not seem to match what happens with the previous posts. I stripped out some stuff comparing author and source below.
if (!intval(get_config('system','wall-to-wall_share'))) {
$ShareAuthor = "Test Share Author";
$ShareAuthorLink = "http://test.com";
$content = '[share author="' . $ShareAuthor . '" profile="' . $ShareAuthorLink . '" avatar="' . $ShareAuthorAvatar . '"]' . $content . '[/share]';
}
*/
return $content;

View file

@ -14,8 +14,23 @@ $db = new dba($db_host, $db_user, $db_pass, $db_data, $install);
//$data = fbsync_fetchfeed($a, 1);
//var_dump($data);
//Test Data Processing
//Test Data Prcoessing -- Constants-- used in both tests
$uid = 1;
$self_id = get_pconfig($uid,'fbsync','self_id');
$last_updated = get_pconfig($uid,'fbsync','last_updated');
$self = q("SELECT * FROM `contact` WHERE `self` = 1 AND `uid` = %d LIMIT 1", intval($uid));
$user = q("SELECT * FROM `user` WHERE `uid` = %d AND `account_expired` = 0 LIMIT 1", intval($uid));
$create_user = get_pconfig($uid, 'fbsync', 'create_user');
//Test Data Processing fql
$data = json_decode(file_get_contents("./addon/fbsync/tests/fql-full.txt"));
$result = fbsync_processfeed($data, $self, $a, $uid, $self_id, $user, $last_updated);
if ($result != 0) die("FQL Processing broken.\n");
echo "Done with fql_data processing.\n";
//Test data processing -- graph`
// Test Base Class
require_once("./addon/fbsync/object/Facebook.php");
@ -30,12 +45,11 @@ if ($myFBSync->uid != 1) die("class did not load");
if ($myFBSync->access_token == '') die("failed to load access_token");
//Test FetchContact
//TODO: build tests for facebook api requests using API test user functions
//Test CreatePost
$posts = json_decode(file_get_contents("./addon/fbsync/tests/graph2.1-no-filter.txt"));
var_dump($posts);
$post = $myFBSync->CreatePost($a,0,0,0,$posts->data[0],0);
//verify data
@ -43,7 +57,8 @@ if ($post['uri'] != "fb::109524391244_10152483187826245") die("uri does not matc
if ($post['plink'] != "https://www.facebook.com/109524391244/posts/10152483187826245") die("plink does not match");
//var_dump($posts->data[0]);
//test creating the same post again
//TODO: test creating the same post again
echo "All done\n";
@ -85,4 +100,4 @@ https://developers.facebook.com/docs/graph-api/reference/v2.1/test-user
*/
?>
?>

101
fbsync/tests/fql-full.txt Normal file
View file

@ -0,0 +1,101 @@
{
"data": [
{
"name": "posts",
"fql_result_set": [
{
"action_links": null,
"actor_id": 568851657,
"app_data": [
],
"app_id": null,
"attachment": {
"description": ""
},
"attribution": null,
"comment_info": {
"can_comment": true,
"comment_count": 3,
"comment_order": "chronological"
},
"created_time": 1413220907,
"filter_key": "nf",
"like_info": {
"can_like": true,
"like_count": 1,
"user_likes": false
},
"message": "Max Gladstone on modern magic:\n\n\"[C]onsider the smartphone I have in my pocket. No single human being knows how to make this phone. I acquired the phone, and I use it. People who know more about the phone can tell it to do more things than I can, but they're still bound by the limits of the hardware. A few communities are dedicated to modding and hacking phones like mine, yes, but for most people most of the time a smartphone is a portable magic mirror. We make mystic passes before the glass, address the indwelling spirit with suitably respectful tones, and LEARN THE FUTURE. (\"Siri, what will the weather be like tomorrow?\") The same thought experiment works for many modern technologies.\"",
"message_tags": [
],
"parent_post_id": null,
"permalink": "https://www.facebook.com/stephen.carr.547/posts/10152403039101658",
"place": null,
"post_id": "568851657_10152403039101658",
"privacy": {
"value": ""
},
"share_count": 0,
"share_info": {
"can_share": true,
"share_count": 0
},
"source_id": 568851657,
"subscribed": false,
"tagged_ids": [
],
"type": 46,
"updated_time": 1413222111,
"with_tags": [
]
}
]
},
{
"name": "comments",
"fql_result_set": [
{
"app_id": 0,
"attachment": null,
"post_id": "568851657_10152403039101658",
"id": "10152403039101658_10152403084216658",
"likes": 0,
"fromid": 568851657,
"time": 1413222111,
"text": "And Siri said unto them, \"I'm sorry, I did not understand that.\" And the pharisees were wroth, and sought to harden the heart of Palmius Pilot against her.",
"text_tags": [
],
"user_likes": false
}
]
},
{
"name": "applications",
"fql_result_set": [
]
},
{
"name": "profiles",
"fql_result_set": [
{
"id": 568851657,
"name": "Stephen Carr",
"username": "stephen.carr.547",
"url": "https://www.facebook.com/stephen.carr.547",
"pic_square": "https://scontent-a.xx.fbcdn.net/hprofile-xpa1/v/t1.0-1/c0.0.50.50/p50x50/1229928_10152376737046658_8387683222153583301_n.jpg?oh=4c9cf110c2953f26e0c83bc644449730&oe=54B95BF9"
}
]
},
{
"name": "avatars",
"fql_result_set": [
{
"id": 568851657,
"real_size": 320,
"size": 256,
"url": "https://scontent-a.xx.fbcdn.net/hprofile-xpa1/v/t1.0-1/c0.0.320.320/p320x320/1229928_10152376737046658_8387683222153583301_n.jpg?oh=9627591ac52d10005212abbae4c1f451&oe=54AD7B88"
}
]
}
]
}