]> git.mxchange.org Git - friendica.git/commitdiff
get tags test improved
authorAlexander Kampmann <programmer@nurfuerspam.de>
Mon, 12 Mar 2012 12:59:00 +0000 (13:59 +0100)
committerAlexander Kampmann <programmer@nurfuerspam.de>
Mon, 12 Mar 2012 12:59:00 +0000 (13:59 +0100)
mod/item.php
tests/get_tags_test.php

index a9edf0da66f7461466861e46bf30c8f324c96757..0ff7f6a7c8f43e176d4eb93ec8a71cc8e761a1cd 100755 (executable)
@@ -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))\r
-               unset($profile);\r
+       //is it a hash tag? 
        if(strpos($tag,'#') === 0) {\r
-               if(strpos($tag,'[url='))\r
-                       continue;\r
+               //if the tag is replaced...
+               if(strpos($tag,'[url='))
+                       //...do nothing\r
+                       continue;
+               //base tag has the tags name only\r
                $basetag = str_replace('_',' ',substr($tag,1));\r
-               $body = str_replace($tag,'#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]',$body);\r
-       \r
-               $newtag = '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';\r
+               //create text for link
+               $newtag = '#[url=' . $a->get_baseurl() . '/search?search=' . rawurlencode($basetag) . ']' . $basetag . '[/url]';
+               //replace tag by the link\r
+               $body = str_replace($tag, $newtag, $body);\r
+       
+               //is the link already in str_tags?\r
                if(! stristr($str_tags,$newtag)) {\r
+                       //append or set str_tags
                        if(strlen($str_tags))\r
                                $str_tags .= ',';\r
                        $str_tags .= $newtag;\r
                }\r
                continue;\r
-       }\r
+       }
+       //is it a person tag? \r
        if(strpos($tag,'@') === 0) {\r
+               //is it already replaced? 
                if(strpos($tag,'[url='))\r
                        continue;\r
                $stat = false;\r
-               $name = substr($tag,1);\r
+               //get the person's name
+               $name = substr($tag,1);
+               //is it a link or a full dfrn address? \r
                if((strpos($name,'@')) || (strpos($name,'http://'))) {\r
                        $newname = $name;\r
+                       //get the profile links
                        $links = @lrdd($name);\r
                        if(count($links)) {\r
+                               //for all links, collect how is to inform and how's profile is to link
                                foreach($links as $link) {\r
                                        if($link['@attributes']['rel'] === 'http://webfinger.net/rel/profile-page')\r
                                                $profile = $link['@attributes']['href'];\r
@@ -857,38 +878,44 @@ function handle_body(&$body, &$inform, &$str_tags, $profile_uid, $tag) {
                                        }\r
                                }\r
                        }\r
-               }\r
-               else {\r
+               } else { //if it is a name rather than an address\r
                        $newname = $name;\r
                        $alias = '';\r
-                       $tagcid = 0;\r
+                       $tagcid = 0;
+                       //is it some generated name?\r
                        if(strrpos($newname,'+')) {\r
+                               //get the id
                                $tagcid = intval(substr($newname,strrpos($newname,'+') + 1));\r
+                               //remove the next word from tag's name
                                if(strpos($name,' '))\r
                                        $name = substr($name,0,strpos($name,' '));\r
-                       }\r
-                       if($tagcid) {\r
+                       }
+                       if($tagcid) { //if there was an id
+                               //select contact with that id from the logged in user's contact list\r
                                $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",\r
                                                intval($tagcid),\r
                                                intval($profile_uid)\r
                                );\r
-                       }\r
-                       elseif(strstr($name,'_') || strstr($name,' ')) {\r
+                       } elseif(strstr($name,'_') || strstr($name,' ')) { //no id
+                               //get the real name\r
                                $newname = str_replace('_',' ',$name);\r
+                               //select someone from this user's contacts by name
                                $r = q("SELECT * FROM `contact` WHERE `name` = '%s' AND `uid` = %d LIMIT 1",\r
                                                dbesc($newname),\r
                                                intval($profile_uid)\r
                                );\r
-                       }\r
-                       else {\r
+                       } else {
+                               //select someone by attag or nick and the name passed in\r
                                $r = q("SELECT * FROM `contact` WHERE `attag` = '%s' OR `nick` = '%s' AND `uid` = %d ORDER BY `attag` DESC LIMIT 1",\r
                                                dbesc($name),\r
                                                dbesc($name),\r
                                                intval($profile_uid)\r
                                );\r
-                       }\r
+                       }
+                       //$r is set, if someone could be selected\r
                        if(count($r)) {\r
-                               $profile = $r[0]['url'];\r
+                               $profile = $r[0]['url'];
+                               //set newname to nick, find alias\r
                                if($r[0]['network'] === 'stat') {\r
                                        $newname = $r[0]['nick'];\r
                                        $stat = true;\r
@@ -897,15 +924,19 @@ function handle_body(&$body, &$inform, &$str_tags, $profile_uid, $tag) {
                                }\r
                                else\r
                                        $newname = $r[0]['name'];\r
+                               //add person's id to $inform
                                if(strlen($inform))\r
                                        $inform .= ',';\r
                                $inform .= 'cid:' . $r[0]['id'];\r
                        }\r
-               }\r
-               if($profile) {\r
-                       $body = str_replace('@' . $name, '@' . '[url=' . $profile . ']' . $newname      . '[/url]', $body);\r
+               }
+               //if there is an url for this persons profile\r
+               if(isset($profile)) {\r
+                       //create profile link
                        $profile = str_replace(',','%2c',$profile);\r
                        $newtag = '@[url=' . $profile . ']' . $newname  . '[/url]';\r
+                       $body = str_replace('@' . $name, $newtag, $body);\r
+                       //append tag to str_tags
                        if(! stristr($str_tags,$newtag)) {\r
                                if(strlen($str_tags))\r
                                        $str_tags .= ',';\r
index a458f0fbc59cb5d2eb8b28332372198a390a016f..ee2daced101d237c98f4dfe7ebb299fcb871f5b1 100644 (file)
@@ -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]) {\r
+                       return $result;\r
+               }
+       }
+       throw new Exception($str);
+       //third call in handle_body, nick or attag
+       if($result[0]['nick']===$args[2] || $result[0]['attag']===$args[1]) {\r
+               return $result;\r
+       }
+//     throw new Exception("Nothing fitted: ".$args[1].", ".$args[2]); 
 }
+
 function dbesc($str) {
-       echo $str; 
+       return $str; 
 }
 
 class GetTagsTest extends PHPUnit_Framework_TestCase {\r
@@ -39,7 +73,7 @@ class GetTagsTest extends PHPUnit_Framework_TestCase {
                handle_body($text, $inform, $str_tags, 11, $tags[0]);
 \r
                $this->assertEquals("@Mike", $tags[0]);
-               $this->assertEquals($text, "hi @[url=http://justatest.de]Mike[/url]");\r
+               $this->assertEquals("hi @[url=http://justatest.de]Mike Lastname[/url]", $text);\r
        }\r
 
        /**