2009-10-25 15:31:31 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SemanticScuttle - your social bookmark manager.
|
|
|
|
*
|
|
|
|
* PHP version 5.
|
|
|
|
*
|
|
|
|
* @category Bookmarking
|
|
|
|
* @package SemanticScuttle
|
|
|
|
* @author Christian Weiske <cweiske@cweiske.de>
|
|
|
|
* @license GPL http://www.gnu.org/licenses/gpl.html
|
|
|
|
* @link http://sourceforge.net/projects/semanticscuttle
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base unittest class that provides several helper methods.
|
|
|
|
*
|
|
|
|
* @category Bookmarking
|
|
|
|
* @package SemanticScuttle
|
|
|
|
* @author Christian Weiske <cweiske@cweiske.de>
|
|
|
|
* @license GPL http://www.gnu.org/licenses/gpl.html
|
|
|
|
* @link http://sourceforge.net/projects/semanticscuttle
|
|
|
|
*/
|
|
|
|
class TestBase extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a new bookmark.
|
|
|
|
*
|
2010-02-16 21:54:31 +00:00
|
|
|
* @param integer $user User ID the bookmark shall belong
|
|
|
|
* @param string $address Bookmark address to use
|
2010-02-16 21:57:03 +00:00
|
|
|
* @param integer $status Bookmark visibility
|
2010-02-20 11:08:52 +00:00
|
|
|
* @param array $tags Array of tags to attach. If "null" is given,
|
|
|
|
* it will automatically be "unittest"
|
2010-03-28 18:07:18 +00:00
|
|
|
* @param string $title Bookmark title
|
2011-03-23 07:23:36 +00:00
|
|
|
* @param string $date strtotime-compatible string
|
2009-10-25 19:32:48 +00:00
|
|
|
*
|
2009-10-25 15:31:31 +00:00
|
|
|
* @return integer ID of bookmark
|
2010-02-16 21:57:03 +00:00
|
|
|
*
|
|
|
|
* @see SemanticScuttle_Service_Bookmark::addBookmark()
|
2009-10-25 15:31:31 +00:00
|
|
|
*/
|
2010-02-16 21:57:03 +00:00
|
|
|
protected function addBookmark(
|
2010-02-20 11:08:52 +00:00
|
|
|
$user = null, $address = null, $status = 0,
|
2011-03-23 07:23:36 +00:00
|
|
|
$tags = null, $title = null, $date = null
|
2010-02-16 21:57:03 +00:00
|
|
|
) {
|
2009-10-25 19:32:48 +00:00
|
|
|
if ($user === null) {
|
|
|
|
$user = $this->addUser();
|
|
|
|
}
|
2010-02-20 11:08:52 +00:00
|
|
|
if ($tags === null) {
|
|
|
|
$tags = array('unittest');
|
|
|
|
}
|
2009-10-25 19:32:48 +00:00
|
|
|
|
|
|
|
$bs = SemanticScuttle_Service_Factory::get('Bookmark');
|
2009-10-25 15:31:31 +00:00
|
|
|
$rand = rand();
|
2010-02-16 21:54:31 +00:00
|
|
|
|
|
|
|
if ($address === null) {
|
|
|
|
$address = 'http://example.org/' . $rand;
|
|
|
|
}
|
2010-03-28 18:07:18 +00:00
|
|
|
if ($title === null) {
|
|
|
|
$title = 'unittest bookmark #' . $rand;
|
|
|
|
}
|
2010-02-16 21:54:31 +00:00
|
|
|
|
2009-10-25 19:32:48 +00:00
|
|
|
$bid = $bs->addBookmark(
|
2010-02-16 21:54:31 +00:00
|
|
|
$address,
|
2010-03-28 18:07:18 +00:00
|
|
|
$title,
|
2009-10-25 15:31:31 +00:00
|
|
|
'description',
|
|
|
|
null,
|
2010-02-16 21:57:03 +00:00
|
|
|
$status,
|
2010-02-20 11:08:52 +00:00
|
|
|
$tags,
|
2011-03-23 07:23:36 +00:00
|
|
|
null, $date, false, false,
|
2009-10-25 19:32:48 +00:00
|
|
|
$user
|
2009-10-25 15:31:31 +00:00
|
|
|
);
|
|
|
|
return $bid;
|
|
|
|
}
|
|
|
|
|
2009-10-25 19:32:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new user in the database.
|
|
|
|
*
|
2011-06-27 17:39:38 +00:00
|
|
|
* @param string $username Username, may be null
|
|
|
|
* @param string $password Password, may be null
|
2011-06-27 17:16:33 +00:00
|
|
|
* @param mixed $privateKey String private key or boolean true to generate one
|
2010-03-28 18:04:53 +00:00
|
|
|
*
|
2009-10-25 19:32:48 +00:00
|
|
|
* @return integer ID of user
|
2011-03-26 14:09:22 +00:00
|
|
|
*
|
|
|
|
* @uses addUserData()
|
2009-10-25 19:32:48 +00:00
|
|
|
*/
|
2011-06-27 17:16:33 +00:00
|
|
|
protected function addUser(
|
|
|
|
$username = null, $password = null, $privateKey = null
|
|
|
|
) {
|
|
|
|
return reset($this->addUserData($username, $password, $privateKey));
|
2011-03-26 14:09:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new user in the database and returns id, username and password.
|
|
|
|
*
|
2011-06-27 17:39:38 +00:00
|
|
|
* @param string $username Username, may be null
|
|
|
|
* @param string $password Password, may be null
|
2011-06-27 17:16:33 +00:00
|
|
|
* @param mixed $privateKey String private key or boolean true to generate one
|
2011-03-26 14:09:22 +00:00
|
|
|
*
|
2011-06-27 17:16:33 +00:00
|
|
|
* @return array ID of user, Name of user, password of user, privatekey
|
2011-03-26 14:09:22 +00:00
|
|
|
*/
|
2011-06-27 17:16:33 +00:00
|
|
|
protected function addUserData(
|
|
|
|
$username = null, $password = null, $privateKey = null
|
|
|
|
) {
|
2009-10-25 19:32:48 +00:00
|
|
|
$us = SemanticScuttle_Service_Factory::get('User');
|
|
|
|
$rand = rand();
|
2010-03-28 18:04:53 +00:00
|
|
|
|
|
|
|
if ($username === null) {
|
|
|
|
$username = 'unittestuser-' . $rand;
|
|
|
|
}
|
|
|
|
if ($password === null) {
|
|
|
|
$password = $rand;
|
|
|
|
}
|
2011-06-27 17:16:33 +00:00
|
|
|
if ($privateKey === true) {
|
|
|
|
$privateKey = $this->us->getNewPrivateKey();
|
|
|
|
}
|
2010-03-28 18:04:53 +00:00
|
|
|
|
2009-10-25 19:32:48 +00:00
|
|
|
$uid = $us->addUser(
|
2010-03-28 18:04:53 +00:00
|
|
|
$username,
|
|
|
|
$password,
|
2011-05-13 18:26:51 +00:00
|
|
|
'unittest-' . $rand . '@example.org',
|
2011-06-27 17:16:33 +00:00
|
|
|
$privateKey
|
2009-10-25 19:32:48 +00:00
|
|
|
);
|
2011-06-27 17:16:33 +00:00
|
|
|
return array($uid, $username, $password, $privateKey);
|
2009-10-25 19:32:48 +00:00
|
|
|
}
|
|
|
|
|
2010-10-08 15:43:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2009-10-25 15:31:31 +00:00
|
|
|
}
|
|
|
|
|
2011-05-13 18:26:51 +00:00
|
|
|
?>
|