From 95c05e49a70d007f41be60f90a7ae0ed7e5996a2 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 30 Sep 2010 07:58:07 +0200 Subject: remove deprecate split() --- src/SemanticScuttle/Service/User.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/User.php b/src/SemanticScuttle/Service/User.php index 6fc3bb7..d74d104 100644 --- a/src/SemanticScuttle/Service/User.php +++ b/src/SemanticScuttle/Service/User.php @@ -359,7 +359,7 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService return $_SESSION[$this->getSessionKey()]; } else if (isset($_COOKIE[$this->getCookieKey()])) { - $cook = split(':', $_COOKIE[$this->getCookieKey()]); + $cook = explode(':', $_COOKIE[$this->getCookieKey()]); //cookie looks like this: 'id:md5(username+password)' $query = 'SELECT * FROM '. $this->getTableName() . ' WHERE MD5(CONCAT('.$this->getFieldName('username') . -- cgit v1.2.3 From 04d360dadbdcd492d3a1e5b7b6105007ad841987 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 1 Oct 2010 18:21:16 +0200 Subject: CS, docblocks --- src/SemanticScuttle/Service/Tag2Tag.php | 45 ++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 7 deletions(-) (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Tag2Tag.php b/src/SemanticScuttle/Service/Tag2Tag.php index 8666209..33d211b 100644 --- a/src/SemanticScuttle/Service/Tag2Tag.php +++ b/src/SemanticScuttle/Service/Tag2Tag.php @@ -14,7 +14,7 @@ */ /** - * SemanticScuttle tag-to-tag service. + * SemanticScuttle tag-to-tag service which works with tag relations. * * @category Bookmarking * @package SemanticScuttle @@ -102,18 +102,49 @@ class SemanticScuttle_Service_Tag2Tag extends SemanticScuttle_DbService return true; } - // Return linked tags just for admin users - function getAdminLinkedTags($tag, $relationType, $inverseRelation = false, $stopList = array()) { + + + /** + * Same as getLinkedTags(), but only tags that have been created + * by admin users are returned. + * + * @param string $tag Tag to get related tags for + * @param string $relationType Type of tag-to-tag relation: >, < or = + * @param boolean $inverseRelation Reverse relation (parent -> child) + * @param array $stopList Array of tags that shall not be returned + * + * @return array Array of tag names + * + * @see getLinkedTags() + */ + public function getAdminLinkedTags( + $tag, $relationType, $inverseRelation = false, $stopList = array() + ) { // look for admin ids $userservice = SemanticScuttle_Service_Factory :: get('User'); - $adminIds = $userservice->getAdminIds(); + $adminIds = $userservice->getAdminIds(); //ask for their linked tags - return $this->getLinkedTags($tag, $relationType, $adminIds, $inverseRelation, $stopList); + return $this->getLinkedTags( + $tag, $relationType, $adminIds, $inverseRelation, $stopList + ); } - // Return the target linked tags. If inverseRelation is true, return the source linked tags. - function getLinkedTags($tag, $relationType, $uId = null, $inverseRelation = false, $stopList = array()) { + + + /** + * Returns an array of tags that are in relation to the given $tag. + * + * @param string $tag Tag to get related tags for + * @param string $relationType Type of tag-to-tag relation: >, < or = + * @param boolean $inverseRelation Reverse relation (parent -> child) + * @param array $stopList Array of tags that shall not be returned + * + * @return array Array of tag names + */ + public function getLinkedTags( + $tag, $relationType, $uId = null, $inverseRelation = false, $stopList = array() + ) { // Set up the SQL query. if($inverseRelation) { $queriedTag = "tag1"; -- cgit v1.2.3 From be126cc958212ed74fe916eabcb1e6ac928df5e4 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 8 Oct 2010 17:43:05 +0200 Subject: tests for getadminlinkedtags --- src/SemanticScuttle/Service/User.php | 18 ++-- tests/TestBase.php | 28 ++++++ tests/ajax/GetAdminLinkedTagsTest.php | 160 ++++++++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 tests/ajax/GetAdminLinkedTagsTest.php (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/User.php b/src/SemanticScuttle/Service/User.php index d74d104..fd9d84f 100644 --- a/src/SemanticScuttle/Service/User.php +++ b/src/SemanticScuttle/Service/User.php @@ -203,18 +203,26 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService } } - /* Takes an numerical "id" or a string "username" - and returns the numerical "id" if the user exists else returns NULL */ - function getIdFromUser($user) { + /** + * Obtains the ID of the given user name. + * If a user ID is passed, it is returned. + * In case the user does not exist, NULL is returned. + * + * @param string|integer $user User name or user ID + * + * @return integer NULL if not found or the user ID + */ + public function getIdFromUser($user) + { if (is_int($user)) { return intval($user); } else { $objectUser = $this->getObjectUserByUsername($user); - if($objectUser != NULL) { + if ($objectUser != null) { return $objectUser->getId(); } } - return NULL; + return null; } /** diff --git a/tests/TestBase.php b/tests/TestBase.php index 402330b..c0acd58 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -104,6 +104,34 @@ class TestBase extends PHPUnit_Framework_TestCase return $uid; } + + + /** + * Retrieves the UID of an admin user. + * If that user does not exist in the database, it is created. + * + * @return integer UID of admin user + */ + protected function getAdminUser() + { + if (count($GLOBALS['admin_users']) == 0) { + $this->fail('No admin users configured'); + } + $adminUserName = reset($GLOBALS['admin_users']); + + $us = SemanticScuttle_Service_Factory::get('User'); + $uid = $us->getIdFromUser($adminUserName); + if ($uid === null) { + //that user does not exist in the database; create it + $uid = $us->addUser( + $adminUserName, + rand(), + 'unittest-admin-' . $adminUserName . '@example.org' + ); + } + + return $uid; + } } ?> \ No newline at end of file diff --git a/tests/ajax/GetAdminLinkedTagsTest.php b/tests/ajax/GetAdminLinkedTagsTest.php new file mode 100644 index 0000000..d8ec447 --- /dev/null +++ b/tests/ajax/GetAdminLinkedTagsTest.php @@ -0,0 +1,160 @@ + + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once dirname(__FILE__) . '/../prepare.php'; +require_once 'HTTP/Request2.php'; + +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'ajax_GetAdminLinkedTagsTest::main'); +} + +/** + * Unit tests for the ajax linked admin tags script + * + * @category Bookmarking + * @package SemanticScuttle + * @author Christian Weiske + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ +class ajax_GetAdminLinkedTagsTest extends TestBaseApi +{ + protected $urlPart = 'ajax/getadminlinkedtags.php'; + + + + /** + * Used to run this test class standalone + * + * @return void + */ + public static function main() + { + require_once 'PHPUnit/TextUI/TestRunner.php'; + PHPUnit_TextUI_TestRunner::run( + new PHPUnit_Framework_TestSuite(__CLASS__) + ); + } + + + + /** + * Verify that we get the configured root tags if + * we do not pass any parameters + */ + public function testRootTags() + { + $req = $this->getRequest(); + $res = $req->send(); + + $this->assertEquals(200, $res->getStatus()); + $this->assertEquals( + 'application/json; charset=utf-8', + $res->getHeader('content-type') + ); + + $data = json_decode($res->getBody()); + $this->assertType('array', $data); + + //same number of elements as the menu2Tags array + $this->assertEquals( + count($GLOBALS['menu2Tags']), + count($data) + ); + + //and the same contents + foreach ($data as $tagObj) { + $tagName = $tagObj->data->title; + $this->assertContains($tagName, $GLOBALS['menu2Tags']); + } + } + + /** + * Verify that we get subtags of a given tag + */ + public function testSubTags() + { + $t2t = SemanticScuttle_Service_Factory::get('Tag2Tag'); + $t2t->deleteAll(); + + $menu2Tag = reset($GLOBALS['menu2Tags']); + //we have a subtag now + $this->addBookmark( + $this->getAdminUser(), + null, + 0, + $menu2Tag . '>adminsubtag' + ); + + $res = $this->getRequest('?tag=' . $menu2Tag)->send(); + $this->assertEquals(200, $res->getStatus()); + $this->assertEquals( + 'application/json; charset=utf-8', + $res->getHeader('content-type') + ); + + $data = json_decode($res->getBody()); + $this->assertType('array', $data); + + //only one subtag + $this->assertEquals(1, count($data)); + $this->assertEquals('adminsubtag', $data[0]->data->title); + } + + /** + * Verify that we only get admin tags, not tags from + * non-admin people + */ + public function testOnlyAdminTags() + { + $t2t = SemanticScuttle_Service_Factory::get('Tag2Tag'); + $t2t->deleteAll(); + + $menu2Tag = reset($GLOBALS['menu2Tags']); + //we have a subtag now + $this->addBookmark( + $this->getAdminUser(), + null, + 0, + $menu2Tag . '>adminsubtag' + ); + //add another bookmark now, but for a normal user + $this->addBookmark( + null, + null, + 0, + $menu2Tag . '>normalsubtag' + ); + + $res = $this->getRequest('?tag=' . $menu2Tag)->send(); + $this->assertEquals(200, $res->getStatus()); + $this->assertEquals( + 'application/json; charset=utf-8', + $res->getHeader('content-type') + ); + + $data = json_decode($res->getBody()); + $this->assertType('array', $data); + + //we should have only one subtag now, the admin one + $this->assertEquals(1, count($data)); + $this->assertEquals('adminsubtag', $data[0]->data->title); + } +} + +if (PHPUnit_MAIN_METHOD == 'ajax_GetAdminLinkedTagsTest::main') { + ajax_GetAdminLinkedTagsTest::main(); +} +?> \ No newline at end of file -- cgit v1.2.3 From 29e7cf1aa5ca0e387fdb61372c7a590def694a2a Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 19 Nov 2010 06:51:01 +0100 Subject: Fix bug #3111254: Search in my_watchlist results in error --- doc/ChangeLog | 1 + src/SemanticScuttle/Service/User.php | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) (limited to 'src/SemanticScuttle/Service') diff --git a/doc/ChangeLog b/doc/ChangeLog index 0a60bff..60401d4 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -6,6 +6,7 @@ ChangeLog for SemantiScuttle - Fix bug in getTagsForBookmarks() that fetched all tags - Fix bug #3073215: Updating bookmark time does not work - Fix bug #3074816: French translation breaks edit javascript +- Fix bug #3111254: Search in my_watchlist results in error - Show error message on mysqli connection errors - Implement patch #3059829: update FR_CA translation - Update php-gettext library to 1.0.10 diff --git a/src/SemanticScuttle/Service/User.php b/src/SemanticScuttle/Service/User.php index fd9d84f..9ef8430 100644 --- a/src/SemanticScuttle/Service/User.php +++ b/src/SemanticScuttle/Service/User.php @@ -364,7 +364,7 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService public function getCurrentUserId() { if (isset($_SESSION[$this->getSessionKey()])) { - return $_SESSION[$this->getSessionKey()]; + return (int)$_SESSION[$this->getSessionKey()]; } else if (isset($_COOKIE[$this->getCookieKey()])) { $cook = explode(':', $_COOKIE[$this->getCookieKey()]); @@ -385,10 +385,10 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService if ($row = $this->db->sql_fetchrow($dbresult)) { $this->setCurrentUserId( - $row[$this->getFieldName('primary')] + (int)$row[$this->getFieldName('primary')] ); $this->db->sql_freeresult($dbresult); - return $_SESSION[$this->getSessionKey()]; + return (int)$_SESSION[$this->getSessionKey()]; } } return false; -- cgit v1.2.3 From 82ada0d75f249733936a0826b115b20cba0657ab Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 15 Mar 2011 19:13:14 +0100 Subject: Implement request #3054906: Show user's full name instead of nickname --- .../bookmarkcommondescriptionedit.tpl.php | 3 +- data/templates/bookmarks.tpl.php | 3 +- data/templates/sidebar.block.users.php | 2 +- data/templates/tagcommondescriptionedit.tpl.php | 3 +- data/templates/users.tpl.php | 9 ++++- doc/ChangeLog | 5 +-- src/SemanticScuttle/Model/UserArray.php | 41 ++++++++++++++++++++++ src/SemanticScuttle/Service/Bookmark.php | 3 +- src/SemanticScuttle/header.php | 1 + www/rss.php | 2 +- 10 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 src/SemanticScuttle/Model/UserArray.php (limited to 'src/SemanticScuttle/Service') diff --git a/data/templates/bookmarkcommondescriptionedit.tpl.php b/data/templates/bookmarkcommondescriptionedit.tpl.php index af5909a..807c58b 100644 --- a/data/templates/bookmarkcommondescriptionedit.tpl.php +++ b/data/templates/bookmarkcommondescriptionedit.tpl.php @@ -30,7 +30,8 @@ window.onload = function() { if(strlen($description['cdDatetime'])>0) { echo T_('Last modification:').' '.$description['cdDatetime'].', '; $lastUser = $userservice->getUser($description['uId']); - echo ''.$lastUser['username'].''; + echo '' + . SemanticScuttle_Model_UserArray::getName($lastUser) . ''; } ?> diff --git a/data/templates/bookmarks.tpl.php b/data/templates/bookmarks.tpl.php index e32d3c9..c404358 100644 --- a/data/templates/bookmarks.tpl.php +++ b/data/templates/bookmarks.tpl.php @@ -309,7 +309,8 @@ if ($currenttag!= '') { $copy .= T_('you'); } else { $copy .= '' - . $row['username'] . ''; + . SemanticScuttle_Model_UserArray::getName($row) + . ''; } // Udders! diff --git a/data/templates/sidebar.block.users.php b/data/templates/sidebar.block.users.php index 3ad18bc..826871e 100644 --- a/data/templates/sidebar.block.users.php +++ b/data/templates/sidebar.block.users.php @@ -18,7 +18,7 @@ if ($lastUsers && count($lastUsers) > 0) { foreach ($lastUsers as $row) { echo ''; echo ''; - echo $row['username']; + echo SemanticScuttle_Model_UserArray::getName($row); echo ''; echo ' ('.T_('bookmarks').')'; echo ''; diff --git a/data/templates/tagcommondescriptionedit.tpl.php b/data/templates/tagcommondescriptionedit.tpl.php index d3a006a..f938f93 100644 --- a/data/templates/tagcommondescriptionedit.tpl.php +++ b/data/templates/tagcommondescriptionedit.tpl.php @@ -20,7 +20,8 @@ window.onload = function() { if(strlen($description['cdDatetime'])>0) { echo T_('Last modification:').' '.$description['cdDatetime'].', '; $lastUser = $userservice->getUser($description['uId']); - echo ''.$lastUser['username'].''; + echo '' + . SemanticScuttle_Model_UserArray::getName($lastUser) . ''; } ?> diff --git a/data/templates/users.tpl.php b/data/templates/users.tpl.php index c209f94..fa92bef 100644 --- a/data/templates/users.tpl.php +++ b/data/templates/users.tpl.php @@ -14,7 +14,14 @@ if ($users && count($users) > 0) { '.$row['username'].' ('.T_('profile').' '.T_('created in').' '.date('M Y',strtotime($row['uDatetime'])).') : '.T_('bookmarks').''; + echo '
  • ' + . SemanticScuttle_Model_UserArray::getName($row) . '' + . ' (' + . T_('profile') . ' ' + . T_('created in') . ' ' + . date('M Y', strtotime($row['uDatetime'])) . ')' + . ' : ' + . T_('bookmarks') . '
  • '; } ?> diff --git a/doc/ChangeLog b/doc/ChangeLog index 6144a81..4c93a9a 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -3,12 +3,13 @@ ChangeLog for SemantiScuttle 0.9X.X - 2010-XX-XX ------------------- +- Fix bug #3187177: Wrong URL / Export XML Bookmarks - Fix bug in getTagsForBookmarks() that fetched all tags -- Show error message on mysqli connection errors +- Implement request #3054906: Show user's full name instead of nickname - Implement patch #3059829: update FR_CA translation +- Show error message on mysqli connection errors - Update php-gettext library to 1.0.10 - api/posts/add respects the "replace" parameter now -- Fix bug #3187177: Wrong URL / Export XML Bookmarks 0.97.2 - 2011-02-17 diff --git a/src/SemanticScuttle/Model/UserArray.php b/src/SemanticScuttle/Model/UserArray.php new file mode 100644 index 0000000..a0d9c9b --- /dev/null +++ b/src/SemanticScuttle/Model/UserArray.php @@ -0,0 +1,41 @@ + + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +/** + * Mostly static methods that help working with a user row array from database. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Christian Weiske + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ +class SemanticScuttle_Model_UserArray +{ + /** + * Returns full user name as specified in the profile if it is set, + * otherwise the nickname/loginname is returned. + * + * @param array $row User row array from database + * + * @return string Full name or username + */ + public static function getName($row) + { + if (isset($row['name']) && $row['name']) { + return $row['name']; + } + return $row['username']; + } +} +?> \ No newline at end of file diff --git a/src/SemanticScuttle/Service/Bookmark.php b/src/SemanticScuttle/Service/Bookmark.php index 6f8a172..a30ad5f 100644 --- a/src/SemanticScuttle/Service/Bookmark.php +++ b/src/SemanticScuttle/Service/Bookmark.php @@ -734,7 +734,8 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService if (SQL_LAYER == 'mysql4') { $query_1 .= 'SQL_CALC_FOUND_ROWS '; } - $query_1 .= 'B.*, U.'. $userservice->getFieldName('username'); + $query_1 .= 'B.*, U.'. $userservice->getFieldName('username') + . ', U.name'; $query_2 = ' FROM '. $userservice->getTableName() .' AS U' . ', '. $this->getTableName() .' AS B'; diff --git a/src/SemanticScuttle/header.php b/src/SemanticScuttle/header.php index d1a5c29..4fecb8f 100644 --- a/src/SemanticScuttle/header.php +++ b/src/SemanticScuttle/header.php @@ -68,6 +68,7 @@ require_once 'SemanticScuttle/Service.php'; require_once 'SemanticScuttle/DbService.php'; require_once 'SemanticScuttle/Service/Factory.php'; require_once 'SemanticScuttle/functions.php'; +require_once 'SemanticScuttle/Model/UserArray.php'; if (count($GLOBALS['serviceoverrides']) > 0 && !defined('UNIT_TEST_MODE') diff --git a/www/rss.php b/www/rss.php index 6dcfb4c..298d9ba 100644 --- a/www/rss.php +++ b/www/rss.php @@ -116,7 +116,7 @@ foreach ($bookmarks_tmp as $key => $row) { 'title' => $row['bTitle'], 'link' => $_link, 'description' => $row['bDescription'], - 'creator' => $row['username'], + 'creator' => SemanticScuttle_Model_UserArray::getName($row), 'pubdate' => $_pubdate, 'tags' => $row['tags'] ); -- cgit v1.2.3 From 5c480c9babcd20c8701fc5e3f651a1e9082d4e8f Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 23 Mar 2011 18:52:01 +0100 Subject: format getPopularTags and add docblock --- src/SemanticScuttle/Service/Bookmark2Tag.php | 75 +++++++++++++++++++++------- 1 file changed, 56 insertions(+), 19 deletions(-) (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index 4d2c969..e3997dd 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -477,35 +477,70 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService return $this->getPopularTags($contacts, $limit, $logged_on_user, $days); } - // $users can be {NULL, an id, an array of id} - function &getPopularTags($user = NULL, $limit = 30, $logged_on_user = NULL, $days = NULL) { + /** + * The the most popular tags and their usage count + * + * @param mixed $user Integer user ID or array of user IDs to limit tag + * finding to + * @param integer $limit Number of tags to return + * @param integer $logged_on_user ID of the user that's currently logged in. + * If the logged in user equals the $user to find + * tags for, tags of private bookmarks are + * returned. + * @param integer $days Bookmarks have to be changed in the last X days + * if their tags shall count* + * + * @return array Array of found tags. Each tag entry is an array with two keys, + * 'tag' (tag name) and 'bCount'. + */ + public function getPopularTags( + $user = null, $limit = 30, $logged_on_user = null, $days = null + ) { // Only count the tags that are visible to the current user. - if (($user != $logged_on_user) || is_null($user) || ($user === false)) - $privacy = ' AND B.bStatus = 0'; - else - $privacy = ''; + if (($user != $logged_on_user) || is_null($user) || ($user === false)) { + $privacy = ' AND B.bStatus = 0'; + } else { + $privacy = ''; + } - if (is_null($days) || !is_int($days)) - $span = ''; - else - $span = ' AND B.bDatetime > "'. date('Y-m-d H:i:s', time() - (86400 * $days)) .'"'; + if (is_null($days) || !is_int($days)) { + $span = ''; + } else { + $span = ' AND B.bDatetime > "' + . date('Y-m-d H:i:s', time() - (86400 * $days)) + . '"'; + } + + $query = 'SELECT' + . ' T.tag, COUNT(T.bId) AS bCount' + . ' FROM ' + . $this->getTableName() . ' AS T' + . ', ' . $GLOBALS['tableprefix'] . 'bookmarks AS B' + . ' WHERE'; - $query = 'SELECT T.tag, COUNT(T.bId) AS bCount FROM '. $this->getTableName() .' AS T, '. $GLOBALS['tableprefix'] .'bookmarks AS B WHERE '; if (is_null($user) || ($user === false)) { - $query .= 'B.bId = T.bId AND B.bStatus = 0'; - } elseif(is_array($user)) { + $query .= ' B.bId = T.bId AND B.bStatus = 0'; + } else if (is_array($user)) { $query .= ' (1 = 0'; //tricks - foreach($user as $u) { - $query .= ' OR B.uId = '. $this->db->sql_escape($u) .' AND B.bId = T.bId'; + foreach ($user as $u) { + $query .= ' OR B.uId = ' . $this->db->sql_escape($u) + . ' AND B.bId = T.bId'; } $query .= ' )'; } else { - $query .= 'B.uId = '. $this->db->sql_escape($user) .' AND B.bId = T.bId'. $privacy; + $query .= ' B.uId = ' . $this->db->sql_escape($user) + . ' AND B.bId = T.bId' . $privacy; } - $query .= $span .' AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC, tag'; - if (!($dbresult =& $this->db->sql_query_limit($query, $limit))) { - message_die(GENERAL_ERROR, 'Could not get popular tags', '', __LINE__, __FILE__, $query, $this->db); + $query .= $span . ' AND LEFT(T.tag, 7) <> "system:"' + . ' GROUP BY T.tag' + . ' ORDER BY bCount DESC, tag'; + + if (!($dbresult = $this->db->sql_query_limit($query, $limit))) { + message_die( + GENERAL_ERROR, 'Could not get popular tags', + '', __LINE__, __FILE__, $query, $this->db + ); return false; } @@ -514,6 +549,8 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService return $output; } + + function hasTag($bookmarkid, $tag) { $query = 'SELECT COUNT(*) AS tCount FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid) .' AND tag ="'. $this->db->sql_escape($tag) .'"'; -- cgit v1.2.3 From cb28cd3371e215646765f6d1d06263a54a5526f1 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 23 Mar 2011 19:11:38 +0100 Subject: unittest for getAdminTags as well as CS and docblock fixes on it --- src/SemanticScuttle/Service/Bookmark2Tag.php | 30 +++++++++++++++++++++++++--- tests/Bookmark2TagTest.php | 20 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index e3997dd..d367b62 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -454,15 +454,37 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService return $output; } - function &getAdminTags($limit = 30, $logged_on_user = NULL, $days = NULL) { + + + /** + * Returns the tags used by admin users + * + * @param integer $limit Number of tags to return + * @param integer $logged_on_user ID of the user that's currently logged in. + * If the logged in user equals the $user to find + * tags for, tags of private bookmarks are + * returned. + * @param integer $days Bookmarks have to be changed in the last X days + * if their tags shall count* + * + * @return array Array of found tags. Each tag entry is an array with two keys, + * 'tag' (tag name) and 'bCount'. + * + * @see getPopularTags() + */ + public function getAdminTags( + $limit = 30, $logged_on_user = null, $days = null + ) { // look for admin ids - $userservice = SemanticScuttle_Service_Factory :: get('User'); - $adminIds = $userservice->getAdminIds(); + $userservice = SemanticScuttle_Service_Factory::get('User'); + $adminIds = $userservice->getAdminIds(); // ask for their tags return $this->getPopularTags($adminIds, $limit, $logged_on_user, $days); } + + function &getContactTags($user, $limit = 30, $logged_on_user = NULL, $days = NULL) { // look for contact ids $userservice = SemanticScuttle_Service_Factory :: get('User'); @@ -477,6 +499,8 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService return $this->getPopularTags($contacts, $limit, $logged_on_user, $days); } + + /** * The the most popular tags and their usage count * diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index a83a826..d85cf73 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -56,6 +56,7 @@ class Bookmark2TagTest extends TestBase protected function setUp() { $this->us = SemanticScuttle_Service_Factory::get('User'); + $this->us->deleteAll(); $this->bs = SemanticScuttle_Service_Factory::get('Bookmark'); $this->bs->deleteAll(); $this->b2ts= SemanticScuttle_Service_Factory::get('Bookmark2Tag'); @@ -447,6 +448,25 @@ class Bookmark2TagTest extends TestBase $this->assertContains(array('tag' => 'two', 'bCount' => '1'), $arTags); $this->assertContains(array('tag' => 'thr', 'bCount' => '1'), $arTags); } + + + public function testGetAdminTags() + { + $admin1 = $this->addUser('admin1'); + $admin2 = $this->addUser('admin2'); + $user1 = $this->addUser(); + $this->addBookmark($admin1, null, 0, array('admintag', 'admintag1')); + $this->addBookmark($admin2, null, 0, array('admintag', 'admintag2')); + $this->addBookmark($user1, null, 0, array('usertag')); + + $GLOBALS['admin_users'] = array('admin1', 'admin2'); + + $arTags = $this->b2ts->getAdminTags(4); + $this->assertEquals(3, count($arTags)); + $this->assertContains(array('tag' => 'admintag', 'bCount' => '2'), $arTags); + $this->assertContains(array('tag' => 'admintag1', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'admintag2', 'bCount' => '1'), $arTags); + } } if (PHPUnit_MAIN_METHOD == 'Bookmark2TagTest::main') { -- cgit v1.2.3 From 637e276ac7dc0e9acdbb79ed68415c1770ce7a3f Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 24 Mar 2011 08:36:54 +0100 Subject: unittest for Bookmark2Tag::getContactTags, CS and docblock --- src/SemanticScuttle/Service/Bookmark2Tag.php | 31 ++++++++++++-- tests/Bookmark2TagTest.php | 62 +++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 10 deletions(-) (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index d367b62..478b47e 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -485,13 +485,33 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService - function &getContactTags($user, $limit = 30, $logged_on_user = NULL, $days = NULL) { + + /** + * Returns the tags used by users that are part of the user's watchlist, + * and the current user's own tags. + * + * @param integer $user ID of the user to get the watchlist from + * @param integer $limit Number of tags to return + * @param integer $logged_on_user ID of the user that's currently logged in. + * If set, that user is added to the list of + * people to get the tags from + * @param integer $days Bookmarks have to be changed in the last X days + * if their tags shall count* + * + * @return array Array of found tags. Each tag entry is an array with two keys, + * 'tag' (tag name) and 'bCount'. + * + * @see getPopularTags() + */ + public function getContactTags( + $user, $limit = 30, $logged_on_user = null, $days = null + ) { // look for contact ids - $userservice = SemanticScuttle_Service_Factory :: get('User'); + $userservice = SemanticScuttle_Service_Factory::get('User'); $contacts = $userservice->getWatchlist($user); - // add the user (to show him/her also his/her tags) - if(!is_null($logged_on_user)) { + // add the user (to show him also his own tags) + if (!is_null($logged_on_user)) { $contacts[] = $logged_on_user; } @@ -516,6 +536,9 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. + * + * @see getAdminTags() + * @see getContactTags() */ public function getPopularTags( $user = null, $limit = 30, $logged_on_user = null, $days = null diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index d85cf73..ad64bf6 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -74,7 +74,7 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmark() when the bookmark has no tags * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmark */ public function testGetTagsForBookmarkNone() { @@ -92,7 +92,7 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmark() when the bookmark has one tag * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmark */ public function testGetTagsForBookmarkOne() { @@ -109,9 +109,9 @@ class Bookmark2TagTest extends TestBase /** - * Test getTagsForBookmark() when the bookmark has thr tags + * Test getTagsForBookmark() when the bookmark has three tags * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmark */ public function testGetTagsForBookmarkThr() { @@ -132,7 +132,7 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmarks() when no bookmarks have tags. * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmarks */ public function testGetTagsForBookmarksNone() { @@ -155,7 +155,7 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmarks() when most bookmarks have tags. * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmarks */ public function testGetTagsForBookmarksMost() { @@ -450,6 +450,9 @@ class Bookmark2TagTest extends TestBase } + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getAdminTags + */ public function testGetAdminTags() { $admin1 = $this->addUser('admin1'); @@ -467,6 +470,53 @@ class Bookmark2TagTest extends TestBase $this->assertContains(array('tag' => 'admintag1', 'bCount' => '1'), $arTags); $this->assertContains(array('tag' => 'admintag2', 'bCount' => '1'), $arTags); } + + + + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getContactTags + */ + public function testGetContactTagsWatchlistOnly() + { + $user1 = $this->addUser(); + $user2 = $this->addUser(); + $user3 = $this->addUser(); + $this->us->setCurrentUserId($user1); + $this->us->setWatchStatus($user2); + //user1 watches user2 now + + $this->addBookmark($user1, null, 0, array('usertag', 'usertag1')); + $this->addBookmark($user2, null, 0, array('usertag', 'usertag2')); + $this->addBookmark($user3, null, 0, array('usertag', 'usertag3')); + + $arTags = $this->b2ts->getContactTags($user1, 10); + $this->assertEquals(2, count($arTags)); + $this->assertContains(array('tag' => 'usertag', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'usertag2', 'bCount' => '1'), $arTags); + } + + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getContactTags + */ + public function testGetContactTagsIncludingUser() + { + $user1 = $this->addUser(); + $user2 = $this->addUser(); + $user3 = $this->addUser(); + $this->us->setCurrentUserId($user1); + $this->us->setWatchStatus($user2); + //user1 watches user2 now + + $this->addBookmark($user1, null, 0, array('usertag', 'usertag1')); + $this->addBookmark($user2, null, 0, array('usertag', 'usertag2')); + $this->addBookmark($user3, null, 0, array('usertag', 'usertag3')); + + $arTags = $this->b2ts->getContactTags($user1, 10, $user1); + $this->assertEquals(3, count($arTags)); + $this->assertContains(array('tag' => 'usertag', 'bCount' => '2'), $arTags); + $this->assertContains(array('tag' => 'usertag1', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'usertag2', 'bCount' => '1'), $arTags); + } } if (PHPUnit_MAIN_METHOD == 'Bookmark2TagTest::main') { -- cgit v1.2.3 From 0f481a99ea1dea8ec993c37f0613a48f97463e6b Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 24 Mar 2011 08:45:03 +0100 Subject: fix docblock --- src/SemanticScuttle/Service/Bookmark2Tag.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index 478b47e..83a8aed 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -465,7 +465,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * tags for, tags of private bookmarks are * returned. * @param integer $days Bookmarks have to be changed in the last X days - * if their tags shall count* + * if their tags shall count * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. @@ -496,7 +496,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * If set, that user is added to the list of * people to get the tags from * @param integer $days Bookmarks have to be changed in the last X days - * if their tags shall count* + * if their tags shall count * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. @@ -532,7 +532,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * tags for, tags of private bookmarks are * returned. * @param integer $days Bookmarks have to be changed in the last X days - * if their tags shall count* + * if their tags shall count * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. -- cgit v1.2.3 From 3f57237b88688f956add02bc18cd4642082fb526 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 24 Mar 2011 08:50:18 +0100 Subject: make getPopularTags a tiny bit easier --- src/SemanticScuttle/Service/Bookmark2Tag.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index 83a8aed..b69cfd4 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -550,14 +550,6 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService $privacy = ''; } - if (is_null($days) || !is_int($days)) { - $span = ''; - } else { - $span = ' AND B.bDatetime > "' - . date('Y-m-d H:i:s', time() - (86400 * $days)) - . '"'; - } - $query = 'SELECT' . ' T.tag, COUNT(T.bId) AS bCount' . ' FROM ' @@ -579,7 +571,13 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService . ' AND B.bId = T.bId' . $privacy; } - $query .= $span . ' AND LEFT(T.tag, 7) <> "system:"' + if (is_int($days)) { + $query .= ' AND B.bDatetime > "' + . date('Y-m-d H:i:s', time() - (86400 * $days)) + . '"'; + } + + $query .= ' AND LEFT(T.tag, 7) <> "system:"' . ' GROUP BY T.tag' . ' ORDER BY bCount DESC, tag'; -- cgit v1.2.3 From 30f84b3ca3242f00926f30c08ece4405be8b61fc Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 24 Mar 2011 19:07:50 +0100 Subject: fix privacy protection issue when fetching tags of several users --- src/SemanticScuttle/Service/Bookmark2Tag.php | 2 +- tests/Bookmark2TagTest.php | 69 ++++++++++++++++++++-------- 2 files changed, 50 insertions(+), 21 deletions(-) (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index b69cfd4..21ea0d5 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -565,7 +565,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService $query .= ' OR B.uId = ' . $this->db->sql_escape($u) . ' AND B.bId = T.bId'; } - $query .= ' )'; + $query .= ' )' . $privacy; } else { $query .= ' B.uId = ' . $this->db->sql_escape($user) . ' AND B.bId = T.bId' . $privacy; diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index ad64bf6..e2020dc 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -37,6 +37,26 @@ class Bookmark2TagTest extends TestBase protected $tts; + /** + * Create a bookmark. Like addBookmark(), just with other paramter order + * to make some tests in that class easier to write. + * + * @param integer $user User ID the bookmark shall belong + * @param array $tags Array of tags to attach. If "null" is given, + * it will automatically be "unittest" + * @param string $date strtotime-compatible string + * @param string $title Bookmark title + * + * @return integer ID of bookmark + */ + protected function addTagBookmark($user, $tags, $date = null, $title = null) + { + return $this->addBookmark( + $user, null, 0, $tags, $title, $date + ); + } + + /** * Used to run this test class standalone @@ -207,25 +227,6 @@ class Bookmark2TagTest extends TestBase } - /** - * Create a bookmark - * - * @param integer $user User ID the bookmark shall belong - * @param array $tags Array of tags to attach. If "null" is given, - * it will automatically be "unittest" - * @param string $date strtotime-compatible string - * @param string $title Bookmark title - * - * @return integer ID of bookmark - */ - protected function addTagBookmark($user, $tags, $date = null, $title = null) - { - return $this->addBookmark( - $user, null, 0, $tags, $title, $date - ); - } - - /** * Fetch the most popular tags in descending order @@ -398,10 +399,12 @@ class Bookmark2TagTest extends TestBase $this->assertContains(array('tag' => 'thr', 'bCount' => '1'), $arTags); } + + /** * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags */ - public function testGetPopularTagsPublicOnly() + public function testGetPopularTagsPublicOnlyNoUser() { $user1 = $this->addUser(); $this->addBookmark($user1, null, 0, array('one')); @@ -416,7 +419,13 @@ class Bookmark2TagTest extends TestBase ), $arTags ); + } + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags + */ + public function testGetPopularTagsPublicOnlySingleUser() + { $user1 = $this->addUser(); $this->addBookmark($user1, null, 0, array('one')); $this->addBookmark($user1, null, 1, array('one', 'two')); @@ -432,6 +441,26 @@ class Bookmark2TagTest extends TestBase ); } + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags + */ + public function testGetPopularTagsPublicOnlySeveralUsers() + { + $user1 = $this->addUser(); + $user2 = $this->addUser(); + $this->addBookmark($user1, null, 0, array('one')); + $this->addBookmark($user1, null, 1, array('one', 'two')); + $this->addBookmark($user1, null, 2, array('thr')); + $this->addBookmark($user2, null, 0, array('fou')); + $this->addBookmark($user2, null, 1, array('fiv')); + $this->addBookmark($user2, null, 2, array('six')); + + $arTags = $this->b2ts->getPopularTags(array($user1, $user2)); + $this->assertEquals(2, count($arTags)); + $this->assertContains(array('tag' => 'one', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'fou', 'bCount' => '1'), $arTags); + } + /** * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags */ -- cgit v1.2.3 From d761abb05e28ef4f345acef739e3b371b462f9fe Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Thu, 24 Mar 2011 19:11:06 +0100 Subject: docblock for deleteAll --- src/SemanticScuttle/Service/Bookmark2Tag.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index 21ea0d5..beb4185 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -674,7 +674,15 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService return $output; } - function deleteAll() { + + + /** + * Deletes all tags in bookmarks2tags + * + * @return void + */ + public function deleteAll() + { $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; $this->db->sql_query($query); } -- cgit v1.2.3 From e667feb0ca9ff30a063149a2ce20b3398585dd4f Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 25 Mar 2011 07:44:07 +0100 Subject: beginsWith-parameter for getPopulartags, getContactTags and getAdminTags --- src/SemanticScuttle/Service/Bookmark2Tag.php | 25 ++++++++--- tests/Bookmark2TagTest.php | 64 ++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 5 deletions(-) (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index beb4185..1dc0ffe 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -466,6 +466,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * returned. * @param integer $days Bookmarks have to be changed in the last X days * if their tags shall count + * @param string $beginsWith The tag name shall begin with that string * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. @@ -473,14 +474,16 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * @see getPopularTags() */ public function getAdminTags( - $limit = 30, $logged_on_user = null, $days = null + $limit = 30, $logged_on_user = null, $days = null, $beginsWith = null ) { // look for admin ids $userservice = SemanticScuttle_Service_Factory::get('User'); $adminIds = $userservice->getAdminIds(); // ask for their tags - return $this->getPopularTags($adminIds, $limit, $logged_on_user, $days); + return $this->getPopularTags( + $adminIds, $limit, $logged_on_user, $days, $beginsWith + ); } @@ -497,6 +500,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * people to get the tags from * @param integer $days Bookmarks have to be changed in the last X days * if their tags shall count + * @param string $beginsWith The tag name shall begin with that string * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. @@ -504,7 +508,8 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * @see getPopularTags() */ public function getContactTags( - $user, $limit = 30, $logged_on_user = null, $days = null + $user, $limit = 30, $logged_on_user = null, $days = null, + $beginsWith = null ) { // look for contact ids $userservice = SemanticScuttle_Service_Factory::get('User'); @@ -516,7 +521,9 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService } // ask for their tags - return $this->getPopularTags($contacts, $limit, $logged_on_user, $days); + return $this->getPopularTags( + $contacts, $limit, $logged_on_user, $days, $beginsWith + ); } @@ -533,6 +540,7 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * returned. * @param integer $days Bookmarks have to be changed in the last X days * if their tags shall count + * @param string $beginsWith The tag name shall begin with that string * * @return array Array of found tags. Each tag entry is an array with two keys, * 'tag' (tag name) and 'bCount'. @@ -541,7 +549,8 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * @see getContactTags() */ public function getPopularTags( - $user = null, $limit = 30, $logged_on_user = null, $days = null + $user = null, $limit = 30, $logged_on_user = null, $days = null, + $beginsWith = null ) { // Only count the tags that are visible to the current user. if (($user != $logged_on_user) || is_null($user) || ($user === false)) { @@ -577,6 +586,12 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService . '"'; } + if (!is_null($beginsWith)) { + $query .= ' AND T.tag LIKE \'' + . $this->db->sql_escape($beginsWith) + . '%\''; + } + $query .= ' AND LEFT(T.tag, 7) <> "system:"' . ' GROUP BY T.tag' . ' ORDER BY bCount DESC, tag'; diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index e2020dc..ffd83c3 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -334,6 +334,31 @@ class Bookmark2TagTest extends TestBase $this->assertContains(array('tag' => 'thr', 'bCount' => '2'), $arTags); } + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags + */ + public function testGetPopularTagsBeginsWith() + { + $user = $this->addUser(); + $this->addTagBookmark($user, array('one', 'two')); + $this->addTagBookmark($user, array('one', 'thr')); + $this->addTagBookmark($user, array('one', 'two')); + $this->addTagBookmark($user, array('one', 'thr')); + + $arTags = $this->b2ts->getPopularTags(null, 10, null, null, 'o'); + $this->assertEquals(1, count($arTags)); + $this->assertContains(array('tag' => 'one', 'bCount' => '4'), $arTags); + + $arTags = $this->b2ts->getPopularTags(null, 10, null, null, 'tw'); + $this->assertEquals(1, count($arTags)); + $this->assertContains(array('tag' => 'two', 'bCount' => '2'), $arTags); + + $arTags = $this->b2ts->getPopularTags(null, 10, null, null, 't'); + $this->assertEquals(2, count($arTags)); + $this->assertContains(array('tag' => 'two', 'bCount' => '2'), $arTags); + $this->assertContains(array('tag' => 'thr', 'bCount' => '2'), $arTags); + } + /** @@ -500,6 +525,23 @@ class Bookmark2TagTest extends TestBase $this->assertContains(array('tag' => 'admintag2', 'bCount' => '1'), $arTags); } + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getAdminTags + */ + public function testGetAdminTagsBeginsWith() + { + $admin1 = $this->addUser('admin1'); + $this->addBookmark($admin1, null, 0, array('admintag', 'admintag1')); + $this->addBookmark($admin1, null, 0, array('tester', 'testos')); + + $GLOBALS['admin_users'] = array('admin1'); + + $arTags = $this->b2ts->getAdminTags(4, null, null, 'test'); + $this->assertEquals(2, count($arTags)); + $this->assertContains(array('tag' => 'tester', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'testos', 'bCount' => '1'), $arTags); + } + /** @@ -546,6 +588,28 @@ class Bookmark2TagTest extends TestBase $this->assertContains(array('tag' => 'usertag1', 'bCount' => '1'), $arTags); $this->assertContains(array('tag' => 'usertag2', 'bCount' => '1'), $arTags); } + + /** + * @covers SemanticScuttle_Service_Bookmark2Tag::getContactTags + */ + public function testGetContactTagsBeginsWith() + { + $user1 = $this->addUser(); + $this->addBookmark($user1, null, 0, array('usertag', 'usertag1')); + $this->addBookmark($user1, null, 0, array('usable', 'undefined')); + $this->addBookmark($user1, null, 0, array('fußbad', 'usable')); + + $arTags = $this->b2ts->getContactTags($user1, 10, $user1, null, 'user'); + $this->assertEquals(2, count($arTags)); + $this->assertContains(array('tag' => 'usertag', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'usertag1', 'bCount' => '1'), $arTags); + + $arTags = $this->b2ts->getContactTags($user1, 10, $user1, null, 'us'); + $this->assertEquals(3, count($arTags)); + $this->assertContains(array('tag' => 'usertag', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'usertag1', 'bCount' => '1'), $arTags); + $this->assertContains(array('tag' => 'usable', 'bCount' => '2'), $arTags); + } } if (PHPUnit_MAIN_METHOD == 'Bookmark2TagTest::main') { -- cgit v1.2.3 From d6e99db40dc88de1782099b30941075ebc8dfa97 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 25 Mar 2011 08:00:32 +0100 Subject: do not generate invalid SQL when called with a not-so valid array --- src/SemanticScuttle/Service/Bookmark2Tag.php | 6 ++++-- tests/Bookmark2TagTest.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index 1dc0ffe..a10cb61 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -571,8 +571,10 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService } else if (is_array($user)) { $query .= ' (1 = 0'; //tricks foreach ($user as $u) { - $query .= ' OR B.uId = ' . $this->db->sql_escape($u) - . ' AND B.bId = T.bId'; + if (is_numeric($u)) { + $query .= ' OR B.uId = ' . $this->db->sql_escape($u) + . ' AND B.bId = T.bId'; + } } $query .= ' )' . $privacy; } else { diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index ffd83c3..fff4222 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -426,6 +426,23 @@ class Bookmark2TagTest extends TestBase + /** + * This may happen when the method is called with a problematic user array. + * In that case we may not generate invalid SQL or so. + * + * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags + */ + public function testGetPopularTagsUserArrayWithNull() + { + $user1 = $this->addUser(); + $this->addTagBookmark($user1, array('one')); + + $arTags = $this->b2ts->getPopularTags(array(null)); + $this->assertEquals(0, count($arTags)); + } + + + /** * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags */ -- cgit v1.2.3 From 90f29d6e671f4c24f75bdc4774f223c53bf0a46c Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Tue, 12 Apr 2011 08:47:44 +0200 Subject: first test for service factory --- src/SemanticScuttle/Service/Factory.php | 3 ++- tests/FactoryTest.php | 13 +++++++++++++ tests/phpunit.xml | 12 ++++++++---- 3 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 tests/FactoryTest.php (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Factory.php b/src/SemanticScuttle/Service/Factory.php index d7ff1d4..b661cdb 100644 --- a/src/SemanticScuttle/Service/Factory.php +++ b/src/SemanticScuttle/Service/Factory.php @@ -107,6 +107,7 @@ class SemanticScuttle_Service_Factory /** * Loads self::$db if it is not loaded already. + * Dies if the connection could not be established. * * @return void */ @@ -141,7 +142,7 @@ class SemanticScuttle_Service_Factory /** * Returns sql database object * - * @return void + * @return sql_db Database instance */ public static function getDb() { diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php new file mode 100644 index 0000000..980e92e --- /dev/null +++ b/tests/FactoryTest.php @@ -0,0 +1,13 @@ +assertInstanceOf( + 'sql_db', + SemanticScuttle_Service_Factory::getDb() + ); + } +} +?> \ No newline at end of file diff --git a/tests/phpunit.xml b/tests/phpunit.xml index 734fa95..86b7b60 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -1,8 +1,12 @@ - + - - . - + + ../src/SemanticScuttle/ + ../data/templates/ + ../www/ + \ No newline at end of file -- cgit v1.2.3