From e5dab0a740e71384031caadecb9f9e283572e8f2 Mon Sep 17 00:00:00 2001 From: cweiske Date: Tue, 16 Feb 2010 21:54:31 +0000 Subject: allow adding of certain addresses git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@654 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/TestBase.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/TestBase.php b/tests/TestBase.php index 05988a5..f23f678 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -29,11 +29,12 @@ class TestBase extends PHPUnit_Framework_TestCase /** * Create a new bookmark. * - * @param integer $user User ID the bookmark shall belong + * @param integer $user User ID the bookmark shall belong + * @param string $address Bookmark address to use * * @return integer ID of bookmark */ - protected function addBookmark($user = null) + protected function addBookmark($user = null, $address = null) { if ($user === null) { $user = $this->addUser(); @@ -41,8 +42,13 @@ class TestBase extends PHPUnit_Framework_TestCase $bs = SemanticScuttle_Service_Factory::get('Bookmark'); $rand = rand(); + + if ($address === null) { + $address = 'http://example.org/' . $rand; + } + $bid = $bs->addBookmark( - 'http://example.org/' . $rand, + $address, 'unittest bookmark #' . $rand, 'description', null, -- cgit v1.2.3 From 157adfd7eb157917d338571426ad94539c37c3f8 Mon Sep 17 00:00:00 2001 From: cweiske Date: Tue, 16 Feb 2010 21:55:50 +0000 Subject: multiple tests for SemanticScuttle_Service_Bookmark::countOthers() git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@655 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/BookmarkTest.php | 101 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) (limited to 'tests') diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php index 741b6bf..ca4ff3e 100644 --- a/tests/BookmarkTest.php +++ b/tests/BookmarkTest.php @@ -778,6 +778,107 @@ class BookmarkTest extends TestBase $this->assertEquals('newShortNambb', $bm['bShort']); } + + + /** + * Test what countOther() returns when the address does not exist + * + * @return void + */ + public function testCountOthersAddressDoesNotExist() + { + $this->assertEquals(0, $this->bs->countOthers('http://example.org')); + } + + + + /** + * Test what countOther() returns when nobody else has the same bookmark + * + * @return void + */ + public function testCountOthersNone() + { + $uid = $this->addUser(); + $address = 'http://example.org'; + $this->addBookmark($uid, $address); + $this->assertEquals(0, $this->bs->countOthers($address)); + } + + + + /** + * Test what countOther() returns when the address exists only once + * and multiple bookmarks are in the database. + * + * @return void + */ + public function testCountOthersMultipleNone() + { + $uid = $this->addUser(); + $address = 'http://example.org'; + $this->addBookmark($uid, $address); + $this->addBookmark($uid); + $this->addBookmark($uid); + $this->assertEquals(0, $this->bs->countOthers($address)); + } + + + + /** + * Test what countOther() returns when the address exists only once + * and the same user and other users have other bookmarks + * + * @return void + */ + public function testCountOthersMultipleUsersNone() + { + $uid = $this->addUser(); + $uid2 = $this->addUser(); + $address = 'http://example.org'; + $this->addBookmark($uid, $address); + $this->addBookmark($uid); + $this->addBookmark($uid2); + $this->assertEquals(0, $this->bs->countOthers($address)); + } + + + + /** + * Test what countOther() returns when the address exists two + * times in the database. + * + * @return void + */ + public function testCountOthersOne() + { + $uid = $this->addUser(); + $uid2 = $this->addUser(); + $address = 'http://example.org'; + $this->addBookmark($uid, $address); + $this->addBookmark($uid2, $address); + $this->assertEquals(1, $this->bs->countOthers($address)); + } + + + + /** + * Test what countOther() returns when the address exists four + * times in the database. + * + * @return void + */ + public function testCountOthersThree() + { + $uid = $this->addUser(); + $address = 'http://example.org'; + $this->addBookmark($uid, $address); + $this->addBookmark(null, $address); + $this->addBookmark(null, $address); + $this->addBookmark(null, $address); + $this->assertEquals(3, $this->bs->countOthers($address)); + } + } -- cgit v1.2.3 From 57d4892eb346bf8837416dd4ea3571d87bd0f486 Mon Sep 17 00:00:00 2001 From: cweiske Date: Tue, 16 Feb 2010 21:57:03 +0000 Subject: allow setting of status during addBookmark in tests git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@656 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/TestBase.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/TestBase.php b/tests/TestBase.php index f23f678..277be02 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -31,11 +31,15 @@ class TestBase extends PHPUnit_Framework_TestCase * * @param integer $user User ID the bookmark shall belong * @param string $address Bookmark address to use + * @param integer $status Bookmark visibility * * @return integer ID of bookmark + * + * @see SemanticScuttle_Service_Bookmark::addBookmark() */ - protected function addBookmark($user = null, $address = null) - { + protected function addBookmark( + $user = null, $address = null, $status = 0 + ) { if ($user === null) { $user = $this->addUser(); } @@ -52,7 +56,7 @@ class TestBase extends PHPUnit_Framework_TestCase 'unittest bookmark #' . $rand, 'description', null, - 0, + $status, array('unittest'), null, null, false, false, $user -- cgit v1.2.3 From 2f13a1c81956dba215ae37c7debf58de5e658ad4 Mon Sep 17 00:00:00 2001 From: cweiske Date: Tue, 16 Feb 2010 21:58:00 +0000 Subject: test complex combination of watches, publics and private bookmarks with countOthers() git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@657 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/BookmarkTest.php | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'tests') diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php index ca4ff3e..d791b9e 100644 --- a/tests/BookmarkTest.php +++ b/tests/BookmarkTest.php @@ -879,6 +879,70 @@ class BookmarkTest extends TestBase $this->assertEquals(3, $this->bs->countOthers($address)); } + + + /** + * Test what countOther() returns when the user is logged in + * and friends (people on the watchlist) have bookmarked + * and shared the same address. + * + * @return void + */ + public function testCountOthersWatchlist() + { + $uid = $this->addUser(); + $address = 'http://example.org'; + //log user in + $this->us->setCurrentUserId($uid); + + //setup users + $otherPublic1 = $this->addUser(); + $otherPublic2 = $this->addUser(); + $otherShared1 = $this->addUser(); + $otherPrivate1 = $this->addUser(); + $friendPublic1 = $this->addUser(); + $friendShared1 = $this->addUser(); + $friendShared2 = $this->addUser(); + $friendPrivate1 = $this->addUser(); + $friendSharing1 = $this->addUser(); + + //setup watchlists + $us = SemanticScuttle_Service_Factory::get('User'); + $this->us->setCurrentUserId($friendPublic1); + $us->setWatchStatus($uid); + $this->us->setCurrentUserId($friendShared1); + $us->setWatchStatus($uid); + $this->us->setCurrentUserId($friendShared2); + $us->setWatchStatus($uid); + $this->us->setCurrentUserId($friendPrivate1); + $us->setWatchStatus($uid); + + //back to login of main user + $this->us->setCurrentUserId($uid); + $us->setWatchStatus($friendSharing1); + + //add bookmarks + $this->addBookmark($uid, $address, 0); + $this->addBookmark($otherPublic1, $address, 0); + $this->addBookmark($otherPublic2, $address, 0); + $this->addBookmark($otherShared1, $address, 1); + $this->addBookmark($otherPrivate1, $address, 2); + $this->addBookmark($friendPublic1, $address, 0); + $this->addBookmark($friendShared1, $address, 1); + $this->addBookmark($friendShared2, $address, 1); + $this->addBookmark($friendPrivate1, $address, 2); + //this user is on our watchlist, but we not on his + $this->addBookmark($friendSharing1, $address, 1); + + //2 public + //1 public (friend) + //2 shared + //-> 5 + $this->assertEquals(5, $this->bs->countOthers($address)); + } + + + } -- cgit v1.2.3 From 6b7217daf298be65347205aa7a0fee17ff37b87c Mon Sep 17 00:00:00 2001 From: cweiske Date: Sat, 20 Feb 2010 11:03:20 +0000 Subject: make countOthers() accept an array of addresses git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@659 b3834d28-1941-0410-a4f8-b48e95affb8f --- src/SemanticScuttle/Service/Bookmark.php | 57 +++++++++++++++++------ tests/BookmarkTest.php | 80 ++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 13 deletions(-) (limited to 'tests') diff --git a/src/SemanticScuttle/Service/Bookmark.php b/src/SemanticScuttle/Service/Bookmark.php index 05ea060..c7bfb3b 100644 --- a/src/SemanticScuttle/Service/Bookmark.php +++ b/src/SemanticScuttle/Service/Bookmark.php @@ -891,17 +891,29 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService /** * Counts the number of bookmarks that have the same address - * as the given address - * - * @param string $address Address/URL to look for - * - * @return integer Number of bookmarks minus one that have the address + * as the given address. + * + * @internal + * We do support fetching counts for multiple addresses at once + * because that allows us to reduce the number of queries + * we need in the web interface when displaying i.e. + * 10 bookmarks - only one SQL query is needed then. + * + * @param string|array $addresses Address/URL to look for, string + * of one address or array with + * multiple ones + * + * @return integer Number of bookmarks minus one that have the address. + * In case $addresses was an array, key-value array + * with key being the address, value said number of + * bookmarks */ - public function countOthers($address) + public function countOthers($addresses) { - if (!$address) { + if (!$addresses) { return false; } + $bArray = is_array($addresses); $us = SemanticScuttle_Service_Factory::get('User'); $sId = (int)$us->getCurrentUserId(); @@ -922,12 +934,22 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService $privacy = ' AND B.bStatus = 0'; } - $sql = 'SELECT COUNT(*) as "0" FROM ' + $addressesSql = ' AND (0'; + foreach ((array)$addresses as $address) { + $addressesSql .= ' OR B.bHash = "' + . $this->db->sql_escape(md5($address)) + . '"'; + } + $addressesSql .= ')'; + + + $sql = 'SELECT B.bAddress, COUNT(*) as count FROM ' . $us->getTableName() . ' AS U' . ', '. $GLOBALS['tableprefix'] . 'bookmarks AS B' . ' WHERE U.'. $us->getFieldName('primary') .' = B.uId' - . ' AND B.bHash = "'. md5($address) . '"' - . $privacy; + . $addressesSql + . $privacy + . ' GROUP BY B.bHash'; if (!($dbresult = $this->db->sql_query($sql))) { message_die( @@ -936,10 +958,19 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService ); } - $count = $this->db->sql_fetchfield(0, 0); - $count = ($count > 0) ? $count - 1 : (int)$count; + //be sure we also list urls in our array + // that are not found in the database + $counts = array_combine( + (array)$addresses, + array_fill(0, count((array)$addresses), 0) + ); + while ($row = $this->db->sql_fetchrow($dbresult)) { + $counts[$row['bAddress']] + = $row['count'] > 0 ? $row['count'] - 1 : 0; + } $this->db->sql_freeresult($dbresult); - return $count; + + return $bArray ? $counts : reset($counts); } diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php index d791b9e..69e6e8a 100644 --- a/tests/BookmarkTest.php +++ b/tests/BookmarkTest.php @@ -943,6 +943,86 @@ class BookmarkTest extends TestBase + /** + * Test what countOther() returns when multiple addresses are + * passed to it and none of them exists. + * + * @return void + */ + public function testCountOthersArrayNone() + { + $this->assertEquals( + array('1' => 0, '2' => 0, '3' => 0), + $this->bs->countOthers(array('1', '2', '3')) + ); + } + + + + /** + * Test what countOther() returns when multiple addresses are + * passed to it and only one of them exists. + * + * @return void + */ + public function testCountOthersArrayOneNone() + { + $uid = $this->addUser(); + $uid2 = $this->addUser(); + $address1 = 'http://example.org/1'; + $address2 = 'http://example.org/2'; + $this->addBookmark($uid, $address1); + $this->addBookmark($uid, $address2); + $this->addBookmark($uid2, $address1); + $this->assertEquals( + array( + $address1 => 1, + $address2 => 0 + ), + $this->bs->countOthers( + array($address1, $address2) + ) + ); + } + + + + /** + * Test what countOther() returns when multiple addresses are passed + * to it and both of them exist with different numbers for each. + * + * @return void + */ + public function testCountOthersArrayTwoOne() + { + $uid = $this->addUser(); + $uid2 = $this->addUser(); + $uid3 = $this->addUser(); + + $address1 = 'http://example.org/1'; + $address2 = 'http://example.org/2'; + + $this->addBookmark($uid, $address1); + $this->addBookmark($uid, $address2); + + $this->addBookmark($uid2, $address1); + $this->addBookmark($uid2, $address2); + + $this->addBookmark($uid3, $address1); + + $this->assertEquals( + array( + $address1 => 2, + $address2 => 1 + ), + $this->bs->countOthers( + array($address1, $address2) + ) + ); + } + + + } -- cgit v1.2.3 From 46b96044fb2375d4b522fa074788199d80b4d243 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sat, 20 Feb 2010 11:07:37 +0000 Subject: test getBookmarks() with tag loading functionality git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@662 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/BookmarkTest.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'tests') diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php index 69e6e8a..39a9974 100644 --- a/tests/BookmarkTest.php +++ b/tests/BookmarkTest.php @@ -331,6 +331,40 @@ class BookmarkTest extends TestBase + /** + * Check tag loading functionality of getBookmarks() + * + * @return void + */ + public function testGetBookmarksIncludeTags() + { + $uid = $this->addUser(); + $bid = $this->addBookmark($uid); + $this->b2ts->attachTags($bid, array('foo', 'bar')); + $bid2 = $this->addBookmark($uid); + $this->b2ts->attachTags($bid2, array('fuu', 'baz')); + + $bms = $this->bs->getBookmarks(); + $this->assertEquals(2, count($bms['bookmarks'])); + $this->assertEquals(2, $bms['total']); + + foreach ($bms['bookmarks'] as $bm) { + $this->assertArrayHasKey('tags', $bm); + $this->assertType('array', $bm['tags']); + if ($bm['bId'] == $bid) { + $this->assertContains('foo', $bm['tags']); + $this->assertContains('bar', $bm['tags']); + } else if ($bm['bId'] == $bid2) { + $this->assertContains('fuu', $bm['tags']); + $this->assertContains('baz', $bm['tags']); + } else { + $this->assertTrue(false, 'Unknown bookmark id'); + } + } + } + + + /** * Test if deleting a bookmark works. * -- cgit v1.2.3 From 97c3d1ed636609ef19dd3cc268d0f5923f5257db Mon Sep 17 00:00:00 2001 From: cweiske Date: Sat, 20 Feb 2010 11:08:52 +0000 Subject: add test for Bookmark2Tag::getTagsForBookmark() git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@663 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/Bookmark2TagTest.php | 128 +++++++++++++++++++++++++++++++++++++++++++++ tests/TestBase.php | 10 +++- 2 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 tests/Bookmark2TagTest.php (limited to 'tests') diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php new file mode 100644 index 0000000..7d003c3 --- /dev/null +++ b/tests/Bookmark2TagTest.php @@ -0,0 +1,128 @@ + + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once 'prepare.php'; + +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'Bookmark2TagTest::main'); +} + +/** + * Unit tests for the SemanticScuttle bookmark-tag combination service. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ +class Bookmark2TagTest extends TestBase +{ + protected $us; + protected $bs; + protected $ts; + protected $tts; + + + + /** + * 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__) + ); + } + + + + protected function setUp() + { + $this->us = SemanticScuttle_Service_Factory::get('User'); + $this->bs = SemanticScuttle_Service_Factory::get('Bookmark'); + $this->bs->deleteAll(); + $this->b2ts= SemanticScuttle_Service_Factory::get('Bookmark2Tag'); + $this->b2ts->deleteAll(); + $this->tts = SemanticScuttle_Service_Factory::get('Tag2Tag'); + $this->tts->deleteAll(); + $this->tsts = SemanticScuttle_Service_Factory::get('TagStat'); + $this->tsts->deleteAll(); + $this->vs = SemanticScuttle_Service_Factory::get('Vote'); + $this->vs->deleteAll(); + } + + + + /** + * Test getTagsForBookmark() when the bookmark has no tags + * + * @return void + */ + public function testGetTagsForBookmarkNone() + { + $bid = $this->addBookmark(null, null, 0, array()); + $this->assertEquals( + array(), + $this->b2ts->getTagsForBookmark($bid) + ); + } + + + + /** + * Test getTagsForBookmark() when the bookmark has one tag + * + * @return void + */ + public function testGetTagsForBookmarkOne() + { + $bid = $this->addBookmark(null, null, 0, array()); + $this->b2ts->attachTags($bid, array('foo')); + $this->assertEquals( + array('foo'), + $this->b2ts->getTagsForBookmark($bid) + ); + } + + + + /** + * Test getTagsForBookmark() when the bookmark has three tags + * + * @return void + */ + public function testGetTagsForBookmarkThree() + { + $bid = $this->addBookmark(null, null, 0, array()); + $this->b2ts->attachTags($bid, array('foo', 'bar', 'fuu')); + + $tags = $this->b2ts->getTagsForBookmark($bid); + $this->assertType('array', $tags); + $this->assertContains('foo', $tags); + $this->assertContains('bar', $tags); + $this->assertContains('fuu', $tags); + } +} + +if (PHPUnit_MAIN_METHOD == 'Bookmark2TagTest::main') { + Bookmark2TagTest::main(); +} +?> \ No newline at end of file diff --git a/tests/TestBase.php b/tests/TestBase.php index 277be02..28bda27 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -32,17 +32,23 @@ class TestBase extends PHPUnit_Framework_TestCase * @param integer $user User ID the bookmark shall belong * @param string $address Bookmark address to use * @param integer $status Bookmark visibility + * @param array $tags Array of tags to attach. If "null" is given, + * it will automatically be "unittest" * * @return integer ID of bookmark * * @see SemanticScuttle_Service_Bookmark::addBookmark() */ protected function addBookmark( - $user = null, $address = null, $status = 0 + $user = null, $address = null, $status = 0, + $tags = null ) { if ($user === null) { $user = $this->addUser(); } + if ($tags === null) { + $tags = array('unittest'); + } $bs = SemanticScuttle_Service_Factory::get('Bookmark'); $rand = rand(); @@ -57,7 +63,7 @@ class TestBase extends PHPUnit_Framework_TestCase 'description', null, $status, - array('unittest'), + $tags, null, null, false, false, $user ); -- cgit v1.2.3 From 512e3a681179c99925121c4f192d8775011909bd Mon Sep 17 00:00:00 2001 From: cweiske Date: Sat, 20 Feb 2010 11:10:08 +0000 Subject: add bookmark2tagtest to alltests git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@664 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/AllTests.php | 1 + 1 file changed, 1 insertion(+) (limited to 'tests') diff --git a/tests/AllTests.php b/tests/AllTests.php index 23ad25e..d29de7f 100644 --- a/tests/AllTests.php +++ b/tests/AllTests.php @@ -56,6 +56,7 @@ class AllTests extends PHPUnit_Framework_TestSuite $suite = new AllTests(); $tdir = dirname(__FILE__); $suite->addTestFile($tdir . '/BookmarkTest.php'); + $suite->addTestFile($tdir . '/Bookmark2TagTest.php'); $suite->addTestFile($tdir . '/Tag2TagTest.php'); $suite->addTestFile($tdir . '/TagsCacheTest.php'); $suite->addTestFile($tdir . '/CommonDescriptionTest.php'); -- cgit v1.2.3 From 0685081d462c0c17fa11e06dd382ba082c2322fd Mon Sep 17 00:00:00 2001 From: cweiske Date: Sat, 20 Feb 2010 11:11:26 +0000 Subject: make tests better git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@665 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/Bookmark2TagTest.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'tests') diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index 7d003c3..d75afd8 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -78,6 +78,8 @@ class Bookmark2TagTest extends TestBase */ public function testGetTagsForBookmarkNone() { + $this->addBookmark(null, null, 0, array('forz', 'barz')); + $bid = $this->addBookmark(null, null, 0, array()); $this->assertEquals( array(), @@ -94,6 +96,8 @@ class Bookmark2TagTest extends TestBase */ public function testGetTagsForBookmarkOne() { + $this->addBookmark(null, null, 0, array('forz', 'barz')); + $bid = $this->addBookmark(null, null, 0, array()); $this->b2ts->attachTags($bid, array('foo')); $this->assertEquals( @@ -111,6 +115,8 @@ class Bookmark2TagTest extends TestBase */ public function testGetTagsForBookmarkThree() { + $this->addBookmark(null, null, 0, array('forz', 'barz')); + $bid = $this->addBookmark(null, null, 0, array()); $this->b2ts->attachTags($bid, array('foo', 'bar', 'fuu')); -- cgit v1.2.3 From 3e9c2cd0a57d798bb7cbcf89575cbc0199e0d2e5 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sat, 20 Feb 2010 11:14:39 +0000 Subject: add new method for better sql performance: Bookmark2Tag::getTagsForBookmarks() git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@667 b3834d28-1941-0410-a4f8-b48e95affb8f --- src/SemanticScuttle/Service/Bookmark2Tag.php | 48 ++++++++++++++++++ tests/Bookmark2TagTest.php | 73 ++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) (limited to 'tests') diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index e0f831e..770b1d9 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -305,6 +305,54 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService return $tags; } + + /** + * Retrieves all tags for an array of bookmark IDs + * + * @param array $bookmarkids Array of bookmark IDs + * + * @return array Array of tag arrays. Key is bookmark ID. + */ + public function getTagsForBookmarks($bookmarkids) + { + if (!is_array($bookmarkids)) { + message_die( + GENERAL_ERROR, 'Could not get tags (invalid bookmarkids)', + '', __LINE__, __FILE__, $query + ); + return false; + } + + $sql = ''; + foreach ($bookmarkids as $bookmarkid) { + $sql .= ' OR bId = ' . intval($bookmarkid); + } + + $query = 'SELECT tag, bId FROM ' . $this->getTableName() + . ' WHERE (1' . $sql . ')' + . ' AND LEFT(tag, 7) <> "system:"' + . ' ORDER BY id, bId ASC'; + + if (!($dbresult = $this->db->sql_query($query))) { + message_die( + GENERAL_ERROR, 'Could not get tags', + '', __LINE__, __FILE__, $query, $this->db + ); + return false; + } + + $tags = array_combine( + $bookmarkids, + array_fill(0, count($bookmarkids), array()) + ); + while ($row = $this->db->sql_fetchrow($dbresult)) { + $tags[$row['bId']][] = $row['tag']; + } + $this->db->sql_freeresult($dbresult); + return $tags; + } + + function &getTags($userid = NULL) { $userservice =SemanticScuttle_Service_Factory::get('User'); $logged_on_user = $userservice->getCurrentUserId(); diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index d75afd8..0afaaf8 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -126,6 +126,79 @@ class Bookmark2TagTest extends TestBase $this->assertContains('bar', $tags); $this->assertContains('fuu', $tags); } + + + + /** + * Test getTagsForBookmarks() when no bookmarks have tags. + * + * @return void + */ + public function testGetTagsForBookmarksNone() + { + $bid1 = $this->addBookmark(null, null, 0, array()); + $bid2 = $this->addBookmark(null, null, 0, array()); + + $alltags = $this->b2ts->getTagsForBookmarks( + array($bid1, $bid2) + ); + $this->assertType('array', $alltags); + $this->assertEquals(2, count($alltags)); + $this->assertType('array', $alltags[$bid1]); + $this->assertType('array', $alltags[$bid2]); + $this->assertEquals(0, count($alltags[$bid1])); + $this->assertEquals(0, count($alltags[$bid2])); + } + + + + /** + * Test getTagsForBookmarks() when most bookmarks have tags. + * + * @return void + */ + public function testGetTagsForBookmarksMost() + { + $bid1 = $this->addBookmark(null, null, 0, array()); + $this->b2ts->attachTags($bid1, array('foo', 'bar', 'fuu')); + + $bid2 = $this->addBookmark(null, null, 0, array()); + $this->b2ts->attachTags($bid2, array('foo', 'bar2', 'fuu2')); + + $bid3 = $this->addBookmark(null, null, 0, array()); + $this->b2ts->attachTags($bid3, array('foo', 'bar2', 'fuu3')); + + $bid4 = $this->addBookmark(null, null, 0, array()); + //no tags + + $alltags = $this->b2ts->getTagsForBookmarks( + array($bid1, $bid2, $bid3, $bid4) + ); + $this->assertType('array', $alltags); + foreach ($alltags as $bid => $btags) { + $this->assertType('array', $btags); + if ($bid == $bid1) { + $this->assertEquals(3, count($btags)); + $this->assertContains('foo', $btags); + $this->assertContains('bar', $btags); + $this->assertContains('fuu', $btags); + } else if ($bid == $bid2) { + $this->assertEquals(3, count($btags)); + $this->assertContains('foo', $btags); + $this->assertContains('bar2', $btags); + $this->assertContains('fuu2', $btags); + } else if ($bid == $bid3) { + $this->assertEquals(3, count($btags)); + $this->assertContains('foo', $btags); + $this->assertContains('bar2', $btags); + $this->assertContains('fuu3', $btags); + } else if ($bid == $bid4) { + $this->assertEquals(0, count($btags)); + } else { + $this->assertTrue(false, 'Unknown bookmark id'); + } + } + } } if (PHPUnit_MAIN_METHOD == 'Bookmark2TagTest::main') { -- cgit v1.2.3 From 17374001b8ccfe806851862c91b29671ba998828 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sat, 20 Feb 2010 11:39:03 +0000 Subject: add SemanticScuttle_Service_Bookmark::bookmarksExist() method to check for existance of multiple bookmarks at once for future sql optimization git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@680 b3834d28-1941-0410-a4f8-b48e95affb8f --- src/SemanticScuttle/Service/Bookmark.php | 56 +++++++++++++- tests/BookmarkTest.php | 122 +++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/src/SemanticScuttle/Service/Bookmark.php b/src/SemanticScuttle/Service/Bookmark.php index 10b0b8b..a1bb1a5 100644 --- a/src/SemanticScuttle/Service/Bookmark.php +++ b/src/SemanticScuttle/Service/Bookmark.php @@ -312,7 +312,7 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService * @return boolean True when the bookmark with the given URL * exists for the user, false if not. */ - function bookmarkExists($address = false, $uid = null) + public function bookmarkExists($address = false, $uid = null) { if (!$address) { return false; @@ -346,6 +346,60 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService + /** + * Checks if the given addresses exist + * + * @param array $addresses Array of addresses + * @param integer $uid User ID the addresses shall belong to + * + * @return array Array with addresses as keys, true/false for existence + * as value + */ + public function bookmarksExist($addresses, $uid = null) + { + if (count($addresses) == 0) { + return array(); + } + + $hashes = array(); + $sql = '(1'; + foreach ($addresses as $key => $address) { + $hash = md5($this->normalize($address)); + $hashes[$hash] = $address; + $sql .= ' OR bHash = "' + . $this->db->sql_escape($hash) + . '"'; + } + $sql .= ')'; + if ($uid !== null) { + $sql .= ' AND uId = ' . intval($uid); + } + + $sql = 'SELECT bHash, COUNT(*) as "count" FROM ' + . $this->getTableName() + . ' WHERE ' . $sql + . ' GROUP BY bHash'; + + if (!($dbresult = $this->db->sql_query($sql))) { + message_die( + GENERAL_ERROR, 'Could not get bookmark counts', '', + __LINE__, __FILE__, $sql, $this->db + ); + } + + $existence = array_combine( + $addresses, + array_fill(0, count($addresses), false) + ); + while ($row = $this->db->sql_fetchrow($dbresult)) { + $existence[$hashes[$row['bHash']]] = $row['count'] > 0; + } + + return $existence; + } + + + /** * Adds a bookmark to the database. * diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php index 39a9974..74685c4 100644 --- a/tests/BookmarkTest.php +++ b/tests/BookmarkTest.php @@ -252,6 +252,128 @@ class BookmarkTest extends TestBase + /** + * Tests if bookmarksExist() returns true when a bookmark + * exists + * + * @return void + */ + public function testBookmarksExistTrueSingle() + { + $bid = $this->addBookmark(); + $bookmark = $this->bs->getBookmark($bid); + + $ret = $this->bs->bookmarksExist(array($bookmark['bAddress'])); + $this->assertType('array', $ret); + $this->assertEquals(1, count($ret)); + $this->assertTrue($ret[$bookmark['bAddress']]); + } + + + + /** + * Tests if bookmarksExist() returns true when all bookmarks + * exist + * + * @return void + */ + public function testBookmarksExistTrueMultiple() + { + $bid = $this->addBookmark(); + $bookmark = $this->bs->getBookmark($bid); + + $bid2 = $this->addBookmark(); + $bookmark2 = $this->bs->getBookmark($bid2); + + + $ret = $this->bs->bookmarksExist( + array( + $bookmark['bAddress'], + $bookmark2['bAddress'] + ) + ); + $this->assertType('array', $ret); + $this->assertEquals(2, count($ret)); + $this->assertTrue($ret[$bookmark['bAddress']]); + $this->assertTrue($ret[$bookmark2['bAddress']]); + } + + + + /** + * Tests if bookmarksExist() returns false when a bookmark + * does not exist + * + * @return void + */ + public function testBookmarksExistFalseSingle() + { + $ret = $this->bs->bookmarksExist(array('does-not-exist')); + $this->assertType('array', $ret); + $this->assertEquals(1, count($ret)); + $this->assertFalse($ret['does-not-exist']); + } + + + + /** + * Tests if bookmarksExist() returns false when all bookmarks + * do not exist + * + * @return void + */ + public function testBookmarksExistFalseMultiple() + { + $bms = array( + 'does-not-exist', + 'does-not-exist-2', + 'does-not-exist-3', + ); + $ret = $this->bs->bookmarksExist($bms); + $this->assertType('array', $ret); + $this->assertEquals(3, count($ret)); + $this->assertFalse($ret['does-not-exist']); + $this->assertFalse($ret['does-not-exist-2']); + $this->assertFalse($ret['does-not-exist-3']); + } + + + + /** + * Tests if bookmarksExist() returns true when some bookmarks + * exist. + * + * @return void + */ + public function testBookmarksExistSome() + { + $bid = $this->addBookmark(); + $bookmark = $this->bs->getBookmark($bid); + + $bid2 = $this->addBookmark(); + $bookmark2 = $this->bs->getBookmark($bid2); + + + $ret = $this->bs->bookmarksExist( + array( + $bookmark['bAddress'], + 'does-not-exist', + $bookmark2['bAddress'], + 'does-not-exist-2', + 'does-not-exist-3' + ) + ); + $this->assertType('array', $ret); + $this->assertEquals(5, count($ret)); + $this->assertTrue($ret[$bookmark['bAddress']]); + $this->assertTrue($ret[$bookmark2['bAddress']]); + $this->assertFalse($ret['does-not-exist']); + $this->assertFalse($ret['does-not-exist-2']); + $this->assertFalse($ret['does-not-exist-3']); + } + + + /** * Test if countBookmarks() works with no bookmarks * -- cgit v1.2.3 From 858b188f87898099a4824d07fbcdf892a76c1876 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sat, 20 Feb 2010 11:44:06 +0000 Subject: add missing piece of faulty merge git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@683 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/BookmarkTest.php | 165 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) (limited to 'tests') diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php index 74685c4..0b4f01d 100644 --- a/tests/BookmarkTest.php +++ b/tests/BookmarkTest.php @@ -1179,6 +1179,171 @@ class BookmarkTest extends TestBase + + + /** + * Test what countOther() returns when the address does not exist + * + * @return void + */ + public function testCountOthersAddressDoesNotExist() + { + $this->assertEquals(0, $this->bs->countOthers('http://example.org')); + } + + + + /** + * Test what countOther() returns when nobody else has the same bookmark + * + * @return void + */ + public function testCountOthersNone() + { + $uid = $this->addUser(); + $address = 'http://example.org'; + $this->addBookmark($uid, $address); + $this->assertEquals(0, $this->bs->countOthers($address)); + } + + + + /** + * Test what countOther() returns when the address exists only once + * and multiple bookmarks are in the database. + * + * @return void + */ + public function testCountOthersMultipleNone() + { + $uid = $this->addUser(); + $address = 'http://example.org'; + $this->addBookmark($uid, $address); + $this->addBookmark($uid); + $this->addBookmark($uid); + $this->assertEquals(0, $this->bs->countOthers($address)); + } + + + + /** + * Test what countOther() returns when the address exists only once + * and the same user and other users have other bookmarks + * + * @return void + */ + public function testCountOthersMultipleUsersNone() + { + $uid = $this->addUser(); + $uid2 = $this->addUser(); + $address = 'http://example.org'; + $this->addBookmark($uid, $address); + $this->addBookmark($uid); + $this->addBookmark($uid2); + $this->assertEquals(0, $this->bs->countOthers($address)); + } + + + + /** + * Test what countOther() returns when the address exists two + * times in the database. + * + * @return void + */ + public function testCountOthersOne() + { + $uid = $this->addUser(); + $uid2 = $this->addUser(); + $address = 'http://example.org'; + $this->addBookmark($uid, $address); + $this->addBookmark($uid2, $address); + $this->assertEquals(1, $this->bs->countOthers($address)); + } + + + + /** + * Test what countOther() returns when the address exists four + * times in the database. + * + * @return void + */ + public function testCountOthersThree() + { + $uid = $this->addUser(); + $address = 'http://example.org'; + $this->addBookmark($uid, $address); + $this->addBookmark(null, $address); + $this->addBookmark(null, $address); + $this->addBookmark(null, $address); + $this->assertEquals(3, $this->bs->countOthers($address)); + } + + + + /** + * Test what countOther() returns when the user is logged in + * and friends (people on the watchlist) have bookmarked + * and shared the same address. + * + * @return void + */ + public function testCountOthersWatchlist() + { + $uid = $this->addUser(); + $address = 'http://example.org'; + //log user in + $this->us->setCurrentUserId($uid); + + //setup users + $otherPublic1 = $this->addUser(); + $otherPublic2 = $this->addUser(); + $otherShared1 = $this->addUser(); + $otherPrivate1 = $this->addUser(); + $friendPublic1 = $this->addUser(); + $friendShared1 = $this->addUser(); + $friendShared2 = $this->addUser(); + $friendPrivate1 = $this->addUser(); + $friendSharing1 = $this->addUser(); + + //setup watchlists + $us = SemanticScuttle_Service_Factory::get('User'); + $this->us->setCurrentUserId($friendPublic1); + $us->setWatchStatus($uid); + $this->us->setCurrentUserId($friendShared1); + $us->setWatchStatus($uid); + $this->us->setCurrentUserId($friendShared2); + $us->setWatchStatus($uid); + $this->us->setCurrentUserId($friendPrivate1); + $us->setWatchStatus($uid); + + //back to login of main user + $this->us->setCurrentUserId($uid); + $us->setWatchStatus($friendSharing1); + + //add bookmarks + $this->addBookmark($uid, $address, 0); + $this->addBookmark($otherPublic1, $address, 0); + $this->addBookmark($otherPublic2, $address, 0); + $this->addBookmark($otherShared1, $address, 1); + $this->addBookmark($otherPrivate1, $address, 2); + $this->addBookmark($friendPublic1, $address, 0); + $this->addBookmark($friendShared1, $address, 1); + $this->addBookmark($friendShared2, $address, 1); + $this->addBookmark($friendPrivate1, $address, 2); + //this user is on our watchlist, but we not on his + $this->addBookmark($friendSharing1, $address, 1); + + //2 public + //1 public (friend) + //2 shared + //-> 5 + $this->assertEquals(5, $this->bs->countOthers($address)); + } + + + } -- cgit v1.2.3 From e0dff57cbb4d2093ef58b3da7654d1305beb043f Mon Sep 17 00:00:00 2001 From: cweiske Date: Wed, 17 Mar 2010 20:04:47 +0000 Subject: remove part of broken merge :/ git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@686 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/BookmarkTest.php | 165 ------------------------------------------------- 1 file changed, 165 deletions(-) (limited to 'tests') diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php index 0b4f01d..74685c4 100644 --- a/tests/BookmarkTest.php +++ b/tests/BookmarkTest.php @@ -1179,171 +1179,6 @@ class BookmarkTest extends TestBase - - - /** - * Test what countOther() returns when the address does not exist - * - * @return void - */ - public function testCountOthersAddressDoesNotExist() - { - $this->assertEquals(0, $this->bs->countOthers('http://example.org')); - } - - - - /** - * Test what countOther() returns when nobody else has the same bookmark - * - * @return void - */ - public function testCountOthersNone() - { - $uid = $this->addUser(); - $address = 'http://example.org'; - $this->addBookmark($uid, $address); - $this->assertEquals(0, $this->bs->countOthers($address)); - } - - - - /** - * Test what countOther() returns when the address exists only once - * and multiple bookmarks are in the database. - * - * @return void - */ - public function testCountOthersMultipleNone() - { - $uid = $this->addUser(); - $address = 'http://example.org'; - $this->addBookmark($uid, $address); - $this->addBookmark($uid); - $this->addBookmark($uid); - $this->assertEquals(0, $this->bs->countOthers($address)); - } - - - - /** - * Test what countOther() returns when the address exists only once - * and the same user and other users have other bookmarks - * - * @return void - */ - public function testCountOthersMultipleUsersNone() - { - $uid = $this->addUser(); - $uid2 = $this->addUser(); - $address = 'http://example.org'; - $this->addBookmark($uid, $address); - $this->addBookmark($uid); - $this->addBookmark($uid2); - $this->assertEquals(0, $this->bs->countOthers($address)); - } - - - - /** - * Test what countOther() returns when the address exists two - * times in the database. - * - * @return void - */ - public function testCountOthersOne() - { - $uid = $this->addUser(); - $uid2 = $this->addUser(); - $address = 'http://example.org'; - $this->addBookmark($uid, $address); - $this->addBookmark($uid2, $address); - $this->assertEquals(1, $this->bs->countOthers($address)); - } - - - - /** - * Test what countOther() returns when the address exists four - * times in the database. - * - * @return void - */ - public function testCountOthersThree() - { - $uid = $this->addUser(); - $address = 'http://example.org'; - $this->addBookmark($uid, $address); - $this->addBookmark(null, $address); - $this->addBookmark(null, $address); - $this->addBookmark(null, $address); - $this->assertEquals(3, $this->bs->countOthers($address)); - } - - - - /** - * Test what countOther() returns when the user is logged in - * and friends (people on the watchlist) have bookmarked - * and shared the same address. - * - * @return void - */ - public function testCountOthersWatchlist() - { - $uid = $this->addUser(); - $address = 'http://example.org'; - //log user in - $this->us->setCurrentUserId($uid); - - //setup users - $otherPublic1 = $this->addUser(); - $otherPublic2 = $this->addUser(); - $otherShared1 = $this->addUser(); - $otherPrivate1 = $this->addUser(); - $friendPublic1 = $this->addUser(); - $friendShared1 = $this->addUser(); - $friendShared2 = $this->addUser(); - $friendPrivate1 = $this->addUser(); - $friendSharing1 = $this->addUser(); - - //setup watchlists - $us = SemanticScuttle_Service_Factory::get('User'); - $this->us->setCurrentUserId($friendPublic1); - $us->setWatchStatus($uid); - $this->us->setCurrentUserId($friendShared1); - $us->setWatchStatus($uid); - $this->us->setCurrentUserId($friendShared2); - $us->setWatchStatus($uid); - $this->us->setCurrentUserId($friendPrivate1); - $us->setWatchStatus($uid); - - //back to login of main user - $this->us->setCurrentUserId($uid); - $us->setWatchStatus($friendSharing1); - - //add bookmarks - $this->addBookmark($uid, $address, 0); - $this->addBookmark($otherPublic1, $address, 0); - $this->addBookmark($otherPublic2, $address, 0); - $this->addBookmark($otherShared1, $address, 1); - $this->addBookmark($otherPrivate1, $address, 2); - $this->addBookmark($friendPublic1, $address, 0); - $this->addBookmark($friendShared1, $address, 1); - $this->addBookmark($friendShared2, $address, 1); - $this->addBookmark($friendPrivate1, $address, 2); - //this user is on our watchlist, but we not on his - $this->addBookmark($friendSharing1, $address, 1); - - //2 public - //1 public (friend) - //2 shared - //-> 5 - $this->assertEquals(5, $this->bs->countOthers($address)); - } - - - } -- cgit v1.2.3 From f9e0714350cbb8efdfeac632fe2211c6c59bdbae Mon Sep 17 00:00:00 2001 From: cweiske Date: Wed, 17 Mar 2010 20:05:43 +0000 Subject: write failing test for bug #2953732 git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@687 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/BookmarkTest.php | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'tests') diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php index 74685c4..7885876 100644 --- a/tests/BookmarkTest.php +++ b/tests/BookmarkTest.php @@ -859,6 +859,50 @@ class BookmarkTest extends TestBase + /** + * Tests if getBookmarkByAddress() works correctly. + * + * @return void + */ + public function testGetBookmarkByAddress() + { + $url = 'http://example.org'; + $uid = $this->addUser(); + $bid = $this->addBookmark($uid, $url); + + $bm = $this->bs->getBookmarkByAddress($url); + $this->assertType('array', $bm); + $this->assertEquals($url, $bm['bAddress']); + } + + + + /** + * Tests if getBookmarkByAddress() works correctly with aliases. + * When passing an incomplete address i.e. without protocol, + * the full URL needs to be searched for. + * + * The failure of this test lead to #2953732. + * + * @return void + * + * @link https://sourceforge.net/tracker/?func=detail&atid=1017430&aid=2953732&group_id=211356 + */ + public function testGetBookmarkByAddressAlias() + { + $url = 'http://example.org'; + $incomplete = 'example.org'; + + $uid = $this->addUser(); + $bid = $this->addBookmark($uid, $url); + + $bm = $this->bs->getBookmarkByAddress($incomplete); + $this->assertType('array', $bm); + $this->assertEquals($url, $bm['bAddress']); + } + + + public function testNormalize() { $this->assertEquals( -- cgit v1.2.3 From 8c213f0e6e97351a64168aab18d69324a90e7b83 Mon Sep 17 00:00:00 2001 From: cweiske Date: Thu, 18 Mar 2010 19:22:26 +0000 Subject: fix typo git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@691 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/LAUNCH_TESTS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/LAUNCH_TESTS b/tests/LAUNCH_TESTS index 7d6ddc0..7ee9227 100644 --- a/tests/LAUNCH_TESTS +++ b/tests/LAUNCH_TESTS @@ -12,7 +12,7 @@ Warning The unit tests are DESTRUCTIBLE! Never ever run them on your normal SemanticScuttle database! Always use a different database for testing - whole tables are emptied and re-filled during the -tests, and you will definitely loose all data in there. +tests, and you will definitely lose all data in there. Running the tests -- cgit v1.2.3 From a42a6eb2ba7aab80b50cf641d643a911e5f032ca Mon Sep 17 00:00:00 2001 From: cweiske Date: Thu, 18 Mar 2010 19:28:42 +0000 Subject: test for the failure we fixed one commit ago git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@694 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/BookmarkTest.php | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tests') diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php index 7885876..40869b2 100644 --- a/tests/BookmarkTest.php +++ b/tests/BookmarkTest.php @@ -353,6 +353,10 @@ class BookmarkTest extends TestBase $bid2 = $this->addBookmark(); $bookmark2 = $this->bs->getBookmark($bid2); + //do not search for this one + $bid3 = $this->addBookmark(); + $bookmark3 = $this->bs->getBookmark($bid3); + $ret = $this->bs->bookmarksExist( array( -- cgit v1.2.3 From ccabc1cdfa6605f4454653360ec00dc171368556 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sun, 28 Mar 2010 18:04:53 +0000 Subject: support username and password change git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@699 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/TestBase.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/TestBase.php b/tests/TestBase.php index 28bda27..aad772f 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -75,15 +75,26 @@ class TestBase extends PHPUnit_Framework_TestCase /** * Creates a new user in the database. * + * @param string $username Username + * @param string $password Password + * * @return integer ID of user */ - protected function addUser() + protected function addUser($username = null, $password = null) { $us = SemanticScuttle_Service_Factory::get('User'); $rand = rand(); + + if ($username === null) { + $username = 'unittestuser-' . $rand; + } + if ($password === null) { + $password = $rand; + } + $uid = $us->addUser( - 'unittestuser-' . $rand, - $rand, + $username, + $password, 'unittest-' . $rand . '@example.org' ); return $uid; -- cgit v1.2.3 From a518928796f87ff61e7fcb34d69b374378fdadf6 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sun, 28 Mar 2010 18:06:07 +0000 Subject: add test for api/export_csv.php git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@700 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/Api/ExportCsvTest.php | 104 ++++++++++++++++++++++++++++++++++++++++++++ tests/TestBaseApi.php | 78 +++++++++++++++++++++++++++++++++ tests/prepare.php | 1 + 3 files changed, 183 insertions(+) create mode 100644 tests/Api/ExportCsvTest.php create mode 100644 tests/TestBaseApi.php (limited to 'tests') diff --git a/tests/Api/ExportCsvTest.php b/tests/Api/ExportCsvTest.php new file mode 100644 index 0000000..438df7f --- /dev/null +++ b/tests/Api/ExportCsvTest.php @@ -0,0 +1,104 @@ + + * @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', 'Api_ExportCsvTest::main'); +} + +/** + * Unit tests for the SemanticScuttle csv export API + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ +class Api_ExportCsvTest extends TestBaseApi +{ + protected $us; + protected $bs; + protected $urlPart = 'api/export_csv.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__) + ); + } + + + + /** + * Test if authentication is required when sending no auth data + */ + public function testAuthWithoutAuthData() + { + $req = $this->getRequest(null, false); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * Test if authentication is required when sending wrong user data + + */ + public function testAuthWrongCredentials() + { + $req = $this->getRequest(null, false); + $req->setAuth('user', 'password', HTTP_Request2::AUTH_BASIC); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * Test MIME content type and filename header fields + */ + public function testMimeTypeFilename() + { + $res = $this->getRequest()->send(); + + $this->assertEquals(200, $res->getStatus()); + //verify MIME content type + $this->assertEquals( + 'application/csv-tab-delimited-table; charset=utf-8', + $res->getHeader('content-type') + ); + //we need a file name + $this->assertNotNull($res->getHeader('content-disposition')); + } +} + +if (PHPUnit_MAIN_METHOD == 'Api_ExportCsvTest::main') { + Api_ExportCsvTest::main(); +} +?> \ No newline at end of file diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php new file mode 100644 index 0000000..03ca016 --- /dev/null +++ b/tests/TestBaseApi.php @@ -0,0 +1,78 @@ + + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once 'PHPUnit/Framework.php'; + +PHPUnit_Util_Filter::addFileToFilter(__FILE__); + +/** + * Base unittest class for web API tests. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Christian Weiske + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ +class TestBaseApi extends TestBase +{ + protected $url; + protected $urlPart = null; + + + + protected function setUp() + { + if ($GLOBALS['unittestUrl'] === null) { + $this->assertTrue(false, 'Unittest URL not set in config'); + } + if ($this->urlPart === null) { + $this->assertTrue(false, 'Set the urlPart variable'); + } + $this->url = $GLOBALS['unittestUrl'] . $this->urlPart; + + $this->us = SemanticScuttle_Service_Factory::get('User'); + $this->bs = SemanticScuttle_Service_Factory::get('Bookmark'); + $this->bs->deleteAll(); + } + + + + /** + * Gets a HTTP request object + * + * @param string $urlSuffix Suffix for the URL + * @param boolean $auth If user authentication is needed + * + * @return HTTP_Request2 HTTP request object + */ + protected function getRequest($urlSuffix = null, $auth = true) + { + $req = new HTTP_Request2( + $this->url . $urlSuffix, + HTTP_Request2::METHOD_GET + ); + + if ($auth) { + $this->addUser('testuser', 'testpassword'); + $req->setAuth( + 'testuser', 'testpassword', + HTTP_Request2::AUTH_BASIC + ); + } + + return $req; + } + +} +?> \ No newline at end of file diff --git a/tests/prepare.php b/tests/prepare.php index 835810e..ce9cd1c 100644 --- a/tests/prepare.php +++ b/tests/prepare.php @@ -21,6 +21,7 @@ define('UNIT_TEST_MODE', true); require_once dirname(__FILE__) . '/../src/SemanticScuttle/header.php'; require_once dirname(__FILE__) . '/TestBase.php'; +require_once dirname(__FILE__) . '/TestBaseApi.php'; if ($GLOBALS['debugMode'] == true && $GLOBALS['dbtype'] == 'mysql4' -- cgit v1.2.3 From 90f7d528d3f34a57d517cca268766bdbae161585 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sun, 28 Mar 2010 18:07:18 +0000 Subject: more tests for csv export api git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@701 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/Api/ExportCsvTest.php | 119 +++++++++++++++++++++++++++++++++++++++++++- tests/TestBase.php | 8 ++- tests/TestBaseApi.php | 44 ++++++++++++---- 3 files changed, 157 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/tests/Api/ExportCsvTest.php b/tests/Api/ExportCsvTest.php index 438df7f..ee7db4b 100644 --- a/tests/Api/ExportCsvTest.php +++ b/tests/Api/ExportCsvTest.php @@ -85,7 +85,7 @@ class Api_ExportCsvTest extends TestBaseApi */ public function testMimeTypeFilename() { - $res = $this->getRequest()->send(); + $res = reset($this->getAuthRequest())->send(); $this->assertEquals(200, $res->getStatus()); //verify MIME content type @@ -96,6 +96,123 @@ class Api_ExportCsvTest extends TestBaseApi //we need a file name $this->assertNotNull($res->getHeader('content-disposition')); } + + + + /** + * Test CSV export without bookmarks + */ + public function testNoBookmarks() + { + list($req, $uid) = $this->getAuthRequest(); + $body = $req->send()->getBody(); + $csv = $this->getCsvArray($body); + + $this->assertEquals(1, count($csv)); + $this->assertCsvHeader($csv); + } + + + + /** + * Test CSV export with some bookmarks + */ + public function testBookmarks() + { + list($req, $uid) = $this->getAuthRequest(); + //public + $this->addBookmark( + $uid, 'http://example.org/testBookmarks', 0, + array('unittest', 'testBookmarks'), 'mytitle' + ); + //shared + $this->addBookmark( + $uid, 'http://example.org/testBookmarks-shared', 1, + array('unittest', 'testBookmarks'), 'mytitle-shared' + ); + //private + $this->addBookmark( + $uid, 'http://example.org/testBookmarks-private', 2, + array('unittest', 'testBookmarks'), 'mytitle-private' + ); + + //private other that should not in the export + $this->addBookmark( + null, 'http://example.org/testBookmarks-private2', 2 + ); + + $body = $req->send()->getBody(); + $csv = $this->getCsvArray($body); + + $this->assertEquals(4, count($csv)); + $this->assertCsvHeader($csv); + + $this->assertEquals('http://example.org/testBookmarks', $csv[1][0]); + $this->assertEquals('mytitle', $csv[1][1]); + $this->assertEquals('unittest,testbookmarks', $csv[1][2]); + + $this->assertEquals('http://example.org/testBookmarks-shared', $csv[2][0]); + $this->assertEquals('mytitle-shared', $csv[2][1]); + $this->assertEquals('unittest,testbookmarks', $csv[2][2]); + + $this->assertEquals('http://example.org/testBookmarks-private', $csv[3][0]); + $this->assertEquals('mytitle-private', $csv[3][1]); + $this->assertEquals('unittest,testbookmarks', $csv[3][2]); + } + + + + /** + * Asserts that the CSV array contains the correct header + * + * @param array $csv CSV array from getCsvArray() + * + * @return void + */ + protected function assertCsvHeader($csv) + { + $this->assertEquals( + array('url', 'title', 'tags', 'description'), + $csv[0] + ); + } + + + + /** + * Converts a string of CSV data to an array + * + * @param string $body String containing the full CSV file + * + * @return array Array of CSV data + */ + protected function getCsvArray($body) + { + $v53 = (version_compare(PHP_VERSION, '5.3.0') === 1); + + //dead simple implementation that does not work with + // advanced CSV files + $ar = array(); + foreach (explode("\n", $body) as $line) { + if ($v53) { + $ar[] = str_getcsv($line, ';'); + } else { + $arl = explode(';', $line); + foreach ($arl as &$str) { + if (substr($str, 0, 1) == '"' + && substr($str, -1) == '"' + ) { + $str = substr($str, 1, -1); + } + } + $ar[] = $arl; + } + } + if (count(end($ar)) == 1 && reset(end($ar)) == '') { + unset($ar[key($ar)]); + } + return $ar; + } } if (PHPUnit_MAIN_METHOD == 'Api_ExportCsvTest::main') { diff --git a/tests/TestBase.php b/tests/TestBase.php index aad772f..402330b 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -34,6 +34,7 @@ class TestBase extends PHPUnit_Framework_TestCase * @param integer $status Bookmark visibility * @param array $tags Array of tags to attach. If "null" is given, * it will automatically be "unittest" + * @param string $title Bookmark title * * @return integer ID of bookmark * @@ -41,7 +42,7 @@ class TestBase extends PHPUnit_Framework_TestCase */ protected function addBookmark( $user = null, $address = null, $status = 0, - $tags = null + $tags = null, $title = null ) { if ($user === null) { $user = $this->addUser(); @@ -56,10 +57,13 @@ class TestBase extends PHPUnit_Framework_TestCase if ($address === null) { $address = 'http://example.org/' . $rand; } + if ($title === null) { + $title = 'unittest bookmark #' . $rand; + } $bid = $bs->addBookmark( $address, - 'unittest bookmark #' . $rand, + $title, 'description', null, $status, diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php index 03ca016..645ead9 100644 --- a/tests/TestBaseApi.php +++ b/tests/TestBaseApi.php @@ -42,8 +42,11 @@ class TestBaseApi extends TestBase $this->url = $GLOBALS['unittestUrl'] . $this->urlPart; $this->us = SemanticScuttle_Service_Factory::get('User'); + $this->us->deleteAll(); $this->bs = SemanticScuttle_Service_Factory::get('Bookmark'); $this->bs->deleteAll(); + $this->b2t = SemanticScuttle_Service_Factory::get('Bookmark2Tag'); + $this->b2t->deleteAll(); } @@ -51,28 +54,47 @@ class TestBaseApi extends TestBase /** * Gets a HTTP request object * - * @param string $urlSuffix Suffix for the URL - * @param boolean $auth If user authentication is needed + * @param string $urlSuffix Suffix for the URL * * @return HTTP_Request2 HTTP request object */ - protected function getRequest($urlSuffix = null, $auth = true) + protected function getRequest($urlSuffix = null) { $req = new HTTP_Request2( $this->url . $urlSuffix, HTTP_Request2::METHOD_GET ); - if ($auth) { - $this->addUser('testuser', 'testpassword'); - $req->setAuth( - 'testuser', 'testpassword', - HTTP_Request2::AUTH_BASIC - ); - } - return $req; } + + + /** + * Gets a HTTP request object + * + * @param string $urlSuffix Suffix for the URL + * @param mixed $auth If user authentication is needed (true/false) + * or array with username and password + * + * @return array(HTTP_Request2, integer) HTTP request object and user id + */ + protected function getAuthRequest($urlSuffix = null, $auth = true) + { + $req = $this->getRequest($urlSuffix); + if (is_array($auth)) { + list($username, $password) = $auth; + } else { + $username = 'testuser'; + $password = 'testpassword'; + } + $uid = $this->addUser($username, $password); + $req->setAuth( + $username, $password, + HTTP_Request2::AUTH_BASIC + ); + return array($req, $uid); + } + } ?> \ No newline at end of file -- cgit v1.2.3 From 74dc9ea6a4e553f38a97928617074fd76793c783 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sun, 28 Mar 2010 18:08:38 +0000 Subject: make export_csv support filtering to multiple tags git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@702 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/Api/ExportCsvTest.php | 61 +++++++++++++++++++++++++++++++++++++++++++++ www/api/export_csv.php | 15 +++++++---- 2 files changed, 71 insertions(+), 5 deletions(-) (limited to 'tests') diff --git a/tests/Api/ExportCsvTest.php b/tests/Api/ExportCsvTest.php index ee7db4b..18008e1 100644 --- a/tests/Api/ExportCsvTest.php +++ b/tests/Api/ExportCsvTest.php @@ -162,6 +162,67 @@ class Api_ExportCsvTest extends TestBaseApi + /** + * Test CSV export with tag filter + */ + public function testTagFilter() + { + list($req, $uid) = $this->getAuthRequest('?tag=tag1'); + $this->addBookmark( + $uid, 'http://example.org/tag-1', 0, + array('unittest', 'tag1') + ); + $this->addBookmark( + $uid, 'http://example.org/tag-2', 0, + array('unittest', 'tag2') + ); + $this->addBookmark( + $uid, 'http://example.org/tag-3', 0, + array('unittest', 'tag1', 'tag2') + ); + + $body = $req->send()->getBody(); + $csv = $this->getCsvArray($body); + + $this->assertEquals(3, count($csv)); + $this->assertCsvHeader($csv); + + $this->assertEquals('http://example.org/tag-1', $csv[1][0]); + $this->assertEquals('http://example.org/tag-3', $csv[2][0]); + } + + + + /** + * Test CSV export with tag filter for multiple tags + */ + public function testTagFilterMultiple() + { + list($req, $uid) = $this->getAuthRequest('?tag=tag1+tag2'); + $this->addBookmark( + $uid, 'http://example.org/tag-1', 0, + array('unittest', 'tag1') + ); + $this->addBookmark( + $uid, 'http://example.org/tag-2', 0, + array('unittest', 'tag2') + ); + $this->addBookmark( + $uid, 'http://example.org/tag-3', 0, + array('unittest', 'tag1', 'tag2') + ); + + $body = $req->send()->getBody(); + $csv = $this->getCsvArray($body); + + $this->assertEquals(2, count($csv)); + $this->assertCsvHeader($csv); + + $this->assertEquals('http://example.org/tag-3', $csv[1][0]); + } + + + /** * Asserts that the CSV array contains the correct header * diff --git a/www/api/export_csv.php b/www/api/export_csv.php index b9cf497..43951ec 100644 --- a/www/api/export_csv.php +++ b/www/api/export_csv.php @@ -10,13 +10,18 @@ header("Content-disposition: filename=exportBookmarks.csv"); $bookmarkservice =SemanticScuttle_Service_Factory::get('Bookmark'); // Check to see if a tag was specified. -if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != '')) - $tag = trim($_REQUEST['tag']); -else - $tag = NULL; +if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != '')) { + //$_GET vars have + replaced to " " automatically + $tag = str_replace(' ', '+', trim($_REQUEST['tag'])); +} else { + $tag = null; +} // Get the posts relevant to the passed-in variables. -$bookmarks =& $bookmarkservice->getBookmarks(0, NULL, $userservice->getCurrentUserId(), $tag, NULL, getSortOrder()); +$bookmarks = $bookmarkservice->getBookmarks( + 0, null, $userservice->getCurrentUserId(), + $tag, null, getSortOrder() +); //columns titles echo 'url;title;tags;description'; -- cgit v1.2.3 From ccbc63aec4869f64fb68706a6d687aa665a60c9c Mon Sep 17 00:00:00 2001 From: cweiske Date: Sun, 28 Mar 2010 18:09:59 +0000 Subject: test that public bookmarks of other people are not exported via csv git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@703 b3834d28-1941-0410-a4f8-b48e95affb8f --- tests/Api/ExportCsvTest.php | 5 +++++ www/api/export_csv.php | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/Api/ExportCsvTest.php b/tests/Api/ExportCsvTest.php index 18008e1..2bff8a5 100644 --- a/tests/Api/ExportCsvTest.php +++ b/tests/Api/ExportCsvTest.php @@ -140,6 +140,11 @@ class Api_ExportCsvTest extends TestBaseApi $this->addBookmark( null, 'http://example.org/testBookmarks-private2', 2 ); + //public bookmark from other people that should not be + // exported, too + $this->addBookmark( + null, 'http://example.org/testBookmarks-other', 0 + ); $body = $req->send()->getBody(); $csv = $this->getCsvArray($body); diff --git a/www/api/export_csv.php b/www/api/export_csv.php index 43951ec..bb469b1 100644 --- a/www/api/export_csv.php +++ b/www/api/export_csv.php @@ -1,5 +1,18 @@ + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ // Force HTTP authentication first! $httpContentType = 'application/csv-tab-delimited-table'; -- cgit v1.2.3 From 31570df64ce6543de407e7b9d751ba9033d930de Mon Sep 17 00:00:00 2001 From: cweiske Date: Fri, 9 Jul 2010 10:04:35 +0000 Subject: Fix bug getTagsForBookmarks() that fetched all tags, see http://sourceforge.net/projects/semanticscuttle/forums/forum/759510/topic/3752670 for details git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@715 b3834d28-1941-0410-a4f8-b48e95affb8f --- doc/ChangeLog | 5 +++++ src/SemanticScuttle/Service/Bookmark2Tag.php | 7 +------ tests/Bookmark2TagTest.php | 6 ++++++ 3 files changed, 12 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/doc/ChangeLog b/doc/ChangeLog index 5b4e4d3..5e24f07 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,6 +1,11 @@ ChangeLog for SemantiScuttle ============================ +0.9X.X - 2010-XX-XX +------------------- +- Fix bug getTagsForBookmarks() that fetched all tags + + 0.97.0 - 2010-06-09 ------------------- - Many SQL optimizations - SemanticScuttle shows bookmarks 4 times faster now diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index d6c0e58..4d2c969 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -325,13 +325,8 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService return array(); } - $sql = ''; - foreach ($bookmarkids as $bookmarkid) { - $sql .= ' OR bId = ' . intval($bookmarkid); - } - $query = 'SELECT tag, bId FROM ' . $this->getTableName() - . ' WHERE (1' . $sql . ')' + . ' WHERE bId IN (' . implode(',', $bookmarkids) . ')' . ' AND LEFT(tag, 7) <> "system:"' . ' ORDER BY id, bId ASC'; diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index 0afaaf8..14b71cc 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -171,6 +171,12 @@ class Bookmark2TagTest extends TestBase $bid4 = $this->addBookmark(null, null, 0, array()); //no tags + //bookmark that does not get queried + //http://sourceforge.net/projects/semanticscuttle/forums/forum/759510/topic/3752670 + $bid5 = $this->addBookmark(null, null, 0, array()); + $this->b2ts->attachTags($bid5, array('foo', 'bar2', 'fuu5')); + + $alltags = $this->b2ts->getTagsForBookmarks( array($bid1, $bid2, $bid3, $bid4) ); -- cgit v1.2.3