diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/AllTests.php | 1 | ||||
-rw-r--r-- | tests/Api/ExportCsvTest.php | 287 | ||||
-rw-r--r-- | tests/Bookmark2TagTest.php | 213 | ||||
-rw-r--r-- | tests/BookmarkTest.php | 449 | ||||
-rw-r--r-- | tests/LAUNCH_TESTS | 2 | ||||
-rw-r--r-- | tests/TestBase.php | 51 | ||||
-rw-r--r-- | tests/TestBaseApi.php | 100 | ||||
-rw-r--r-- | tests/prepare.php | 1 |
8 files changed, 1093 insertions, 11 deletions
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'); diff --git a/tests/Api/ExportCsvTest.php b/tests/Api/ExportCsvTest.php new file mode 100644 index 0000000..2bff8a5 --- /dev/null +++ b/tests/Api/ExportCsvTest.php @@ -0,0 +1,287 @@ +<?php +/** + * SemanticScuttle - your social bookmark manager. + * + * PHP version 5. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net> + * @author Christian Weiske <cweiske@cweiske.de> + * @author Eric Dane <ericdane@users.sourceforge.net> + * @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 <mensonge@users.sourceforge.net> + * @author Christian Weiske <cweiske@cweiske.de> + * @author Eric Dane <ericdane@users.sourceforge.net> + * @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 = reset($this->getAuthRequest())->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')); + } + + + + /** + * 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 + ); + //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); + + $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]); + } + + + + /** + * 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 + * + * @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') { + Api_ExportCsvTest::main(); +} +?>
\ No newline at end of file diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php new file mode 100644 index 0000000..14b71cc --- /dev/null +++ b/tests/Bookmark2TagTest.php @@ -0,0 +1,213 @@ +<?php +/** + * SemanticScuttle - your social bookmark manager. + * + * PHP version 5. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net> + * @author Christian Weiske <cweiske@cweiske.de> + * @author Eric Dane <ericdane@users.sourceforge.net> + * @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 <mensonge@users.sourceforge.net> + * @author Christian Weiske <cweiske@cweiske.de> + * @author Eric Dane <ericdane@users.sourceforge.net> + * @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() + { + $this->addBookmark(null, null, 0, array('forz', 'barz')); + + $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() + { + $this->addBookmark(null, null, 0, array('forz', 'barz')); + + $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() + { + $this->addBookmark(null, null, 0, array('forz', 'barz')); + + $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); + } + + + + /** + * 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 + + //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) + ); + $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') { + Bookmark2TagTest::main(); +} +?>
\ No newline at end of file diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php index 741b6bf..40869b2 100644 --- a/tests/BookmarkTest.php +++ b/tests/BookmarkTest.php @@ -253,6 +253,132 @@ 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); + + //do not search for this one + $bid3 = $this->addBookmark(); + $bookmark3 = $this->bs->getBookmark($bid3); + + + $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 * * @return void @@ -332,6 +458,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. * * @return void @@ -703,6 +863,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( @@ -778,6 +982,251 @@ 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)); + } + + + + /** + * 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)); + } + + + + /** + * 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) + ) + ); + } + + + } 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 diff --git a/tests/TestBase.php b/tests/TestBase.php index 05988a5..402330b 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -29,25 +29,45 @@ 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 + * @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 + * + * @see SemanticScuttle_Service_Bookmark::addBookmark() */ - protected function addBookmark($user = null) - { + protected function addBookmark( + $user = null, $address = null, $status = 0, + $tags = null, $title = null + ) { if ($user === null) { $user = $this->addUser(); } + if ($tags === null) { + $tags = array('unittest'); + } $bs = SemanticScuttle_Service_Factory::get('Bookmark'); $rand = rand(); + + if ($address === null) { + $address = 'http://example.org/' . $rand; + } + if ($title === null) { + $title = 'unittest bookmark #' . $rand; + } + $bid = $bs->addBookmark( - 'http://example.org/' . $rand, - 'unittest bookmark #' . $rand, + $address, + $title, 'description', null, - 0, - array('unittest'), + $status, + $tags, null, null, false, false, $user ); @@ -59,15 +79,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; diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php new file mode 100644 index 0000000..645ead9 --- /dev/null +++ b/tests/TestBaseApi.php @@ -0,0 +1,100 @@ +<?php +/** + * SemanticScuttle - your social bookmark manager. + * + * PHP version 5. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Christian Weiske <cweiske@cweiske.de> + * @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 <cweiske@cweiske.de> + * @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->us->deleteAll(); + $this->bs = SemanticScuttle_Service_Factory::get('Bookmark'); + $this->bs->deleteAll(); + $this->b2t = SemanticScuttle_Service_Factory::get('Bookmark2Tag'); + $this->b2t->deleteAll(); + } + + + + /** + * Gets a HTTP request object + * + * @param string $urlSuffix Suffix for the URL + * + * @return HTTP_Request2 HTTP request object + */ + protected function getRequest($urlSuffix = null) + { + $req = new HTTP_Request2( + $this->url . $urlSuffix, + HTTP_Request2::METHOD_GET + ); + + 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 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' |