diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/AllTests.php | 7 | ||||
-rw-r--r-- | tests/Api/PostsAddTest.php | 435 | ||||
-rw-r--r-- | tests/Api/PostsDeleteTest.php | 303 | ||||
-rw-r--r-- | tests/Api/PostsUpdateTest.php | 135 | ||||
-rw-r--r-- | tests/Bookmark2TagTest.php | 452 | ||||
-rw-r--r-- | tests/BookmarkTest.php | 145 | ||||
-rw-r--r-- | tests/CommonDescriptionTest.php | 5 | ||||
-rw-r--r-- | tests/SearchHistoryTest.php | 393 | ||||
-rw-r--r-- | tests/Tag2TagTest.php | 5 | ||||
-rw-r--r-- | tests/TagTest.php | 5 | ||||
-rw-r--r-- | tests/TagsCacheTest.php | 8 | ||||
-rw-r--r-- | tests/TestBase.php | 56 | ||||
-rw-r--r-- | tests/TestBaseApi.php | 137 | ||||
-rw-r--r-- | tests/UserArrayTest.php | 68 | ||||
-rw-r--r-- | tests/UserTest.php | 9 | ||||
-rw-r--r-- | tests/VoteTest.php | 5 | ||||
-rw-r--r-- | tests/ajax/GetAdminLinkedTagsTest.php | 146 | ||||
-rw-r--r-- | tests/ajax/GetAdminTagsTest.php | 122 | ||||
-rw-r--r-- | tests/ajax/GetContactTagsTest.php | 102 | ||||
-rw-r--r-- | tests/phpunit.xml | 8 | ||||
-rw-r--r-- | tests/prepare.php | 8 |
21 files changed, 2427 insertions, 127 deletions
diff --git a/tests/AllTests.php b/tests/AllTests.php index d29de7f..4afcc6b 100644 --- a/tests/AllTests.php +++ b/tests/AllTests.php @@ -17,9 +17,6 @@ if (!defined('PHPUnit_MAIN_METHOD')) { } require_once 'prepare.php'; -require_once 'PHPUnit/Framework/TestSuite.php'; - -PHPUnit_Util_Filter::addFileToFilter(__FILE__); /** * SemanticScuttle unit tests. @@ -64,6 +61,10 @@ class AllTests extends PHPUnit_Framework_TestSuite $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/PostsAddTest.php'); + $suite->addTestFile($tdir . '/Api/PostsDeleteTest.php'); + $suite->addTestFile($tdir . '/Api/PostsUpdateTest.php'); return $suite; } diff --git a/tests/Api/PostsAddTest.php b/tests/Api/PostsAddTest.php new file mode 100644 index 0000000..1f21d04 --- /dev/null +++ b/tests/Api/PostsAddTest.php @@ -0,0 +1,435 @@ +<?php +/** + * SemanticScuttle - your social bookmark manager. + * + * PHP version 5. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net> + * @author Christian Weiske <cweiske@cweiske.de> + * @author Eric Dane <ericdane@users.sourceforge.net> + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once dirname(__FILE__) . '/../prepare.php'; +require_once 'HTTP/Request2.php'; + +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'Api_PostsAddTest::main'); +} + +/** + * 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'; + + + + /** + * Used to run this test class standalone + * + * @return void + */ + public static function main() + { + require_once 'PHPUnit/TextUI/TestRunner.php'; + PHPUnit_TextUI_TestRunner::run( + new PHPUnit_Framework_TestSuite(__CLASS__) + ); + } + + + + /** + * Test if authentication is required when sending no auth data + */ + public function testAuthWithoutAuthData() + { + $req = $this->getRequest(null, false); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * Test if authentication is required when sending wrong user data + */ + public function testAuthWrongCredentials() + { + $req = $this->getRequest(null, false); + $req->setAuth('user', 'password', HTTP_Request2::AUTH_BASIC); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * Test 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, $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, $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']); + } +} + +if (PHPUnit_MAIN_METHOD == 'Api_PostsAddTest::main') { + Api_PostsAddTest::main(); +} +?>
\ No newline at end of file diff --git a/tests/Api/PostsDeleteTest.php b/tests/Api/PostsDeleteTest.php new file mode 100644 index 0000000..d9fb6cd --- /dev/null +++ b/tests/Api/PostsDeleteTest.php @@ -0,0 +1,303 @@ +<?php +/** + * SemanticScuttle - your social bookmark manager. + * + * PHP version 5. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net> + * @author Christian Weiske <cweiske@cweiske.de> + * @author Eric Dane <ericdane@users.sourceforge.net> + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once dirname(__FILE__) . '/../prepare.php'; +require_once 'HTTP/Request2.php'; + +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'Api_PostsDeleteTest::main'); +} + +/** + * 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'; + + + + /** + * Used to run this test class standalone + * + * @return void + */ + public static function main() + { + require_once 'PHPUnit/TextUI/TestRunner.php'; + PHPUnit_TextUI_TestRunner::run( + new PHPUnit_Framework_TestSuite(__CLASS__) + ); + } + + + + /** + * Test if authentication is required when sending no auth data + */ + public function testAuthWithoutAuthData() + { + $req = $this->getRequest(null, false); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * Test if authentication is required when sending wrong user data + + */ + public function testAuthWrongCredentials() + { + $req = $this->getRequest(null, false); + $req->setAuth('user', 'password', HTTP_Request2::AUTH_BASIC); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * Test 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']); + } + +} + +if (PHPUnit_MAIN_METHOD == 'Api_PostsDeleteTest::main') { + Api_PostsDeleteTest::main(); +} +?>
\ No newline at end of file diff --git a/tests/Api/PostsUpdateTest.php b/tests/Api/PostsUpdateTest.php new file mode 100644 index 0000000..c497a55 --- /dev/null +++ b/tests/Api/PostsUpdateTest.php @@ -0,0 +1,135 @@ +<?php +/** + * SemanticScuttle - your social bookmark manager. + * + * PHP version 5. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net> + * @author Christian Weiske <cweiske@cweiske.de> + * @author Eric Dane <ericdane@users.sourceforge.net> + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once dirname(__FILE__) . '/../prepare.php'; +require_once 'HTTP/Request2.php'; + +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'Api_PostsUpdateTest::main'); +} + +/** + * 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'; + + + + /** + * Used to run this test class standalone + * + * @return void + */ + public static function main() + { + require_once 'PHPUnit/TextUI/TestRunner.php'; + PHPUnit_TextUI_TestRunner::run( + new PHPUnit_Framework_TestSuite(__CLASS__) + ); + } + + + + /** + * Test if authentication is required when sending no auth data + */ + public function testAuthWithoutAuthData() + { + $req = $this->getRequest(null, false); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * Test if authentication is required when sending wrong user data + + */ + public function testAuthWrongCredentials() + { + $req = $this->getRequest(null, false); + $req->setAuth('user', 'password', HTTP_Request2::AUTH_BASIC); + $res = $req->send(); + $this->assertEquals(401, $res->getStatus()); + } + + + + /** + * 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'] + ) + ); + } + +} + +if (PHPUnit_MAIN_METHOD == 'Api_PostsUpdateTest::main') { + Api_PostsUpdateTest::main(); +} +?>
\ No newline at end of file diff --git a/tests/Bookmark2TagTest.php b/tests/Bookmark2TagTest.php index 14b71cc..fff4222 100644 --- a/tests/Bookmark2TagTest.php +++ b/tests/Bookmark2TagTest.php @@ -12,13 +12,12 @@ * @license GPL http://www.gnu.org/licenses/gpl.html * @link http://sourceforge.net/projects/semanticscuttle */ - -require_once 'prepare.php'; - if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'Bookmark2TagTest::main'); } +require_once 'prepare.php'; + /** * Unit tests for the SemanticScuttle bookmark-tag combination service. * @@ -38,6 +37,26 @@ class Bookmark2TagTest extends TestBase protected $tts; + /** + * Create a bookmark. Like addBookmark(), just with other paramter order + * to make some tests in that class easier to write. + * + * @param integer $user User ID the bookmark shall belong + * @param array $tags Array of tags to attach. If "null" is given, + * it will automatically be "unittest" + * @param string $date strtotime-compatible string + * @param string $title Bookmark title + * + * @return integer ID of bookmark + */ + protected function addTagBookmark($user, $tags, $date = null, $title = null) + { + return $this->addBookmark( + $user, null, 0, $tags, $title, $date + ); + } + + /** * Used to run this test class standalone @@ -57,6 +76,7 @@ class Bookmark2TagTest extends TestBase protected function setUp() { $this->us = SemanticScuttle_Service_Factory::get('User'); + $this->us->deleteAll(); $this->bs = SemanticScuttle_Service_Factory::get('Bookmark'); $this->bs->deleteAll(); $this->b2ts= SemanticScuttle_Service_Factory::get('Bookmark2Tag'); @@ -74,7 +94,7 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmark() when the bookmark has no tags * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmark */ public function testGetTagsForBookmarkNone() { @@ -92,7 +112,7 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmark() when the bookmark has one tag * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmark */ public function testGetTagsForBookmarkOne() { @@ -111,9 +131,9 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmark() when the bookmark has three tags * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmark */ - public function testGetTagsForBookmarkThree() + public function testGetTagsForBookmarkThr() { $this->addBookmark(null, null, 0, array('forz', 'barz')); @@ -121,7 +141,7 @@ class Bookmark2TagTest extends TestBase $this->b2ts->attachTags($bid, array('foo', 'bar', 'fuu')); $tags = $this->b2ts->getTagsForBookmark($bid); - $this->assertType('array', $tags); + $this->assertInternalType('array', $tags); $this->assertContains('foo', $tags); $this->assertContains('bar', $tags); $this->assertContains('fuu', $tags); @@ -132,7 +152,7 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmarks() when no bookmarks have tags. * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmarks */ public function testGetTagsForBookmarksNone() { @@ -142,10 +162,10 @@ class Bookmark2TagTest extends TestBase $alltags = $this->b2ts->getTagsForBookmarks( array($bid1, $bid2) ); - $this->assertType('array', $alltags); + $this->assertInternalType('array', $alltags); $this->assertEquals(2, count($alltags)); - $this->assertType('array', $alltags[$bid1]); - $this->assertType('array', $alltags[$bid2]); + $this->assertInternalType('array', $alltags[$bid1]); + $this->assertInternalType('array', $alltags[$bid2]); $this->assertEquals(0, count($alltags[$bid1])); $this->assertEquals(0, count($alltags[$bid2])); } @@ -155,7 +175,7 @@ class Bookmark2TagTest extends TestBase /** * Test getTagsForBookmarks() when most bookmarks have tags. * - * @return void + * @covers SemanticScuttle_Service_Bookmark2Tag::getTagsForBookmarks */ public function testGetTagsForBookmarksMost() { @@ -180,9 +200,9 @@ class Bookmark2TagTest extends TestBase $alltags = $this->b2ts->getTagsForBookmarks( array($bid1, $bid2, $bid3, $bid4) ); - $this->assertType('array', $alltags); + $this->assertInternalType('array', $alltags); foreach ($alltags as $bid => $btags) { - $this->assertType('array', $btags); + $this->assertInternalType('array', $btags); if ($bid == $bid1) { $this->assertEquals(3, count($btags)); $this->assertContains('foo', $btags); @@ -205,6 +225,408 @@ class Bookmark2TagTest extends TestBase } } } + + + + /** + * 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'), 'today'); + $this->addTagBookmark($user, array('one', 'thr'), 'today'); + $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); + } + + + /** + * @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); + } } if (PHPUnit_MAIN_METHOD == 'Bookmark2TagTest::main') { diff --git a/tests/BookmarkTest.php b/tests/BookmarkTest.php index 40869b2..f54fe9a 100644 --- a/tests/BookmarkTest.php +++ b/tests/BookmarkTest.php @@ -12,13 +12,12 @@ * @license GPL http://www.gnu.org/licenses/gpl.html * @link http://sourceforge.net/projects/semanticscuttle */ - -require_once 'prepare.php'; - if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'BookmarkTest::main'); } +require_once 'prepare.php'; + /** * Unit tests for the SemanticScuttle bookmark service. * @@ -264,7 +263,7 @@ class BookmarkTest extends TestBase $bookmark = $this->bs->getBookmark($bid); $ret = $this->bs->bookmarksExist(array($bookmark['bAddress'])); - $this->assertType('array', $ret); + $this->assertInternalType('array', $ret); $this->assertEquals(1, count($ret)); $this->assertTrue($ret[$bookmark['bAddress']]); } @@ -292,7 +291,7 @@ class BookmarkTest extends TestBase $bookmark2['bAddress'] ) ); - $this->assertType('array', $ret); + $this->assertInternalType('array', $ret); $this->assertEquals(2, count($ret)); $this->assertTrue($ret[$bookmark['bAddress']]); $this->assertTrue($ret[$bookmark2['bAddress']]); @@ -309,7 +308,7 @@ class BookmarkTest extends TestBase public function testBookmarksExistFalseSingle() { $ret = $this->bs->bookmarksExist(array('does-not-exist')); - $this->assertType('array', $ret); + $this->assertInternalType('array', $ret); $this->assertEquals(1, count($ret)); $this->assertFalse($ret['does-not-exist']); } @@ -330,7 +329,7 @@ class BookmarkTest extends TestBase 'does-not-exist-3', ); $ret = $this->bs->bookmarksExist($bms); - $this->assertType('array', $ret); + $this->assertInternalType('array', $ret); $this->assertEquals(3, count($ret)); $this->assertFalse($ret['does-not-exist']); $this->assertFalse($ret['does-not-exist-2']); @@ -367,7 +366,7 @@ class BookmarkTest extends TestBase 'does-not-exist-3' ) ); - $this->assertType('array', $ret); + $this->assertInternalType('array', $ret); $this->assertEquals(5, count($ret)); $this->assertTrue($ret[$bookmark['bAddress']]); $this->assertTrue($ret[$bookmark2['bAddress']]); @@ -476,7 +475,7 @@ class BookmarkTest extends TestBase foreach ($bms['bookmarks'] as $bm) { $this->assertArrayHasKey('tags', $bm); - $this->assertType('array', $bm['tags']); + $this->assertInternalType('array', $bm['tags']); if ($bm['bId'] == $bid) { $this->assertContains('foo', $bm['tags']); $this->assertContains('bar', $bm['tags']); @@ -757,7 +756,7 @@ class BookmarkTest extends TestBase $bm = $this->bs->getBookmark($bid, true); $this->assertArrayHasKey('tags', $bm); - $this->assertType('array', $bm['tags']); + $this->assertInternalType('array', $bm['tags']); $this->assertContains('foo', $bm['tags']); $this->assertContains('bar', $bm['tags']); } @@ -875,7 +874,7 @@ class BookmarkTest extends TestBase $bid = $this->addBookmark($uid, $url); $bm = $this->bs->getBookmarkByAddress($url); - $this->assertType('array', $bm); + $this->assertInternalType('array', $bm); $this->assertEquals($url, $bm['bAddress']); } @@ -901,7 +900,7 @@ class BookmarkTest extends TestBase $bid = $this->addBookmark($uid, $url); $bm = $this->bs->getBookmarkByAddress($incomplete); - $this->assertType('array', $bm); + $this->assertInternalType('array', $bm); $this->assertEquals($url, $bm['bAddress']); } @@ -952,7 +951,7 @@ class BookmarkTest extends TestBase $this->assertEquals('new description', $bm['bDescription']); $this->assertEquals('new private note', $bm['bPrivateNote']); $this->assertEquals(1, $bm['bStatus']); - $this->assertType('array', $bm['tags']); + $this->assertInternalType('array', $bm['tags']); $this->assertEquals(1, count($bm['tags'])); $this->assertContains('new', $bm['tags']); } @@ -982,6 +981,38 @@ class BookmarkTest extends TestBase $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']); + } + /** @@ -1087,12 +1118,98 @@ class BookmarkTest extends TestBase /** * 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 testCountOthersWatchlist() + public function testCountOthersWatchlistComplex() { $uid = $this->addUser(); $address = 'http://example.org'; diff --git a/tests/CommonDescriptionTest.php b/tests/CommonDescriptionTest.php index 63b9e0c..94f431d 100644 --- a/tests/CommonDescriptionTest.php +++ b/tests/CommonDescriptionTest.php @@ -12,13 +12,12 @@ * @license GPL http://www.gnu.org/licenses/gpl.html * @link http://sourceforge.net/projects/semanticscuttle */ - -require_once 'prepare.php'; - if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'CommonDescriptionTest::main'); } +require_once 'prepare.php'; + /** * Unit tests for the SemanticScuttle common description service. * diff --git a/tests/SearchHistoryTest.php b/tests/SearchHistoryTest.php index 3716b37..69d1efa 100644 --- a/tests/SearchHistoryTest.php +++ b/tests/SearchHistoryTest.php @@ -12,13 +12,12 @@ * @license GPL http://www.gnu.org/licenses/gpl.html * @link http://sourceforge.net/projects/semanticscuttle */ - -require_once 'prepare.php'; - if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'SearchHistoryTest::main'); } +require_once 'prepare.php'; + /** * Unit tests for the SemanticScuttle search history service. * @@ -55,63 +54,341 @@ class SearchHistoryTest extends TestBase + /** + * 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(); - } - - public function testSearchHistory() - { - $shs = $this->shs; - - $terms = 'bbqsdkbb;,:,:q;,qddds&é"\'\\\\\(-è_çà)'; - $terms2 = '~#{|`]'; - $range = 'all'; - $nbResults = 10908; - $uId = 10; - - $shs->addSearch($terms, $range, $nbResults, $uId); - $shs->addSearch($terms2, $range, $nbResults, $uId); - $shs->addSearch('', $range, $nbResults, $uId); // A void search must not be saved - - $searches = $shs->getAllSearches(); - $this->assertSame(2, count($searches)); - $searches = $shs->getAllSearches($range, $uId); - $this->assertEquals(2, count($searches)); - $searches = $shs->getAllSearches($range, 20); // fake userid - $this->assertEquals(0, count($searches)); - $searches = $shs->getAllSearches($range, $uId, 1); - $this->assertEquals(1, count($searches)); - $searches = $shs->getAllSearches($range, null, 1, 1); - $this->assertEquals(1, count($searches)); - - //test content of results - $searches = $shs->getAllSearches(); - $this->assertSame($terms2, $searches[0]['shTerms']); - $this->assertSame($range, $searches[0]['shRange']); - $this->assertEquals($nbResults, $searches[0]['shNbResults']); - $this->assertEquals($uId, $searches[0]['uId']); - $this->assertSame($terms, $searches[1]['shTerms']); - $this->assertSame($range, $searches[1]['shRange']); - $this->assertEquals($nbResults, $searches[1]['shNbResults']); - $this->assertEquals($uId, $searches[1]['uId']); - - //test distinct parameter - $shs->addSearch($terms, $range, $nbResults, 30); // we repeat a search (same terms) - $searches = $shs->getAllSearches(); - $this->assertSame(3, count($searches)); - $searches = $shs->getAllSearches(NULL, NULL, NULL, NULL, true); - $this->assertSame(2, count($searches)); + $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/Tag2TagTest.php b/tests/Tag2TagTest.php index d1b6100..033fc91 100644 --- a/tests/Tag2TagTest.php +++ b/tests/Tag2TagTest.php @@ -12,13 +12,12 @@ * @license GPL http://www.gnu.org/licenses/gpl.html * @link http://sourceforge.net/projects/semanticscuttle */ - -require_once 'prepare.php'; - if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'Tag2TagTest::main'); } +require_once 'prepare.php'; + /** * Unit tests for the SemanticScuttle tag2tag service. * diff --git a/tests/TagTest.php b/tests/TagTest.php index c08aba2..25d1a77 100644 --- a/tests/TagTest.php +++ b/tests/TagTest.php @@ -12,13 +12,12 @@ * @license GPL http://www.gnu.org/licenses/gpl.html * @link http://sourceforge.net/projects/semanticscuttle */ - -require_once 'prepare.php'; - if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'TagTest::main'); } +require_once 'prepare.php'; + /** * Unit tests for the SemanticScuttle tag service. * diff --git a/tests/TagsCacheTest.php b/tests/TagsCacheTest.php index 9097bcb..94200dd 100644 --- a/tests/TagsCacheTest.php +++ b/tests/TagsCacheTest.php @@ -12,13 +12,12 @@ * @license GPL http://www.gnu.org/licenses/gpl.html * @link http://sourceforge.net/projects/semanticscuttle */ - -require_once 'prepare.php'; - if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'TagsCacheTest::main'); } +require_once 'prepare.php'; + /** * Unit tests for the SemanticScuttle tags cache service. * @@ -54,9 +53,6 @@ class TagsCacheTest extends PHPUnit_Framework_TestCase protected function setUp() { - global $dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbpersist, $dbtype, $tableprefix, $TEMPLATES_DIR, $debugMode; - require_once dirname(__FILE__) . '/../src/SemanticScuttle/header.php'; - $this->us =SemanticScuttle_Service_Factory::get('User'); $this->bs =SemanticScuttle_Service_Factory::get('Bookmark'); $this->bs->deleteAll(); diff --git a/tests/TestBase.php b/tests/TestBase.php index 402330b..095f32d 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -11,10 +11,6 @@ * @link http://sourceforge.net/projects/semanticscuttle */ -require_once 'PHPUnit/Framework.php'; - -PHPUnit_Util_Filter::addFileToFilter(__FILE__); - /** * Base unittest class that provides several helper methods. * @@ -35,6 +31,7 @@ class TestBase extends PHPUnit_Framework_TestCase * @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 * @@ -42,7 +39,7 @@ class TestBase extends PHPUnit_Framework_TestCase */ protected function addBookmark( $user = null, $address = null, $status = 0, - $tags = null, $title = null + $tags = null, $title = null, $date = null ) { if ($user === null) { $user = $this->addUser(); @@ -68,7 +65,7 @@ class TestBase extends PHPUnit_Framework_TestCase null, $status, $tags, - null, null, false, false, + null, $date, false, false, $user ); return $bid; @@ -83,9 +80,26 @@ class TestBase extends PHPUnit_Framework_TestCase * @param string $password Password * * @return integer ID of user + * + * @uses addUserData() */ protected function addUser($username = null, $password = null) { + return reset($this->addUserData($username, $password)); + } + + + + /** + * Creates a new user in the database and returns id, username and password. + * + * @param string $username Username + * @param string $password Password + * + * @return array ID of user, Name of user, password of user + */ + protected function addUserData($username = null, $password = null) + { $us = SemanticScuttle_Service_Factory::get('User'); $rand = rand(); @@ -101,9 +115,37 @@ class TestBase extends PHPUnit_Framework_TestCase $password, 'unittest-' . $rand . '@example.org' ); - return $uid; + return array($uid, $username, $password); } + + + /** + * Retrieves the UID of an admin user. + * If that user does not exist in the database, it is created. + * + * @return integer UID of admin user + */ + protected function getAdminUser() + { + if (count($GLOBALS['admin_users']) == 0) { + $this->fail('No admin users configured'); + } + $adminUserName = reset($GLOBALS['admin_users']); + + $us = SemanticScuttle_Service_Factory::get('User'); + $uid = $us->getIdFromUser($adminUserName); + if ($uid === null) { + //that user does not exist in the database; create it + $uid = $us->addUser( + $adminUserName, + rand(), + 'unittest-admin-' . $adminUserName . '@example.org' + ); + } + + return $uid; + } } ?>
\ No newline at end of file diff --git a/tests/TestBaseApi.php b/tests/TestBaseApi.php index 645ead9..f054973 100644 --- a/tests/TestBaseApi.php +++ b/tests/TestBaseApi.php @@ -11,10 +11,6 @@ * @link http://sourceforge.net/projects/semanticscuttle */ -require_once 'PHPUnit/Framework.php'; - -PHPUnit_Util_Filter::addFileToFilter(__FILE__); - /** * Base unittest class for web API tests. * @@ -29,6 +25,16 @@ class TestBaseApi extends TestBase protected $url; protected $urlPart = null; + /** + * @var SemanticScuttle_Service_User + */ + protected $us; + + /** + * @var SemanticScuttle_Service_Bookmark + */ + protected $bs; + protected function setUp() @@ -52,11 +58,26 @@ class TestBaseApi extends TestBase /** - * Gets a HTTP request object + * Clean up after test + */ + public function tearDown() + { + if (file_exists($GLOBALS['datadir'] . '/config.unittest.php')) { + unlink($GLOBALS['datadir'] . '/config.unittest.php'); + } + } + + + + /** + * Gets a HTTP request object. + * Uses $this->url plus $urlSuffix as request URL. * * @param string $urlSuffix Suffix for the URL * * @return HTTP_Request2 HTTP request object + * + * @uses $url */ protected function getRequest($urlSuffix = null) { @@ -71,13 +92,20 @@ class TestBaseApi extends TestBase /** - * Gets a HTTP request object + * Creates a user and a HTTP 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() */ protected function getAuthRequest($urlSuffix = null, $auth = true) { @@ -96,5 +124,102 @@ class TestBaseApi extends TestBase return array($req, $uid); } + + + /** + * Creates a user and a HTTP_Request2 object, does a normal login + * and prepares the cookies for the HTTP 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 + * + * @return array(HTTP_Request2, integer) HTTP request object and user id + * + * @uses getRequest() + */ + protected function getLoggedInRequest($urlSuffix = null, $auth = true) + { + if (is_array($auth)) { + list($username, $password) = $auth; + } else { + $username = 'testuser'; + $password = 'testpassword'; + } + $uid = $this->addUser($username, $password); + + $req = new HTTP_Request2( + $GLOBALS['unittestUrl'] . '/login.php', + 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\r\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.unittest.php', $str), + 'Writing config.unittest.php failed' + ); + } } ?>
\ No newline at end of file diff --git a/tests/UserArrayTest.php b/tests/UserArrayTest.php new file mode 100644 index 0000000..cb53f15 --- /dev/null +++ b/tests/UserArrayTest.php @@ -0,0 +1,68 @@ +<?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 'prepare.php'; + +/** + * 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 index 18870c8..f0a7427 100644 --- a/tests/UserTest.php +++ b/tests/UserTest.php @@ -12,13 +12,12 @@ * @license GPL http://www.gnu.org/licenses/gpl.html * @link http://sourceforge.net/projects/semanticscuttle */ - -require_once 'prepare.php'; - if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'UserTest::main'); } +require_once 'prepare.php'; + /** * Unit tests for the SemanticScuttle user service. * @@ -211,7 +210,7 @@ class UserTest extends TestBase $uid = $this->addUser(); $users = $this->us->getObjectUsers(); $this->assertEquals(1, count($users)); - $this->assertType('SemanticScuttle_Model_User', reset($users)); + $this->assertInstanceOf('SemanticScuttle_Model_User', reset($users)); } @@ -228,7 +227,7 @@ class UserTest extends TestBase $uid3 = $this->addUser(); $users = $this->us->getObjectUsers(); $this->assertEquals(3, count($users)); - $this->assertType('SemanticScuttle_Model_User', reset($users)); + $this->assertInstanceOf('SemanticScuttle_Model_User', reset($users)); } diff --git a/tests/VoteTest.php b/tests/VoteTest.php index 8e65917..1623826 100644 --- a/tests/VoteTest.php +++ b/tests/VoteTest.php @@ -10,13 +10,12 @@ * @license GPL http://www.gnu.org/licenses/gpl.html * @link http://sourceforge.net/projects/semanticscuttle */ - -require_once 'prepare.php'; - if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'VoteTest::main'); } +require_once 'prepare.php'; + /** * Unit tests for the SemanticScuttle voting system. * diff --git a/tests/ajax/GetAdminLinkedTagsTest.php b/tests/ajax/GetAdminLinkedTagsTest.php new file mode 100644 index 0000000..aded834 --- /dev/null +++ b/tests/ajax/GetAdminLinkedTagsTest.php @@ -0,0 +1,146 @@ +<?php +/** + * SemanticScuttle - your social bookmark manager. + * + * PHP version 5. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net> + * @author Christian Weiske <cweiske@cweiske.de> + * @author Eric Dane <ericdane@users.sourceforge.net> + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once dirname(__FILE__) . '/../prepare.php'; +require_once 'HTTP/Request2.php'; + +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'ajax_GetAdminLinkedTagsTest::main'); +} + +/** + * 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'; + + + + /** + * Used to run this test class standalone + * + * @return void + */ + public static function main() + { + require_once 'PHPUnit/TextUI/TestRunner.php'; + PHPUnit_TextUI_TestRunner::run( + new PHPUnit_Framework_TestSuite(__CLASS__) + ); + } + + + + /** + * Verify that we get the configured root tags if + * we do not pass any parameters + */ + public function testRootTags() + { + $req = $this->getRequest(); + $res = $req->send(); + $this->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); + } +} + +if (PHPUnit_MAIN_METHOD == 'ajax_GetAdminLinkedTagsTest::main') { + ajax_GetAdminLinkedTagsTest::main(); +} +?>
\ No newline at end of file diff --git a/tests/ajax/GetAdminTagsTest.php b/tests/ajax/GetAdminTagsTest.php new file mode 100644 index 0000000..80d702f --- /dev/null +++ b/tests/ajax/GetAdminTagsTest.php @@ -0,0 +1,122 @@ +<?php +/** + * SemanticScuttle - your social bookmark manager. + * + * PHP version 5. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net> + * @author Christian Weiske <cweiske@cweiske.de> + * @author Eric Dane <ericdane@users.sourceforge.net> + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once dirname(__FILE__) . '/../prepare.php'; +require_once 'HTTP/Request2.php'; + +/** + * 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..559040f --- /dev/null +++ b/tests/ajax/GetContactTagsTest.php @@ -0,0 +1,102 @@ +<?php +/** + * SemanticScuttle - your social bookmark manager. + * + * PHP version 5. + * + * @category Bookmarking + * @package SemanticScuttle + * @author Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net> + * @author Christian Weiske <cweiske@cweiske.de> + * @author Eric Dane <ericdane@users.sourceforge.net> + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once dirname(__FILE__) . '/../prepare.php'; +require_once 'HTTP/Request2.php'; + +/** + * 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/phpunit.xml b/tests/phpunit.xml new file mode 100644 index 0000000..734fa95 --- /dev/null +++ b/tests/phpunit.xml @@ -0,0 +1,8 @@ +<?xml version="1.0" encoding="utf-8" ?> +<phpunit> + <filter> + <blacklist> + <directory suffix=".php">.</directory> + </blacklist> + </filter> +</phpunit>
\ No newline at end of file diff --git a/tests/prepare.php b/tests/prepare.php index ce9cd1c..6afc284 100644 --- a/tests/prepare.php +++ b/tests/prepare.php @@ -19,7 +19,13 @@ $_SERVER['HTTP_HOST'] = 'http://localhost/'; define('UNIT_TEST_MODE', true); -require_once dirname(__FILE__) . '/../src/SemanticScuttle/header.php'; +if ('@data_dir@' == '@' . 'data_dir@') { + //non pear-install + 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'; |