tests for getadminlinkedtags

This commit is contained in:
Christian Weiske 2010-10-08 17:43:05 +02:00
parent cd023dea53
commit be126cc958
3 changed files with 201 additions and 5 deletions

View file

@ -203,18 +203,26 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService
}
}
/* Takes an numerical "id" or a string "username"
and returns the numerical "id" if the user exists else returns NULL */
function getIdFromUser($user) {
/**
* Obtains the ID of the given user name.
* If a user ID is passed, it is returned.
* In case the user does not exist, NULL is returned.
*
* @param string|integer $user User name or user ID
*
* @return integer NULL if not found or the user ID
*/
public function getIdFromUser($user)
{
if (is_int($user)) {
return intval($user);
} else {
$objectUser = $this->getObjectUserByUsername($user);
if($objectUser != NULL) {
if ($objectUser != null) {
return $objectUser->getId();
}
}
return NULL;
return null;
}
/**

View file

@ -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;
}
}
?>

View file

@ -0,0 +1,160 @@
<?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->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();
}
?>