get tags test improved

This commit is contained in:
Alexander Kampmann 2012-03-12 13:59:00 +01:00
parent f8042d0445
commit bce3bfff38
2 changed files with 92 additions and 27 deletions

View File

@ -820,33 +820,54 @@ function item_content(&$a) {
} }
} }
/**
* This function removes the tag $tag from the text $body and replaces it with
* the appropiate link.
*
* @param unknown_type $body the text to replace the tag in
* @param unknown_type $inform a comma-seperated string containing everybody to inform
* @param unknown_type $str_tags string to add the tag to
* @param unknown_type $profile_uid
* @param unknown_type $tag the tag to replace
*/
function handle_body(&$body, &$inform, &$str_tags, $profile_uid, $tag) { function handle_body(&$body, &$inform, &$str_tags, $profile_uid, $tag) {
$profile=null; //is it a hash tag?
if(isset($profile))
unset($profile);
if(strpos($tag,'#') === 0) { if(strpos($tag,'#') === 0) {
//if the tag is replaced...
if(strpos($tag,'[url=')) if(strpos($tag,'[url='))
//...do nothing
continue; continue;
//base tag has the tags name only
$basetag = str_replace('_',' ',substr($tag,1)); $basetag = str_replace('_',' ',substr($tag,1));
$body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body); //create text for link
$newtag = '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; $newtag = '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
//replace tag by the link
$body = str_replace($tag, $newtag, $body);
//is the link already in str_tags?
if(! stristr($str_tags,$newtag)) { if(! stristr($str_tags,$newtag)) {
//append or set str_tags
if(strlen($str_tags)) if(strlen($str_tags))
$str_tags .= ','; $str_tags .= ',';
$str_tags .= $newtag; $str_tags .= $newtag;
} }
continue; continue;
} }
//is it a person tag?
if(strpos($tag,'@') === 0) { if(strpos($tag,'@') === 0) {
//is it already replaced?
if(strpos($tag,'[url=')) if(strpos($tag,'[url='))
continue; continue;
$stat = false; $stat = false;
//get the person's name
$name = substr($tag,1); $name = substr($tag,1);
//is it a link or a full dfrn address?
if((strpos($name,'@')) || (strpos($name,'http://'))) { if((strpos($name,'@')) || (strpos($name,'http://'))) {
$newname = $name; $newname = $name;
//get the profile links
$links = @lrdd($name); $links = @lrdd($name);
if(count($links)) { if(count($links)) {
//for all links, collect how is to inform and how's profile is to link
foreach($links as $link) { foreach($links as $link) {
if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page') if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')
$profile = $link['@attributes']['href']; $profile = $link['@attributes']['href'];
@ -857,38 +878,44 @@ function handle_body(&$body, &$inform, &$str_tags, $profile_uid, $tag) {
} }
} }
} }
} } else { //if it is a name rather than an address
else {
$newname = $name; $newname = $name;
$alias = ''; $alias = '';
$tagcid = 0; $tagcid = 0;
//is it some generated name?
if(strrpos($newname,'+')) { if(strrpos($newname,'+')) {
//get the id
$tagcid = intval(substr($newname,strrpos($newname,'+') + 1)); $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));
//remove the next word from tag's name
if(strpos($name,' ')) if(strpos($name,' '))
$name = substr($name,0,strpos($name,' ')); $name = substr($name,0,strpos($name,' '));
} }
if($tagcid) { if($tagcid) { //if there was an id
//select contact with that id from the logged in user's contact list
$r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1", $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
intval($tagcid), intval($tagcid),
intval($profile_uid) intval($profile_uid)
); );
} } elseif(strstr($name,'_') || strstr($name,' ')) { //no id
elseif(strstr($name,'_') || strstr($name,' ')) { //get the real name
$newname = str_replace('_',' ',$name); $newname = str_replace('_',' ',$name);
//select someone from this user's contacts by name
$r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1", $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",
dbesc($newname), dbesc($newname),
intval($profile_uid) intval($profile_uid)
); );
} } else {
else { //select someone by attag or nick and the name passed in
$r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1", $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",
dbesc($name), dbesc($name),
dbesc($name), dbesc($name),
intval($profile_uid) intval($profile_uid)
); );
} }
//$r is set, if someone could be selected
if(count($r)) { if(count($r)) {
$profile = $r[0]['url']; $profile = $r[0]['url'];
//set newname to nick, find alias
if($r[0]['network'] === 'stat') { if($r[0]['network'] === 'stat') {
$newname = $r[0]['nick']; $newname = $r[0]['nick'];
$stat = true; $stat = true;
@ -897,15 +924,19 @@ function handle_body(&$body, &$inform, &$str_tags, $profile_uid, $tag) {
} }
else else
$newname = $r[0]['name']; $newname = $r[0]['name'];
//add person's id to $inform
if(strlen($inform)) if(strlen($inform))
$inform .= ','; $inform .= ',';
$inform .= 'cid:' . $r[0]['id']; $inform .= 'cid:' . $r[0]['id'];
} }
} }
if($profile) { //if there is an url for this persons profile
$body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname . '[/url]', $body); if(isset($profile)) {
//create profile link
$profile = str_replace(',','%2c',$profile); $profile = str_replace(',','%2c',$profile);
$newtag = '@[url=' . $profile . ']' . $newname . '[/url]'; $newtag = '@[url=' . $profile . ']' . $newname . '[/url]';
$body = str_replace('@' . $name, $newtag, $body);
//append tag to str_tags
if(! stristr($str_tags,$newtag)) { if(! stristr($str_tags,$newtag)) {
if(strlen($str_tags)) if(strlen($str_tags))
$str_tags .= ','; $str_tags .= ',';

View File

@ -8,11 +8,45 @@ require_once 'include/text.php';
require_once 'mod/item.php'; require_once 'mod/item.php';
function q($sql) { function q($sql) {
return array(array('id'=>15, 'network'=>'stat', 'alias'=>'Mike', 'nick'=>'Mike', 'url'=>"http://justatest.de"));
$result=array(array('id'=>15,
'attag'=>'', 'network'=>'dfrn',
'name'=>'Mike Lastname', 'alias'=>'Mike',
'nick'=>'Mike', 'url'=>"http://justatest.de"));
$args=func_get_args();
$str="";
foreach($args as $arg) {
$str.=", ".$arg;
}
//last parameter is always (in this test) uid, so, it should be 11
if($args[count($args)-1]!=11) {
throw new Exception("q from get_tags_test was used and uid was not 11. ");
}
if(2==count($args)) {
//first call in handle_body, id only
if($result[0]['id']===$args[1]) {
return $result;
}
throw new Exception($str);
//second call in handle_body, name
if($result[0]['name']===$args[1]) {
return $result;
}
}
throw new Exception($str);
//third call in handle_body, nick or attag
if($result[0]['nick']===$args[2] || $result[0]['attag']===$args[1]) {
return $result;
}
// throw new Exception("Nothing fitted: ".$args[1].", ".$args[2]);
} }
function dbesc($str) { function dbesc($str) {
echo $str; return $str;
} }
class GetTagsTest extends PHPUnit_Framework_TestCase { class GetTagsTest extends PHPUnit_Framework_TestCase {
@ -39,7 +73,7 @@ class GetTagsTest extends PHPUnit_Framework_TestCase {
handle_body($text, $inform, $str_tags, 11, $tags[0]); handle_body($text, $inform, $str_tags, 11, $tags[0]);
$this->assertEquals("@Mike", $tags[0]); $this->assertEquals("@Mike", $tags[0]);
$this->assertEquals($text, "hi @[url=http://justatest.de]Mike[/url]"); $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url]", $text);
} }
/** /**