From be126cc958212ed74fe916eabcb1e6ac928df5e4 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 8 Oct 2010 17:43:05 +0200 Subject: tests for getadminlinkedtags --- tests/TestBase.php | 28 ++++++ tests/ajax/GetAdminLinkedTagsTest.php | 160 ++++++++++++++++++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 tests/ajax/GetAdminLinkedTagsTest.php (limited to 'tests') diff --git a/tests/TestBase.php b/tests/TestBase.php index 402330b..c0acd58 100644 --- a/tests/TestBase.php +++ b/tests/TestBase.php @@ -104,6 +104,34 @@ class TestBase extends PHPUnit_Framework_TestCase return $uid; } + + + /** + * Retrieves the UID of an admin user. + * If that user does not exist in the database, it is created. + * + * @return integer UID of admin user + */ + protected function getAdminUser() + { + if (count($GLOBALS['admin_users']) == 0) { + $this->fail('No admin users configured'); + } + $adminUserName = reset($GLOBALS['admin_users']); + + $us = SemanticScuttle_Service_Factory::get('User'); + $uid = $us->getIdFromUser($adminUserName); + if ($uid === null) { + //that user does not exist in the database; create it + $uid = $us->addUser( + $adminUserName, + rand(), + 'unittest-admin-' . $adminUserName . '@example.org' + ); + } + + return $uid; + } } ?> \ No newline at end of file diff --git a/tests/ajax/GetAdminLinkedTagsTest.php b/tests/ajax/GetAdminLinkedTagsTest.php new file mode 100644 index 0000000..d8ec447 --- /dev/null +++ b/tests/ajax/GetAdminLinkedTagsTest.php @@ -0,0 +1,160 @@ + + * @author Christian Weiske + * @author Eric Dane + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ + +require_once dirname(__FILE__) . '/../prepare.php'; +require_once 'HTTP/Request2.php'; + +if (!defined('PHPUnit_MAIN_METHOD')) { + define('PHPUnit_MAIN_METHOD', 'ajax_GetAdminLinkedTagsTest::main'); +} + +/** + * Unit tests for the ajax linked admin tags script + * + * @category Bookmarking + * @package SemanticScuttle + * @author Christian Weiske + * @license GPL http://www.gnu.org/licenses/gpl.html + * @link http://sourceforge.net/projects/semanticscuttle + */ +class ajax_GetAdminLinkedTagsTest extends TestBaseApi +{ + protected $urlPart = 'ajax/getadminlinkedtags.php'; + + + + /** + * Used to run this test class standalone + * + * @return void + */ + public static function main() + { + require_once 'PHPUnit/TextUI/TestRunner.php'; + PHPUnit_TextUI_TestRunner::run( + new PHPUnit_Framework_TestSuite(__CLASS__) + ); + } + + + + /** + * Verify that we get the configured root tags if + * we do not pass any parameters + */ + public function testRootTags() + { + $req = $this->getRequest(); + $res = $req->send(); + + $this->assertEquals(200, $res->getStatus()); + $this->assertEquals( + 'application/json; charset=utf-8', + $res->getHeader('content-type') + ); + + $data = json_decode($res->getBody()); + $this->assertType('array', $data); + + //same number of elements as the menu2Tags array + $this->assertEquals( + count($GLOBALS['menu2Tags']), + count($data) + ); + + //and the same contents + foreach ($data as $tagObj) { + $tagName = $tagObj->data->title; + $this->assertContains($tagName, $GLOBALS['menu2Tags']); + } + } + + /** + * Verify that we get subtags of a given tag + */ + public function testSubTags() + { + $t2t = SemanticScuttle_Service_Factory::get('Tag2Tag'); + $t2t->deleteAll(); + + $menu2Tag = reset($GLOBALS['menu2Tags']); + //we have a subtag now + $this->addBookmark( + $this->getAdminUser(), + null, + 0, + $menu2Tag . '>adminsubtag' + ); + + $res = $this->getRequest('?tag=' . $menu2Tag)->send(); + $this->assertEquals(200, $res->getStatus()); + $this->assertEquals( + 'application/json; charset=utf-8', + $res->getHeader('content-type') + ); + + $data = json_decode($res->getBody()); + $this->assertType('array', $data); + + //only one subtag + $this->assertEquals(1, count($data)); + $this->assertEquals('adminsubtag', $data[0]->data->title); + } + + /** + * Verify that we only get admin tags, not tags from + * non-admin people + */ + public function testOnlyAdminTags() + { + $t2t = SemanticScuttle_Service_Factory::get('Tag2Tag'); + $t2t->deleteAll(); + + $menu2Tag = reset($GLOBALS['menu2Tags']); + //we have a subtag now + $this->addBookmark( + $this->getAdminUser(), + null, + 0, + $menu2Tag . '>adminsubtag' + ); + //add another bookmark now, but for a normal user + $this->addBookmark( + null, + null, + 0, + $menu2Tag . '>normalsubtag' + ); + + $res = $this->getRequest('?tag=' . $menu2Tag)->send(); + $this->assertEquals(200, $res->getStatus()); + $this->assertEquals( + 'application/json; charset=utf-8', + $res->getHeader('content-type') + ); + + $data = json_decode($res->getBody()); + $this->assertType('array', $data); + + //we should have only one subtag now, the admin one + $this->assertEquals(1, count($data)); + $this->assertEquals('adminsubtag', $data[0]->data->title); + } +} + +if (PHPUnit_MAIN_METHOD == 'ajax_GetAdminLinkedTagsTest::main') { + ajax_GetAdminLinkedTagsTest::main(); +} +?> \ No newline at end of file -- cgit v1.2.3-54-g00ecf