aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/AllTests.php62
-rw-r--r--tests/Api/ExportCsvTest.php262
-rw-r--r--tests/Api/OpenSearchTest.php76
-rw-r--r--tests/Api/PostsAddTest.php456
-rw-r--r--tests/Api/PostsDeleteTest.php278
-rw-r--r--tests/Api/PostsUpdateTest.php110
-rw-r--r--tests/Bookmark2TagTest.php688
-rw-r--r--tests/BookmarkTest.php1380
-rw-r--r--tests/CommonDescriptionTest.php111
-rw-r--r--tests/FactoryTest.php13
-rw-r--r--tests/LAUNCH_TESTS28
-rw-r--r--tests/Model/BookmarkTest.php65
-rw-r--r--tests/SearchHistoryTest.php373
-rw-r--r--tests/SemanticScuttle/ConfigTest.php206
-rw-r--r--tests/SemanticScuttle/EnvironmentTest.php234
-rw-r--r--tests/Tag2TagTest.php537
-rw-r--r--tests/TagTest.php99
-rw-r--r--tests/TagsCacheTest.php192
-rw-r--r--tests/TestBase.php159
-rw-r--r--tests/TestBaseApi.php271
-rw-r--r--tests/UserArrayTest.php66
-rw-r--r--tests/UserTest.php527
-rw-r--r--tests/VoteTest.php536
-rw-r--r--tests/ajax/GetAdminLinkedTagsTest.php120
-rw-r--r--tests/ajax/GetAdminTagsTest.php120
-rw-r--r--tests/ajax/GetContactTagsTest.php100
-rwxr-xr-xtests/data/BookmarkTest_deliciousbookmarks.xml7
-rw-r--r--tests/data/BookmarkTest_deliciousbookmarks_longdesc_3469257.xml4
-rw-r--r--tests/data/BookmarkTest_deliciousbookmarks_private.xml1
-rwxr-xr-xtests/data/BookmarkTest_netscapebookmarks.html27
-rw-r--r--tests/dox.html1
-rw-r--r--tests/phpunit.xml15
-rw-r--r--tests/prepare.php41
-rwxr-xr-xtests/www/bookmarksTest.php156
-rwxr-xr-xtests/www/editTest.php48
-rwxr-xr-xtests/www/importNetscapeTest.php33
-rwxr-xr-xtests/www/importTest.php33
-rw-r--r--tests/www/indexTest.php58
-rw-r--r--tests/www/rssTest.php151
-rw-r--r--tests/www/searchTest.php70
40 files changed, 7714 insertions, 0 deletions
diff --git a/tests/AllTests.php b/tests/AllTests.php
new file mode 100644
index 0000000..92f8960
--- /dev/null
+++ b/tests/AllTests.php
@@ -0,0 +1,62 @@
+<?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
+ */
+
+/**
+ * SemanticScuttle unit tests.
+ *
+ * To launch this tests, you need PHPUnit 3.
+ * Run them with:
+ * $ cd tests; phpunit .
+ * or single files like:
+ * $ cd tests; phpunit BookmarkTest.php
+ *
+ * @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 AllTests extends PHPUnit_Framework_TestSuite
+{
+ public static function suite()
+ {
+ $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');
+ $suite->addTestFile($tdir . '/SearchHistoryTest.php');
+ $suite->addTestFile($tdir . '/TagTest.php');
+ $suite->addTestFile($tdir . '/VoteTest.php');
+ $suite->addTestFile($tdir . '/UserTest.php');
+ $suite->addTestFile($tdir . '/Api/ExportCsvTest.php');
+ $suite->addTestFile($tdir . '/Api/OpenSearchTest.php');
+ $suite->addTestFile($tdir . '/Api/PostsAddTest.php');
+ $suite->addTestFile($tdir . '/Api/PostsDeleteTest.php');
+ $suite->addTestFile($tdir . '/Api/PostsUpdateTest.php');
+ return $suite;
+ }
+
+
+
+ protected function tearDown()
+ {
+ }
+}
+?>
diff --git a/tests/Api/ExportCsvTest.php b/tests/Api/ExportCsvTest.php
new file mode 100644
index 0000000..a0a9eae
--- /dev/null
+++ b/tests/Api/ExportCsvTest.php
@@ -0,0 +1,262 @@
+<?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 'HTTP/Request2.php';
+
+/**
+ * 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';
+
+
+
+ /**
+ * 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 = rreset($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 && rreset(end($ar)) == '') {
+ unset($ar[key($ar)]);
+ }
+ return $ar;
+ }
+}
+?> \ No newline at end of file
diff --git a/tests/Api/OpenSearchTest.php b/tests/Api/OpenSearchTest.php
new file mode 100644
index 0000000..09166da
--- /dev/null
+++ b/tests/Api/OpenSearchTest.php
@@ -0,0 +1,76 @@
+<?php
+
+
+class Api_OpenSearchTest extends TestBaseApi
+{
+ protected $urlPart = '';
+
+
+ public function testOpenSearchAvailable()
+ {
+ $req = $this->getRequest();
+ $xhtml = $req->send()->getBody();
+
+ $xml = simplexml_load_string($xhtml);
+ $xml->registerXPathNamespace('h', rreset($xml->getDocNamespaces()));
+
+ $this->assertInstanceOf(
+ 'SimpleXMLElement', $xml,
+ 'SemanticScuttle main page XHTML could not be loaded - maybe invalid?'
+ );
+
+ $arElements = $xml->xpath(
+ '//h:head/h:link'
+ . '[@rel="search" and @type="application/opensearchdescription+xml"]'
+ );
+ $this->assertEquals(
+ 1, count($arElements),
+ 'OpenSearch link in HTML is missing'
+ );
+ $searchDescUrl = $this->completeUrl((string)$arElements[0]['href']);
+ $this->assertNotNull($searchDescUrl, 'Search description URL is empty');
+
+ $req = new HTTP_Request2($searchDescUrl);
+ $res = $req->send();
+ $this->assertEquals(
+ 200, $res->getStatus(),
+ 'HTTP response status code is not 200'
+ );
+
+ $this->assertEquals(
+ $GLOBALS['unittestUrl'] . 'api/opensearch.php',
+ $searchDescUrl,
+ 'OpenSearch URL found, but it is not the expected one.'
+ . ' It may be that you misconfigured the "unittestUrl" setting'
+ );
+ }
+
+ public function testOpenSearchContentType()
+ {
+ $res = $this->getRequest('api/opensearch.php')->send();
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+ }
+
+ public function testOpenSearchSearchUrl()
+ {
+ $xml = $this->getRequest('api/opensearch.php')->send()->getBody();
+ $x = simplexml_load_string($xml);
+ $x->registerXPathNamespace('os', rreset($x->getDocNamespaces()));
+
+ $arElements = $x->xpath('//os:Url[@type="text/html"]');
+ $this->assertEquals(
+ 1, count($arElements),
+ 'Url in OpenSearch description is missing'
+ );
+ $this->assertEquals(
+ $GLOBALS['unittestUrl'] . 'search.php/all/{searchTerms}',
+ (string)$arElements[0]['template']
+ );
+ }
+
+}
+
+?> \ No newline at end of file
diff --git a/tests/Api/PostsAddTest.php b/tests/Api/PostsAddTest.php
new file mode 100644
index 0000000..e6d0531
--- /dev/null
+++ b/tests/Api/PostsAddTest.php
@@ -0,0 +1,456 @@
+<?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 'HTTP/Request2.php';
+
+/**
+ * Unit tests for the SemanticScuttle post addition 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_PostsAddTest extends TestBaseApi
+{
+ protected $urlPart = 'api/posts/add';
+
+
+
+ /**
+ * 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 if adding a bookmark via POST works.
+ */
+ public function testAddBookmarkPost()
+ {
+ $this->bs->deleteAll();
+
+ $bmUrl = 'http://example.org/tag-1';
+ $bmTags = array('foo', 'bar', 'baz');
+ $bmDatetime = '2010-09-08T03:02:01Z';
+ $bmTitle = 'This is a foo title';
+ $bmDescription = <<<TXT
+This is the description of
+my bookmark with some
+newlines and <some>?&\$ÄÖ'"§special"'
+characters
+TXT;
+
+ list($req, $uId) = $this->getAuthRequest();
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->addPostParameter('url', $bmUrl);
+ $req->addPostParameter('description', $bmTitle);
+ $req->addPostParameter('extended', $bmDescription);
+ $req->addPostParameter('tags', implode(' ', $bmTags));
+ $req->addPostParameter('dt', $bmDatetime);
+ $res = $req->send();
+
+ //all should be well
+ $this->assertEquals(200, $res->getStatus());
+ //verify MIME content type
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+
+ //verify xml
+ $this->assertTag(
+ array(
+ 'tag' => 'result',
+ 'attributes' => array('code' => 'done')
+ ),
+ $res->getBody(),
+ null, false
+ );
+
+ //user should have one bookmark now
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(1, $data['total']);
+ $bm = $data['bookmarks'][0];
+
+ $this->assertEquals($bmUrl, $bm['bAddress']);
+ $this->assertEquals($bmTitle, $bm['bTitle']);
+ $this->assertEquals($bmDescription, stripslashes($bm['bDescription']));
+ $this->assertEquals($bmTags, $bm['tags']);
+ $this->assertEquals(
+ gmdate('Y-m-d H:i:s', strtotime($bmDatetime)),
+ $bm['bDatetime']
+ );
+ }
+
+
+
+ /**
+ * Test if adding a bookmark via GET works.
+ */
+ public function testAddBookmarkGet()
+ {
+ $this->bs->deleteAll();
+
+ $bmUrl = 'http://example.org/tag-1';
+ $bmTags = array('foo', 'bar', 'baz');
+ $bmDatetime = '2010-09-08T03:02:01Z';
+ $bmTitle = 'This is a foo title';
+ $bmDescription = <<<TXT
+This is the description of
+my bookmark with some
+newlines and <some>?&\$ÄÖ'"§special"'
+characters
+TXT;
+
+ list($req, $uId) = $this->getAuthRequest(
+ '?url=' . urlencode($bmUrl)
+ . '&description=' . urlencode($bmTitle)
+ . '&extended=' . urlencode($bmDescription)
+ . '&tags=' . urlencode(implode(' ', $bmTags))
+ . '&dt=' . urlencode($bmDatetime)
+ );
+ $res = $req->send();
+
+ //all should be well
+ $this->assertEquals(200, $res->getStatus());
+ //verify MIME content type
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+
+ //verify xml
+ $this->assertTag(
+ array(
+ 'tag' => 'result',
+ 'attributes' => array('code' => 'done')
+ ),
+ $res->getBody(),
+ null, false
+ );
+
+ //user should have one bookmark now
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(1, $data['total']);
+ $bm = $data['bookmarks'][0];
+
+ $this->assertEquals($bmUrl, $bm['bAddress']);
+ $this->assertEquals($bmTitle, $bm['bTitle']);
+ $this->assertEquals($bmDescription, stripslashes($bm['bDescription']));
+ $this->assertEquals($bmTags, $bm['tags']);
+ $this->assertEquals(
+ gmdate('Y-m-d H:i:s', strtotime($bmDatetime)),
+ $bm['bDatetime']
+ );
+ }
+
+ /**
+ * Verify that the URL and description/title are enough parameters
+ * to add a bookmark.
+ */
+ public function testUrlDescEnough()
+ {
+ $this->bs->deleteAll();
+
+ list($req, $uId) = $this->getAuthRequest();
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->addPostParameter('url', 'http://example.org/tag2');
+ $req->addPostParameter('description', 'foo bar');
+ $res = $req->send();
+
+ //all should be well
+ $this->assertEquals(200, $res->getStatus());
+ //verify MIME content type
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+
+ //verify xml
+ $this->assertTag(
+ array(
+ 'tag' => 'result',
+ 'attributes' => array('code' => 'done')
+ ),
+ $res->getBody(),
+ null, false
+ );
+
+ //user has 1 bookmark now
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(1, $data['total']);
+ }
+
+ /**
+ * Verify that the URL is required
+ */
+ public function testUrlRequired()
+ {
+ $this->bs->deleteAll();
+
+ list($req, $uId) = $this->getAuthRequest();
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ //$req->addPostParameter('url', 'http://example.org/tag2');
+ $req->addPostParameter('description', 'foo bar');
+ $res = $req->send();
+
+ //all should be well
+ $this->assertEquals(400, $res->getStatus());
+ //verify MIME content type
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+
+ //verify xml
+ $this->assertTag(
+ array(
+ 'tag' => 'result',
+ 'attributes' => array('code' => 'URL missing')
+ ),
+ $res->getBody(),
+ null, false
+ );
+
+ //user still has 0 bookmarks
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(0, $data['total']);
+ }
+
+ /**
+ * Verify that the description/title is required
+ */
+ public function testDescriptionRequired()
+ {
+ $this->bs->deleteAll();
+
+ list($req, $uId) = $this->getAuthRequest();
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->addPostParameter('url', 'http://example.org/tag2');
+ $res = $req->send();
+
+ //all should be well
+ $this->assertEquals(400, $res->getStatus());
+ //verify MIME content type
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+
+ //verify xml
+ $this->assertTag(
+ array(
+ 'tag' => 'result',
+ 'attributes' => array('code' => 'Description missing')
+ ),
+ $res->getBody(),
+ null, false
+ );
+
+ //user still has 0 bookmarks
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(0, $data['total']);
+ }
+
+ /**
+ * Test that the replace=no parameter prevents the bookmark from being
+ * overwritten.
+ */
+ public function testReplaceNo()
+ {
+ $this->bs->deleteAll();
+
+ $url = 'http://example.org/tag2';
+ $title1 = 'foo bar 1';
+ $title2 = 'bar 2 foo';
+
+ list($req, $uId) = $this->getAuthRequest();
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->addPostParameter('url', $url);
+ $req->addPostParameter('description', $title1);
+ $res = $req->send();
+
+ //all should be well
+ $this->assertEquals(200, $res->getStatus());
+
+ //send it a second time, with different title
+ list($req, $dummy) = $this->getAuthRequest();
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->addPostParameter('url', $url);
+ $req->addPostParameter('description', $title2);
+ $req->addPostParameter('replace', 'no');
+ $res = $req->send();
+
+ //this time we should get an error
+ $this->assertEquals(409, $res->getStatus());
+ //verify MIME content type
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+
+ //verify xml
+ $this->assertTag(
+ array(
+ 'tag' => 'result',
+ 'attributes' => array('code' => 'bookmark does already exist')
+ ),
+ $res->getBody(),
+ null, false
+ );
+
+ //user still has 1 bookmark now
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(1, $data['total']);
+ $this->assertEquals($title1, $data['bookmarks'][0]['bTitle']);
+
+ //send it a third time, without the replace parameter
+ // it defaults to "no", so the bookmark should not get overwritten
+ list($req, $dummy) = $this->getAuthRequest();
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->addPostParameter('url', $url);
+ $req->addPostParameter('description', $title2);
+ $res = $req->send();
+
+ //this time we should get an error
+ $this->assertEquals(409, $res->getStatus());
+
+ //bookmark should not have changed
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(1, $data['total']);
+ $this->assertEquals($title1, $data['bookmarks'][0]['bTitle']);
+ }
+
+ /**
+ * Test that the replace=yes parameter causes the bookmark to be updated.
+ */
+ public function testReplaceYes()
+ {
+ $this->bs->deleteAll();
+
+ $url = 'http://example.org/tag2';
+ $title1 = 'foo bar 1';
+ $title2 = 'bar 2 foo';
+
+ list($req, $uId) = $this->getAuthRequest();
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->addPostParameter('url', $url);
+ $req->addPostParameter('description', $title1);
+ $res = $req->send();
+
+ //all should be well
+ $this->assertEquals(200, $res->getStatus());
+
+ //send it a second time, with different title
+ list($req, $dummy) = $this->getAuthRequest();
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->addPostParameter('url', $url);
+ $req->addPostParameter('description', $title2);
+ $req->addPostParameter('replace', 'yes');
+ $res = $req->send();
+
+ //no error
+ $this->assertEquals(200, $res->getStatus());
+ //verify MIME content type
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+
+ //verify xml
+ $this->assertTag(
+ array(
+ 'tag' => 'result',
+ 'attributes' => array('code' => 'done')
+ ),
+ $res->getBody(),
+ null, false
+ );
+
+ //user still has 1 bookmark now, but with the new title
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(1, $data['total']);
+ $this->assertEquals($title2, $data['bookmarks'][0]['bTitle']);
+ }
+
+
+ /**
+ * Test that a default privacy setting of 2 (Private) is used in adding
+ * a bookmark.
+ */
+ public function testDefaultPrivacyPrivate()
+ {
+ $this->setUnittestConfig(
+ array('defaults' => array('privacy' => 2))
+ );
+ list($req, $uId) = $this->getAuthRequest('?unittestMode=1');
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->addPostParameter('url', 'http://www.example.org/testdefaultprivacyposts_addprivate');
+ $req->addPostParameter('description', 'Test bookmark 1 for default privacy.');
+ $req->send();
+ $this->us->setCurrentUserId($uId);
+ $bms = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(1, count($bms['bookmarks']));
+ $bm = reset($bms['bookmarks']);
+ $this->assertEquals('2', $bm['bStatus']);
+ }//end testDefaultPrivacyPrivate
+
+
+ /**
+ * Test that a default privacy setting of 0 (Public) is used in adding
+ * a bookmark.
+ */
+ public function testDefaultPrivacyPublic()
+ {
+ $this->setUnittestConfig(
+ array('defaults' => array('privacy' => 0))
+ );
+ list($req, $uId) = $this->getAuthRequest('?unittestMode=1');
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->addPostParameter('url', 'http://www.example.org/testdefaultprivacyposts_addpublic');
+ $req->addPostParameter('description', 'Test bookmark 1 for default privacy.');
+ $req->send();
+ $this->us->setCurrentUserId($uId);
+ $bms = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(1, count($bms['bookmarks']));
+ $bm = reset($bms['bookmarks']);
+ $this->assertEquals('0', $bm['bStatus']);
+ }//end testDefaultPrivacyPublic
+
+
+}
+?>
diff --git a/tests/Api/PostsDeleteTest.php b/tests/Api/PostsDeleteTest.php
new file mode 100644
index 0000000..7ba1285
--- /dev/null
+++ b/tests/Api/PostsDeleteTest.php
@@ -0,0 +1,278 @@
+<?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 'HTTP/Request2.php';
+
+/**
+ * Unit tests for the SemanticScuttle post deletion 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_PostsDeleteTest extends TestBaseApi
+{
+ protected $urlPart = 'api/posts/delete';
+
+
+
+ /**
+ * 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 if deleting an own bookmark works.
+ */
+ public function testDeleteOwnBookmark()
+ {
+ $this->bs->deleteAll();
+
+ $bookmarkUrl = 'http://example.org/tag-1';
+
+ list($req, $uId) = $this->getAuthRequest(
+ '?url=' . urlencode($bookmarkUrl)
+ );
+
+ $bId = $this->addBookmark(
+ $uId, $bookmarkUrl, 0,
+ array('unittest', 'tag1')
+ );
+ //user has one bookmark now
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(1, $data['total']);
+
+ //send request
+ $res = $req->send();
+
+ $this->assertEquals(200, $res->getStatus());
+ //verify MIME content type
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+
+ //verify xml
+ $this->assertTag(
+ array(
+ 'tag' => 'result',
+ 'attributes' => array('code' => 'done')
+ ),
+ $res->getBody(),
+ null, false
+ );
+
+ //bookmark should be deleted now
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(0, $data['total']);
+ }
+
+
+
+ /**
+ * Test if deleting an own bookmark via POST works.
+ */
+ public function testDeleteOwnBookmarkPost()
+ {
+ $this->bs->deleteAll();
+
+ $bookmarkUrl = 'http://example.org/tag-1';
+
+ list($req, $uId) = $this->getAuthRequest();
+
+ $bId = $this->addBookmark(
+ $uId, $bookmarkUrl, 0,
+ array('unittest', 'tag1')
+ );
+ //user has one bookmark now
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(1, $data['total']);
+
+ //send request
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->addPostParameter('url', $bookmarkUrl);
+ $res = $req->send();
+
+ $this->assertEquals(200, $res->getStatus());
+ //verify MIME content type
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+
+ //verify xml
+ $this->assertTag(
+ array(
+ 'tag' => 'result',
+ 'attributes' => array('code' => 'done')
+ ),
+ $res->getBody(),
+ null, false
+ );
+
+ //bookmark should be deleted now
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(0, $data['total']);
+ }
+
+
+
+ /**
+ * Verify that deleting a bookmark of a different does not work
+ */
+ public function testDeleteOtherBookmark()
+ {
+ $this->bs->deleteAll();
+
+ $bookmarkUrl = 'http://example.org/tag-1';
+
+ list($req, $uId) = $this->getAuthRequest(
+ '?url=' . urlencode($bookmarkUrl)
+ );
+ $uId2 = $this->addUser();
+
+ $bId = $this->addBookmark(
+ $uId2, $bookmarkUrl, 0,
+ array('unittest', 'tag1')
+ );
+ //user 1 has no bookmarks
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(0, $data['total']);
+ //user 2 has one bookmark
+ $data = $this->bs->getBookmarks(0, null, $uId2);
+ $this->assertEquals(1, $data['total']);
+
+ //send request
+ $res = $req->send();
+
+ //404 - user does not have that bookmark
+ $this->assertEquals(404, $res->getStatus());
+
+ //verify MIME content type
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+
+ //verify xml
+ $this->assertTag(
+ array(
+ 'tag' => 'result',
+ 'attributes' => array('code' => 'item not found')
+ ),
+ $res->getBody(),
+ '', false
+ );
+
+ //bookmark should still be there
+ $data = $this->bs->getBookmarks(0, null, $uId2);
+ $this->assertEquals(1, $data['total']);
+ }
+
+
+
+ /**
+ * Test if deleting a bookmark works that also other users
+ * bookmarked.
+ */
+ public function testDeleteBookmarkOneOfTwo()
+ {
+ $this->bs->deleteAll();
+
+ $bookmarkUrl = 'http://example.org/tag-1';
+
+ list($req, $uId) = $this->getAuthRequest(
+ '?url=' . urlencode($bookmarkUrl)
+ );
+ $uId2 = $this->addUser();
+ $uId3 = $this->addUser();
+
+ //important: the order of addition is crucial here
+ $this->addBookmark(
+ $uId2, $bookmarkUrl, 0,
+ array('unittest', 'tag1')
+ );
+ $bId = $this->addBookmark(
+ $uId, $bookmarkUrl, 0,
+ array('unittest', 'tag1')
+ );
+ $this->addBookmark(
+ $uId3, $bookmarkUrl, 0,
+ array('unittest', 'tag1')
+ );
+
+ //user one and two have a bookmark now
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(1, $data['total']);
+ $data = $this->bs->getBookmarks(0, null, $uId2);
+ $this->assertEquals(1, $data['total']);
+
+ //send request
+ $res = $req->send();
+
+ $this->assertEquals(200, $res->getStatus());
+ //verify MIME content type
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+
+ //verify xml
+ $this->assertTag(
+ array(
+ 'tag' => 'result',
+ 'attributes' => array('code' => 'done')
+ ),
+ $res->getBody(),
+ '', false
+ );
+
+ //bookmark should be deleted now
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(0, $data['total']);
+ //user 2 should still have his
+ $data = $this->bs->getBookmarks(0, null, $uId2);
+ $this->assertEquals(1, $data['total']);
+ //user 3 should still have his, too
+ $data = $this->bs->getBookmarks(0, null, $uId3);
+ $this->assertEquals(1, $data['total']);
+ }
+
+}
+?> \ No newline at end of file
diff --git a/tests/Api/PostsUpdateTest.php b/tests/Api/PostsUpdateTest.php
new file mode 100644
index 0000000..51f8be2
--- /dev/null
+++ b/tests/Api/PostsUpdateTest.php
@@ -0,0 +1,110 @@
+<?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 'HTTP/Request2.php';
+
+/**
+ * Unit tests for the SemanticScuttle last-update time 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_PostsUpdateTest extends TestBaseApi
+{
+ protected $urlPart = 'api/posts/update';
+
+
+
+ /**
+ * 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());
+ }
+
+
+
+ /**
+ * See if posts/update behaves correct if there is one bookmark
+ */
+ public function testPostUpdateOneBookmark()
+ {
+ $this->bs->deleteAll();
+
+ list($req, $uId) = $this->getAuthRequest();
+ $bId = $this->addBookmark(
+ $uId, 'http://example.org/tag1', 0,
+ array('unittest', 'tag1')
+ );
+
+ $data = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(1, $data['total']);
+ $bookmark = $data['bookmarks'][0];
+
+ //send request
+ $res = $req->send();
+
+ $this->assertEquals(200, $res->getStatus());
+ //verify MIME content type
+ $this->assertEquals(
+ 'text/xml; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+
+ //verify xml
+ $this->assertTag(
+ array(
+ 'tag' => 'update',
+ 'attributes' => array(
+ 'inboxnew' => '0'
+ )
+ ),
+ $res->getBody(),
+ '', false
+ );
+ //check time
+ $xml = simplexml_load_string($res->getBody());
+ $this->assertTrue(isset($xml['time']));
+ $this->assertEquals(
+ strtotime($bookmark['bDatetime']),
+ strtotime(
+ (string)$xml['time']
+ )
+ );
+ }
+
+}
+?> \ No newline at end of file
diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php
new file mode 100644
index 0000000..76390d9
--- /dev/null
+++ b/tests/Bookmark2TagTest.php
@@ -0,0 +1,688 @@
+<?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
+ */
+
+/**
+ * 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;
+
+
+ /**
+ * Create a bookmark. Like addBookmark(), just with other paramter order
+ * to make some tests in that class easier to write.
+ *
+ * @param integer $user User ID the bookmark shall belong
+ * @param array $tags Array of tags to attach. If "null" is given,
+ * it will automatically be "unittest"
+ * @param string $date strtotime-compatible string
+ * @param string $title Bookmark title
+ *
+ * @return integer ID of bookmark
+ */
+ protected function addTagBookmark($user, $tags, $date = null, $title = null)
+ {
+ return $this->addBookmark(
+ $user, null, 0, $tags, $title, $date
+ );
+ }
+
+
+
+ protected function setUp()
+ {
+ $this->us = SemanticScuttle_Service_Factory::get('User');
+ $this->us->deleteAll();
+ $this->bs = SemanticScuttle_Service_Factory::get('Bookmark');
+ $this->bs->deleteAll();
+ $this->b2ts= SemanticScuttle_Service_Factory::get('Bookmark2Tag');
+ $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();
+ }
+
+
+
+ public function testAttachTagsWithoutTagsAddsSystemUnfiled()
+ {
+ $bid = $this->addBookmark(null, null, 0, array());
+ $this->assertEquals(
+ array('system:unfiled'),
+ $this->b2ts->getTagsForBookmark($bid, true)
+ );
+ }
+
+ public function testAttachTagsWithArrayWithEmptyStringAddsSystemUnfiled()
+ {
+ $bid = $this->addBookmark(null, null, 0, array(''));
+ $this->assertEquals(
+ array('system:unfiled'),
+ $this->b2ts->getTagsForBookmark($bid, true)
+ );
+ }
+
+ public function testAttachTagsWithEmptyStringAddsSystemUnfiled()
+ {
+ $originalDisplayErros = ini_get('display_errors');
+ $originalErrorReporting = ini_get('error_reporting');
+ ini_set('display_errors', 1);
+ error_reporting(E_ALL);
+ $bid = $this->addBookmark(null, null, 0, '');
+ $this->assertEquals(
+ array('system:unfiled'),
+ $this->b2ts->getTagsForBookmark($bid, true)
+ );
+ ini_set('display_errors', $originalDisplayErros);
+ error_reporting($originalErrorReporting);
+ }
+
+ public function testAttachTagsWithSomeEmptyTags()
+ {
+ $bid = $this->addBookmark(null, null, 0, array());
+ $this->b2ts->attachTags($bid, array('foo', '', 'bar', 'baz'));
+ $this->assertEquals(
+ array('foo', 'bar', 'baz'),
+ $this->b2ts->getTagsForBookmark($bid)
+ );
+ }
+
+ /**
+ * Test getTagsForBookmark() when the bookmark has no tags
+ *
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmark
+ */
+ 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
+ *
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmark
+ */
+ 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
+ *
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmark
+ */
+ public function testGetTagsForBookmarkThr()
+ {
+ $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->assertInternalType('array', $tags);
+ $this->assertContains('foo', $tags);
+ $this->assertContains('bar', $tags);
+ $this->assertContains('fuu', $tags);
+ }
+
+
+
+ /**
+ * Test getTagsForBookmarks() when no bookmarks have tags.
+ *
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmarks
+ */
+ 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->assertInternalType('array', $alltags);
+ $this->assertEquals(2, count($alltags));
+ $this->assertInternalType('array', $alltags[$bid1]);
+ $this->assertInternalType('array', $alltags[$bid2]);
+ $this->assertEquals(0, count($alltags[$bid1]));
+ $this->assertEquals(0, count($alltags[$bid2]));
+ }
+
+
+
+ /**
+ * Test getTagsForBookmarks() when most bookmarks have tags.
+ *
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmarks
+ */
+ 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->assertInternalType('array', $alltags);
+ foreach ($alltags as $bid => $btags) {
+ $this->assertInternalType('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');
+ }
+ }
+ }
+
+
+
+ /**
+ * Fetch the most popular tags in descending order
+ *
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags
+ */
+ public function testGetPopularTagsOrder()
+ {
+ $user = $this->addUser();
+ $this->addTagBookmark($user, array('one', 'two'));
+ $this->addTagBookmark($user, array('one', 'thr'));
+ $this->addTagBookmark($user, array('one', 'two'));
+
+ $arTags = $this->b2ts->getPopularTags();
+ $this->assertInternalType('array', $arTags);
+ $this->assertEquals(3, count($arTags));
+
+ $this->assertInternalType('array', $arTags[0]);
+
+ $this->assertEquals(
+ array(
+ array('tag' => 'one', 'bCount' => '3'),
+ array('tag' => 'two', 'bCount' => '2'),
+ array('tag' => 'thr', 'bCount' => '1')
+ ),
+ $arTags
+ );
+ }
+
+
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags
+ */
+ public function testGetPopularTagsLimit()
+ {
+ $user = $this->addUser();
+ $this->addTagBookmark($user, array('one', 'two'));
+ $this->addTagBookmark($user, array('one', 'thr'));
+ $this->addTagBookmark($user, array('one', 'two'));
+
+ $arTags = $this->b2ts->getPopularTags();
+ $this->assertInternalType('array', $arTags);
+ $this->assertEquals(3, count($arTags));
+
+ $arTags = $this->b2ts->getPopularTags(null, 2);
+ $this->assertInternalType('array', $arTags);
+ $this->assertEquals(2, count($arTags));
+ $this->assertEquals(
+ array(
+ array('tag' => 'one', 'bCount' => '3'),
+ array('tag' => 'two', 'bCount' => '2'),
+ ),
+ $arTags
+ );
+
+ $arTags = $this->b2ts->getPopularTags(null, 1);
+ $this->assertInternalType('array', $arTags);
+ $this->assertEquals(1, count($arTags));
+ $this->assertEquals(
+ array(
+ array('tag' => 'one', 'bCount' => '3'),
+ ),
+ $arTags
+ );
+ }
+
+
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags
+ */
+ public function testGetPopularTagsDays()
+ {
+ $user = $this->addUser();
+ $this->addTagBookmark($user, array('one', 'two'), 'now');
+ $this->addTagBookmark($user, array('one', 'thr'), 'now');
+ $this->addTagBookmark($user, array('one', 'two'), '-1 day -1 hour');
+ $this->addTagBookmark($user, array('one', 'thr'), '-3 days -1 hour');
+
+ $arTags = $this->b2ts->getPopularTags(null, 10, null, 1);
+ $this->assertInternalType('array', $arTags);
+ $this->assertEquals(3, count($arTags));
+ $this->assertContains(array('tag' => 'one', 'bCount' => '2'), $arTags);
+ $this->assertContains(array('tag' => 'two', 'bCount' => '1'), $arTags);
+ $this->assertContains(array('tag' => 'thr', 'bCount' => '1'), $arTags);
+
+ $arTags = $this->b2ts->getPopularTags(null, 10, null, 2);
+ $this->assertInternalType('array', $arTags);
+ $this->assertEquals(3, count($arTags));
+ $this->assertEquals(
+ array(
+ array('tag' => 'one', 'bCount' => '3'),
+ array('tag' => 'two', 'bCount' => '2'),
+ array('tag' => 'thr', 'bCount' => '1'),
+ ),
+ $arTags
+ );
+
+ $arTags = $this->b2ts->getPopularTags(null, 10, null, 5);
+ $this->assertInternalType('array', $arTags);
+ $this->assertEquals(3, count($arTags));
+ $this->assertContains(array('tag' => 'one', 'bCount' => '4'), $arTags);
+ $this->assertContains(array('tag' => 'two', 'bCount' => '2'), $arTags);
+ $this->assertContains(array('tag' => 'thr', 'bCount' => '2'), $arTags);
+ }
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags
+ */
+ public function testGetPopularTagsBeginsWith()
+ {
+ $user = $this->addUser();
+ $this->addTagBookmark($user, array('one', 'two'));
+ $this->addTagBookmark($user, array('one', 'thr'));
+ $this->addTagBookmark($user, array('one', 'two'));
+ $this->addTagBookmark($user, array('one', 'thr'));
+
+ $arTags = $this->b2ts->getPopularTags(null, 10, null, null, 'o');
+ $this->assertEquals(1, count($arTags));
+ $this->assertContains(array('tag' => 'one', 'bCount' => '4'), $arTags);
+
+ $arTags = $this->b2ts->getPopularTags(null, 10, null, null, 'tw');
+ $this->assertEquals(1, count($arTags));
+ $this->assertContains(array('tag' => 'two', 'bCount' => '2'), $arTags);
+
+ $arTags = $this->b2ts->getPopularTags(null, 10, null, null, 't');
+ $this->assertEquals(2, count($arTags));
+ $this->assertContains(array('tag' => 'two', 'bCount' => '2'), $arTags);
+ $this->assertContains(array('tag' => 'thr', 'bCount' => '2'), $arTags);
+ }
+
+
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags
+ */
+ public function testGetPopularTagsExcludesSystemTags()
+ {
+ $user = $this->addUser();
+ $this->addTagBookmark($user, array('one', 'system:test'));
+ $this->addTagBookmark($user, array('one', 'system:unittest'));
+ $this->addTagBookmark($user, array('one', 'sys:unittest'));
+
+ $arTags = $this->b2ts->getPopularTags();
+ $this->assertInternalType('array', $arTags);
+ $this->assertEquals(2, count($arTags));
+ $this->assertEquals(
+ array(
+ array('tag' => 'one', 'bCount' => '3'),
+ array('tag' => 'sys:unittest', 'bCount' => '1'),
+ ),
+ $arTags
+ );
+ }
+
+
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags
+ */
+ public function testGetPopularTagsUserTags()
+ {
+ $user1 = $this->addUser();
+ $user2 = $this->addUser();
+ $user3 = $this->addUser();
+ $this->addTagBookmark($user1, array('one'));
+ $this->addTagBookmark($user2, array('one', 'two'));
+ $this->addTagBookmark($user2, array('two'));
+ $this->addTagBookmark($user3, array('one', 'thr'));
+
+ $arTags = $this->b2ts->getPopularTags($user1);
+ $this->assertEquals(1, count($arTags));
+ $this->assertEquals(
+ array(
+ array('tag' => 'one', 'bCount' => '1'),
+ ),
+ $arTags
+ );
+
+ $arTags = $this->b2ts->getPopularTags($user2);
+ $this->assertEquals(2, count($arTags));
+ $this->assertEquals(
+ array(
+ array('tag' => 'two', 'bCount' => '2'),
+ array('tag' => 'one', 'bCount' => '1'),
+ ),
+ $arTags
+ );
+
+ $arTags = $this->b2ts->getPopularTags(array($user2, $user3));
+ $this->assertEquals(3, count($arTags));
+ $this->assertContains(array('tag' => 'one', 'bCount' => '2'), $arTags);
+ $this->assertContains(array('tag' => 'two', 'bCount' => '2'), $arTags);
+ $this->assertContains(array('tag' => 'thr', 'bCount' => '1'), $arTags);
+ }
+
+
+
+ /**
+ * This may happen when the method is called with a problematic user array.
+ * In that case we may not generate invalid SQL or so.
+ *
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags
+ */
+ public function testGetPopularTagsUserArrayWithNull()
+ {
+ $user1 = $this->addUser();
+ $this->addTagBookmark($user1, array('one'));
+
+ $arTags = $this->b2ts->getPopularTags(array(null));
+ $this->assertEquals(0, count($arTags));
+ }
+
+
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags
+ */
+ public function testGetPopularTagsPublicOnlyNoUser()
+ {
+ $user1 = $this->addUser();
+ $this->addBookmark($user1, null, 0, array('one'));
+ $this->addBookmark($user1, null, 1, array('one', 'two'));
+ $this->addBookmark($user1, null, 2, array('thr'));
+
+ $arTags = $this->b2ts->getPopularTags();
+ $this->assertEquals(1, count($arTags));
+ $this->assertEquals(
+ array(
+ array('tag' => 'one', 'bCount' => '1'),
+ ),
+ $arTags
+ );
+ }
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags
+ */
+ public function testGetPopularTagsPublicOnlySingleUser()
+ {
+ $user1 = $this->addUser();
+ $this->addBookmark($user1, null, 0, array('one'));
+ $this->addBookmark($user1, null, 1, array('one', 'two'));
+ $this->addBookmark($user1, null, 2, array('thr'));
+
+ $arTags = $this->b2ts->getPopularTags($user1);
+ $this->assertEquals(1, count($arTags));
+ $this->assertEquals(
+ array(
+ array('tag' => 'one', 'bCount' => '1'),
+ ),
+ $arTags
+ );
+ }
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags
+ */
+ public function testGetPopularTagsPublicOnlySeveralUsers()
+ {
+ $user1 = $this->addUser();
+ $user2 = $this->addUser();
+ $this->addBookmark($user1, null, 0, array('one'));
+ $this->addBookmark($user1, null, 1, array('one', 'two'));
+ $this->addBookmark($user1, null, 2, array('thr'));
+ $this->addBookmark($user2, null, 0, array('fou'));
+ $this->addBookmark($user2, null, 1, array('fiv'));
+ $this->addBookmark($user2, null, 2, array('six'));
+
+ $arTags = $this->b2ts->getPopularTags(array($user1, $user2));
+ $this->assertEquals(2, count($arTags));
+ $this->assertContains(array('tag' => 'one', 'bCount' => '1'), $arTags);
+ $this->assertContains(array('tag' => 'fou', 'bCount' => '1'), $arTags);
+ }
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags
+ */
+ public function testGetPopularTagsUserPrivatesWhenLoggedIn()
+ {
+ $user1 = $this->addUser();
+ $this->addBookmark($user1, null, 0, array('one'));
+ $this->addBookmark($user1, null, 1, array('one', 'two'));
+ $this->addBookmark($user1, null, 2, array('thr'));
+
+ $arTags = $this->b2ts->getPopularTags($user1, 10, $user1);
+ $this->assertEquals(3, count($arTags));
+ $this->assertContains(array('tag' => 'one', 'bCount' => '2'), $arTags);
+ $this->assertContains(array('tag' => 'two', 'bCount' => '1'), $arTags);
+ $this->assertContains(array('tag' => 'thr', 'bCount' => '1'), $arTags);
+ }
+
+ /**
+ * Should return the logged on user's public, protected and private tags
+ * as well as public ones of other specified users.
+ *
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getPopularTags
+ */
+ public function testGetPopularTagsUserPrivatesAndOthersWhenLoggedIn()
+ {
+ $user1 = $this->addUser();
+ $user2 = $this->addUser();
+ $this->addBookmark($user1, null, 0, array('one'));
+ $this->addBookmark($user1, null, 1, array('one', 'two'));
+ $this->addBookmark($user1, null, 2, array('thr'));
+ $this->addBookmark($user2, null, 0, array('fou'));
+ $this->addBookmark($user2, null, 1, array('fiv'));
+ $this->addBookmark($user2, null, 2, array('six'));
+
+ $arTags = $this->b2ts->getPopularTags(array($user2, $user1), 10, $user1);
+ $this->assertContains(array('tag' => 'one', 'bCount' => '2'), $arTags);
+ $this->assertContains(array('tag' => 'two', 'bCount' => '1'), $arTags);
+ $this->assertContains(array('tag' => 'thr', 'bCount' => '1'), $arTags);
+ $this->assertContains(array('tag' => 'fou', 'bCount' => '1'), $arTags);
+ $this->assertEquals(4, count($arTags));
+ }
+
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getAdminTags
+ */
+ public function testGetAdminTags()
+ {
+ $admin1 = $this->addUser('admin1');
+ $admin2 = $this->addUser('admin2');
+ $user1 = $this->addUser();
+ $this->addBookmark($admin1, null, 0, array('admintag', 'admintag1'));
+ $this->addBookmark($admin2, null, 0, array('admintag', 'admintag2'));
+ $this->addBookmark($user1, null, 0, array('usertag'));
+
+ $GLOBALS['admin_users'] = array('admin1', 'admin2');
+
+ $arTags = $this->b2ts->getAdminTags(4);
+ $this->assertEquals(3, count($arTags));
+ $this->assertContains(array('tag' => 'admintag', 'bCount' => '2'), $arTags);
+ $this->assertContains(array('tag' => 'admintag1', 'bCount' => '1'), $arTags);
+ $this->assertContains(array('tag' => 'admintag2', 'bCount' => '1'), $arTags);
+ }
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getAdminTags
+ */
+ public function testGetAdminTagsBeginsWith()
+ {
+ $admin1 = $this->addUser('admin1');
+ $this->addBookmark($admin1, null, 0, array('admintag', 'admintag1'));
+ $this->addBookmark($admin1, null, 0, array('tester', 'testos'));
+
+ $GLOBALS['admin_users'] = array('admin1');
+
+ $arTags = $this->b2ts->getAdminTags(4, null, null, 'test');
+ $this->assertEquals(2, count($arTags));
+ $this->assertContains(array('tag' => 'tester', 'bCount' => '1'), $arTags);
+ $this->assertContains(array('tag' => 'testos', 'bCount' => '1'), $arTags);
+ }
+
+
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getContactTags
+ */
+ public function testGetContactTagsWatchlistOnly()
+ {
+ $user1 = $this->addUser();
+ $user2 = $this->addUser();
+ $user3 = $this->addUser();
+ $this->us->setCurrentUserId($user1);
+ $this->us->setWatchStatus($user2);
+ //user1 watches user2 now
+
+ $this->addBookmark($user1, null, 0, array('usertag', 'usertag1'));
+ $this->addBookmark($user2, null, 0, array('usertag', 'usertag2'));
+ $this->addBookmark($user3, null, 0, array('usertag', 'usertag3'));
+
+ $arTags = $this->b2ts->getContactTags($user1, 10);
+ $this->assertEquals(2, count($arTags));
+ $this->assertContains(array('tag' => 'usertag', 'bCount' => '1'), $arTags);
+ $this->assertContains(array('tag' => 'usertag2', 'bCount' => '1'), $arTags);
+ }
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getContactTags
+ */
+ public function testGetContactTagsIncludingUser()
+ {
+ $user1 = $this->addUser();
+ $user2 = $this->addUser();
+ $user3 = $this->addUser();
+ $this->us->setCurrentUserId($user1);
+ $this->us->setWatchStatus($user2);
+ //user1 watches user2 now
+
+ $this->addBookmark($user1, null, 0, array('usertag', 'usertag1'));
+ $this->addBookmark($user2, null, 0, array('usertag', 'usertag2'));
+ $this->addBookmark($user3, null, 0, array('usertag', 'usertag3'));
+
+ $arTags = $this->b2ts->getContactTags($user1, 10, $user1);
+ $this->assertEquals(3, count($arTags));
+ $this->assertContains(array('tag' => 'usertag', 'bCount' => '2'), $arTags);
+ $this->assertContains(array('tag' => 'usertag1', 'bCount' => '1'), $arTags);
+ $this->assertContains(array('tag' => 'usertag2', 'bCount' => '1'), $arTags);
+ }
+
+ /**
+ * @covers SemanticScuttle_Service_Bookmark2Tag::getContactTags
+ */
+ public function testGetContactTagsBeginsWith()
+ {
+ $user1 = $this->addUser();
+ $this->addBookmark($user1, null, 0, array('usertag', 'usertag1'));
+ $this->addBookmark($user1, null, 0, array('usable', 'undefined'));
+ $this->addBookmark($user1, null, 0, array('fußbad', 'usable'));
+
+ $arTags = $this->b2ts->getContactTags($user1, 10, $user1, null, 'user');
+ $this->assertEquals(2, count($arTags));
+ $this->assertContains(array('tag' => 'usertag', 'bCount' => '1'), $arTags);
+ $this->assertContains(array('tag' => 'usertag1', 'bCount' => '1'), $arTags);
+
+ $arTags = $this->b2ts->getContactTags($user1, 10, $user1, null, 'us');
+ $this->assertEquals(3, count($arTags));
+ $this->assertContains(array('tag' => 'usertag', 'bCount' => '1'), $arTags);
+ $this->assertContains(array('tag' => 'usertag1', 'bCount' => '1'), $arTags);
+ $this->assertContains(array('tag' => 'usable', 'bCount' => '2'), $arTags);
+ }
+
+ public function testHasTag()
+ {
+ $bid = $this->addBookmark(null, null, 0, array('foo'));
+
+ $this->assertTrue($this->b2ts->hasTag($bid, 'foo'));
+ $this->assertFalse($this->b2ts->hasTag($bid, 'bar'));
+
+ }
+}
+?>
diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php
new file mode 100644
index 0000000..c436859
--- /dev/null
+++ b/tests/BookmarkTest.php
@@ -0,0 +1,1380 @@
+<?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
+ */
+
+/**
+ * Unit tests for the SemanticScuttle bookmark 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 BookmarkTest extends TestBase
+{
+ protected $us;
+ protected $bs;
+ protected $ts;
+ protected $tts;
+
+
+ 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();
+ }
+
+ /**
+ * Tests if adding a bookmark with short url name
+ * saves it in the database.
+ */
+ public function testAddBookmarkShort()
+ {
+ $bid = $this->bs->addBookmark(
+ 'http://example.org', 'title', 'desc', 'priv',
+ 0, array(), 'myShortName'
+ );
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals('http://example.org', $bm['bAddress']);
+ $this->assertArrayHasKey('bShort', $bm);
+ $this->assertEquals('myShortName', $bm['bShort']);
+ }
+
+ public function testAddBookmarkInvalidUrl()
+ {
+ $retval = $this->bs->addBookmark(
+ 'javascript:alert(123)', 'title', 'desc', 'priv',
+ 0, array()
+ );
+ $this->assertFalse($retval, 'Bookmark with invalid URL was accepted');
+ }
+
+ public function testAddBookmarkWithSpecialCharacters()
+ {
+ $bs = $this->bs;
+ $title = "title&é\"'(-è_çà)=";
+ $desc = "description#{[|`\^@]}³<> ¹¡÷׿&é\"'(-è\\_çà)=";
+ $tag1 = "#{|`^@]³¹¡¿<&é\"'(-è\\_çà)";
+ $tag2 = "&é\"'(-è.[?./§!_çà)";
+
+ $uid = $this->addUser();
+ $bid = $bs->addBookmark(
+ 'http://site1.com', $title, $desc, 'note',
+ 0, array($tag1, $tag2),
+ null, null, false, false, $uid
+ );
+
+ $bookmarks = $bs->getBookmarks(0, 1);
+
+ $b0 = $bookmarks['bookmarks'][0];
+ $this->assertEquals($title, $b0['bTitle']);
+ $this->assertEquals($desc, $b0['bDescription']);
+ $this->assertEquals(
+ str_replace(array('"', '\'', '/'), "_", $tag1),
+ $b0['tags'][0]
+ );
+ $this->assertEquals(
+ str_replace(array('"', '\'', '/'), "_", $tag2),
+ $b0['tags'][1]
+ );
+ }
+
+ public function testAddBookmarkUnification()
+ {
+ $bs = $this->bs;
+
+ $uid = $this->addUser();
+ $uid2 = $this->addUser();
+
+ $bs->addBookmark(
+ 'http://site1.com', "title", "description", 'note',
+ 0, array('tag1'), null, null, false, false,
+ $uid
+ );
+ $bs->addBookmark(
+ "http://site1.com", "title2", "description2", 'note',
+ 0, array('tag2'), null, null, false, false,
+ $uid2
+ );
+
+ $bookmarks = $bs->getBookmarks();
+ $this->assertEquals(1, $bookmarks['total']);
+ }
+
+ /*public function testSearchingBookmarksAccentsInsensible()
+ {
+ $bs = $this->bs;
+
+ $bs->addBookmark("http://site1.com", "title", "éèüaàê", "status", array('tag1'), null, false, false, 1);
+ $bookmarks = $bs->getBookmarks(0, NULL, NULL, NULL, $terms = "eeaae"); //void
+ $this->assertEquals(0, $bookmarks['total']);
+ $bookmarks = $bs->getBookmarks(0, NULL, NULL, NULL, $terms = "eeuaae");
+ $this->assertEquals(1, $bookmarks['total']);
+ }*/
+
+
+
+ /**
+ * Tests if bookmarkExists() returns false when the given
+ * parameter is invalid.
+ *
+ * @return void
+ */
+ public function testBookmarkExistsInvalidParam()
+ {
+ $this->assertFalse($this->bs->bookmarkExists(false));
+ $this->assertFalse($this->bs->bookmarkExists(null));
+ }
+
+
+
+ /**
+ * Tests if bookmarkExists() returns true when a bookmark
+ * exists
+ *
+ * @return void
+ */
+ public function testBookmarkExistsTrue()
+ {
+ $bid = $this->addBookmark();
+ $bookmark = $this->bs->getBookmark($bid);
+
+ $this->assertTrue($this->bs->bookmarkExists($bookmark['bAddress']));
+ }
+
+
+
+ /**
+ * Tests if bookmarkExists() returns false when a bookmark
+ * does not exist
+ *
+ * @return void
+ */
+ public function testBookmarkExistsFalse()
+ {
+ $this->assertFalse($this->bs->bookmarkExists('does-not-exist'));
+ }
+
+
+
+ /**
+ * Tests if bookmarkExists() returns true when a bookmark
+ * exists for a user
+ *
+ * @return void
+ */
+ public function testBookmarkExistsUserTrue()
+ {
+ $bid = $this->addBookmark();
+ $bookmark = $this->bs->getBookmark($bid);
+
+ $this->assertTrue(
+ $this->bs->bookmarkExists(
+ $bookmark['bAddress'],
+ $bookmark['uId']
+ )
+ );
+ }
+
+
+
+ /**
+ * Tests if bookmarkExists() returns false when a bookmark
+ * does not exist for a user
+ *
+ * @return void
+ */
+ public function testBookmarkExistsUserFalse()
+ {
+ $this->assertFalse(
+ $this->bs->bookmarkExists('does-not-exist', 1234)
+ );
+ }
+
+
+
+ /**
+ * Tests if bookmarkExists() returns false when a bookmark
+ * does not exist for a user but for another user
+ *
+ * @return void
+ */
+ public function testBookmarkExistsOtherUser()
+ {
+ $bid = $this->addBookmark();
+ $bookmark = $this->bs->getBookmark($bid);
+
+ $this->assertFalse(
+ $this->bs->bookmarkExists(
+ $bookmark['bAddress'],
+ $bookmark['uId'] + 1
+ )
+ );
+ }
+
+
+
+ /**
+ * 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->assertInternalType('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->assertInternalType('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->assertInternalType('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->assertInternalType('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->assertInternalType('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
+ */
+ public function testCountBookmarksNone()
+ {
+ $uid = $this->addUser();
+ $this->assertEquals(0, $this->bs->countBookmarks($uid));
+ $this->assertEquals(0, $this->bs->countBookmarks($uid, 'public'));
+ $this->assertEquals(0, $this->bs->countBookmarks($uid, 'private'));
+ $this->assertEquals(0, $this->bs->countBookmarks($uid, 'shared'));
+ $this->assertEquals(0, $this->bs->countBookmarks($uid, 'all'));
+ }
+
+
+
+ /**
+ * Test if countBookmarks() works with one public bookmark
+ *
+ * @return void
+ */
+ public function testCountBookmarksOnePublic()
+ {
+ $uid = $this->addUser();
+ $this->addBookmark($uid);
+ $this->assertEquals(1, $this->bs->countBookmarks($uid));
+ $this->assertEquals(1, $this->bs->countBookmarks($uid, 'public'));
+ $this->assertEquals(0, $this->bs->countBookmarks($uid, 'private'));
+ $this->assertEquals(0, $this->bs->countBookmarks($uid, 'shared'));
+ $this->assertEquals(1, $this->bs->countBookmarks($uid, 'all'));
+ }
+
+
+
+ /**
+ * Test if countBookmarks() works with one private bookmark
+ *
+ * @return void
+ */
+ public function testCountBookmarksOnePrivate()
+ {
+ $uid = $this->addUser();
+ $this->bs->addBookmark(
+ 'http://test', 'test', 'desc', 'note',
+ 2,//private
+ array(), null, null, false, false, $uid
+ );
+ $this->assertEquals(0, $this->bs->countBookmarks($uid));
+ $this->assertEquals(0, $this->bs->countBookmarks($uid, 'public'));
+ $this->assertEquals(1, $this->bs->countBookmarks($uid, 'private'));
+ $this->assertEquals(0, $this->bs->countBookmarks($uid, 'shared'));
+ $this->assertEquals(1, $this->bs->countBookmarks($uid, 'all'));
+ }
+
+
+
+ /**
+ * Test if countBookmarks() works with one shared bookmark
+ *
+ * @return void
+ */
+ public function testCountBookmarksOneShared()
+ {
+ $uid = $this->addUser();
+ $this->bs->addBookmark(
+ 'http://test', 'test', 'desc', 'note',
+ 1,//shared
+ array(), null, null, false, false, $uid
+ );
+ $this->assertEquals(0, $this->bs->countBookmarks($uid));
+ $this->assertEquals(0, $this->bs->countBookmarks($uid, 'public'));
+ $this->assertEquals(0, $this->bs->countBookmarks($uid, 'private'));
+ $this->assertEquals(1, $this->bs->countBookmarks($uid, 'shared'));
+ $this->assertEquals(1, $this->bs->countBookmarks($uid, 'all'));
+ }
+
+
+
+ /**
+ * 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->assertInternalType('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
+ */
+ public function testDeleteBookmark()
+ {
+ $bookmarks = $this->bs->getBookmarks();
+ $this->assertEquals(0, $bookmarks['total']);
+
+ $bid = $this->addBookmark();
+ $bookmarks = $this->bs->getBookmarks();
+ $this->assertEquals(1, $bookmarks['total']);
+
+ $bid2 = $this->addBookmark();
+ $bookmarks = $this->bs->getBookmarks();
+ $this->assertEquals(2, $bookmarks['total']);
+
+ $this->assertTrue($this->bs->deleteBookmark($bid));
+ $bookmarks = $this->bs->getBookmarks();
+ $this->assertEquals(1, $bookmarks['total']);
+
+ $this->assertTrue($this->bs->deleteBookmark($bid2));
+ $bookmarks = $this->bs->getBookmarks();
+ $this->assertEquals(0, $bookmarks['total']);
+ }
+
+
+
+ /**
+ * Test if deleting all bookmarks for a user works.
+ *
+ * @return void
+ */
+ public function testDeleteBookmarksForUser()
+ {
+ $uid = $this->addUser();
+ $bookmarks = $this->bs->getBookmarks(0, null, $uid);
+ $this->assertEquals(0, $bookmarks['total']);
+
+ $this->addBookmark($uid);
+ $this->addBookmark($uid);
+ $bookmarks = $this->bs->getBookmarks(0, null, $uid);
+ $this->assertEquals(2, $bookmarks['total']);
+
+ $this->bs->deleteBookmarksForUser($uid);
+ $bookmarks = $this->bs->getBookmarks(0, null, $uid);
+ $this->assertEquals(0, $bookmarks['total']);
+ }
+
+
+
+ /**
+ * Test if deleting all bookmarks for a user works
+ * and does not damage other user's bookmarks.
+ *
+ * @return void
+ */
+ public function testDeleteBookmarksForUserOthers()
+ {
+ $uidOther = $this->addUser();
+ $this->addBookmark($uidOther);
+
+ $uid = $this->addUser();
+ $bookmarks = $this->bs->getBookmarks(0, null, $uid);
+ $this->assertEquals(0, $bookmarks['total']);
+
+ $this->addBookmark($uid);
+ $this->addBookmark($uid);
+ $bookmarks = $this->bs->getBookmarks(0, null, $uid);
+ $this->assertEquals(2, $bookmarks['total']);
+
+ $this->bs->deleteBookmarksForUser($uid);
+ $bookmarks = $this->bs->getBookmarks(0, null, $uid);
+ $this->assertEquals(0, $bookmarks['total']);
+
+ $bookmarks = $this->bs->getBookmarks(0, null, $uidOther);
+ $this->assertEquals(1, $bookmarks['total']);
+ }
+
+
+
+ /**
+ * Test if deleting a bookmark with a vote works.
+ *
+ * @return void
+ */
+ public function testDeleteBookmarkWithVote()
+ {
+ $GLOBALS['enableVoting'] = true;
+
+ $uid = $this->addUser();
+ $bid = $this->addBookmark();
+
+ $bid = $this->addBookmark();
+ $this->vs->vote($bid, $uid, 1);
+ $this->assertTrue($this->vs->hasVoted($bid, $uid));
+
+ $bid2 = $this->addBookmark();
+ $this->vs->vote($bid2, $uid, 1);
+ $this->assertTrue($this->vs->hasVoted($bid2, $uid));
+
+ $this->assertTrue($this->bs->deleteBookmark($bid));
+ $this->assertFalse($this->vs->hasVoted($bid, $uid));
+ $this->assertTrue($this->vs->hasVoted($bid2, $uid));
+ }
+
+
+
+ /**
+ * Test if editAllowed() returns false when the bookmark
+ * id is invalid.
+ *
+ * @return void
+ */
+ public function testEditAllowedInvalidBookmarkId()
+ {
+ $this->assertFalse($this->bs->editAllowed('invalid'));
+ $this->assertFalse($this->bs->editAllowed(array()));
+ $this->assertFalse($this->bs->editAllowed(array('some', 'where')));
+ $this->assertFalse($this->bs->editAllowed(array('bId' => false)));
+ $this->assertFalse($this->bs->editAllowed(array('bId' => 'foo')));
+ }
+
+
+
+ /**
+ * Test if editAllowed() works when passing the ID of
+ * an existing bookmark.
+ *
+ * @return void
+ */
+ public function testEditAllowedBookmarkId()
+ {
+ $uid = $this->addUser();
+ $bid = $this->addBookmark($uid);
+ $this->us->setCurrentUserId($uid);
+ $this->assertTrue($this->bs->editAllowed($bid));
+ }
+
+
+
+ /**
+ * Test if editAllowed() works when passing the ID of
+ * an existing bookmark that does not belong to the current
+ * user.
+ *
+ * @return void
+ */
+ public function testEditAllowedBookmarkIdNotOwn()
+ {
+ $uid = $this->addUser();
+ $bid = $this->addBookmark();
+ $this->us->setCurrentUserId($uid);
+ $this->assertFalse($this->bs->editAllowed($bid));
+ }
+
+
+
+ /**
+ * Test if editAllowed() works when passing the ID of
+ * an existing bookmark that does not belong to the current
+ * user.
+ *
+ * @return void
+ */
+ public function testEditAllowedBookmarkIdNoUser()
+ {
+ $bid = $this->addBookmark();
+ $this->us->setCurrentUserId(null);
+ $this->assertFalse($this->bs->editAllowed($bid));
+ }
+
+
+
+ /**
+ * Test if editAllowed() works when passing a bookmark
+ * row.
+ *
+ * @return void
+ */
+ public function testEditAllowedBookmarkRow()
+ {
+ $uid = $this->addUser();
+ $this->us->setCurrentUserId($uid);
+
+ $bid = $this->addBookmark($uid);
+ $bookmark = $this->bs->getBookmark($bid);
+ $this->assertTrue($this->bs->editAllowed($bookmark));
+ }
+
+
+
+ /**
+ * Test if editAllowed() returns false when the bookmark
+ * specified by the ID does not exist.
+ *
+ * @return void
+ */
+ public function testEditAllowedIdNotFound()
+ {
+ $this->assertFalse($this->bs->editAllowed(98765));
+ }
+
+
+
+ /**
+ * Test if editAllowed() works when the user is an administrator.
+ *
+ * @return void
+ */
+ public function testEditAllowedBookmarkAdmin()
+ {
+ //make the user admin
+ $uid = $this->addUser();
+ $user = $this->us->getUser($uid);
+ $GLOBALS['admin_users'][] = $user['username'];
+
+ $bid = $this->addBookmark($uid);
+ $this->us->setCurrentUserId($uid);
+ $this->assertTrue($this->bs->editAllowed($bid));
+ }
+
+
+
+ /**
+ * Verify that getBookmark() returns false when the
+ * bookmark cannot be found.
+ *
+ * @return void
+ */
+ public function testGetBookmarkNotFound()
+ {
+ $this->assertFalse($this->bs->getBookmark(987654));
+ }
+
+
+
+ /**
+ * Verify that getBookmark() returns false when the
+ * bookmark ID is not numeric
+ *
+ * @return void
+ */
+ public function testGetBookmarkInvalidParam()
+ {
+ $this->assertFalse($this->bs->getBookmark('foo'));
+ }
+
+
+
+ /**
+ * Check tag loading functionality of getBookmark()
+ *
+ * @return void
+ */
+ public function testGetBookmarkIncludeTags()
+ {
+ $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'));
+
+ $bm = $this->bs->getBookmark($bid, true);
+ $this->assertArrayHasKey('tags', $bm);
+ $this->assertInternalType('array', $bm['tags']);
+ $this->assertContains('foo', $bm['tags']);
+ $this->assertContains('bar', $bm['tags']);
+ }
+
+
+
+ /**
+ * Verify that getBookmark() does not include user voting
+ * data when no user is logged on.
+ *
+ * @return void
+ */
+ public function testGetBookmarkUserVotingNoUser()
+ {
+ $GLOBALS['enableVoting'] = true;
+
+ $uid = $this->addUser();
+ $bid = $this->addBookmark($uid);
+ //no user
+ $this->us->setCurrentUserId(null);
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertArrayNotHasKey('hasVoted', $bm);
+ $this->assertArrayNotHasKey('vote', $bm);
+ }
+
+
+
+ /**
+ * Verify that getBookmark() automatically includes
+ * voting data of the currently logged on user,
+ * even if he did not vote yet.
+ *
+ * @return void
+ */
+ public function testGetBookmarkUserVotingWithUserNoVote()
+ {
+ $GLOBALS['enableVoting'] = true;
+
+ $uid = $this->addUser();
+ $bid = $this->addBookmark($uid);
+ //log user in
+ $this->us->setCurrentUserId($uid);
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertArrayHasKey('hasVoted', $bm);
+ $this->assertArrayHasKey('vote', $bm);
+ $this->assertEquals(0, $bm['hasVoted']);
+ $this->assertEquals(null, $bm['vote']);
+ }
+
+
+
+ /**
+ * Verify that getBookmark() automatically includes
+ * voting data of the currently logged on user
+ * when he voted positive.
+ *
+ * @return void
+ */
+ public function testGetBookmarkUserVotingWithUserPositiveVote()
+ {
+ $GLOBALS['enableVoting'] = true;
+
+ $uid = $this->addUser();
+ $bid = $this->addBookmark($uid);
+ //log user in
+ $this->us->setCurrentUserId($uid);
+ $this->assertTrue($this->vs->vote($bid, $uid, 1));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertArrayHasKey('hasVoted', $bm);
+ $this->assertArrayHasKey('vote', $bm);
+ $this->assertEquals(1, $bm['hasVoted']);
+ $this->assertEquals(1, $bm['vote']);
+ }
+
+
+
+ /**
+ * Verify that getBookmark() automatically includes
+ * voting data of the currently logged on user
+ * when he voted positive.
+ *
+ * @return void
+ */
+ public function testGetBookmarkUserVotingWithUserNegativeVote()
+ {
+ $GLOBALS['enableVoting'] = true;
+
+ $uid = $this->addUser();
+ $bid = $this->addBookmark($uid);
+ //log user in
+ $this->us->setCurrentUserId($uid);
+ $this->assertTrue($this->vs->vote($bid, $uid, -1));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertArrayHasKey('hasVoted', $bm);
+ $this->assertArrayHasKey('vote', $bm);
+ $this->assertEquals(1, $bm['hasVoted']);
+ $this->assertEquals(-1, $bm['vote']);
+ }
+
+
+
+ /**
+ * 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->assertInternalType('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->assertInternalType('array', $bm);
+ $this->assertEquals($url, $bm['bAddress']);
+ }
+
+
+
+ public function testNormalize()
+ {
+ $this->assertEquals(
+ 'http://example.org', $this->bs->normalize('http://example.org')
+ );
+ $this->assertEquals(
+ 'ftp://example.org', $this->bs->normalize('ftp://example.org')
+ );
+ $this->assertEquals(
+ 'http://example.org', $this->bs->normalize('http://example.org/')
+ );
+ $this->assertEquals(
+ 'http://example.org', $this->bs->normalize('example.org')
+ );
+ $this->assertEquals(
+ 'mailto:foo@example.org',
+ $this->bs->normalize('mailto:foo@example.org')
+ );
+ }
+
+
+
+ /**
+ * test if updating an existing bookmark works
+ */
+ public function testUpdateBookmark()
+ {
+ $bid = $this->addBookmark();
+ $this->assertTrue(
+ $this->bs->updateBookmark(
+ $bid,
+ 'http://example.org/foo',
+ 'my new title',
+ 'new description',
+ 'new private note',
+ 1,
+ array('new')
+ )
+ );
+ $bm = $this->bs->getBookmark($bid, true);
+ $this->assertEquals('http://example.org/foo', $bm['bAddress']);
+ $this->assertEquals('my new title', $bm['bTitle']);
+ $this->assertEquals('new description', $bm['bDescription']);
+ $this->assertEquals('new private note', $bm['bPrivateNote']);
+ $this->assertEquals(1, $bm['bStatus']);
+ $this->assertInternalType('array', $bm['tags']);
+ $this->assertEquals(1, count($bm['tags']));
+ $this->assertContains('new', $bm['tags']);
+ }
+
+ /**
+ * Tests if updating a bookmark's short url name
+ * saves it in the database.
+ *
+ * @return void
+ */
+ public function testUpdateBookmarkShort()
+ {
+ $bid = $this->bs->addBookmark(
+ 'http://example.org', 'title', 'desc', 'priv',
+ 0, array(), 'myShortName'
+ );
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals('myShortName', $bm['bShort']);
+
+ $this->assertTrue(
+ $this->bs->updateBookmark(
+ $bid, 'http://example2.org', 'my title', 'desc',
+ 'priv', 0, array(), 'newShortNambb'
+ )
+ );
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals('newShortNambb', $bm['bShort']);
+ }
+
+ /**
+ * Tests if updating a bookmark's date works.
+ * This once was a bug, see bug #3073215.
+ *
+ * @return void
+ *
+ * @link https://sourceforge.net/tracker/?func=detail&atid=1017430&aid=3073215&group_id=211356
+ */
+ public function testUpdateBookmarkDate()
+ {
+ $bid = $this->bs->addBookmark(
+ 'http://example.org', 'title', 'desc', 'priv',
+ 0, array(), 'myShortName'
+ );
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals('myShortName', $bm['bShort']);
+
+ $this->assertTrue(
+ $this->bs->updateBookmark(
+ $bid, 'http://example2.org', 'my title', 'desc',
+ 'priv', 0, array(), 'newShortNambb',
+ //we need to use zulu (GMT) time zone here
+ // since the dates/times are stored as that
+ // in the database
+ '2002-03-04T05:06:07Z'
+ )
+ );
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals('newShortNambb', $bm['bShort']);
+ $this->assertEquals('2002-03-04 05:06:07', $bm['bDatetime']);
+ }
+
+
+
+ /**
+ * 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 a friend (people on the watchlist) has bookmarked
+ * and the same address with public status.
+ *
+ * @return void
+ */
+ public function testCountOthersWatchlistPublic()
+ {
+ $uid = $this->addUser();
+ $address = 'http://example.org';
+
+ //create other user and add main user to his watchlist
+ $friendPublic1 = $this->addUser();
+ $this->us->setCurrentUserId($friendPublic1);
+ $this->us->setWatchStatus($uid);
+
+ //create bookmarks for main user and other one
+ $this->addBookmark($uid, $address, 0);
+ $this->addBookmark($friendPublic1, $address, 0);//0 is public
+
+ //log main user in
+ $this->us->setCurrentUserId($uid);
+
+ $this->assertEquals(1, $this->bs->countOthers($address));
+ }
+
+
+
+ /**
+ * Test what countOther() returns when the user is logged in
+ * and a friend (people on the watchlist) has bookmarked
+ * and shared the same address for the watchlist.
+ *
+ * @return void
+ */
+ public function testCountOthersWatchlistShared()
+ {
+ $uid = $this->addUser();
+ $address = 'http://example.org';
+
+ //create other user and add main user to his watchlist
+ $friendPublic1 = $this->addUser();
+ $this->us->setCurrentUserId($friendPublic1);
+ $this->us->setWatchStatus($uid);
+
+ //create bookmarks for main user and other one
+ $this->addBookmark($uid, $address, 0);
+ $this->addBookmark($friendPublic1, $address, 1);//1 is shared
+
+ //log main user in
+ $this->us->setCurrentUserId($uid);
+
+ $this->assertEquals(1, $this->bs->countOthers($address));
+ }
+
+
+
+ /**
+ * Test what countOther() returns when the user is logged in
+ * and one friends (people on the watchlist) has bookmarked
+ * the same address but made it private.
+ *
+ * @return void
+ */
+ public function testCountOthersWatchlistPrivate()
+ {
+ $uid = $this->addUser();
+ $address = 'http://example.org';
+
+ //create other user and add main user to his watchlist
+ $friendPublic1 = $this->addUser();
+ $this->us->setCurrentUserId($friendPublic1);
+ $this->us->setWatchStatus($uid);
+
+ //create bookmarks for main user and other one
+ $this->addBookmark($uid, $address, 0);
+ $this->addBookmark($friendPublic1, $address, 2);//2 is private
+
+ //log main user in
+ $this->us->setCurrentUserId($uid);
+
+ $this->assertEquals(0, $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 testCountOthersWatchlistComplex()
+ {
+ $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)
+ )
+ );
+ }
+
+
+
+ /**
+ * Test private bookmarks
+ *
+ * @return void
+ */
+ public function testPrivateBookmarks()
+ {
+ $uid = $this->addUser();
+ /* create private bookmark */
+ $this->bs->addBookmark(
+ 'http://test', 'test', 'desc', 'note',
+ 2,//private
+ array(), null, null, false, false, $uid
+ );
+ /* create public bookmark */
+ $this->bs->addBookmark(
+ 'http://example.org', 'title', 'desc', 'priv',
+ 0,//public
+ array(), null, null, false, false, $uid
+ );
+
+ $this->assertEquals(1, $this->bs->countBookmarks($uid, 'public'));
+ $this->assertEquals(1, $this->bs->countBookmarks($uid, 'private'));
+ $this->assertEquals(0, $this->bs->countBookmarks($uid, 'shared'));
+ $this->assertEquals(2, $this->bs->countBookmarks($uid, 'all'));
+
+ $this->us->setCurrentUserId($uid);
+ $bookmarks = $this->bs->getBookmarks();
+ // first record should be private bookmark
+ $b0 = $bookmarks['bookmarks'][0];
+ $this->assertEquals('test', $b0['bTitle']);
+ // second record should be public bookmark
+ $b0 = $bookmarks['bookmarks'][1];
+ $this->assertEquals('title', $b0['bTitle']);
+
+ // test non authenticated query
+ $this->us->setCurrentUserId(null);
+ $bookmarks = $this->bs->getBookmarks();
+ // should only result in one link - public
+ $b2 = $bookmarks['bookmarks'][0];
+ $this->assertEquals('title', $b2['bTitle']);
+ // there should be no second record
+ $this->assertEquals(1,count($bookmarks['bookmarks']));
+
+ }
+
+}
+?>
diff --git a/tests/CommonDescriptionTest.php b/tests/CommonDescriptionTest.php
new file mode 100644
index 0000000..7748cb3
--- /dev/null
+++ b/tests/CommonDescriptionTest.php
@@ -0,0 +1,111 @@
+<?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
+ */
+
+/**
+ * Unit tests for the SemanticScuttle common description 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 CommonDescriptionTest extends TestBase
+{
+ protected $us;
+ protected $bs;
+ protected $b2ts;
+ protected $tts;
+ protected $tsts;
+ protected $cds;
+
+
+ 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->cds =SemanticScuttle_Service_Factory::get('CommonDescription');
+ $this->cds->deleteAll();
+ }
+
+ public function testModifyDescription()
+ {
+ $cds = $this->cds;
+
+ $uId1 = 1;
+ $uId2 = 2;
+ $title1 = "title1";
+ $title2 = "title2";
+ $desc1 = "&é\"'(-è_çà)=´~#'#{{[\\\\[||`\^\^@^@}¹²¡×¿ ?./§µ%";
+ $desc2 = "æâ€êþÿûîîôôöŀï'üð’‘ßä«≤»©»  ↓¿×÷¡¹²³";
+ $time1 = time();
+ $time2 = time()+200;
+
+ $tagDesc1 = array('cdId'=>1, 'tag'=>'taghouse', 'cdDescription'=>$desc1, 'uId'=>$uId1,'cdDatetime'=>$time1);
+ $tagDesc2 = array('cdId'=>2, 'tag'=>'taghouse', 'cdDescription'=>$desc2, 'uId'=>$uId2,'cdDatetime'=>$time2);
+
+ $cds->addTagDescription('taghouse', $desc1, $uId1, $time1);
+ $cds->addTagDescription('taghouse', $desc2, $uId2, $time2);
+
+ $desc = $cds->getLastTagDescription('taghouse');
+ $this->assertContains('taghouse', $desc);
+ $this->assertContains($desc2, $desc);
+ $this->assertContains(gmdate('Y-m-d H:i:s', $time2), $desc);
+
+ $desc = $cds->getAllTagsDescription('taghouse');
+ $this->assertContains($desc1, $desc[1]);
+ $this->assertContains(gmdate('Y-m-d H:i:s', $time1), $desc[1]);
+ $this->assertContains($desc2, $desc[0]);
+ $this->assertContains(gmdate('Y-m-d H:i:s', $time2), $desc[0]);
+
+ $desc = $cds->getDescriptionById(1);
+ $this->assertContains($desc1, $desc);
+
+ $bkDesc1 = array('cdId'=>3, 'bHash'=>'10', 'cdTitle'=>$title1, 'cdDescription'=>$desc1, 'uId'=>$uId1,'cdDatetime'=>$time1);
+ $bkDesc2 = array('cdId'=>4, 'bHash'=>'10', 'cdTitle'=>$title2, 'cdDescription'=>$desc2, 'uId'=>$uId2,'cdDatetime'=>$time2);
+
+ $cds->addBookmarkDescription(10, $title1, $desc1, $uId1, $time1);
+ $cds->addBookmarkDescription(10, $title2, $desc2, $uId2, $time2);
+
+ $desc = $cds->getLastBookmarkDescription(10);
+ $this->assertContains($title2, $desc);
+ $this->assertContains($desc2, $desc);
+ $this->assertContains(gmdate('Y-m-d H:i:s', $time2), $desc);
+
+ $desc = $cds->getAllBookmarksDescription(10);
+ $this->assertContains($title1, $desc[1]);
+ $this->assertContains($desc1, $desc[1]);
+ $this->assertContains(gmdate('Y-m-d H:i:s', $time1), $desc[1]);
+ $this->assertContains($title2, $desc[0]);
+ $this->assertContains($desc2, $desc[0]);
+ $this->assertContains(gmdate('Y-m-d H:i:s', $time2), $desc[0]);
+
+ $desc = $cds->getDescriptionById(3);
+ $this->assertContains($desc1, $desc);
+
+
+ }
+
+}
+?>
diff --git a/tests/FactoryTest.php b/tests/FactoryTest.php
new file mode 100644
index 0000000..980e92e
--- /dev/null
+++ b/tests/FactoryTest.php
@@ -0,0 +1,13 @@
+<?php
+
+class FactoryTest extends TestBase
+{
+ public function testGetDb()
+ {
+ $this->assertInstanceOf(
+ 'sql_db',
+ SemanticScuttle_Service_Factory::getDb()
+ );
+ }
+}
+?> \ No newline at end of file
diff --git a/tests/LAUNCH_TESTS b/tests/LAUNCH_TESTS
new file mode 100644
index 0000000..7ee9227
--- /dev/null
+++ b/tests/LAUNCH_TESTS
@@ -0,0 +1,28 @@
+Running SemanticScuttle unit tests
+==================================
+
+Prerequisites
+-------------
+The unit tests use PHPUnit.
+For installation instructions, see http://phpunit.de/
+
+
+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 lose all data in there.
+
+
+Running the tests
+-----------------
+To run a single test class, execute it like a normal php script:
+ php tests/BookmarkTest.php
+
+To run *all* the tests, run
+ php tests/AllTests.php
+
+If you want features like code coverage, you need to use
+the phpunit executable:
+ phpunit --coverage-html /tmp/sc-coverage/ tests/AllTests.php
diff --git a/tests/Model/BookmarkTest.php b/tests/Model/BookmarkTest.php
new file mode 100644
index 0000000..9f55143
--- /dev/null
+++ b/tests/Model/BookmarkTest.php
@@ -0,0 +1,65 @@
+<?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
+ */
+
+/**
+ * Unit tests for the SemanticScuttle Bookmark model
+ *
+ * @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 Model_BookmarkTest extends TestBase
+{
+ public function testIsValidUrlValid()
+ {
+ $this->assertTrue(
+ SemanticScuttle_Model_Bookmark::isValidUrl(
+ 'http://example.org/foo/bar?baz=foorina'
+ )
+ );
+ $this->assertTrue(
+ SemanticScuttle_Model_Bookmark::isValidUrl(
+ 'https://example.org/'
+ )
+ );
+ $this->assertTrue(
+ SemanticScuttle_Model_Bookmark::isValidUrl(
+ 'ftp://user:pass@example.org/'
+ )
+ );
+ $this->assertTrue(
+ SemanticScuttle_Model_Bookmark::isValidUrl(
+ 'mailto:cweiske@example.org'
+ )
+ );
+ }
+
+ public function testIsValidUrlInvalid()
+ {
+ $this->assertFalse(
+ SemanticScuttle_Model_Bookmark::isValidUrl(
+ 'javascript:alert("foo")'
+ )
+ );
+ $this->assertFalse(
+ SemanticScuttle_Model_Bookmark::isValidUrl(
+ 'foo://example.org/foo/bar'
+ )
+ );
+ }
+
+}
+
+?> \ No newline at end of file
diff --git a/tests/SearchHistoryTest.php b/tests/SearchHistoryTest.php
new file mode 100644
index 0000000..f05dd29
--- /dev/null
+++ b/tests/SearchHistoryTest.php
@@ -0,0 +1,373 @@
+<?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
+ */
+
+/**
+ * Unit tests for the SemanticScuttle search history 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 SearchHistoryTest extends TestBase
+{
+ protected $us;
+ protected $bs;
+ protected $b2ts;
+ protected $tts;
+ protected $shs;
+
+
+ /**
+ * Set up all services
+ *
+ * @return void
+ */
+ 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->shs = SemanticScuttle_Service_Factory::get('SearchHistory');
+ $this->shs->deleteAll();
+ }
+
+ /**
+ * Tests if adding searches to the database works
+ *
+ * @covers SemanticScuttle_Service_SearchHistory::addSearch
+ */
+ public function testAddSearch()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+
+ $this->assertTrue(
+ $this->shs->addSearch('testsearchterm', 'all', 0)
+ );
+ $this->assertEquals(1, $this->shs->countSearches());
+ }
+
+ /**
+ * Tests if adding a search without terms should fail
+ *
+ * @covers SemanticScuttle_Service_SearchHistory::addSearch
+ */
+ public function testAddSearchNoTerms()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+
+ $this->assertFalse(
+ $this->shs->addSearch('', 'all', 0)
+ );
+ $this->assertEquals(0, $this->shs->countSearches());
+ }
+
+ /**
+ * Tests if adding a search deletes the history if it is too
+ * large.
+ *
+ * @covers SemanticScuttle_Service_SearchHistory::addSearch
+ */
+ public function testAddSearchDeleteHistory()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+
+ $this->shs->sizeSearchHistory = 5;
+ $this->shs->addSearch('eins', 'all', 1);
+ $this->shs->addSearch('zwei', 'all', 1);
+ $this->shs->addSearch('drei', 'all', 1);
+ $this->shs->addSearch('view', 'all', 1);
+ $this->shs->addSearch('fünf', 'all', 1);
+ $this->assertEquals(5, $this->shs->countSearches());
+
+ $this->shs->addSearch('sechs', 'all', 1);
+ $this->assertEquals(5, $this->shs->countSearches());
+
+ $this->shs->sizeSearchHistory = 6;
+ $this->shs->addSearch('sieben', 'all', 1);
+ $this->assertEquals(6, $this->shs->countSearches());
+ $this->shs->addSearch('acht', 'all', 1);
+ $this->assertEquals(6, $this->shs->countSearches());
+ }
+
+ /**
+ * Test getAllSearches() without any parameters
+ */
+ public function testGetAllSearches()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+
+ $this->shs->addSearch('eins', 'all', 1);
+ $this->shs->addSearch('zwei', 'all', 1);
+ $this->shs->addSearch('drei', 'all', 1);
+
+ $rows = $this->shs->getAllSearches();
+ $this->assertEquals(3, count($rows));
+
+ $terms = array();
+ foreach ($rows as $row) {
+ $terms[] = $row['shTerms'];
+ }
+ sort($terms);
+ $this->assertEquals(
+ array('drei', 'eins', 'zwei'),
+ $terms
+ );
+ }
+
+ /**
+ * Test getAllSearches() return value row array keys.
+ */
+ public function testGetAllSearchesTypes()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+
+ $this->shs->addSearch('eins', 'all', 1);
+
+ $rows = $this->shs->getAllSearches();
+ $this->assertEquals(1, count($rows));
+ $row = reset($rows);
+
+ $this->assertArrayHasKey('shTerms', $row);
+ $this->assertArrayHasKey('shId', $row);
+ $this->assertArrayHasKey('shRange', $row);
+ $this->assertArrayHasKey('shNbResults', $row);
+ $this->assertArrayHasKey('shDatetime', $row);
+ $this->assertArrayHasKey('uId', $row);
+ }
+
+ /**
+ * Test getAllSearches() range parameter
+ */
+ public function testGetAllSearchesRange()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+
+ $this->shs->addSearch('eins', 'all', 1);
+ $this->shs->addSearch('zwei', 'watchlist', 1);
+ $this->shs->addSearch('drei', 'watchlist', 1);
+ $this->shs->addSearch('vier', 'user1', 1);
+ $this->shs->addSearch('fünf', 'user2', 1);
+
+ $rows = $this->shs->getAllSearches('all');
+ $this->assertEquals(1, count($rows));
+
+ $rows = $this->shs->getAllSearches('watchlist');
+ $this->assertEquals(2, count($rows));
+
+ $rows = $this->shs->getAllSearches('user0');
+ $this->assertEquals(0, count($rows));
+
+ $rows = $this->shs->getAllSearches('user1');
+ $this->assertEquals(1, count($rows));
+ $this->assertEquals('vier', $rows[0]['shTerms']);
+ }
+
+ /**
+ * Test getAllSearches() uId parameter
+ */
+ public function testGetAllSearchesUid()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+
+ $this->shs->addSearch('eins', 'all', 1, 0);
+ $this->shs->addSearch('zwei', 'all', 1, 0);
+ $this->shs->addSearch('drei', 'all', 1, 1);
+
+ $rows = $this->shs->getAllSearches(null, null);
+ $this->assertEquals(3, count($rows));
+
+ $rows = $this->shs->getAllSearches(null, 1);
+ $this->assertEquals(1, count($rows));
+ $this->assertEquals('drei', $rows[0]['shTerms']);
+ }
+
+ /**
+ * Test getAllSearches() number parameter
+ */
+ public function testGetAllSearchesNb()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+
+ $this->shs->addSearch('eins', 'all', 1, 0);
+ $this->shs->addSearch('zwei', 'all', 1, 0);
+ $this->shs->addSearch('drei', 'all', 1, 1);
+
+ $rows = $this->shs->getAllSearches(null, null, 1);
+ $this->assertEquals(1, count($rows));
+
+ $rows = $this->shs->getAllSearches(null, null, 2);
+ $this->assertEquals(2, count($rows));
+
+ $rows = $this->shs->getAllSearches(null, null, 3);
+ $this->assertEquals(3, count($rows));
+
+ $rows = $this->shs->getAllSearches(null, null, 4);
+ $this->assertEquals(3, count($rows));
+ }
+
+ /**
+ * Test getAllSearches() paging start parameter
+ */
+ public function testGetAllSearchesStart()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+
+ $this->shs->addSearch('eins', 'all', 1, 0);
+ $this->shs->addSearch('zwei', 'all', 1, 0);
+ $this->shs->addSearch('drei', 'all', 1, 1);
+
+ $rows = $this->shs->getAllSearches(null, null, 1, 0);
+ $this->assertEquals(1, count($rows));
+ $this->assertEquals('drei', $rows[0]['shTerms']);
+
+ $rows = $this->shs->getAllSearches(null, null, 1, 1);
+ $this->assertEquals(1, count($rows));
+ $this->assertEquals('zwei', $rows[0]['shTerms']);
+
+ $rows = $this->shs->getAllSearches(null, null, 3, 2);
+ $this->assertEquals(1, count($rows));
+ $this->assertEquals('eins', $rows[0]['shTerms']);
+ }
+
+ /**
+ * Test getAllSearches() distinct parameter
+ */
+ public function testGetAllSearchesDistinct()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+
+ $this->shs->addSearch('eins', 'all', 1);
+ $this->shs->addSearch('eins', 'all', 1);
+ $this->shs->addSearch('drei', 'all', 1);
+
+ $rows = $this->shs->getAllSearches(null, null, null, null, false);
+ $this->assertEquals(3, count($rows));
+
+ $rows = $this->shs->getAllSearches(null, null, null, null, true);
+ $this->assertEquals(2, count($rows));
+ }
+
+ /**
+ * Test getAllSearches() withResults parameter
+ */
+ public function testGetAllSearchesWithResults()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+
+ $this->shs->addSearch('eins', 'all', 0);
+ $this->shs->addSearch('zwei', 'all', 0);
+ $this->shs->addSearch('drei', 'all', 1);
+
+ $rows = $this->shs->getAllSearches(null, null, null, null, false, false);
+ $this->assertEquals(3, count($rows));
+
+ $rows = $this->shs->getAllSearches(null, null, null, null, false, true);
+ $this->assertEquals(1, count($rows));
+ }
+
+ /**
+ * Deleting the oldest search without any historical searches
+ *
+ * @covers SemanticScuttle_Service_SearchHistory::deleteOldestSearch
+ */
+ public function testDeleteOldestSearchNone()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+ $this->assertTrue($this->shs->deleteOldestSearch());
+ $this->assertEquals(0, $this->shs->countSearches());
+ }
+
+ /**
+ * Test deleting the oldest search
+ *
+ * @covers SemanticScuttle_Service_SearchHistory::deleteOldestSearch
+ */
+ public function testDeleteOldestSearchSome()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+ $this->shs->addSearch('testsearchterm1', 'all', 0);
+ $this->shs->addSearch('testsearchterm2', 'all', 0);
+
+ $rows = $this->shs->getAllSearches();
+ $this->assertEquals(2, count($rows));
+
+ $highestId = -1;
+ foreach ($rows as $row) {
+ if ($row['shId'] > $highestId) {
+ $highestId = $row['shId'];
+ }
+ }
+
+ $this->shs->deleteOldestSearch();
+
+ $this->assertEquals(1, $this->shs->countSearches());
+
+ $rows = $this->shs->getAllSearches();
+ $this->assertEquals(1, count($rows));
+ $this->assertEquals(
+ $highestId,
+ $rows[0]['shId']
+ );
+ }
+
+ /**
+ * Test if deleting the search history for a certain user works
+ */
+ public function testDeleteSearchHistoryForUser()
+ {
+ $this->assertEquals(0, $this->shs->countSearches());
+
+ $this->shs->addSearch('eins', 'all', 1, 0);
+ $this->shs->addSearch('zwei', 'all', 1, 22);
+ $this->shs->addSearch('drei', 'all', 1, 1);
+ $this->shs->addSearch('vier', 'all', 1, 22);
+
+ $this->shs->deleteSearchHistoryForUser(22);
+ $this->assertEquals(2, $this->shs->countSearches());
+
+ $this->shs->deleteSearchHistoryForUser(20);
+ $this->assertEquals(2, $this->shs->countSearches());
+
+ $this->shs->deleteSearchHistoryForUser(1);
+ $this->assertEquals(1, $this->shs->countSearches());
+ }
+
+
+ /**
+ * Test deleting all of the search history
+ */
+ public function testDeleteAll()
+ {
+ $this->shs->addSearch('testsearchterm1', 'all', 0);
+ $this->shs->addSearch('testsearchterm2', 'all', 0);
+ $this->shs->deleteAll();
+ $this->assertEquals(0, $this->shs->countSearches());
+ }
+}
+?>
diff --git a/tests/SemanticScuttle/ConfigTest.php b/tests/SemanticScuttle/ConfigTest.php
new file mode 100644
index 0000000..670f82a
--- /dev/null
+++ b/tests/SemanticScuttle/ConfigTest.php
@@ -0,0 +1,206 @@
+<?php
+//that's PEAR's Stream_Var package
+require_once 'Stream/Var.php';
+
+class SemanticScuttle_ConfigTest_StreamVar extends Stream_Var {
+ public function url_stat($path, $flags)
+ {
+ $url = parse_url($path);
+
+ $scope = $url['host'];
+ if (isset($url['path'])) {
+ $varpath = substr($url['path'], 1);
+ } else {
+ $varpath = '';
+ }
+
+ if (!$this->_setPointer($scope, $varpath)) {
+ return false;
+ }
+
+ return parent::url_stat($path, $flags);
+ }
+}
+
+class SemanticScuttle_ConfigTest extends PHPUnit_Framework_TestCase
+{
+ /**
+ * Configuration object to test
+ */
+ protected $cfg;
+
+
+ public function setUpWrapper()
+ {
+ if (!in_array('unittest', stream_get_wrappers())) {
+ stream_wrapper_register(
+ 'unittest', 'SemanticScuttle_ConfigTest_StreamVar'
+ );
+ }
+
+ $this->cfg = $this->getMock(
+ 'SemanticScuttle_Config',
+ array('getDataDir')
+ );
+ $this->cfg->expects($this->once())
+ ->method('getDataDir')
+ ->will($this->returnValue('/data-dir/'));
+
+ $this->cfg->filePrefix = 'unittest://GLOBALS/unittest-dir';
+ }
+
+
+
+ public function testFindLocalData()
+ {
+ $this->setUpWrapper();
+ $GLOBALS['unittest-dir']['data-dir'] = array(
+ 'config.php' => 'content',
+ 'config.default.php' => 'content'
+ );
+ $this->assertEquals(
+ array(
+ '/data-dir/config.php',
+ '/data-dir/config.default.php'
+ ),
+ $this->cfg->findFiles()
+ );
+ }
+
+ public function testFindHostPreferredOverNonHostConfig()
+ {
+ $this->setUpWrapper();
+ $_SERVER['HTTP_HOST'] = 'foo.example.org';
+
+ $GLOBALS['unittest-dir']['data-dir'] = array(
+ 'config.php' => 'content',
+ 'config.foo.example.org.php' => 'content',
+ 'config.default.php' => 'content'
+ );
+ $this->assertEquals(
+ array(
+ '/data-dir/config.foo.example.org.php',
+ '/data-dir/config.default.php'
+ ),
+ $this->cfg->findFiles()
+ );
+ }
+
+ public function testFindEtcHostPreferredOverLocalConfigPhp()
+ {
+ $this->setUpWrapper();
+ $_SERVER['HTTP_HOST'] = 'foo.example.org';
+
+ $GLOBALS['unittest-dir'] = array(
+ 'etc' => array(
+ 'semanticscuttle' => array(
+ 'config.foo.example.org.php' => 'content',
+ )
+ ),
+ 'data-dir' => array(
+ 'config.php' => 'content',
+ 'config.default.php' => 'content'
+ )
+ );
+
+ $this->assertEquals(
+ array(
+ '/etc/semanticscuttle/config.foo.example.org.php',
+ '/data-dir/config.default.php'
+ ),
+ $this->cfg->findFiles()
+ );
+ }
+
+ public function testFindEtcConfig()
+ {
+ $this->setUpWrapper();
+ $GLOBALS['unittest-dir'] = array(
+ 'etc' => array(
+ 'semanticscuttle' => array(
+ 'config.php' => 'content'
+ )
+ ),
+ 'data-dir' => array(
+ 'config.default.php' => 'content'
+ )
+ );
+ $this->assertEquals(
+ array(
+ '/etc/semanticscuttle/config.php',
+ '/data-dir/config.default.php'
+ ),
+ $this->cfg->findFiles()
+ );
+ }
+
+ public function testFindEtcDefaultConfig()
+ {
+ $this->setUpWrapper();
+ $GLOBALS['unittest-dir'] = array(
+ 'etc' => array(
+ 'semanticscuttle' => array(
+ 'config.php' => 'content',
+ 'config.default.php' => 'content'
+ )
+ ),
+ );
+ $this->assertEquals(
+ array(
+ '/etc/semanticscuttle/config.php',
+ '/etc/semanticscuttle/config.default.php'
+ ),
+ $this->cfg->findFiles()
+ );
+ }
+
+ public function testFindLocalDefaultPreferredOverEtcDefault()
+ {
+ $this->setUpWrapper();
+ $GLOBALS['unittest-dir'] = array(
+ 'etc' => array(
+ 'semanticscuttle' => array(
+ 'config.php' => 'content',
+ 'config.default.php' => 'content'
+ )
+ ),
+ 'data-dir' => array(
+ 'config.php' => 'content',
+ 'config.default.php' => 'content'
+ )
+ );
+ $this->assertEquals(
+ array(
+ '/data-dir/config.php',
+ '/data-dir/config.default.php'
+ ),
+ $this->cfg->findFiles()
+ );
+ }
+
+ public function testFindSameDirDefaultPreferred()
+ {
+ $this->setUpWrapper();
+ $GLOBALS['unittest-dir'] = array(
+ 'etc' => array(
+ 'semanticscuttle' => array(
+ 'config.php' => 'content',
+ 'config.default.php' => 'content'
+ )
+ ),
+ 'data-dir' => array(
+ 'config.default.php' => 'content'
+ )
+ );
+ $this->assertEquals(
+ array(
+ '/etc/semanticscuttle/config.php',
+ '/etc/semanticscuttle/config.default.php'
+ ),
+ $this->cfg->findFiles()
+ );
+ }
+
+}
+
+?> \ No newline at end of file
diff --git a/tests/SemanticScuttle/EnvironmentTest.php b/tests/SemanticScuttle/EnvironmentTest.php
new file mode 100644
index 0000000..3baa9ed
--- /dev/null
+++ b/tests/SemanticScuttle/EnvironmentTest.php
@@ -0,0 +1,234 @@
+<?php
+
+class SemanticScuttle_EnvironmentTest extends PHPUnit_Framework_TestCase
+{
+ public function testServerPathInfoModPhpNoPath()
+ {
+ $_SERVER = array (
+ 'HTTP_USER_AGENT' => 'Opera/9.80 (X11; Linux x86_64) Presto/2.12.388 Version/12.16',
+ 'HTTP_HOST' => 'bm.bogo',
+ 'HTTP_ACCEPT' => 'text/html, application/xml;q=0.9, applicaton/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1',
+ 'HTTP_ACCEPT_LANGUAGE' => 'en,de-DE;q=0.9,de;q=0.8',
+ 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
+ 'HTTP_CONNECTION' => 'Keep-Alive',
+ 'HTTP_DNT' => '1',
+ 'PATH' => '/usr/local/bin:/usr/bin:/bin',
+ 'SERVER_SIGNATURE' => '<address>Apache/2.2.22 (Ubuntu) Server at bm.bogo Port 80</address>',
+ 'SERVER_SOFTWARE' => 'Apache/2.2.22 (Ubuntu)',
+ 'SERVER_NAME' => 'bm.bogo',
+ 'SERVER_ADDR' => '127.0.0.1',
+ 'SERVER_PORT' => '80',
+ 'REMOTE_ADDR' => '127.0.0.1',
+ 'DOCUMENT_ROOT' => '/var/www',
+ 'SERVER_ADMIN' => '[no address given]',
+ 'SCRIPT_FILENAME' => '/home/cweiske/Dev/html/hosts/bm.bogo/test.php',
+ 'REMOTE_PORT' => '38545',
+ 'GATEWAY_INTERFACE' => 'CGI/1.1',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ 'REQUEST_METHOD' => 'GET',
+ 'QUERY_STRING' => '',
+ 'REQUEST_URI' => '/test.php',
+ 'SCRIPT_NAME' => '/test.php',
+ 'PHP_SELF' => '/test.php',
+ 'REQUEST_TIME_FLOAT' => 1377024570.296,
+ 'REQUEST_TIME' => 1377024570,
+ );
+ $this->assertEquals(
+ '', SemanticScuttle_Environment::getServerPathInfo()
+ );
+ }
+
+ public function testServerPathInfoModPhp()
+ {
+ $_SERVER = array(
+ 'HTTP_USER_AGENT' => 'Opera/9.80 (X11; Linux x86_64; U; de) Presto/2.9.168 Version/11.50',
+ 'HTTP_HOST' => 'bm-cgi.bogo',
+ 'HTTP_ACCEPT' => 'text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1',
+ 'HTTP_ACCEPT_LANGUAGE' => 'de-DE,de;q=0.9,en;q=0.8',
+ 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
+ 'HTTP_COOKIE' => 'PHPSESSID=ga446jhs0e09hkt60u9bsmp0n0',
+ 'HTTP_CACHE_CONTROL' => 'no-cache',
+ 'HTTP_CONNECTION' => 'Keep-Alive',
+ 'PATH' => '/usr/local/bin:/usr/bin:/bin',
+ 'SERVER_SIGNATURE' => '<address>Apache/2.2.17 (Ubuntu) Server at bm-cgi.bogo Port 80</address>',
+ 'SERVER_SOFTWARE' => 'Apache/2.2.17 (Ubuntu)',
+ 'SERVER_NAME' => 'bm-cgi.bogo',
+ 'SERVER_ADDR' => '127.0.0.1',
+ 'SERVER_PORT' => '80',
+ 'REMOTE_ADDR' => '127.0.0.1',
+ 'DOCUMENT_ROOT' => '/etc/apache2/htdocs',
+ 'SERVER_ADMIN' => '[no address given]',
+ 'SCRIPT_FILENAME' => '/home/cweiske/Dev/html/hosts/bm-cgi.bogo/profile.php',
+ 'REMOTE_PORT' => '45349',
+ 'GATEWAY_INTERFACE' => 'CGI/1.1',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ 'REQUEST_METHOD' => 'GET',
+ 'QUERY_STRING' => '',
+ 'REQUEST_URI' => '/profile.php/dummy',
+ 'SCRIPT_NAME' => '/profile.php',
+ 'PATH_INFO' => '/dummy',
+ 'PATH_TRANSLATED' => '/home/cweiske/Dev/html/hosts/bm-cgi.bogo/dummy',
+ 'PHP_SELF' => '/profile.php/dummy',
+ 'REQUEST_TIME' => 1311422546,
+ );
+ $this->assertEquals(
+ '/dummy', SemanticScuttle_Environment::getServerPathInfo()
+ );
+ }
+
+
+ public function testServerPathInfoFastCgi()
+ {
+ $_SERVER = array(
+ 'PHP_FCGI_MAX_REQUESTS' => '5000',
+ 'PHPRC' => '/etc/php5/cgi/5.3.6/',
+ 'PHP_FCGI_CHILDREN' => '3',
+ 'PWD' => '/var/www/cgi-bin',
+ 'FCGI_ROLE' => 'RESPONDER',
+ 'REDIRECT_HANDLER' => 'php-cgi',
+ 'REDIRECT_STATUS' => '200',
+ 'HTTP_USER_AGENT' => 'Opera/9.80 (X11; Linux x86_64; U; de) Presto/2.9.168 Version/11.50',
+ 'HTTP_HOST' => 'bm-cgi.bogo',
+ 'HTTP_ACCEPT' => 'text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1',
+ 'HTTP_ACCEPT_LANGUAGE' => 'de-DE,de;q=0.9,en;q=0.8',
+ 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
+ 'HTTP_COOKIE' => 'PHPSESSID=ga446jhs0e09hkt60u9bsmp0n0',
+ 'HTTP_CONNECTION' => 'Keep-Alive',
+ 'PATH' => '/usr/local/bin:/usr/bin:/bin',
+ 'SERVER_SIGNATURE' => '<address>Apache/2.2.17 (Ubuntu) Server at bm-cgi.bogo Port 80</address>',
+ 'SERVER_SOFTWARE' => 'Apache/2.2.17 (Ubuntu)',
+ 'SERVER_NAME' => 'bm-cgi.bogo',
+ 'SERVER_ADDR' => '127.0.0.1',
+ 'SERVER_PORT' => '80',
+ 'REMOTE_ADDR' => '127.0.0.1',
+ 'DOCUMENT_ROOT' => '/etc/apache2/htdocs',
+ 'SERVER_ADMIN' => '[no address given]',
+ 'SCRIPT_FILENAME' => '/home/cweiske/Dev/html/hosts/bm-cgi.bogo/profile.php',
+ 'REMOTE_PORT' => '45342',
+ 'REDIRECT_URL' => '/profile.php/dummy',
+ 'GATEWAY_INTERFACE' => 'CGI/1.1',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ 'REQUEST_METHOD' => 'GET',
+ 'QUERY_STRING' => '',
+ 'REQUEST_URI' => '/profile.php/dummy',
+ 'SCRIPT_NAME' => '/profile.php',
+ 'PATH_INFO' => '/dummy',
+ 'PATH_TRANSLATED' => '/etc/apache2/htdocs/dummy',
+ 'ORIG_PATH_INFO' => '/profile.php/dummy',
+ 'ORIG_SCRIPT_NAME' => '/cgi-bin-php/php-cgi-5.3.6',
+ 'ORIG_SCRIPT_FILENAME' => '/var/www/cgi-bin/php-cgi-5.3.6',
+ 'ORIG_PATH_TRANSLATED' => '/home/cweiske/Dev/html/hosts/bm-cgi.bogo/profile.php/dummy',
+ 'PHP_SELF' => '/profile.php/dummy',
+ 'REQUEST_TIME' => 1311422521,
+ );
+ $this->assertEquals(
+ '/dummy', SemanticScuttle_Environment::getServerPathInfo()
+ );
+ }
+
+ public function testServerPathInfo1and1NoPath()
+ {
+ $_SERVER = array(
+ 'REDIRECT_SCRIPT_URL' => '/dummy.php',
+ 'REDIRECT_SCRIPT_URI' => 'http://www.example.org/dummy.php',
+ 'REDIRECT_DOCUMENT_ROOT' => '/kunden/homepages/44/dexample/htdocs/example/www',
+ 'REDIRECT_HANDLER' => 'x-mapp-php6',
+ 'REDIRECT_STATUS' => '200',
+ 'DBENTRY_HOST' => 'example.org',
+ 'DBENTRY' => '/kunden/homepages/44/dexample/htdocs/example/www:d0000#CPU 6 #MEM 10240 #CGI 18 #NPROC 12 #TAID 46322755 #WERB 0 #LANG 2 #STAT 1',
+ 'SCRIPT_URL' => '/dummy.php',
+ 'SCRIPT_URI' => 'http://www.example.org/dummy.php',
+ 'HTTP_USER_AGENT' => 'Opera/9.80 (X11; Linux x86_64) Presto/2.12.388 Version/12.16',
+ 'HTTP_HOST' => 'www.example.org',
+ 'HTTP_ACCEPT' => 'text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1',
+ 'HTTP_ACCEPT_LANGUAGE' => 'en,de-DE;q=0.9,de;q=0.8',
+ 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
+ 'HTTP_COOKIE' => 'PHPSESSID=8c7853d7f639b3c6d24c224cf7d4cb1c',
+ 'HTTP_CONNECTION' => 'Keep-Alive',
+ 'HTTP_DNT' => '1',
+ 'PATH' => '/bin:/usr/bin',
+ 'SERVER_SIGNATURE' => '',
+ 'SERVER_SOFTWARE' => 'Apache',
+ 'SERVER_NAME' => 'example.org',
+ 'SERVER_ADDR' => '127.0.0.1',
+ 'SERVER_PORT' => '80',
+ 'REMOTE_ADDR' => '127.0.0.1',
+ 'DOCUMENT_ROOT' => '/kunden/homepages/44/dexample/htdocs/example/www',
+ 'SERVER_ADMIN' => 'webmaster@example.org',
+ 'SCRIPT_FILENAME' => '/kunden/homepages/44/dexample/htdocs/example/www/dummy.php',
+ 'REMOTE_PORT' => '35368',
+ 'REDIRECT_URL' => '/dummy.php',
+ 'GATEWAY_INTERFACE' => 'CGI/1.1',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ 'REQUEST_METHOD' => 'GET',
+ 'QUERY_STRING' => '',
+ 'REQUEST_URI' => '/dummy.php',
+ 'SCRIPT_NAME' => '/dummy.php',
+ 'STATUS' => '200',
+ 'ORIG_PATH_INFO' => '/dummy.php',
+ 'ORIG_PATH_TRANSLATED' => '/kunden/homepages/44/dexample/htdocs/example/www/dummy.php',
+ 'PHP_SELF' => '/dummy.php',
+ 'REQUEST_TIME_FLOAT' => 1377022156.0101,
+ 'REQUEST_TIME' => 1377022156,
+ 'argv' => array(),
+ 'argc' => 0,
+ );
+ $this->assertEquals(
+ '', SemanticScuttle_Environment::getServerPathInfo()
+ );
+ }
+
+ public function testServerPathInfo1and1WithPath()
+ {
+ $_SERVER = array(
+ 'REDIRECT_SCRIPT_URL' => '/dummy.php/dummy/foo',
+ 'REDIRECT_SCRIPT_URI' => 'http://www.example.org/dummy.php/dummy/foo',
+ 'REDIRECT_DOCUMENT_ROOT' => '/kunden/homepages/44/dexample/htdocs/example/www',
+ 'REDIRECT_HANDLER' => 'x-mapp-php6',
+ 'REDIRECT_STATUS' => '200',
+ 'DBENTRY_HOST' => 'example.org',
+ 'DBENTRY' => '/kunden/homepages/44/dexample/htdocs/example/www:d0000#CPU 6 #MEM 10240 #CGI 18 #NPROC 12 #TAID 46322755 #WERB 0 #LANG 2 #STAT 1',
+ 'SCRIPT_URL' => '/dummy.php/dummy/foo',
+ 'SCRIPT_URI' => 'http://www.example.org/dummy.php/dummy/foo',
+ 'HTTP_USER_AGENT' => 'Opera/9.80 (X11; Linux x86_64) Presto/2.12.388 Version/12.16',
+ 'HTTP_HOST' => 'www.example.org',
+ 'HTTP_ACCEPT' => 'text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/webp, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1',
+ 'HTTP_ACCEPT_LANGUAGE' => 'en,de-DE;q=0.9,de;q=0.8',
+ 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
+ 'HTTP_COOKIE' => 'PHPSESSID=8c7853d7f639b3c6d24c224cf7d4cb1c',
+ 'HTTP_CONNECTION' => 'Keep-Alive',
+ 'HTTP_DNT' => '1',
+ 'PATH' => '/bin:/usr/bin',
+ 'SERVER_SIGNATURE' => '',
+ 'SERVER_SOFTWARE' => 'Apache',
+ 'SERVER_NAME' => 'example.org',
+ 'SERVER_ADDR' => '127.0.0.1',
+ 'SERVER_PORT' => '80',
+ 'REMOTE_ADDR' => '127.0.0.1',
+ 'DOCUMENT_ROOT' => '/kunden/homepages/44/dexample/htdocs/example/www',
+ 'SERVER_ADMIN' => 'webmaster@example.org',
+ 'SCRIPT_FILENAME' => '/kunden/homepages/44/dexample/htdocs/example/www/dummy.php',
+ 'REMOTE_PORT' => '35857',
+ 'REDIRECT_URL' => '/dummy.php/dummy/foo',
+ 'GATEWAY_INTERFACE' => 'CGI/1.1',
+ 'SERVER_PROTOCOL' => 'HTTP/1.1',
+ 'REQUEST_METHOD' => 'GET',
+ 'QUERY_STRING' => '',
+ 'REQUEST_URI' => '/dummy.php/dummy/foo',
+ 'SCRIPT_NAME' => '/dummy.php',
+ 'STATUS' => '200',
+ 'ORIG_PATH_INFO' => '/dummy/foo',
+ 'ORIG_PATH_TRANSLATED' => '/kunden/homepages/44/dexample/htdocs/example/www/dummy.php',
+ 'PHP_SELF' => '/dummy.php',
+ 'REQUEST_TIME_FLOAT' => 1377024137.8098,
+ 'REQUEST_TIME' => 1377024137,
+ 'argv' => array(),
+ 'argc' => 0,
+ );
+ $this->assertEquals(
+ '/dummy/foo', SemanticScuttle_Environment::getServerPathInfo()
+ );
+ }
+}
+
+?> \ No newline at end of file
diff --git a/tests/Tag2TagTest.php b/tests/Tag2TagTest.php
new file mode 100644
index 0000000..89da483
--- /dev/null
+++ b/tests/Tag2TagTest.php
@@ -0,0 +1,537 @@
+<?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
+ */
+
+/**
+ * Unit tests for the SemanticScuttle tag2tag 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 Tag2TagTest extends TestBase
+{
+ protected $us;
+ protected $bs;
+ protected $b2ts;
+ protected $tts;
+
+
+ protected function setUp()
+ {
+ $this->us =SemanticScuttle_Service_Factory::get('User');
+ $this->us->deleteAll();
+ $this->addUser();
+ $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();
+ }
+
+ public function testManipulateTag2TagRelationsOfInclusion()
+ {
+ $tts = $this->tts;
+
+ $tts->addLinkedTags('a', 'b', '>', 1);
+ $tts->addLinkedTags('a', 'c', '>', 1);
+ $tts->addLinkedTags('a', 'd', '>', 20);
+ $tts->addLinkedTags('b', 'd', '>', 1);
+ $tts->addLinkedTags('d', 'e', '>', 1);
+ $tts->addLinkedTags('d', 'e', '>', 20);
+ $tts->addLinkedTags('f', 'g', '>', 20);
+
+ // basic test
+
+
+ $links = $tts->getLinks(1);
+ $this->assertEquals(4, count($links));
+
+ $allLinkedTags = $tts->getAllLinkedTags('e', '>', 1);
+ $this->assertEquals(array(), $allLinkedTags);
+
+ $allLinkedTags = $tts->getAllLinkedTags('d', '>', 1);
+ $this->assertEquals(array('e'), $allLinkedTags);
+
+ $allLinkedTags = $tts->getAllLinkedTags('b', '>', 1);
+ $this->assertEquals(array('d', 'e'), $allLinkedTags);
+ $this->assertEquals(2, sizeof($allLinkedTags));
+ $this->assertTrue(in_array('d', $allLinkedTags));
+ $this->assertTrue(in_array('e', $allLinkedTags));
+
+ $allLinkedTags = $tts->getAllLinkedTags('a', '>', 1);
+ $this->assertEquals(4, sizeof($allLinkedTags));
+ $this->assertTrue(in_array('b', $allLinkedTags));
+ $this->assertTrue(in_array('c', $allLinkedTags));
+ $this->assertTrue(in_array('d', $allLinkedTags));
+ $this->assertTrue(in_array('e', $allLinkedTags));
+
+
+ // warning: we add recursive link
+ $tts->addLinkedTags('b', 'a', '>', 1);
+
+ $allLinkedTags = $tts->getAllLinkedTags('a', '>', 1);
+ $this->assertEquals(4, sizeof($allLinkedTags));
+ //$this->assertTrue(in_array('a', $allLinkedTags));
+ $this->assertTrue(in_array('b', $allLinkedTags));
+ $this->assertTrue(in_array('c', $allLinkedTags));
+ $this->assertTrue(in_array('d', $allLinkedTags));
+ $this->assertTrue(in_array('e', $allLinkedTags));
+
+ $orphewTags = $tts->getOrphewTags('>', 1);
+ $this->assertEquals(0, sizeof($orphewTags));
+ $orphewTags = $tts->getOrphewTags('>', 20);
+ $this->assertEquals(2, sizeof($orphewTags));
+ $this->assertSame('a', $orphewTags[0]['tag']);
+ $this->assertSame('f', $orphewTags[1]['tag']);
+ $orphewTags = $tts->getOrphewTags('>');
+ $this->assertEquals(1, sizeof($orphewTags));
+ $this->assertSame('f', $orphewTags[0]['tag']);
+
+ $linkedTags = $tts->getLinkedTags('a', '>');
+ $this->assertSame(array('b', 'c', 'd'), $linkedTags);
+ $linkedTags = $tts->getLinkedTags('a', '>', 1);
+ $this->assertSame(array('b', 'c'), $linkedTags);
+ $tts->removeLinkedTags('a', 'b', '>', 1);
+ $linkedTags = $tts->getLinkedTags('a', '>', 1);
+ $this->assertSame(array('c'), $linkedTags);
+ $tts->removeLinkedTags('a', 'c', '>', 1);
+ $linkedTags = $tts->getLinkedTags('a', '>', 1);
+ $this->assertEquals(0, sizeof($linkedTags));
+ }
+
+ public function testManipulateTag2TagRelationsOfSynonym()
+ {
+ $tts = $this->tts;
+
+ $tts->addLinkedTags('a', 'b', '>', 1);
+ $tts->addLinkedTags('b', 'c', '>', 1);
+ $tts->addLinkedTags('b', 'd', '=', 1);
+ $tts->addLinkedTags('d', 'e', '=', 1);
+ $tts->addLinkedTags('d', 'f', '=', 1);
+ $tts->addLinkedTags('e', 'g', '>', 1);
+
+ $linkedTags = $tts->getLinkedTags('a', '>', 1);
+ $this->assertSame(array('b'), $linkedTags);
+
+ $linkedTags = $tts->getLinkedTags('a', '=', 1);
+ $this->assertSame(array(), $linkedTags);
+
+ $linkedTags = $tts->getLinkedTags('b', '=', 1);
+ $this->assertSame(array('d'), $linkedTags);
+
+ $linkedTags = $tts->getLinkedTags('d', '=', 1);
+ $this->assertEquals(3, sizeof($linkedTags));
+ $this->assertTrue(in_array('b', $linkedTags)); // '=' is bijective
+ $this->assertTrue(in_array('e', $linkedTags));
+ $this->assertTrue(in_array('f', $linkedTags));
+
+ $linkedTags = $tts->getLinkedTags('f', '=', 1);
+ $this->assertEquals(1, sizeof($linkedTags));
+ $this->assertTrue(in_array('d', $linkedTags)); // '=' is bijective
+
+ // test allLinkTags (with inference)
+ $allLinkedTags = $tts->getAllLinkedTags('a', '=', 1);
+ $this->assertEquals(0, sizeof($allLinkedTags));
+
+ $allLinkedTags = $tts->getAllLinkedTags('b', '=', 1);
+ $this->assertEquals(3, sizeof($allLinkedTags));
+ $this->assertTrue(in_array('d', $allLinkedTags));
+ $this->assertTrue(in_array('e', $allLinkedTags));
+ $this->assertTrue(in_array('f', $allLinkedTags));
+
+ $allLinkedTags = $tts->getAllLinkedTags('f', '>', 1);
+ $this->assertEquals(5, sizeof($allLinkedTags));
+ $this->assertTrue(in_array('b', $allLinkedTags));
+ $this->assertTrue(in_array('d', $allLinkedTags));
+ $this->assertTrue(in_array('e', $allLinkedTags));
+ $this->assertTrue(in_array('c', $allLinkedTags));
+ $this->assertTrue(in_array('g', $allLinkedTags));
+
+ $allLinkedTags = $tts->getAllLinkedTags('a', '>', 1);
+ $this->assertEquals(6, sizeof($allLinkedTags));
+ $this->assertTrue(in_array('b', $allLinkedTags));
+ $this->assertTrue(in_array('c', $allLinkedTags));
+ $this->assertTrue(in_array('d', $allLinkedTags));
+ $this->assertTrue(in_array('e', $allLinkedTags));
+ $this->assertTrue(in_array('f', $allLinkedTags));
+ $this->assertTrue(in_array('g', $allLinkedTags));
+
+ $tts->addLinkedTags('g', 'h', '>', 1);
+ $tts->addLinkedTags('i', 'h', '=', 1);
+ $tts->addLinkedTags('j', 'f', '>', 1);
+
+ $allLinkedTags = $tts->getAllLinkedTags('j', '>', 1);
+ $this->assertEquals(8, sizeof($allLinkedTags));
+ $this->assertTrue(in_array('b', $allLinkedTags));
+ $this->assertTrue(in_array('c', $allLinkedTags));
+ $this->assertTrue(in_array('d', $allLinkedTags));
+ $this->assertTrue(in_array('e', $allLinkedTags));
+ $this->assertTrue(in_array('f', $allLinkedTags));
+ $this->assertTrue(in_array('g', $allLinkedTags));
+ $this->assertTrue(in_array('h', $allLinkedTags));
+ $this->assertTrue(in_array('i', $allLinkedTags));
+
+ // complex case: test cycle
+ $tts->addLinkedTags('g', 'a', '>', 1);
+ $allLinkedTags = $tts->getAllLinkedTags('b', '>', 1);
+ $this->assertEquals(8, sizeof($allLinkedTags));
+ $this->assertTrue(in_array('a', $allLinkedTags));
+ $this->assertTrue(in_array('c', $allLinkedTags));
+ $this->assertTrue(in_array('d', $allLinkedTags));
+ $this->assertTrue(in_array('e', $allLinkedTags));
+ $this->assertTrue(in_array('f', $allLinkedTags));
+ $this->assertTrue(in_array('g', $allLinkedTags));
+ $this->assertTrue(in_array('h', $allLinkedTags));
+ $this->assertTrue(in_array('i', $allLinkedTags));
+
+ }
+
+ // Test function that select the best tags to display?
+ public function testViewTag2TagRelations()
+ {
+ $tts = $this->tts;
+
+ $tts->addLinkedTags('a', 'b', '>', 1);
+ $tts->addLinkedTags('c', 'd', '>', 1);
+ $tts->addLinkedTags('d', 'e', '>', 1);
+ $tts->addLinkedTags('f', 'g', '>', 1);
+ $tts->addLinkedTags('f', 'h', '>', 1);
+ $tts->addLinkedTags('f', 'i', '>', 1);
+
+ $orphewTags = $tts->getOrphewTags('>', 1);
+ $this->assertEquals(3, sizeof($orphewTags));
+ $this->assertSame('a', $orphewTags[0]['tag']);
+ $this->assertSame('c', $orphewTags[1]['tag']);
+ $this->assertSame('f', $orphewTags[2]['tag']);
+
+ // with limit
+ $orphewTags = $tts->getOrphewTags('>', 1, 2);
+ $this->assertEquals(2, sizeof($orphewTags));
+ $this->assertSame('a', $orphewTags[0]['tag']);
+ $this->assertSame('c', $orphewTags[1]['tag']);
+
+ // with sorting
+ $orphewTags = $tts->getOrphewTags('>', 1, 2, 'nb'); // nb descendants
+ $this->assertEquals(2, sizeof($orphewTags));
+ $this->assertSame('f', $orphewTags[0]['tag']);
+ $this->assertSame('c', $orphewTags[1]['tag']);
+
+ $orphewTags = $tts->getOrphewTags('>', 1, 1, 'depth');
+ $this->assertEquals(1, sizeof($orphewTags));
+ $this->assertSame('c', $orphewTags[0]['tag']);
+
+ $orphewTags = $tts->getOrphewTags('>', 1, null, 'nbupdate');
+ $this->assertEquals(3, sizeof($orphewTags));
+ $this->assertSame('f', $orphewTags[0]['tag']);
+ $this->assertSame('c', $orphewTags[1]['tag']);
+ $this->assertSame('a', $orphewTags[2]['tag']);
+
+ }
+
+ public function testAddLinkedTagsThroughBookmarking()
+ {
+ $bs = $this->bs;
+ $tags = array('a>b', 'b>c', 'a>d>e', 'a>a', 'a', 'r=s', 's=t=u');
+ $uid = $this->addUser();
+ $bs->addBookmark(
+ "http://google.com", "title", "description", 'note',
+ 0, $tags, null, null, false, false,
+ $uid
+ );
+ $bookmark = $bs->getBookmarkByAddress("http://google.com");
+
+ $b2ts = $this->b2ts;
+ $savedTags = $b2ts->getTagsForBookmark(intval($bookmark['bId']));
+ $this->assertEquals(6, sizeof($savedTags));
+ $this->assertContains('b', $savedTags);
+ $this->assertContains('c', $savedTags);
+ $this->assertContains('e', $savedTags);
+ $this->assertContains('a', $savedTags);
+ $this->assertContains('r', $savedTags);
+ $this->assertContains('s', $savedTags);
+
+ $tts = $this->tts;
+ $linkedTags = $tts->getLinkedTags('a', '>', $uid);
+ $this->assertEquals(2, count($linkedTags));
+ $this->assertInternalType('string', $linkedTags[0]);
+ $this->assertSame('b', $linkedTags[0]);
+ $this->assertInternalType('string', $linkedTags[1]);
+ $this->assertSame('d', $linkedTags[1]);
+
+ $linkedTags = $tts->getLinkedTags('b', '>', $uid);
+ $this->assertEquals(1, count($linkedTags));
+ $this->assertSame('c', $linkedTags[0]);
+ $this->assertTrue($tts->existsLinkedTags('d', 'e', '>', $uid));
+ $this->assertFalse($tts->existsLinkedTags('e', 'd', '>', $uid));
+ }
+
+ public function testSearchThroughLinkedTags()
+ {
+ $tts = $this->tts;
+ $bs = $this->bs;
+
+ $tts->addLinkedTags('aa', 'bb', '>', 1);
+
+ $tags = array('aa>bb>cc', 'dd');
+ $bs->addBookmark(
+ "web1.com", "B1", "description", 'note', 0,
+ $tags, null, null, false, false, 1
+ );
+ $tags = array('bb>gg', 'ee>ff');
+ $bs->addBookmark(
+ "web2.com", "B2", "description", 'note', 0,
+ $tags, null, null, false, false, 1
+ );
+ $tags = array('ee=ii');
+ $bs->addBookmark(
+ "web3.com", "B3", "description", 'note', 0,
+ $tags, null, null, false, false, 1
+ );
+
+ // Query format:
+ // $bs->getBookmarks($start = 0, $perpage = NULL, $user = NULL, $tags = NULL, $terms = NULL, $sortOrder = NULL, $watched = NULL, $startdate = NULL, $enddate = NULL, $hash = NULL);
+
+ // basic queries
+ $results = $bs->getBookmarks(0, NULL, 1, 'dd');
+ $this->assertSame(1, intval($results['total']));
+ $this->assertSame('B1', $results['bookmarks'][0]['bTitle']);
+
+ $results = $bs->getBookmarks(0, NULL, 1, 'cc');
+ $this->assertSame(1, intval($results['total']));
+ $this->assertSame('B1', $results['bookmarks'][0]['bTitle']);
+
+ //advanced queries
+ $results = $bs->getBookmarks(0, NULL, 1, 'aa', null, 'title_asc');
+ $this->assertSame(2, intval($results['total']));
+ $this->assertSame('B1', $results['bookmarks'][0]['bTitle']);
+ $this->assertSame('B2', $results['bookmarks'][1]['bTitle']);
+
+ $results = $bs->getBookmarks(0, NULL, 1, 'ee', null, 'title_asc');
+ $this->assertSame(2, intval($results['total']));
+ $this->assertSame('B2', $results['bookmarks'][0]['bTitle']);
+ $this->assertSame('B3', $results['bookmarks'][1]['bTitle']);
+
+ $results = $bs->getBookmarks(0, NULL, 1, 'ii', null, 'title_asc');
+ $this->assertSame(2, intval($results['total']));
+ $this->assertSame('B2', $results['bookmarks'][0]['bTitle']);
+ $this->assertSame('B3', $results['bookmarks'][1]['bTitle']);
+
+ $results = $bs->getBookmarks(0, NULL, 1, 'aa+ee');
+
+ $this->assertSame(1, intval($results['total']));
+ $this->assertSame('B2', $results['bookmarks'][0]['bTitle']);
+
+ }
+
+ public function testStatsBetweenTags()
+ {
+ $tsts = $this->tsts;
+ $tts = $this->tts;
+
+ // basic functions
+ $this->assertFalse($tsts->existStat('a', '>', 10));
+ $tsts->setNbDescendants('a', '>', 10, 2);
+ $this->assertSame(2, $tsts->getNbDescendants('a', '>', 10));
+ $tsts->setMaxDepth('a', '>', 10, 3);
+ $this->assertSame(3, $tsts->getMaxDepth('a', '>', 10));
+ $this->assertTrue($tsts->existStat('a', '>', 10));
+ $this->assertFalse($tsts->existStat('a', '>', 20));
+ $tsts->increaseNbUpdate('a', '>', 10);
+ $this->assertSame(1, $tsts->getNbUpdates('a', '>', 10));
+
+ $tsts->deleteAll();
+
+ // no structure
+ $nbC = $tsts->getNbChildren('a', '>', 1);
+ $nbD = $tsts->getNbDescendants('a', '>', 1);
+ $maxDepth = $tsts->getMaxDepth('a', '>', 1);
+ $this->assertSame(0, $nbC);
+ $this->assertSame(0, $nbD);
+ $this->assertSame(0, $maxDepth);
+
+ // simple case
+ $tts->addLinkedTags('b', 'c', '>', 1);
+ $tts->addLinkedTags('a', 'd', '>', 1);
+ $tts->addLinkedTags('a', 'b', '>', 1);
+ $tts->addLinkedTags('b', 'e', '>', 1);
+
+ $this->assertSame(3, $tsts->getNbUpdates('a', '>', '1'));
+ $this->assertSame(2, $tsts->getNbUpdates('b', '>', '1'));
+ $this->assertSame(0, $tsts->getNbUpdates('c', '>', '1'));
+ $this->assertSame(0, $tsts->getNbUpdates('d', '>', '1'));
+ $this->assertSame(0, $tsts->getNbUpdates('e', '>', '1'));
+
+
+ $nbC = $tsts->getNbChildren('a', '>', 1);
+ $nbD = $tsts->getNbDescendants('a', '>', 1);
+ $maxDepth = $tsts->getMaxDepth('a', '>', 1);
+ $this->assertSame(2, $nbC);
+ $this->assertSame(4, $nbD);
+ $this->assertSame(2, $maxDepth);
+
+ $nbC = $tsts->getNbChildren('b', '>', 1);
+ $nbD = $tsts->getNbDescendants('b', '>', 1);
+ $maxDepth = $tsts->getMaxDepth('b', '>', 1);
+ $this->assertSame(2, $nbC);
+ $this->assertSame(2, $nbD);
+ $this->assertSame(1, $maxDepth);
+
+ $nbC = $tsts->getNbChildren('c', '>', 1);
+ $nbD = $tsts->getNbDescendants('c', '>', 1);
+ $maxDepth = $tsts->getMaxDepth('c', '>', 1);
+ $this->assertSame(0, $nbC);
+ $this->assertSame(0, $nbD);
+ $this->assertSame(0, $maxDepth);
+
+ $nbC = $tsts->getNbChildren('d', '>', 1);
+ $nbD = $tsts->getNbDescendants('d', '>', 1);
+ $maxDepth = $tsts->getMaxDepth('d', '>', 1);
+ $this->assertSame(0, $nbC);
+ $this->assertSame(0, $nbD);
+ $this->assertSame(0, $maxDepth);
+
+ // deletion
+ $tts->removeLinkedTags('b', 'e', '>', 1);
+
+ $nbC = $tsts->getNbChildren('b', '>', 1);
+ $nbD = $tsts->getNbDescendants('b', '>', 1);
+ $maxDepth = $tsts->getMaxDepth('b', '>', 1);
+ $this->assertSame(1, $nbC);
+ $this->assertSame(1, $nbD);
+ $this->assertSame(1, $maxDepth);
+
+ $nbC = $tsts->getNbChildren('a', '>', 1);
+ $nbD = $tsts->getNbDescendants('a', '>', 1);
+ $maxDepth = $tsts->getMaxDepth('a', '>', 1);
+ $this->assertSame(2, $nbC);
+ $this->assertSame(3, $nbD);
+ $this->assertSame(2, $maxDepth);
+
+ //do cases for synonyms
+
+ $this->markTestSkipped('Check stats');
+
+ $tsts->deleteAll();
+ $tts->deleteAll();
+
+ $tts->addLinkedTags('a', 'b', '>', 1);
+ $tts->addLinkedTags('b', 'c', '=', 1);
+ /*$tts->addLinkedTags('a', 'c', '>', 1);
+ $tts->addLinkedTags('j', 'i', '=', 1);
+ $tts->addLinkedTags('f', 'i', '=', 1);
+ $tts->addLinkedTags('d', 'f', '>', 1);
+ $tts->addLinkedTags('d', 'e', '>', 1);
+ $tts->addLinkedTags('j', 'k', '>', 1);*/
+
+ $nbC = $tsts->getNbChildren('a', '>', 1);
+ $nbD = $tsts->getNbDescendants('a', '>', 1);
+ $nbU = $tsts->getNbUpdates('a', '>', 1);
+ $maxDepth = $tsts->getMaxDepth('a', '>', 1);
+ //$this->assertSame(2, $tts->getLinkedTags('a', '>', 1));
+ $this->assertSame(1, $nbC);
+ //$this->assertSame(2, $nbD);
+ $this->assertSame(2, $nbU);
+ $this->assertSame(1, $maxDepth);
+
+ // advanced case with fore loop
+ //$tts->addLinkedTags('d', 'c', '>', 1);
+
+ // advanced case with back loop
+ //$tts->addLinkedTags('e', 'a', '>', 1);
+ }
+
+ public function testRenameFunction()
+ {
+ $tts = $this->tts;
+ $b2ts = $this->b2ts;
+ $bs = $this->bs;
+ $tsts = $this->tsts;
+
+ $uid1 = $this->addUser();
+ $uid2 = $this->addUser();
+
+ // with classic tags (users 10 & 20)
+ $bid1 = $bs->addBookmark(
+ "http://site1.com", "title", "description", 'note', 0,
+ array('tag1', 'tag11', 'tag111'), null, null, false, false,
+ $uid1
+ );
+ $bid2 = $bs->addBookmark(
+ "http://site1.com", "title2", "description2", 'note', 0,
+ array('tag2', 'tag22', 'tag222'), null, null, false, false,
+ $uid2
+ );
+
+ $bookmarks = $bs->getBookmarks();
+ $this->assertEquals(1, $bookmarks['total']);
+
+ $b2ts->renameTag($uid1, 'tag1', 'newtag1');
+ $tags1 = $b2ts->getTagsForBookmark($bid1);
+ $this->assertContains('newtag1', $tags1);
+ $this->assertContains('tag11', $tags1);
+ $this->assertContains('tag111', $tags1);
+
+ //should not be changed
+ $tags2 = $b2ts->getTagsForBookmark($bid2);
+ $this->assertContains('tag2', $tags2);
+ $this->assertContains('tag22', $tags2);
+ $this->assertContains('tag222', $tags2);
+
+
+ // with linked tags
+
+ $tts->addLinkedTags('b', 'c', '>', 1);
+ $tts->addLinkedTags('a', 'b', '>', 1);
+ $tts->addLinkedTags('b', 'a', '>', 2); // should not be modified because of userid
+
+ $tts->renameTag(1, 'b', 'e');
+ $linkedTags = $tts->getLinkedTags('e', '>', 1);
+ $this->assertSame(array('c'), $linkedTags);
+ $linkedTags = $tts->getLinkedTags('a', '>', 1);
+ $this->assertSame(array('e'), $linkedTags);
+ $linkedTags = $tts->getLinkedTags('b', '>', 2);
+ $this->assertSame(array('a'), $linkedTags);
+
+ //with stats
+
+ }
+
+ // Cannot be test because the function use GLOBALS variables
+ // not taken into account by tests
+ /*public function testMenuTags()
+ {
+ $tts = $this->tts;
+ $bs = $this->bs;
+
+ $bs->addBookmark("http://site1.com", "title", "description", "status", array('menu>tag1'), null, false, false, 1);
+ $bs->addBookmark("http://site1.com", "title2", "description2", "status", array('menu>tag2>tag3'), null, false, false, 1);
+ $bs->addBookmark("http://site1.com", "title3", "description3", "status", array('menu>tag1', 'menu>tag4'), null, false, false, 2);
+
+ $menuTags = $tts->getMenuTags($uId);
+ $this->assertEquals(3, sizeof($menuTags));
+ $this->assertContains('tag1', $menuTags);
+ $this->assertContains('tag2', $menuTags);
+ $this->assertContains('tag4', $menuTags);
+
+ }*/
+}
+?>
diff --git a/tests/TagTest.php b/tests/TagTest.php
new file mode 100644
index 0000000..bac8f28
--- /dev/null
+++ b/tests/TagTest.php
@@ -0,0 +1,99 @@
+<?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
+ */
+
+/**
+ * Unit tests for the SemanticScuttle tag 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 TagTest extends TestBase
+{
+ protected $ts;
+
+
+ protected function setUp()
+ {
+ $this->ts =SemanticScuttle_Service_Factory::get('Tag');
+ $this->ts->deleteAll();
+ $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();
+ }
+
+ public function testTagDescriptions()
+ {
+ $ts = $this->ts;
+
+ $desc = $ts->getAllDescriptions('tag1');
+ $this->assertSame(array(), $desc);
+
+ $desc = $ts->getDescription('tag1', 1); // user 1
+ $this->assertSame(array('tDescription'=>''), $desc);
+
+ $desc1 = "test description";
+ $ts->updateDescription('tag1', 1, $desc1); // first desc
+ $desc = $ts->getDescription('tag1', 1);
+ $this->assertEquals(array('tag'=>'tag1', 'uId'=>1, 'tDescription'=>$desc1), $desc);
+
+ $desc1 = "&é\"'(-è_çà)=´~#'#{{[\\\\[||`\^\^@^@}¹²¡×¿ ?./§µ%";
+ $ts->updateDescription('tag1', 1, $desc1); // update desc
+ $desc = $ts->getDescription('tag1', 1);
+ $this->assertEquals(array('tag'=>'tag1', 'uId'=>1, 'tDescription'=>$desc1), $desc);
+
+ $desc2 = "æâ€êþÿûîîôôöŀï'üð’‘ßä«≤»©»  ↓¿×÷¡¹²³";
+ $ts->updateDescription('tag1', 2, $desc2); // user 2
+ $desc = $ts->getDescription('tag1', 2);
+ $this->assertEquals(array('tag'=>'tag1', 'uId'=>2, 'tDescription'=>$desc2), $desc);
+
+ $desc = $ts->getAllDescriptions('tag1');
+ $this->assertEquals($desc, array(array('tag'=>'tag1', 'uId'=>1, 'tDescription'=>$desc1), array('tag'=>'tag1', 'uId'=>2, 'tDescription'=>$desc2)));
+
+ }
+
+ public function testRenameFunction()
+ {
+ $ts = $this->ts;
+
+ $ts->updateDescription('tag1', 10, 'xxx');
+ $ts->renameTag(10, 'tag1', 'tag2');
+ $desc = $ts->getDescription('tag1', 10);
+ $this->assertSame(array('tDescription'=>''), $desc);
+ $desc = $ts->getDescription('tag2', 10);
+ $this->assertEquals(array('tag'=>'tag2', 'uId'=>10, 'tDescription'=>'xxx'), $desc);
+
+ }
+
+ public function testNormalizeEmptyValues()
+ {
+ $tags = $this->ts->normalize(
+ array('foo', '', 'bar', 'baz')
+ );
+ $this->assertEquals(array(0 => 'foo', 2 => 'bar', 3 => 'baz'), $tags);
+ }
+
+}
+?>
diff --git a/tests/TagsCacheTest.php b/tests/TagsCacheTest.php
new file mode 100644
index 0000000..1f69b58
--- /dev/null
+++ b/tests/TagsCacheTest.php
@@ -0,0 +1,192 @@
+<?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
+ */
+
+/**
+ * Unit tests for the SemanticScuttle tags cache 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 TagsCacheTest extends PHPUnit_Framework_TestCase
+{
+ protected $us;
+ protected $bs;
+ protected $b2ts;
+ protected $tts;
+
+
+
+ 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->tcs =SemanticScuttle_Service_Factory::get('TagCache');
+ $this->tcs->deleteAll();
+ }
+
+ public function testInclusionAllowsToAddAndDeleteChildrenTags() {
+ //message_die(GENERAL_ERROR, $GLOBALS['dbname'].'1');
+
+ $tts = $this->tts;
+ $tcs = $this->tcs;
+
+ // test adding children
+ $tcs->addChild('a', 'b', 1);
+ $tcs->addChild('a', 'c', 1);
+ $this->assertEquals(array('b','c'), $tcs->getChildren('a', 1));
+
+ // test adding a same child
+ $tcs->addChild('a', 'b', 1);
+ $this->assertEquals(array('b','c'), $tcs->getChildren('a', 1));
+
+ // test removing a child
+ $tcs->removeChild('a', 'b', 1);
+ $this->assertEquals(array('c'), $tcs->getChildren('a', 1));
+
+ // test removing a same child
+ $tcs->removeChild('a', 'b', 1);
+ $this->assertEquals(array('c'), $tcs->getChildren('a', 1));
+
+ // test existing child
+ $this->assertTrue($tcs->existsChild('a', 'c', 1));
+ $this->assertTrue(!$tcs->existsChild('a', 'c', 2)); // wrong user
+ $this->assertTrue(!$tcs->existsChild('a', 'b', 1)); // wrong child
+
+ // test removing several children
+ $tcs->addChild('e', 'f', 1);
+ $tcs->addChild('e', 'g', 1);
+ $tcs->addChild('e', 'h', 1);
+ $tcs->removeChild('e', NULL, 1);
+
+ $this->assertTrue(!$tcs->existsChild('e', 'f', 1));
+ $this->assertTrue(!$tcs->existsChild('e', 'g', 1));
+ $this->assertTrue(!$tcs->existsChild('e', 'h', 1));
+
+ }
+
+ public function testInclusionCacheIsUpdatedWhenATag2TagLinkIsCreatedOrRemoved() {
+ $tts = $this->tts;
+ $tcs = $this->tcs;
+
+ // test inclusion without possible errors
+ $tts->addLinkedTags('a', 'b', '>', 1);
+ $tts->addLinkedTags('b', 'c', '>', 1);
+ $tts->addLinkedTags('c', 'd', '>', 1);
+ $tts->addLinkedTags('e', 'f', '>', 1);
+ $tts->addLinkedTags('b', 'e', '>', 1);
+
+ $this->assertSame(array('b','c','d','e','f'), $tts->getAllLinkedTags('a', '>', 1));
+ $this->assertSame(array('c','d','e','f'), $tts->getAllLinkedTags('b', '>', 1));
+
+ // test inclusion with deletion
+ $tts->removeLinkedTags('b', 'c', '>', 1);
+ $this->assertSame(array('b','e','f'), $tts->getAllLinkedTags('a', '>', 1));
+ $this->assertSame(array('e','f'), $tts->getAllLinkedTags('b', '>', 1));
+ $this->assertSame(array('d'), $tts->getAllLinkedTags('c', '>', 1));
+ $this->assertSame(array('f'), $tts->getAllLinkedTags('e', '>', 1));
+
+ }
+
+ public function testInclusionResistsToTagCycles() {
+ $tts = $this->tts;
+ $tcs = $this->tcs;
+
+ $tts->addLinkedTags('a', 'b', '>', 1);
+ $tts->addLinkedTags('b', 'c', '>', 1);
+ $tts->addLinkedTags('c', 'a', '>', 1); // creates cycle a>c>a
+
+ $this->assertSame(array('b','c'), $tts->getAllLinkedTags('a', '>', 1));
+ $this->assertSame(array('c', 'a'), $tts->getAllLinkedTags('b', '>', 1));
+ $this->assertSame(array('a', 'b'), $tts->getAllLinkedTags('c', '>', 1));
+ }
+
+ public function testSynonymyAllowsToAddAndDeleteSynonyms() {
+ $tts = $this->tts;
+ $tcs = $this->tcs;
+
+ // simple synonymy
+ $tcs->addSynonym('a', 'b', 1);
+ $tcs->addSynonym('a', 'c', 1);
+
+ $this->assertEquals(array('b', 'c'), $tcs->getSynonyms('a', 1));
+ $this->assertEquals(array('c', 'a'), $tcs->getSynonyms('b', 1));
+ $this->assertEquals(array('b', 'a'), $tcs->getSynonyms('c', 1));
+
+ //more advanced one 1
+ $tcs->deleteByUser(1);
+ $tcs->addSynonym('a', 'b', 1);
+ $tcs->addSynonym('a', 'c', 1);
+ $tcs->addSynonym('d', 'e', 1);
+ $tcs->addSynonym('a', 'e', 1);
+ $this->assertEquals(array('b', 'c', 'e', 'd'), $tcs->getSynonyms('a', 1));
+
+ //more advanced one 2
+ $tcs->deleteByUser(1);
+ $tcs->addSynonym('a', 'b', 1);
+ $tcs->addSynonym('a', 'c', 1);
+ $tcs->addSynonym('d', 'e', 1);
+ $tcs->addSynonym('a', 'd', 1);
+ $this->assertEquals(array('b', 'c', 'd', 'e'), $tcs->getSynonyms('a', 1));
+
+ //with Linked tags
+ $tcs->deleteByUser(1);
+ $tts->addLinkedTags('a', 'b', '=', 1);
+ $tts->addLinkedTags('c', 'd', '=', 1);
+ $tts->addLinkedTags('c', 'e', '=', 1);
+ $tts->addLinkedTags('e', 'a', '=', 1);
+ $this->assertEquals(array('b', 'e', 'c', 'd'), $tts->getAllLinkedTags('a', '=', 1));
+
+ }
+
+ public function testInclusionTakesSynonymsIntoAccount() {
+ $tts = $this->tts;
+ $tcs = $this->tcs;
+
+ $tts->addLinkedTags('a', 'b', '>', 1);
+ $tts->addLinkedTags('b', 'c', '>', 1);
+ $tts->addLinkedTags('d', 'e', '>', 1);
+ $tts->addLinkedTags('c', 'd', '=', 1);
+
+ // results are put into cache
+ $this->assertEquals(array('b', 'c', 'd', 'e'), $tts->getAllLinkedTags('a', '>', 1));
+ $this->assertEquals(array('d', 'e'), $tts->getAllLinkedTags('c', '>', 1));
+
+ // same results must be taken out from cache
+ $this->assertEquals(array('b', 'c', 'd', 'e'), $tts->getAllLinkedTags('a', '>', 1));
+ $this->assertEquals(array('d', 'e'), $tts->getAllLinkedTags('c', '>', 1));
+
+ // check that result comes from cache (artificial changes in cache must appear in result)
+ $tcs->removeChild('a', 'e', 1);
+ $this->assertEquals(array('b', 'c', 'd'), $tts->getAllLinkedTags('a', '>', 1));
+
+ //cache must be deleted for user when links are modified
+ $tts->addLinkedTags('a', 'f', '=', 1);
+ $this->assertEquals(array(), $tcs->getChildren('a', 1));
+ $this->assertEquals(array(), $tcs->getSynonyms('d', 1));
+ }
+}
+?>
diff --git a/tests/TestBase.php b/tests/TestBase.php
new file mode 100644
index 0000000..4bffd1b
--- /dev/null
+++ b/tests/TestBase.php
@@ -0,0 +1,159 @@
+<?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
+ */
+
+/**
+ * Base unittest class that provides several helper methods.
+ *
+ * @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 TestBase extends PHPUnit_Framework_TestCase
+{
+ /**
+ * Create a new bookmark.
+ *
+ * @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
+ * @param string $date strtotime-compatible string
+ *
+ * @return integer ID of bookmark
+ *
+ * @see SemanticScuttle_Service_Bookmark::addBookmark()
+ */
+ protected function addBookmark(
+ $user = null, $address = null, $status = 0,
+ $tags = null, $title = null, $date = 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(
+ $address,
+ $title,
+ 'description',
+ null,
+ $status,
+ $tags,
+ null, $date, false, false,
+ $user
+ );
+ return $bid;
+ }
+
+
+
+ /**
+ * Creates a new user in the database.
+ *
+ * @param string $username Username, may be null
+ * @param string $password Password, may be null
+ * @param mixed $privateKey String private key or boolean true to generate one
+ *
+ * @return integer ID of user
+ *
+ * @uses addUserData()
+ */
+ protected function addUser(
+ $username = null, $password = null, $privateKey = null
+ ) {
+ return rreset($this->addUserData($username, $password, $privateKey));
+ }
+
+
+
+ /**
+ * Creates a new user in the database and returns id, username and password.
+ *
+ * @param string $username Username, may be null
+ * @param string $password Password, may be null
+ * @param mixed $privateKey String private key or boolean true to generate one
+ *
+ * @return array ID of user, Name of user, password of user, privateKey
+ */
+ protected function addUserData(
+ $username = null, $password = null, $privateKey = null
+ ) {
+ $us = SemanticScuttle_Service_Factory::get('User');
+ $rand = rand();
+
+ if ($username === null) {
+ $username = 'unittestuser-' . $rand;
+ }
+ if ($password === null) {
+ $password = $rand;
+ }
+ if ($privateKey === true) {
+ $privateKey = $this->us->getNewPrivateKey();
+ }
+
+ $uid = $us->addUser(
+ $username,
+ $password,
+ 'unittest-' . $rand . '@example.org',
+ $privateKey
+ );
+ return array($uid, $username, $password, $privateKey);
+ }
+
+
+
+ /**
+ * Retrieves the UID of an admin user.
+ * If that user does not exist in the database, it is created.
+ *
+ * @return integer UID of admin user
+ */
+ protected function getAdminUser()
+ {
+ if (count($GLOBALS['admin_users']) == 0) {
+ $this->fail('No admin users configured');
+ }
+ $adminUserName = reset($GLOBALS['admin_users']);
+
+ $us = SemanticScuttle_Service_Factory::get('User');
+ $uid = $us->getIdFromUser($adminUserName);
+ if ($uid === null) {
+ //that user does not exist in the database; create it
+ $uid = $us->addUser(
+ $adminUserName,
+ rand(),
+ 'unittest-admin-' . $adminUserName . '@example.org'
+ );
+ }
+
+ return $uid;
+ }
+}
+
+?>
diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php
new file mode 100644
index 0000000..ff3b695
--- /dev/null
+++ b/tests/TestBaseApi.php
@@ -0,0 +1,271 @@
+<?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 'HTTP/Request2.php';
+
+/**
+ * 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
+{
+ /**
+ * Created from the configured host and the $urlPart.
+ * Should be used as base for all generated URLs
+ *
+ * @var string
+ */
+ protected $url;
+
+ /**
+ * Part of the URL behind the configured host.
+ * Needs to be overwritten in each derived test case class.
+ *
+ * @var string
+ */
+ protected $urlPart = null;
+
+ /**
+ * @var SemanticScuttle_Service_User
+ */
+ protected $us;
+
+ /**
+ * @var SemanticScuttle_Service_Bookmark
+ */
+ protected $bs;
+
+
+
+ protected function setUp()
+ {
+ if ($GLOBALS['unittestUrl'] === null) {
+ $this->markTestSkipped('Unittest URL not set in config');
+ }
+ if ($this->urlPart === null) {
+ $this->assertTrue(false, 'Set the urlPart variable');
+ }
+ $this->url = $GLOBALS['unittestUrl'] . $this->urlPart;
+
+ //clean up before test
+ $configFile = $GLOBALS['datadir'] . '/config.testing-tmp.php';
+ if (file_exists($configFile)) {
+ unlink($configFile);
+ }
+
+ $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();
+ }
+
+
+
+ /**
+ * Creates and returns a HTTP GET request object.
+ * Uses $this->url plus $urlSuffix as request URL.
+ *
+ * @param string $urlSuffix Suffix for the URL
+ *
+ * @return HTTP_Request2 HTTP request object
+ */
+ protected function getRequest($urlSuffix = null)
+ {
+ $url = $this->getTestUrl($urlSuffix);
+ $req = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);
+
+ return $req;
+ }
+
+ /**
+ * Creates an URL from $this->url plus $urlSuffix and an appended
+ * unittestMode=1 parameter.
+ *
+ * @param string $urlSuffix Suffix for the URL
+ *
+ * @return string URL
+ *
+ * @uses $url
+ */
+ protected function getTestUrl($urlSuffix = null)
+ {
+ $url = $this->url . $urlSuffix;
+ if (strpos($urlSuffix, '?') !== false) {
+ $url .= '&unittestMode=1';
+ } else {
+ $url .= '?unittestMode=1';
+ }
+ return $url;
+ }
+
+
+ /**
+ * Completes an URL that's missing the protocol.
+ * Useful when re-using URLs extracted from HTML
+ *
+ * @param string $url Potentially partial URL
+ *
+ * @return string Full URL
+ */
+ protected function completeUrl($url)
+ {
+ if (substr($url, 0, 2) == '//') {
+ $url = 'http:' . $url;
+ }
+ return $url;
+ }
+
+
+
+ /**
+ * Creates a user and a HTTP GET request object and prepares
+ * the request object with authentication details, so that
+ * the user is logged in.
+ *
+ * Useful for HTTP API methods only, cannot be used with
+ * "normal" HTML pages since they do not support HTTP auth.
+ *
+ * @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
+ *
+ * @uses getRequest()
+ * @see getLoggedInRequest()
+ */
+ 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);
+ }
+
+
+
+ /**
+ * Creates a user and a HTTP_Request2 object, does a normal login
+ * and prepares the cookies for the HTTP GET request object so that
+ * the user is seen as logged in when requesting any HTML page.
+ *
+ * Useful for testing HTML pages or ajax URLs.
+ *
+ * @param string $urlSuffix Suffix for the URL
+ * @param mixed $auth If user authentication is needed (true/false)
+ * or array with username and password
+ * @param boolean $privateKey True if to add user with private key
+ *
+ * @return array(HTTP_Request2, integer) HTTP request object and user id
+ *
+ * @uses getRequest()
+ */
+ protected function getLoggedInRequest(
+ $urlSuffix = null, $auth = true, $privateKey = null
+ ) {
+ if (is_array($auth)) {
+ list($username, $password) = $auth;
+ } else {
+ $username = 'testuser';
+ $password = 'testpassword';
+ }
+ $uid = $this->addUser($username, $password, $privateKey);
+
+ $req = new HTTP_Request2(
+ $GLOBALS['unittestUrl'] . '/login.php?unittestMode=1',
+ HTTP_Request2::METHOD_POST
+ );
+ $cookies = $req->setCookieJar()->getCookieJar();
+ $req->addPostParameter('username', $username);
+ $req->addPostParameter('password', $password);
+ $req->addPostParameter('submitted', 'Log In');
+ $res = $req->send();
+
+ //after login, we normally get redirected
+ $this->assertEquals(302, $res->getStatus(), 'Login failure');
+
+ $req = $this->getRequest($urlSuffix);
+ $req->setCookieJar($cookies);
+
+ return array($req, $uid);
+ }
+
+
+
+ /**
+ * Verifies that the HTTP response has status code 200 and
+ * content-type application/json; charset=utf-8
+ *
+ * @param HTTP_Request2_Response $res HTTP Response object
+ *
+ * @return void
+ */
+ protected function assertResponseJson200(HTTP_Request2_Response $res)
+ {
+ $this->assertEquals(200, $res->getStatus());
+ $this->assertEquals(
+ 'application/json; charset=utf-8',
+ $res->getHeader('content-type')
+ );
+ }
+
+
+
+ /**
+ * Writes a special unittest configuration file.
+ * The unittest config file is read when a GET request with unittestMode=1
+ * is sent, and the user allowed unittestmode in config.php.
+ *
+ * @param array $arConfig Array with config names as key and their value as
+ * value
+ *
+ * @return void
+ */
+ protected function setUnittestConfig($arConfig)
+ {
+ $str = '<' . "?php\n";
+ foreach ($arConfig as $name => $value) {
+ $str .= '$' . $name . ' = '
+ . var_export($value, true) . ";\n";
+ }
+
+ if (!is_dir($GLOBALS['datadir'])) {
+ $this->fail(
+ 'datadir not set or not a directory: ' . $GLOBALS['datadir']
+ );
+ }
+
+ $this->assertInternalType(
+ 'integer',
+ file_put_contents($GLOBALS['datadir'] . '/config.testing-tmp.php', $str),
+ 'Writing config.unittest.php failed'
+ );
+ }
+}
+?>
diff --git a/tests/UserArrayTest.php b/tests/UserArrayTest.php
new file mode 100644
index 0000000..a60e37f
--- /dev/null
+++ b/tests/UserArrayTest.php
@@ -0,0 +1,66 @@
+<?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
+ */
+
+/**
+ * Unit tests for the SemanticScuttle user array model.
+ *
+ * @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 UserArrayTest extends PHPUnit_Framework_TestCase
+{
+
+ public function testGetNameLongName()
+ {
+ $this->assertEquals(
+ 'John Doe',
+ SemanticScuttle_Model_UserArray::getName(
+ array(
+ 'name' => 'John Doe',
+ 'username' => 'jdoe'
+ )
+ )
+ );
+ }
+
+ public function testGetNameUsernameIfNameIsEmpty()
+ {
+ $this->assertEquals(
+ 'jdoe',
+ SemanticScuttle_Model_UserArray::getName(
+ array(
+ 'name' => '',
+ 'username' => 'jdoe'
+ )
+ )
+ );
+ }
+
+ public function testGetNameUsernameIfNameIsNotSet()
+ {
+ $this->assertEquals(
+ 'jdoe',
+ SemanticScuttle_Model_UserArray::getName(
+ array(
+ 'username' => 'jdoe'
+ )
+ )
+ );
+ }
+
+}
+
+?> \ No newline at end of file
diff --git a/tests/UserTest.php b/tests/UserTest.php
new file mode 100644
index 0000000..85d2204
--- /dev/null
+++ b/tests/UserTest.php
@@ -0,0 +1,527 @@
+<?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
+ */
+
+/**
+ * Unit tests for the SemanticScuttle user 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 UserTest extends TestBase
+{
+ protected function setUp()
+ {
+ $this->us = SemanticScuttle_Service_Factory::get('User');
+ $this->us->deleteAll();
+ }
+
+
+
+ /**
+ * @covers SemanticScuttle_Service_User::addUser
+ */
+ public function testAddUserPrivateKey()
+ {
+ $name = substr(md5(uniqid()), 0, 6);
+ $pkey = 'my-privateKey';
+ $id = $this->us->addUser(
+ $name, uniqid(), 'foo@example.org', $pkey
+ );
+ $this->assertNotEquals(false, $id);
+ $this->assertInternalType('integer', $id);
+
+ $arUser = $this->us->getUserByPrivateKey($pkey);
+ $this->assertNotEquals(false, $arUser, 'user not found by private key');
+ $this->assertEquals($id, $arUser['uId'], 'wrong user loaded');
+ }
+
+
+ /**
+ * @covers SemanticScuttle_Service_User::updateUser
+ */
+ public function testUpdateUserFalseWhenIdNotNumeric()
+ {
+ $this->assertFalse(
+ $this->us->updateUser('foo', null, null, null, null, null)
+ );
+ }
+
+
+ /**
+ * @covers SemanticScuttle_Service_User::updateUser
+ */
+ public function testUpdateUserPrivateKeyNewKeyEnabled()
+ {
+ $pkey = 'testUpdateUserPrivateKeyNewKey12';
+ $uid = $this->addUser();
+
+ $this->assertTrue(
+ $this->us->updateUser(
+ $uid, 'password', 'name', 'test@example.org', '', '',
+ $pkey, true
+ )
+ );
+ $arUser = $this->us->getUser($uid);
+ $this->assertInternalType('array', $arUser);
+ $this->assertEquals($pkey, $arUser['privateKey']);
+ }
+
+
+ /**
+ * @covers SemanticScuttle_Service_User::updateUser
+ */
+ public function testUpdateUserPrivateKeyNewKeyDisabled()
+ {
+ $pkey = 'testUpdateUserPrivateKeyNewKeyDi';
+ $uid = $this->addUser();
+
+ $this->assertTrue(
+ $this->us->updateUser(
+ $uid, 'password', 'name', 'test@example.org', '', '',
+ $pkey, false
+ )
+ );
+ $arUser = $this->us->getUser($uid);
+ $this->assertInternalType('array', $arUser);
+ $this->assertEquals(
+ '-' . $pkey, $arUser['privateKey'],
+ 'private key did not get disabled'
+ );
+ }
+
+
+ /**
+ * Passing an empty string / NULL as key but enabling it
+ * should automatically create a new key.
+ *
+ * @covers SemanticScuttle_Service_User::updateUser
+ */
+ public function testUpdateUserPrivateKeyNoKeyEnabled()
+ {
+ $pkey = 'testUpdateUserPrivateKeyNoKeyEna';
+ $uid = $this->addUser();
+
+ $this->assertTrue(
+ $this->us->updateUser(
+ $uid, 'password', 'name', 'test@example.org', '', '',
+ null, true
+ )
+ );
+ $arUser = $this->us->getUser($uid);
+ $this->assertInternalType('array', $arUser);
+ $this->assertNotEquals(
+ '', $arUser['privateKey'], 'private key was not created'
+ );
+ }
+
+
+ /**
+ * Passing an empty string / NULL as key and disabling it
+ * should keep no key
+ *
+ * @covers SemanticScuttle_Service_User::updateUser
+ */
+ public function testUpdateUserPrivateKeyNoKeyDisabled()
+ {
+ $pkey = 'testUpdateUserPrivateKeyNoKeyDis';
+ $uid = $this->addUser();
+
+ $this->assertTrue(
+ $this->us->updateUser(
+ $uid, 'password', 'name', 'test@example.org', '', '',
+ null, false
+ )
+ );
+ $arUser = $this->us->getUser($uid);
+ $this->assertInternalType('array', $arUser);
+ $this->assertEquals(
+ '', $arUser['privateKey'], 'private key was set'
+ );
+ }
+
+
+ /**
+ * Passing an empty string / NULL as key and disabling it
+ * should keep no key
+ *
+ * @covers SemanticScuttle_Service_User::updateUser
+ */
+ public function testUpdateUserPrivateKeyExistingKeyEnabled()
+ {
+ $pkey = '12345678901234567890123456789012';
+ $uid = $this->addUser();
+
+ $this->assertTrue(
+ $this->us->updateUser(
+ $uid, 'password', 'name', 'test@example.org', '', '',
+ '-' . $pkey, true
+ )
+ );
+ $arUser = $this->us->getUser($uid);
+ $this->assertInternalType('array', $arUser);
+ $this->assertEquals(
+ $pkey, $arUser['privateKey'], 'private key was not enabled'
+ );
+ }
+
+ //FIXME: verify I cannot re-use private key of different user
+
+
+
+ /**
+ * Test that setting the current user ID is permanent.
+ * and that the current user array is the same ID
+ *
+ * @return void
+ */
+ public function testSetCurrentUserId()
+ {
+ $uid = $this->addUser();
+ $uid2 = $this->addUser();
+
+ $this->us->setCurrentUserId($uid);
+ $this->assertEquals($uid, $this->us->getCurrentUserId());
+
+ $user = $this->us->getCurrentUser();
+ $this->assertEquals($uid, $user['uId']);
+ }
+
+
+
+ /**
+ * Test that changing the current user also
+ * changes the current user array
+ *
+ * @return void
+ */
+ public function testSetCurrentUserIdChange()
+ {
+ $uid = $this->addUser();
+ $uid2 = $this->addUser();
+ $this->assertNotEquals($uid, $uid2);
+
+ $this->us->setCurrentUserId($uid);
+ $this->assertEquals($uid, $this->us->getCurrentUserId());
+
+ $user = $this->us->getCurrentUser();
+ $this->assertEquals($uid, $user['uId']);
+
+ //change it
+ $this->us->setCurrentUserId($uid2);
+ $this->assertEquals($uid2, $this->us->getCurrentUserId());
+
+ $user = $this->us->getCurrentUser();
+ $this->assertEquals($uid2, $user['uId']);
+ }
+
+
+
+ /**
+ * Test login() function with invalid creditentials
+ *
+ * @return void
+ */
+ public function testLoginInvalid()
+ {
+ $this->us->deleteAll();
+ $this->assertFalse(
+ $this->us->login('doesnot', 'exist', false)
+ );
+ }
+
+ public function testGetIdFromUserParamId()
+ {
+ $uid = $this->addUser();
+ $newId = $this->us->getIdFromUser($uid);
+ $this->assertInternalType('integer', $newId);
+ $this->assertEquals($uid, $newId);
+ }
+
+ public function testGetIdFromUserParamUsername()
+ {
+ $uid = $this->addUser('someusername');
+ $newId = $this->us->getIdFromUser('someusername');
+ $this->assertInternalType('integer', $newId);
+ $this->assertEquals($uid, $newId);
+ }
+
+
+
+ /**
+ * Check if getObjectUsers() without any user works
+ *
+ * @return void
+ */
+ public function testGetObjectUsersNone()
+ {
+ $users = $this->us->getObjectUsers();
+ $this->assertEquals(0, count($users));
+ }
+
+
+
+ /**
+ * Check if getObjectUsers() with a single user works
+ *
+ * @return void
+ */
+ public function testGetObjectUsersSingle()
+ {
+ $uid = $this->addUser();
+ $users = $this->us->getObjectUsers();
+ $this->assertEquals(1, count($users));
+ $this->assertInstanceOf('SemanticScuttle_Model_User', reset($users));
+ }
+
+
+
+ /**
+ * Check if getObjectUsers() with a several users works
+ *
+ * @return void
+ */
+ public function testGetObjectUsersMultiple()
+ {
+ $uid = $this->addUser();
+ $uid2 = $this->addUser();
+ $uid3 = $this->addUser();
+ $users = $this->us->getObjectUsers();
+ $this->assertEquals(3, count($users));
+ $this->assertInstanceOf('SemanticScuttle_Model_User', reset($users));
+ }
+
+
+
+ /**
+ * Test if the email validation function works
+ *
+ * @return void
+ */
+ public function testIsValidEmail()
+ {
+ $this->assertTrue(
+ $this->us->isValidEmail('foo@example.org')
+ );
+ $this->assertTrue(
+ $this->us->isValidEmail('foo-bar@semantic-scuttle.example.net')
+ );
+ $this->assertTrue(
+ $this->us->isValidEmail('2334ABC@302.example.org')
+ );
+
+ $this->assertFalse(
+ $this->us->isValidEmail('302.example.org')
+ );
+ $this->assertFalse(
+ $this->us->isValidEmail('foo@302')
+ );
+ $this->assertFalse(
+ $this->us->isValidEmail('foo@example!org')
+ );
+ $this->assertFalse(
+ $this->us->isValidEmail('foo@@example.org')
+ );
+ $this->assertFalse(
+ $this->us->isValidEmail('f@oo@example.org')
+ );
+ }
+
+
+ public function testGetUserByPrivateKeyEmptyKey()
+ {
+ $arUser = $this->us->getUserByPrivateKey(null);
+ $this->assertFalse($arUser);
+ }
+
+
+ public function testGetUserByPrivateKeyInvalid()
+ {
+ $arUser = $this->us->getUserByPrivateKey('foobar');
+ $this->assertFalse($arUser);
+
+ $arUser = $this->us->getUserByPrivateKey('%');
+ $this->assertFalse($arUser);
+ }
+
+
+ public function testGetUserByPrivateKeyValidKey()
+ {
+ $pkey = $this->us->getNewPrivateKey();
+ $uId = $this->addUser(null, null, $pkey);
+
+ $arUser = $this->us->getUserByPrivateKey($pkey);
+ $this->assertInternalType('array', $arUser);
+ $this->assertArrayHasKey('uId', $arUser);
+ $this->assertArrayHasKey('username', $arUser);
+
+ $this->assertEquals($uId, $arUser['uId']);
+ }
+
+
+ /**
+ * @covers SemanticScuttle_Service_User::privateKeyExists
+ */
+ public function testPrivateKeyExistsEmpty()
+ {
+ $this->assertFalse($this->us->privateKeyExists(null));
+ $this->assertFalse($this->us->privateKeyExists(''));
+ }
+
+
+ /**
+ * @covers SemanticScuttle_Service_User::privateKeyExists
+ */
+ public function testPrivateKeyExistsInvalid()
+ {
+ $this->assertFalse($this->us->privateKeyExists('-1'));
+ }
+
+
+ /**
+ * @covers SemanticScuttle_Service_User::privateKeyExists
+ */
+ public function testPrivateKeyExists()
+ {
+ $randKey = $this->us->getNewPrivateKey();
+ $this->assertFalse($this->us->privateKeyExists($randKey));
+ $uid = $this->addUser(null, null, $randKey);
+
+ $this->us->setCurrentUserId($uid);
+ $this->assertEquals($uid, $this->us->getCurrentUserId());
+
+ $this->assertTrue($this->us->privateKeyExists($randKey));
+ }
+
+
+ /**
+ * @covers SemanticScuttle_Service_User::isPrivateKeyValid
+ */
+ public function testIsPrivateKeyValid()
+ {
+ $this->assertFalse(
+ $this->us->isPrivateKeyValid(null),
+ 'NULL is an invalid private key'
+ );
+
+ $randKey = $this->us->getNewPrivateKey();
+ $this->assertTrue(
+ $this->us->isPrivateKeyValid($randKey),
+ 'generated key should be valid'
+ );
+
+ $randKey2 = '-'.$this->us->getNewPrivateKey();
+ $this->assertFalse(
+ $this->us->isPrivateKeyValid($randKey2),
+ 'disabled privateKey should return false'
+ );
+ }
+
+
+ public function testLoginPrivateKeyInvalid()
+ {
+ /* normal user with enabled privateKey */
+ $randKey = $this->us->getNewPrivateKey();
+ $uid1 = $this->addUser('testusername', 'passw0rd', $randKey);
+ /* user that has disabled privateKey */
+ $randKey2 = '-'.$this->us->getNewPrivateKey();
+ $uid2 = $this->addUser('seconduser', 'passw0RD', $randKey2);
+
+ /* test invalid private key */
+ $this->assertFalse(
+ $this->us->loginPrivateKey('02848248084082408240824802408248')
+ );
+ }
+
+
+ public function testLoginPrivateKeyValidEnabledKey()
+ {
+ /* normal user with enabled privateKey */
+ $randKey = $this->us->getNewPrivateKey();
+ $uid1 = $this->addUser('testusername', 'passw0rd', $randKey);
+ /* user that has disabled privateKey */
+ $randKey2 = '-'.$this->us->getNewPrivateKey();
+ $uid2 = $this->addUser('seconduser', 'passw0RD', $randKey2);
+
+
+ /* test valid credentials with private key enabled */
+ $this->assertTrue(
+ $this->us->loginPrivateKey($randKey)
+ );
+ }
+
+
+ public function testLoginPrivateKeyInvalidEnabledKey()
+ {
+ /* normal user with enabled privateKey */
+ $randKey = $this->us->getNewPrivateKey();
+ $uid1 = $this->addUser('testusername', 'passw0rd', $randKey);
+ /* user that has disabled privateKey */
+ $randKey2 = '-'.$this->us->getNewPrivateKey();
+ $uid2 = $this->addUser('seconduser', 'passw0RD', $randKey2);
+
+
+ /* test valid credentials with private key enabled but invalid key */
+ $this->assertFalse(
+ $this->us->loginPrivateKey('123')
+ );
+ }
+
+
+ public function testLoginPrivateKeyValidDisabledKey()
+ {
+ /* normal user with enabled privateKey */
+ $randKey = $this->us->getNewPrivateKey();
+ $uid1 = $this->addUser('testusername', 'passw0rd', $randKey);
+ /* user that has disabled privateKey */
+ $randKey2 = '-'.$this->us->getNewPrivateKey();
+ $uid2 = $this->addUser('seconduser', 'passw0RD', $randKey2);
+
+ /* confirm user exists so future fails should be due to randkey */
+ $this->assertTrue(
+ $this->us->login('seconduser', 'passw0RD', false)
+ );
+
+ /* test valid credentials with private key disabled */
+ $this->assertFalse(
+ $this->us->loginPrivateKey($randKey2)
+ );
+ }
+
+
+ public function testLoginPrivateKeyInvalidDisabled()
+ {
+ /* normal user with enabled privateKey */
+ $randKey = $this->us->getNewPrivateKey();
+ $uid1 = $this->addUser('testusername', 'passw0rd', $randKey);
+ /* user that has disabled privateKey */
+ $randKey2 = '-'.$this->us->getNewPrivateKey();
+ $uid2 = $this->addUser('seconduser', 'passw0RD', $randKey2);
+
+ /* test valid credentials with private key disabled and invalid key */
+ $this->assertFalse(
+ $this->us->loginPrivateKey('-1')
+ );
+ $this->assertFalse(
+ $this->us->loginPrivateKey(null)
+ );
+ }
+
+}
+?>
diff --git a/tests/VoteTest.php b/tests/VoteTest.php
new file mode 100644
index 0000000..0073678
--- /dev/null
+++ b/tests/VoteTest.php
@@ -0,0 +1,536 @@
+<?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
+ */
+
+/**
+ * Unit tests for the SemanticScuttle voting system.
+ *
+ * @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 VoteTest extends TestBase
+{
+ /**
+ * Vote service instance to test.
+ *
+ * @var SemanticScuttle_Service_Vote
+ */
+ protected $vs = null;
+
+ /**
+ * Bookmark service instance.
+ *
+ * @var SemanticScuttle_Service_Bookmark
+ */
+ protected $bs = null;
+
+
+
+ public function setUp()
+ {
+ $GLOBALS['enableVoting'] = true;
+ //FIXME: create true new instance
+ $this->vs = SemanticScuttle_Service_Factory::get('Vote');
+ $this->vs->deleteAll();
+
+ $this->bs = SemanticScuttle_Service_Factory::get('Bookmark');
+ }
+
+
+
+ /**
+ * Test getVoting() when no votes have been cast.
+ *
+ * @return void
+ */
+ public function testGetVotingZero()
+ {
+ $bid = $this->addBookmark();
+ $this->assertEquals(0, $this->vs->getVoting($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(0, $bm['bVoting']);
+ $this->assertEquals(0, $bm['bVotes']);
+ }
+
+
+
+ /**
+ * Test getVoting() when one positive vote has been cast.
+ *
+ * @return void
+ */
+ public function testGetVotingOne()
+ {
+ $bid = $this->addBookmark();
+ $this->vs->vote($bid, 1, 1);
+ $this->assertEquals(1, $this->vs->getVoting($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
+ }
+
+
+
+ /**
+ * Test getVoting() when one nevative vote has been cast.
+ *
+ * @return void
+ */
+ public function testGetVotingMinusOne()
+ {
+ $bid = $this->addBookmark();
+ $this->vs->vote($bid, 1, -1);
+ $this->assertEquals(-1, $this->vs->getVoting($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(-1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
+ }
+
+
+
+ /**
+ * Test getVoting() when several votes have been cast.
+ *
+ * @return void
+ */
+ public function testGetVotingSum()
+ {
+ $bid = $this->addBookmark();
+ $this->vs->vote($bid, 1, 1);
+ $this->vs->vote($bid, 2, -1);
+ $this->vs->vote($bid, 3, 1);
+ $this->vs->vote($bid, 4, 1);
+ $this->assertEquals(2, $this->vs->getVoting($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(2, $bm['bVoting']);
+ $this->assertEquals(4, $bm['bVotes']);
+ }
+
+
+
+ /**
+ * Test getVotes() when no vote has been cast.
+ *
+ * @return void
+ */
+ public function testGetVotesZero()
+ {
+ $bid = $this->addBookmark();
+ $this->assertEquals(0, $this->vs->getVotes($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(0, $bm['bVoting']);
+ $this->assertEquals(0, $bm['bVotes']);
+ }
+
+
+
+ /**
+ * Test getVotes() when one vote has been cast.
+ *
+ * @return void
+ */
+ public function testGetVotesOne()
+ {
+ $bid = $this->addBookmark();
+ $this->vs->vote($bid, 1, 1);
+ $this->assertEquals(1, $this->vs->getVotes($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
+ }
+
+
+
+ /**
+ * Test getVotes() when several votes have been cast.
+ *
+ * @return void
+ */
+ public function testGetVotesMultiple()
+ {
+ $bid = $this->addBookmark();
+ $this->vs->vote($bid, 1, 1);
+ $this->vs->vote($bid, 2, -1);
+ $this->vs->vote($bid, 3, 1);
+ $this->vs->vote($bid, 4, 1);
+ $this->assertEquals(4, $this->vs->getVotes($bid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(2, $bm['bVoting']);
+ $this->assertEquals(4, $bm['bVotes']);
+ }
+
+
+
+ /**
+ * Test hasVoted() when a no vote has been cast
+ *
+ * @return void
+ */
+ public function testHasVotedFalse()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+ $this->assertFalse($this->vs->hasVoted($bid, $uid));
+ }
+
+
+
+ /**
+ * Test hasVoted() when a vote has been cast
+ *
+ * @return void
+ */
+ public function testHasVotedTrue()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+ $this->vs->vote($bid, $uid, 1);
+ $this->assertTrue($this->vs->hasVoted($bid, $uid));
+ }
+
+
+
+ /**
+ * Test hasVoted() when a vote has been cast for other bookmarks.
+ * Also verify that the bookmark voting did not change for
+ * the other bookmarks.
+ *
+ * @return void
+ */
+ public function testHasVotedFalseOthers()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+ $bid2 = $this->addBookmark();
+ $bid3 = $this->addBookmark();
+
+ $this->vs->vote($bid, $uid, 1);
+ $this->vs->vote($bid3, $uid, 1);
+
+ $this->assertFalse($this->vs->hasVoted($bid2, $uid));
+
+ $bm2 = $this->bs->getBookmark($bid2);
+ $this->assertEquals(0, $bm2['bVoting']);
+ $this->assertEquals(0, $bm2['bVotes']);
+ }
+
+
+
+ /**
+ * Test getVote() when no vote has been cast.
+ *
+ * @return void
+ */
+ public function testGetVoteNone()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+ $this->assertNull($this->vs->getVote($bid, $uid));
+ }
+
+
+
+ /**
+ * Test getVote() when a positive vote has been cast.
+ *
+ * @return void
+ */
+ public function testGetVoteOne()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+ $this->vs->vote($bid, $uid, 1);
+ $this->assertEquals(1, $this->vs->getVote($bid, $uid));
+ }
+
+
+
+ /**
+ * Test getVote() when a negavitve vote has been cast.
+ *
+ * @return void
+ */
+ public function testGetVoteMinusOne()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+ $this->vs->vote($bid, $uid, -1);
+ $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
+ }
+
+
+
+ /**
+ * Test vote() when voting is deactivated
+ *
+ * @return void
+ */
+ public function testVoteVotingDeactivated()
+ {
+ $GLOBALS['enableVoting'] = false;
+
+ $uid = 1;
+ $bid = $this->addBookmark();
+ $this->assertFalse($this->vs->vote($bid, $uid, 1));
+ }
+
+
+
+ /**
+ * Test vote() with wrong vote parameter
+ *
+ * @return void
+ */
+ public function testVoteWrongVoteParam()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+ $this->assertFalse($this->vs->vote($bid, $uid, 2));
+ $this->assertFalse($this->vs->vote($bid, $uid, 0));
+ $this->assertFalse($this->vs->vote($bid, $uid, 1.5));
+ $this->assertFalse($this->vs->vote($bid, $uid, -1.1));
+ $this->assertFalse($this->vs->vote($bid, $uid, 'yes'));
+ $this->assertFalse($this->vs->vote($bid, $uid, 'no'));
+ }
+
+
+
+ /**
+ * Test vote() when the user already has voted
+ *
+ * @return void
+ */
+ public function testVoteHasVoted()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+ $this->assertTrue($this->vs->vote($bid, $uid, 1));
+ $this->assertTrue($this->vs->vote($bid, $uid, 1));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
+
+ $bid = $this->addBookmark();
+ $this->assertTrue($this->vs->vote($bid, $uid, -1));
+ $this->assertTrue($this->vs->vote($bid, $uid, 1));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
+ }
+
+
+
+ /**
+ * Test vote() with positive vote
+ *
+ * @return void
+ */
+ public function testVotePositive()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+ $this->assertTrue($this->vs->vote($bid, $uid, 1));
+ $this->assertEquals(1, $this->vs->getVote($bid, $uid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
+ }
+
+
+
+ /**
+ * Test vote() with negative vote
+ *
+ * @return void
+ */
+ public function testVoteNegative()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+ $this->assertTrue($this->vs->vote($bid, $uid, -1));
+ $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(-1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
+ }
+
+
+
+ /**
+ * Verify that changing the vote from positive to negative
+ * works.
+ *
+ * @return void
+ */
+ public function testVoteChangePosNeg()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+
+ $this->assertTrue($this->vs->vote($bid, $uid, 1));
+ $this->assertEquals(1, $this->vs->getVote($bid, $uid));
+ $this->assertEquals(1, $this->vs->getVotes($bid));
+
+ $b = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
+
+ //change vote
+ $this->assertTrue($this->vs->vote($bid, $uid, -1));
+ $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
+ $this->assertEquals(1, $this->vs->getVotes($bid));
+
+ $b = $this->bs->getBookmark($bid);
+ $this->assertEquals(-1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
+ }
+
+
+
+ /**
+ * Verify that changing the vote from negative to positive
+ * works.
+ *
+ * @return void
+ */
+ public function testVoteChangeNegPos()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+
+ $this->assertTrue($this->vs->vote($bid, $uid, -1));
+ $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
+ $this->assertEquals(1, $this->vs->getVotes($bid));
+
+ $b = $this->bs->getBookmark($bid);
+ $this->assertEquals(-1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
+
+ //change vote
+ $this->assertTrue($this->vs->vote($bid, $uid, 1));
+ $this->assertEquals(1, $this->vs->getVote($bid, $uid));
+ $this->assertEquals(1, $this->vs->getVotes($bid));
+
+ $b = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
+ }
+
+
+
+ /**
+ * Verify that changing the vote from postitive to positive
+ * has no strange effects
+ *
+ * @return void
+ */
+ public function testVoteChangePosPos()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+
+ $this->assertTrue($this->vs->vote($bid, $uid, 1));
+ $this->assertEquals(1, $this->vs->getVote($bid, $uid));
+ $this->assertEquals(1, $this->vs->getVotes($bid));
+
+ $b = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
+
+ //change vote
+ $this->assertTrue($this->vs->vote($bid, $uid, 1));
+ $this->assertEquals(1, $this->vs->getVote($bid, $uid));
+ $this->assertEquals(1, $this->vs->getVotes($bid));
+
+ $b = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
+ }
+
+
+
+ /**
+ * Verify that changing the vote from negative to negative
+ * has no strange effects
+ *
+ * @return void
+ */
+ public function testVoteChangeNegNeg()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+
+ $this->assertTrue($this->vs->vote($bid, $uid, -1));
+ $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
+ $this->assertEquals(1, $this->vs->getVotes($bid));
+
+ $b = $this->bs->getBookmark($bid);
+ $this->assertEquals(-1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
+
+ //change vote to same value
+ $this->assertTrue($this->vs->vote($bid, $uid, -1));
+ $this->assertEquals(-1, $this->vs->getVote($bid, $uid));
+ $this->assertEquals(1, $this->vs->getVotes($bid));
+
+ $b = $this->bs->getBookmark($bid);
+ $this->assertEquals(-1, $b['bVoting']);
+ $this->assertEquals(1, $b['bVotes']);
+ }
+
+
+
+ /**
+ * Test that rewriting votings does work
+ *
+ * @return void
+ */
+ public function testRewriteVotings()
+ {
+ $uid = 1;
+ $bid = $this->addBookmark();
+ $this->assertTrue($this->vs->vote($bid, $uid, 1));
+
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
+
+ $this->vs->deleteAll();
+ //we assume that $vs->deleteAll() does *not* reset
+ //voting in bookmarks table
+ $bm = $this->bs->getBookmark($bid);
+ $this->assertEquals(1, $bm['bVoting']);
+ $this->assertEquals(1, $bm['bVotes']);
+
+ $this->vs->rewriteVotings();
+ $bm = $this->bs->getBookmark($bid);
+ //now it should be reset to 0
+ $this->assertEquals(0, $bm['bVoting']);
+ $this->assertEquals(0, $bm['bVotes']);
+ }
+
+}//class VoteTest extends TestBase
+?> \ No newline at end of file
diff --git a/tests/ajax/GetAdminLinkedTagsTest.php b/tests/ajax/GetAdminLinkedTagsTest.php
new file mode 100644
index 0000000..43cb17a
--- /dev/null
+++ b/tests/ajax/GetAdminLinkedTagsTest.php
@@ -0,0 +1,120 @@
+<?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 'HTTP/Request2.php';
+
+/**
+ * Unit tests for the ajax linked admin tags script
+ *
+ * @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 ajax_GetAdminLinkedTagsTest extends TestBaseApi
+{
+ protected $urlPart = 'ajax/getadminlinkedtags.php';
+
+
+ /**
+ * Verify that we get the configured root tags if
+ * we do not pass any parameters
+ */
+ public function testRootTags()
+ {
+ $req = $this->getRequest();
+ $res = $req->send();
+ $this->assertResponseJson200($res);
+ $data = json_decode($res->getBody());
+ $this->assertInternalType('array', $data);
+
+ //same number of elements as the menu2Tags array
+ $this->assertEquals(
+ count($GLOBALS['menu2Tags']),
+ count($data)
+ );
+
+ //and the same contents
+ foreach ($data as $tagObj) {
+ $tagName = $tagObj->data->title;
+ $this->assertContains($tagName, $GLOBALS['menu2Tags']);
+ }
+ }
+
+ /**
+ * Verify that we get subtags of a given tag
+ */
+ public function testSubTags()
+ {
+ $t2t = SemanticScuttle_Service_Factory::get('Tag2Tag');
+ $t2t->deleteAll();
+
+ $menu2Tag = reset($GLOBALS['menu2Tags']);
+ //we have a subtag now
+ $this->addBookmark(
+ $this->getAdminUser(),
+ null,
+ 0,
+ $menu2Tag . '>adminsubtag'
+ );
+
+ $res = $this->getRequest('?tag=' . $menu2Tag)->send();
+ $this->assertResponseJson200($res);
+
+ $data = json_decode($res->getBody());
+ $this->assertInternalType('array', $data);
+
+ //only one subtag
+ $this->assertEquals(1, count($data));
+ $this->assertEquals('adminsubtag', $data[0]->data->title);
+ }
+
+ /**
+ * Verify that we only get admin tags, not tags from
+ * non-admin people
+ */
+ public function testOnlyAdminTags()
+ {
+ $t2t = SemanticScuttle_Service_Factory::get('Tag2Tag');
+ $t2t->deleteAll();
+
+ $menu2Tag = reset($GLOBALS['menu2Tags']);
+ //we have a subtag now
+ $this->addBookmark(
+ $this->getAdminUser(),
+ null,
+ 0,
+ $menu2Tag . '>adminsubtag'
+ );
+ //add another bookmark now, but for a normal user
+ $this->addBookmark(
+ null,
+ null,
+ 0,
+ $menu2Tag . '>normalsubtag'
+ );
+
+ $res = $this->getRequest('?tag=' . $menu2Tag)->send();
+ $this->assertResponseJson200($res);
+
+ $data = json_decode($res->getBody());
+ $this->assertInternalType('array', $data);
+
+ //we should have only one subtag now, the admin one
+ $this->assertEquals(1, count($data));
+ $this->assertEquals('adminsubtag', $data[0]->data->title);
+ }
+}
+?> \ No newline at end of file
diff --git a/tests/ajax/GetAdminTagsTest.php b/tests/ajax/GetAdminTagsTest.php
new file mode 100644
index 0000000..8bf8a83
--- /dev/null
+++ b/tests/ajax/GetAdminTagsTest.php
@@ -0,0 +1,120 @@
+<?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 'HTTP/Request2.php';
+
+/**
+ * Unit tests for the ajax getadmintags.php script
+ *
+ * @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 ajax_GetAdminTagsTest extends TestBaseApi
+{
+ protected $urlPart = 'ajax/getadmintags.php';
+
+
+ public function testTags()
+ {
+ list($user1, $uname1) = $this->addUserData();
+ $user2 = $this->addUser();
+ $this->addBookmark($user1, null, 0, array('admintag', 'admintag2'));
+ $this->addBookmark($user2, null, 0, array('lusertag', 'lusertag2'));
+
+ $this->setUnittestConfig(
+ array(
+ 'admin_users' => array($uname1)
+ )
+ );
+
+ $req = $this->getRequest('?unittestMode=1');
+ $res = $req->send();
+ $this->assertResponseJson200($res);
+ $data = json_decode($res->getBody());
+ $this->assertInternalType('array', $data);
+ $this->assertEquals(2, count($data));
+ $this->assertContains('admintag', $data);
+ $this->assertContains('admintag2', $data);
+ }
+
+ public function testParameterBeginsWith()
+ {
+ list($user1, $uname1) = $this->addUserData();
+ $this->addBookmark($user1, null, 0, array('foo', 'foobar', 'bar'));
+
+ $this->setUnittestConfig(
+ array(
+ 'admin_users' => array($uname1)
+ )
+ );
+
+ $req = $this->getRequest('?unittestMode=1&beginsWith=foo');
+ $res = $req->send();
+ $data = json_decode($res->getBody());
+ $this->assertResponseJson200($res);
+ $this->assertInternalType('array', $data);
+ $this->assertEquals(2, count($data));
+ $this->assertContains('foo', $data);
+ $this->assertContains('foobar', $data);
+ }
+
+
+
+ public function testParameterLimit()
+ {
+ list($user1, $uname1) = $this->addUserData();
+ list($user2, $uname2) = $this->addUserData();
+ $this->addBookmark($user1, null, 0, array('foo', 'foobar'));
+ $this->addBookmark($user2, null, 0, array('foo', 'bar'));
+
+ $this->setUnittestConfig(
+ array(
+ 'admin_users' => array($uname1, $uname2)
+ )
+ );
+
+ $req = $this->getRequest('?unittestMode=1&limit=1');
+ $res = $req->send();
+ $this->assertResponseJson200($res);
+ $data = json_decode($res->getBody());
+ $this->assertInternalType('array', $data);
+ $this->assertEquals(1, count($data));
+ $this->assertContains('foo', $data);
+
+ $req = $this->getRequest('?unittestMode=1&limit=2');
+ $res = $req->send();
+ $this->assertResponseJson200($res);
+ $data = json_decode($res->getBody());
+ $this->assertInternalType('array', $data);
+ $this->assertEquals(2, count($data));
+ $this->assertContains('foo', $data);
+
+ $req = $this->getRequest('?unittestMode=1&limit=3');
+ $res = $req->send();
+ $this->assertResponseJson200($res);
+ $data = json_decode($res->getBody());
+ $this->assertInternalType('array', $data);
+ $this->assertEquals(3, count($data));
+ $this->assertContains('foo', $data);
+ $this->assertContains('foobar', $data);
+ $this->assertContains('bar', $data);
+ }
+
+}
+
+
+?> \ No newline at end of file
diff --git a/tests/ajax/GetContactTagsTest.php b/tests/ajax/GetContactTagsTest.php
new file mode 100644
index 0000000..268ed66
--- /dev/null
+++ b/tests/ajax/GetContactTagsTest.php
@@ -0,0 +1,100 @@
+<?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 'HTTP/Request2.php';
+
+/**
+ * Unit tests for the ajax getcontacttags.php script
+ *
+ * @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 ajax_GetContactTagsTest extends TestBaseApi
+{
+ protected $urlPart = 'ajax/getcontacttags.php';
+
+
+ /**
+ * If no user is logged in, no data are returned
+ */
+ public function testNoUserLoggedIn()
+ {
+ $res = $this->getRequest()->send();
+ $this->assertResponseJson200($res);
+ $data = json_decode($res->getBody());
+ $this->assertInternalType('array', $data);
+ $this->assertEquals(0, count($data));
+ }
+
+
+ public function testUserLoggedInWatchlist()
+ {
+ list($req, $uId) = $this->getLoggedInRequest();
+ $this->addBookmark($uId, null, 0, array('public', 'public2'));
+
+ $user2 = $this->addUser();
+ $this->us->setCurrentUserId($uId);
+ $this->us->setWatchStatus($user2);
+ //uId watches user2 now
+ $this->addBookmark($user2, null, 0, array('user2tag'));
+
+ $res = $req->send();
+ $this->assertResponseJson200($res);
+ $data = json_decode($res->getBody());
+ $this->assertInternalType('array', $data);
+ $this->assertEquals(3, count($data));
+ $this->assertContains('public', $data);
+ $this->assertContains('public2', $data);
+ $this->assertContains('user2tag', $data);
+ }
+
+ public function testParameterBeginsWith()
+ {
+ list($req, $uId) = $this->getLoggedInRequest('?beginsWith=bar');
+ $this->addBookmark($uId, null, 0, array('foobar', 'barmann'));
+
+ $res = $req->send();
+ $this->assertResponseJson200($res);
+ $data = json_decode($res->getBody());
+ $this->assertInternalType('array', $data);
+ $this->assertEquals(1, count($data));
+ $this->assertContains('barmann', $data);
+ }
+
+ public function testParameterLimit()
+ {
+ list($req, $uId) = $this->getLoggedInRequest('?limit=2');
+ $this->addBookmark($uId, null, 0, array('foo', 'bar', 'baz', 'omg'));
+
+ $res = $req->send();
+ $this->assertResponseJson200($res);
+ $data = json_decode($res->getBody());
+ $this->assertInternalType('array', $data);
+ $this->assertEquals(2, count($data));
+
+ $req2 = $this->getRequest('?limit=3');
+ $req2->setCookieJar($req->getCookieJar());
+ $res = $req2->send();
+ $this->assertResponseJson200($res);
+ $data = json_decode($res->getBody());
+ $this->assertInternalType('array', $data);
+ $this->assertEquals(3, count($data));
+ }
+}
+
+
+?> \ No newline at end of file
diff --git a/tests/data/BookmarkTest_deliciousbookmarks.xml b/tests/data/BookmarkTest_deliciousbookmarks.xml
new file mode 100755
index 0000000..87c67dc
--- /dev/null
+++ b/tests/data/BookmarkTest_deliciousbookmarks.xml
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<posts user="dpuser" update="2011-03-24T21:09:22Z" tag="" total="3">
+ <post href="http://www.example.org/testdefaultprivacyposts_import1/" hash="4f8533885bb5740b98b6415140a0c8c6" description="Test bookmark 1 for default privacy." tag="tag1, tag2" time="2011-03-24T21:09:11Z" extended="" meta="5c80704730a1bf1b9eb615d4ba7a59bd" />
+ <post href="http://www.example.org/testdefaultprivacyposts_import2/" hash="21eee08d7945ac23c4cc2b6d08d02212" description="Test bookmark 2 for default privacy." tag="tag2, tag3" time="2011-03-24T21:08:33Z" extended="" meta="7514ab84f61ba1dd0413572f7c12348d" />
+ <post href="http://www.example.org/testdefaultprivacyposts_import3/" hash="cd64042205a083f1cf1e009344daef84" description="Test bookmark 3 for default privacy." tag="tag1, tag2, tag3" time="2011-03-24T21:07:44Z" extended="" meta="9cb24fddbc011e9634595d2f5e1a6537" />
+</posts>
+<!-- fe09.api.del.ac4.yahoo.net uncompressed/chunked Thur Mar 24 21:09:33 UTC 2011 -->
diff --git a/tests/data/BookmarkTest_deliciousbookmarks_longdesc_3469257.xml b/tests/data/BookmarkTest_deliciousbookmarks_longdesc_3469257.xml
new file mode 100644
index 0000000..9c09f29
--- /dev/null
+++ b/tests/data/BookmarkTest_deliciousbookmarks_longdesc_3469257.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<posts user="dpuser" update="2012-01-20T21:09:22Z" tag="" total="1">
+ <post href="http://www.example.org/" hash="4f8533885bb5740b98b6415140a0c8c6" description="title" tag="unittest" time="2012-01-20T21:09:11Z" extended="0.........10........20........30........40........50........60........70........80........90........100.......110.......120.......130.......140.......150.......160.......170.......180.......190.......200.......210.......220.......230.......240.......250.......260.......270.......280.......290......." meta="5c80704730a1bf1b9eb615d4ba7a59bd" />
+</posts> \ No newline at end of file
diff --git a/tests/data/BookmarkTest_deliciousbookmarks_private.xml b/tests/data/BookmarkTest_deliciousbookmarks_private.xml
new file mode 100644
index 0000000..0ad8142
--- /dev/null
+++ b/tests/data/BookmarkTest_deliciousbookmarks_private.xml
@@ -0,0 +1 @@
+<?xml version="1.0" encoding="UTF-8"?><posts tag="" total="1" user="cweiske"><post description="Christians Tagebuch" extended="" hash="d82ca757a4583a24260a1126b5dafb0d" href="http://cweiske.de/tagebuch/" private="yes" shared="no" tag="private" time="2012-01-20T20:18:56Z"/></posts> \ No newline at end of file
diff --git a/tests/data/BookmarkTest_netscapebookmarks.html b/tests/data/BookmarkTest_netscapebookmarks.html
new file mode 100755
index 0000000..305662c
--- /dev/null
+++ b/tests/data/BookmarkTest_netscapebookmarks.html
@@ -0,0 +1,27 @@
+<!DOCTYPE NETSCAPE-Bookmark-file-1>
+
+<!-- This is an automatically generated file.
+
+It will be read and overwritten.
+
+Do Not Edit! -->
+
+<TITLE>Bookmarks for testuser</TITLE>
+
+<H1>Bookmarks for testuser</H1>
+
+
+<DL><p>
+
+ <DT><A HREF="http://www.example.org/testdefaultprivacyposts_importNetscape1" ADD_DATE="1301178264" LAST_VISIT="1301178075" LAST_MODIFIED="1301178075">Test bookmark 1 for default privacy.</A>
+ <DT><A HREF="http://www.example.org/testdefaultprivacyposts_importNetscape2" ADD_DATE="1301178264" LAST_VISIT="1301178075" LAST_MODIFIED="1301178075">Test bookmark 2 for default privacy.</A>
+ <DT><A HREF="http://www.example.org/testdefaultprivacyposts_importNetscape3" ADD_DATE="1301178264" LAST_VISIT="1301178075" LAST_MODIFIED="1301178075">Test bookmark 3 for default privacy.</A>
+ <DT><A HREF="http://www.thisbookmarkwillnotbeadded.com" ADD_DATE="1301178264" LAST_VISIT="1301178075" LAST_MODIFIED="1301178075">This bookmark will be ignored by importNetscape.php.</A>
+
+</DL><p>
+
+<DL><p>
+
+
+
+</DL><p>
diff --git a/tests/dox.html b/tests/dox.html
new file mode 100644
index 0000000..98f6d00
--- /dev/null
+++ b/tests/dox.html
@@ -0,0 +1 @@
+<html><body><h2>TagsCache</h2><ul> \ No newline at end of file
diff --git a/tests/phpunit.xml b/tests/phpunit.xml
new file mode 100644
index 0000000..3956445
--- /dev/null
+++ b/tests/phpunit.xml
@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<phpunit strict="true" colors="true"
+ bootstrap="prepare.php"
+>
+ <filter>
+ <whitelist addUncoveredFilesFromWhitelist="false">
+ <directory suffix=".php">../src/SemanticScuttle/</directory>
+ <directory suffix=".php">../data/templates/</directory>
+ <directory suffix=".php">../www/</directory>
+ <exclude>
+ <directory suffix=".php">../src/SemanticScuttle/db/</directory>
+ </exclude>
+ </whitelist>
+ </filter>
+</phpunit> \ No newline at end of file
diff --git a/tests/prepare.php b/tests/prepare.php
new file mode 100644
index 0000000..ea4d77d
--- /dev/null
+++ b/tests/prepare.php
@@ -0,0 +1,41 @@
+<?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
+ */
+
+/**
+ * Prepare the application for unit testing
+ */
+//that's needed in constants.php
+$_SERVER['HTTP_HOST'] = 'http://localhost/';
+
+define('UNIT_TEST_MODE', true);
+
+if ('@data_dir@' == '@' . 'data_dir@') {
+ //non pear-installation (i.e. git checkout)
+ require_once dirname(__FILE__) . '/../src/SemanticScuttle/header.php';
+} else {
+ //pear installation; files are in include path
+ require_once 'SemanticScuttle/header.php';
+}
+require_once dirname(__FILE__) . '/TestBase.php';
+require_once dirname(__FILE__) . '/TestBaseApi.php';
+
+if ($GLOBALS['debugMode'] == true
+ && $GLOBALS['dbtype'] == 'mysql4'
+) {
+ echo "\n"
+ . '!! The combination of debugMode and dbtype==mysql4'
+ . ' will wreck some tests' . "\n\n";
+}
+?> \ No newline at end of file
diff --git a/tests/www/bookmarksTest.php b/tests/www/bookmarksTest.php
new file mode 100755
index 0000000..ac549d8
--- /dev/null
+++ b/tests/www/bookmarksTest.php
@@ -0,0 +1,156 @@
+<?php
+require_once dirname(__FILE__) . '/../prepare.php';
+require_once 'HTTP/Request2.php';
+
+class www_bookmarksTest extends TestBaseApi
+{
+ protected $urlPart = 'bookmarks.php';
+
+ /**
+ * Test that the default privacy setting is selected in the Privacy
+ * drop-down list when adding a new bookmark, sending the form and
+ * missing the title and the privacy setting.
+ */
+ public function testDefaultPrivacyBookmarksAddMissingTitleMissingPrivacy()
+ {
+ $this->setUnittestConfig(
+ array('defaults' => array('privacy' => 2))
+ );
+
+ list($req, $uId) = $this->getLoggedInRequest();
+ $user = $this->us->getUser($uId);
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->setUrl($this->getTestUrl('/' . $user['username'] . '?action=get'));
+ $req->addPostParameter('submitted', '1');
+ $response = $req->send();
+ $response_body = $response->getBody();
+
+ $x = simplexml_load_string($response_body);
+ $ns = $x->getDocNamespaces();
+ $x->registerXPathNamespace('ns', reset($ns));
+
+ $elements = $x->xpath(
+ '//ns:select[@name="status"]/ns:option[@selected="selected"]'
+ );
+ $this->assertEquals(1, count($elements), 'No selected status option found');
+ $this->assertEquals(2, (string)$elements[0]['value']);
+ }//end testDefaultPrivacyBookmarksAddMissingTitleMissingPrivacy
+
+
+
+ /**
+ * Test that the default privacy setting is selected in the Privacy
+ * drop-down list when a new bookmark is being created.
+ */
+ public function testDefaultPrivacyBookmarksAdd()
+ {
+ $this->setUnittestConfig(
+ array('defaults' => array('privacy' => 1))
+ );
+ list($req, $uId) = $this->getLoggedInRequest();
+
+ $user = $this->us->getUser($uId);
+ $req->setUrl($this->getTestUrl('/' . $user['username'] . '?action=add'));
+ $response = $req->send();
+ $response_body = $response->getBody();
+ $this->assertNotEquals('', $response_body, 'Response is empty');
+
+ $x = simplexml_load_string($response_body);
+ $ns = $x->getDocNamespaces();
+ $x->registerXPathNamespace('ns', reset($ns));
+
+ $elements = $x->xpath(
+ '//ns:select[@name="status"]/ns:option[@selected="selected"]'
+ );
+ $this->assertEquals(1, count($elements), 'No selected status option found');
+ $this->assertEquals(1, (string)$elements[0]['value']);
+ }//end testDefaultPrivacyBookmarksAdd
+
+
+
+ /**
+ * Test that the private RSS link exists when a user
+ * has a private key and is enabled
+ */
+ public function testVerifyPrivateRSSLinkExists()
+ {
+ list($req, $uId) = $this->getLoggedInRequest('?unittestMode=1', true, true);
+
+ $user = $this->us->getUser($uId);
+ $req->setUrl($this->getTestUrl('/' . $user['username']));
+ $response = $req->send();
+ $response_body = $response->getBody();
+ $this->assertNotEquals('', $response_body, 'Response is empty');
+
+ $x = simplexml_load_string($response_body);
+ $ns = $x->getDocNamespaces();
+ $x->registerXPathNamespace('ns', reset($ns));
+
+ $elements = $x->xpath(
+ '//ns:link[@rel="alternate" and @type="application/rss+xml"]'
+ );
+ $this->assertEquals(
+ 2, count($elements), 'Number of Links in Head not correct'
+ );
+ $this->assertContains('privateKey=', (string)$elements[1]['href']);
+ }//end testVerifyPrivateRSSLinkExists
+
+
+
+ /**
+ * Test that the private RSS link doesn't exists when a user
+ * does not have a private key or is not enabled
+ */
+ public function testVerifyPrivateRSSLinkDoesNotExist()
+ {
+ list($req, $uId) = $this->getLoggedInRequest('?unittestMode=1', true);
+
+ $user = $this->us->getUser($uId);
+ $req->setUrl($this->getTestUrl('/' . $user['username']));
+ $response = $req->send();
+ $response_body = $response->getBody();
+ $this->assertNotEquals('', $response_body, 'Response is empty');
+
+ $x = simplexml_load_string($response_body);
+ $ns = $x->getDocNamespaces();
+ $x->registerXPathNamespace('ns', reset($ns));
+
+ $elements = $x->xpath(
+ '//ns:link[@rel="alternate" and @type="application/rss+xml"]'
+ );
+ $this->assertEquals(
+ 1, count($elements), 'Number of Links in Head not correct'
+ );
+ $this->assertNotContains('privateKey=', (string)$elements[0]['href']);
+ }//end testVerifyPrivateRSSLinkDoesNotExist
+
+
+
+ /**
+ * We once had the bug that URLs with special characters were escaped too
+ * often. & -> &amp;
+ */
+ public function testAddressEncoding()
+ {
+ $this->addBookmark(null, 'http://example.org?foo&bar=baz');
+
+ //get rid of bookmarks.php
+ $this->url = $GLOBALS['unittestUrl'];
+
+ $html = $this->getRequest()->send()->getBody();
+ $x = simplexml_load_string($html);
+ $ns = $x->getDocNamespaces();
+ $x->registerXPathNamespace('ns', reset($ns));
+
+ $elements = $x->xpath('//ns:a[@class="taggedlink"]');
+ $this->assertEquals(
+ 1, count($elements), 'Number of links is not 1'
+ );
+ $this->assertEquals(
+ 'http://example.org?foo&bar=baz',
+ (string)$elements[0]['href']
+ );
+ }
+
+}//end class www_bookmarksTest
+?>
diff --git a/tests/www/editTest.php b/tests/www/editTest.php
new file mode 100755
index 0000000..1e0fbd5
--- /dev/null
+++ b/tests/www/editTest.php
@@ -0,0 +1,48 @@
+<?php
+require_once dirname(__FILE__) . '/../prepare.php';
+require_once 'HTTP/Request2.php';
+
+class www_editTest extends TestBaseApi
+{
+ protected $urlPart = 'api/posts/add';
+
+ /**
+ * Test that the default privacy setting is used when an existing
+ * bookmark is updated with edit.php.
+ */
+ public function testDefaultPrivacyEdit()
+ {
+ $this->setUnittestConfig(
+ array('defaults' => array('privacy' => 2))
+ );
+
+ list($req, $uId) = $this->getLoggedInRequest('?unittestMode=1');
+ $cookies = $req->getCookieJar();
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->addPostParameter('url', 'http://www.example.org/testdefaultprivacyposts_edit');
+ $req->addPostParameter('description', 'Test bookmark 2 for default privacy.');
+ $req->addPostParameter('status', '0');
+ $res = $req->send();
+ $this->assertEquals(
+ 200, $res->getStatus(),
+ 'Adding bookmark failed: ' . $res->getBody());
+ $bms = $this->bs->getBookmarks(0, null, $uId);
+ $bm = reset($bms['bookmarks']);
+ $bmId = $bm['bId'];
+
+ $reqUrl = $GLOBALS['unittestUrl'] . 'edit.php/' . $bmId . '?unittestMode=1';
+ $req2 = new HTTP_Request2($reqUrl, HTTP_Request2::METHOD_POST);
+ $req2->setCookieJar($cookies);
+ $req2->addPostParameter('address', 'http://www.example.org/testdefaultprivacyposts_edit');
+ $req2->addPostParameter('title', 'Test bookmark 2 for default privacy.');
+ $req2->addPostParameter('submitted', '1');
+ $res = $req2->send();
+
+ $this->assertEquals(302, $res->getStatus(), 'Editing bookmark failed');
+
+ $bm = $this->bs->getBookmark($bmId);
+ $this->assertEquals('2', $bm['bStatus']);
+ }//end testDefaultPrivacyEdit
+
+}//end class www_editTest
+?>
diff --git a/tests/www/importNetscapeTest.php b/tests/www/importNetscapeTest.php
new file mode 100755
index 0000000..9d4cacd
--- /dev/null
+++ b/tests/www/importNetscapeTest.php
@@ -0,0 +1,33 @@
+<?php
+require_once dirname(__FILE__) . '/../prepare.php';
+require_once 'HTTP/Request2.php';
+
+class www_importNetscapeTest extends TestBaseApi
+{
+ protected $urlPart = 'importNetscape.php';
+
+ /**
+ * Test that the default privacy setting is used when bookmarks
+ * are imported from an HTML bookmarks file using importNetscape.php.
+ */
+ public function testDefaultPrivacyImportNetscape()
+ {
+ $this->setUnittestConfig(
+ array('defaults' => array('privacy' => 1))
+ );
+ list($req, $uId) = $this->getLoggedInRequest();
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->setUrl($GLOBALS['unittestUrl'] . 'importNetscape.php' . '?unittestMode=1');
+ $req->addUpload('userfile', dirname(__FILE__) . '/../data/BookmarkTest_netscapebookmarks.html');
+ $res = $req->send();
+ $this->assertEquals(200, $res->getStatus(), 'Bookmark import failed');
+
+ $this->us->setCurrentUserId($uId);
+ $bms = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(3, count($bms['bookmarks']));
+ $bm = reset($bms['bookmarks']);
+ $this->assertEquals('1', $bm['bStatus']);
+ }//end testDefaultPrivacyImportNetscape
+
+}//end class www_importNetscapeTest
+?>
diff --git a/tests/www/importTest.php b/tests/www/importTest.php
new file mode 100755
index 0000000..895a320
--- /dev/null
+++ b/tests/www/importTest.php
@@ -0,0 +1,33 @@
+<?php
+require_once dirname(__FILE__) . '/../prepare.php';
+require_once 'HTTP/Request2.php';
+
+class www_importTest extends TestBaseApi
+{
+ protected $urlPart = 'import.php';
+
+ /**
+ * Test that the default privacy setting is used when bookmarks
+ * are imported from an XML bookmarks file using import.php.
+ */
+ public function testDefaultPrivacyImport()
+ {
+ $this->setUnittestConfig(
+ array('defaults' => array('privacy' => 2))
+ );
+ list($req, $uId) = $this->getLoggedInRequest();
+ $req->setMethod(HTTP_Request2::METHOD_POST);
+ $req->setUrl($GLOBALS['unittestUrl'] . 'import.php' . '?unittestMode=1');
+ $req->addUpload('userfile', dirname(__FILE__) . '/../data/BookmarkTest_deliciousbookmarks.xml');
+ $res = $req->send();
+ $this->assertEquals(302, $res->getStatus(), 'Bookmark import failed');
+
+ $this->us->setCurrentUserId($uId);
+ $bms = $this->bs->getBookmarks(0, null, $uId);
+ $this->assertEquals(3, count($bms['bookmarks']));
+ $bm = reset($bms['bookmarks']);
+ $this->assertEquals('2', $bm['bStatus']);
+ }//end testDefaultPrivacyImport
+
+}//end class www_importTest
+?>
diff --git a/tests/www/indexTest.php b/tests/www/indexTest.php
new file mode 100644
index 0000000..503fd1f
--- /dev/null
+++ b/tests/www/indexTest.php
@@ -0,0 +1,58 @@
+<?php
+require_once dirname(__FILE__) . '/../prepare.php';
+require_once 'HTTP/Request2.php';
+
+class www_indexTest extends TestBaseApi
+{
+ protected $urlPart = '';
+
+ /**
+ * Test that the private rss feed exists when user is setup
+ * with a private key and is enabled
+ */
+ public function testVerifyPrivateRSSLinkExists()
+ {
+ list($req, $uId) = $this->getLoggedInRequest('?unittestMode=1', true, true);
+
+ $user = $this->us->getUser($uId);
+ $response = $req->send();
+ $response_body = $response->getBody();
+
+ $this->assertNotEquals('', $response_body, 'Response is empty');
+
+ $x = simplexml_load_string($response_body);
+ $ns = $x->getDocNamespaces();
+ $x->registerXPathNamespace('ns', reset($ns));
+
+ $elements = $x->xpath('//ns:link[@rel="alternate" and @type="application/rss+xml"]');
+ $this->assertEquals(2, count($elements), 'Number of Links in Head not correct');
+ $this->assertContains('privateKey=', (string)$elements[1]['href']);
+ }//end testVerifyPrivateRSSLinkExists
+
+
+
+ /**
+ * Test that the private RSS link doesn't exists when a user
+ * does not have a private key, or the private key is not enabled
+ */
+ public function testVerifyPrivateRSSLinkDoesNotExist()
+ {
+ list($req, $uId) = $this->getLoggedInRequest('?unittestMode=1', true);
+
+ $user = $this->us->getUser($uId);
+ $response = $req->send();
+ $response_body = $response->getBody();
+ $this->assertNotEquals('', $response_body, 'Response is empty');
+
+ $x = simplexml_load_string($response_body);
+ $ns = $x->getDocNamespaces();
+ $x->registerXPathNamespace('ns', reset($ns));
+
+ $elements = $x->xpath('//ns:link[@rel="alternate" and @type="application/rss+xml"]');
+ $this->assertEquals(1, count($elements), 'Number of Links in Head not correct');
+ $this->assertNotContains('privateKey=', (string)$elements[0]['href']);
+ }//end testVerifyPrivateRSSLinkDoesNotExist
+
+
+}//end class www_bookmarksTest
+?>
diff --git a/tests/www/rssTest.php b/tests/www/rssTest.php
new file mode 100644
index 0000000..71d0198
--- /dev/null
+++ b/tests/www/rssTest.php
@@ -0,0 +1,151 @@
+<?php
+require_once dirname(__FILE__) . '/../prepare.php';
+require_once 'HTTP/Request2.php';
+
+class www_rssTest extends TestBaseApi
+{
+ protected $urlPart = 'rss.php';
+
+
+ /**
+ * Verifies that the given number of feed items exist in the feed
+ * XML tree.
+ *
+ * @var SimpleXMLElement $simpleXml RSS feed root element
+ * @var integer $nCount Number of expected feed items
+ * @var string $msg Error message
+ */
+ protected function assertItemCount(
+ SimpleXMLElement $simpleXml, $nCount, $msg = null
+ ) {
+ $this->assertEquals($nCount, count($simpleXml->channel->item), $msg);
+ }
+
+
+
+
+ /**
+ * A private bookmark should not show up in the global rss feed if the
+ * user is not logged in nor passes the private key
+ */
+ public function testAllPrivateBookmarkNotLoggedIn()
+ {
+ list($uId, $username) = $this->addUserData();
+ $this->addBookmark(
+ $uId, null, SemanticScuttle_Model_Bookmark::SPRIVATE
+ );
+
+ $req = $this->getRequest();
+ $response_body = $req->send()->getBody();
+
+ $rss = simplexml_load_string($response_body);
+ $this->assertItemCount($rss, 0, 'I see a private bookmark');
+ }
+
+
+
+ /**
+ * A private bookmark should not show up in the user's rss feed if the
+ * user is not logged in nor passes the private key
+ */
+ public function testUserPrivateBookmarkNotLoggedIn()
+ {
+ list($uId, $username) = $this->addUserData();
+ $this->addBookmark(
+ $uId, null, SemanticScuttle_Model_Bookmark::SPRIVATE
+ );
+
+ $req = $this->getRequest('/' . $username);
+ $response_body = $req->send()->getBody();
+
+ $rss = simplexml_load_string($response_body);
+ $this->assertItemCount($rss, 0, 'I see a private bookmark');
+ }
+
+
+
+
+ /**
+ * Test the global feed by passing the private key
+ */
+ public function testAllPrivateBookmarkWithPrivateKey()
+ {
+ list($uId, $username, $password, $privateKey) = $this->addUserData(
+ null, null, true
+ );
+ $this->addBookmark(
+ $uId, null, SemanticScuttle_Model_Bookmark::SPRIVATE,
+ null, 'private bookmark'
+ );
+
+ $req = $this->getRequest('?privateKey=' . $privateKey);
+ $response_body = $req->send()->getBody();
+
+ $rss = simplexml_load_string($response_body);
+ $this->assertItemCount($rss, 1, 'I miss the private bookmark');
+ $this->assertEquals(
+ 'private bookmark', (string)$rss->channel->item[0]->title
+ );
+ }
+
+
+
+ /**
+ * Test the user feed by passing the private key
+ */
+ public function testUserPrivateBookmarkWithPrivateKey()
+ {
+ list($uId, $username, $password, $privateKey) = $this->addUserData(
+ null, null, true
+ );
+ $this->addBookmark(
+ $uId, null, SemanticScuttle_Model_Bookmark::SPRIVATE,
+ null, 'private bookmark'
+ );
+
+ $req = $this->getRequest('/' . $username . '?privateKey=' . $privateKey);
+ $response_body = $req->send()->getBody();
+
+ $rss = simplexml_load_string($response_body);
+ $this->assertItemCount($rss, 1, 'I miss the private bookmark');
+ $this->assertEquals(
+ 'private bookmark', (string)$rss->channel->item[0]->title
+ );
+ }
+
+
+
+ /**
+ * Verify that fetching the feed with a private key
+ * does not keep you logged in
+ */
+ public function testUserPrivateKeyDoesNotKeepLoggedYouIn()
+ {
+ list($uId, $username, $password, $privateKey) = $this->addUserData(
+ null, null, true
+ );
+ $this->addBookmark(
+ $uId, null, SemanticScuttle_Model_Bookmark::SPRIVATE,
+ null, 'private bookmark'
+ );
+
+ $req = $this->getRequest('/' . $username . '?privateKey=' . $privateKey);
+ $cookies = $req->setCookieJar()->getCookieJar();
+ $response_body = $req->send()->getBody();
+
+ $rss = simplexml_load_string($response_body);
+ $items = $rss->channel->item;
+
+ $this->assertEquals(1, count($items), 'I miss the private bookmark');
+ $this->assertEquals('private bookmark', (string)$items[0]->title);
+
+ //request the feed again, with the same cookies
+ $req = $this->getRequest('/' . $username);
+ $req->setCookieJar($cookies);
+ $response_body = $req->send()->getBody();
+ $rss = simplexml_load_string($response_body);
+ $this->assertItemCount($rss, 0, 'I still see the private bookmark');
+ }
+
+}//end class www_rssTest
+?>
diff --git a/tests/www/searchTest.php b/tests/www/searchTest.php
new file mode 100644
index 0000000..89af32d
--- /dev/null
+++ b/tests/www/searchTest.php
@@ -0,0 +1,70 @@
+<?php
+require_once dirname(__FILE__) . '/../prepare.php';
+require_once 'HTTP/Request2.php';
+
+class www_SearchTest extends TestBaseApi
+{
+ protected $urlPart = 'search.php';
+
+
+ /**
+ * Some browsers using opensearch do "urlencode" on the terms,
+ * for example Firefox. Multiple terms separated with space
+ * appear as "foo+bar" in the URL.
+ */
+ public function testMultipleTermsUrlEncoded()
+ {
+ $this->addBookmark(null, null, 0, null, 'unittest foo bar');
+ $res = $this->getRequest('/all/foo+bar')->send();
+ $this->assertSelectCount(
+ '.xfolkentry', true, $res->getBody(),
+ 'No bookmark found', false
+ );
+
+ $res = $this->getRequest('/all/baz+bat')->send();
+ $this->assertSelectCount(
+ '.xfolkentry', false, $res->getBody(),
+ 'Bookmarks found', false
+ );
+ }
+
+
+ /**
+ * Some browsers using opensearch do "rawurlencode" on the terms,
+ * for example Opera. Multiple terms separated with space
+ * appear as "foo%20bar" in the URL.
+ */
+ public function testMultipleTermsRawUrlEncoded()
+ {
+ $this->addBookmark(null, null, 0, null, 'unittest foo bar');
+ $res = $this->getRequest('/all/foo%20bar')->send();
+ $this->assertSelectCount(
+ '.xfolkentry', true, $res->getBody(),
+ 'No bookmark found', false
+ );
+
+ $res = $this->getRequest('/all/baz%20bat')->send();
+ $this->assertSelectCount(
+ '.xfolkentry', false, $res->getBody(),
+ 'Bookmarks found', false
+ );
+ }
+
+
+ public function testMultipleTags()
+ {
+ $this->markTestSkipped(
+ 'FIXME: SemanticScuttle currently does not search multiple tags'
+ );
+
+ $this->addBookmark(null, null, 0, array('foo', 'bar'));
+ $res = $this->getRequest('/all/foo+bar')->send();
+ $this->assertSelectCount(
+ '.xfolkentry', true, $res->getBody(),
+ 'No bookmark found', false
+ );
+ }
+
+}
+
+?>