From bce3bfff38ad6f540dca39f6ed5b1d8cae19edb1 Mon Sep 17 00:00:00 2001 From: Alexander Kampmann Date: Mon, 12 Mar 2012 13:59:00 +0100 Subject: [PATCH] get tags test improved --- mod/item.php | 79 ++++++++++++++++++++++++++++------------- tests/get_tags_test.php | 40 +++++++++++++++++++-- 2 files changed, 92 insertions(+), 27 deletions(-) diff --git a/mod/item.php b/mod/item.php index a9edf0da66..0ff7f6a7c8 100755 --- a/mod/item.php +++ b/mod/item.php @@ -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) { - $profile=null; - if(isset($profile)) - unset($profile); + //is it a hash tag? if(strpos($tag,'#') === 0) { - if(strpos($tag,'[url=')) - continue; + //if the tag is replaced... + if(strpos($tag,'[url=')) + //...do nothing + continue; + //base tag has the tags name only $basetag = str_replace('_',' ',substr($tag,1)); - $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body); - - $newtag = '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]'; + //create text for link + $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)) { + //append or set str_tags if(strlen($str_tags)) $str_tags .= ','; $str_tags .= $newtag; } continue; - } + } + //is it a person tag? if(strpos($tag,'@') === 0) { + //is it already replaced? if(strpos($tag,'[url=')) continue; $stat = false; - $name = substr($tag,1); + //get the person's name + $name = substr($tag,1); + //is it a link or a full dfrn address? if((strpos($name,'@')) || (strpos($name,'http://'))) { $newname = $name; + //get the profile links $links = @lrdd($name); if(count($links)) { + //for all links, collect how is to inform and how's profile is to link foreach($links as $link) { if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page') $profile = $link['@attributes']['href']; @@ -857,38 +878,44 @@ function handle_body(&$body, &$inform, &$str_tags, $profile_uid, $tag) { } } } - } - else { + } else { //if it is a name rather than an address $newname = $name; $alias = ''; - $tagcid = 0; + $tagcid = 0; + //is it some generated name? if(strrpos($newname,'+')) { + //get the id $tagcid = intval(substr($newname,strrpos($newname,'+') + 1)); + //remove the next word from tag's name if(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", intval($tagcid), intval($profile_uid) ); - } - elseif(strstr($name,'_') || strstr($name,' ')) { + } elseif(strstr($name,'_') || strstr($name,' ')) { //no id + //get the real 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", dbesc($newname), 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", dbesc($name), dbesc($name), intval($profile_uid) ); - } + } + //$r is set, if someone could be selected if(count($r)) { - $profile = $r[0]['url']; + $profile = $r[0]['url']; + //set newname to nick, find alias if($r[0]['network'] === 'stat') { $newname = $r[0]['nick']; $stat = true; @@ -897,15 +924,19 @@ function handle_body(&$body, &$inform, &$str_tags, $profile_uid, $tag) { } else $newname = $r[0]['name']; + //add person's id to $inform if(strlen($inform)) $inform .= ','; $inform .= 'cid:' . $r[0]['id']; } - } - if($profile) { - $body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname . '[/url]', $body); + } + //if there is an url for this persons profile + if(isset($profile)) { + //create profile link $profile = str_replace(',','%2c',$profile); $newtag = '@[url=' . $profile . ']' . $newname . '[/url]'; + $body = str_replace('@' . $name, $newtag, $body); + //append tag to str_tags if(! stristr($str_tags,$newtag)) { if(strlen($str_tags)) $str_tags .= ','; diff --git a/tests/get_tags_test.php b/tests/get_tags_test.php index a458f0fbc5..ee2daced10 100644 --- a/tests/get_tags_test.php +++ b/tests/get_tags_test.php @@ -8,11 +8,45 @@ require_once 'include/text.php'; require_once 'mod/item.php'; 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) { - echo $str; + return $str; } 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]); $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); } /**