2010-03-28 18:06:07 +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
|
|
|
|
*/
|
|
|
|
|
2011-04-14 14:53:09 +00:00
|
|
|
require_once 'HTTP/Request2.php';
|
|
|
|
|
2010-03-28 18:06:07 +00:00
|
|
|
/**
|
|
|
|
* Base unittest class for web API tests.
|
|
|
|
*
|
|
|
|
* @category Bookmarking
|
|
|
|
* @package SemanticScuttle
|
|
|
|
* @author Christian Weiske <cweiske@cweiske.de>
|
|
|
|
* @license GPL http://www.gnu.org/licenses/gpl.html
|
|
|
|
* @link http://sourceforge.net/projects/semanticscuttle
|
|
|
|
*/
|
|
|
|
class TestBaseApi extends TestBase
|
|
|
|
{
|
2011-06-08 05:21:33 +00:00
|
|
|
/**
|
|
|
|
* Created from the configured host and the $urlPart.
|
|
|
|
* Should be used as base for all generated URLs
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-03-28 18:06:07 +00:00
|
|
|
protected $url;
|
2011-06-08 05:21:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Part of the URL behind the configured host.
|
|
|
|
* Needs to be overwritten in each derived test case class.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2010-03-28 18:06:07 +00:00
|
|
|
protected $urlPart = null;
|
|
|
|
|
2010-09-28 22:10:53 +00:00
|
|
|
/**
|
|
|
|
* @var SemanticScuttle_Service_User
|
|
|
|
*/
|
|
|
|
protected $us;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SemanticScuttle_Service_Bookmark
|
|
|
|
*/
|
|
|
|
protected $bs;
|
|
|
|
|
2010-03-28 18:06:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
if ($GLOBALS['unittestUrl'] === null) {
|
|
|
|
$this->assertTrue(false, 'Unittest URL not set in config');
|
|
|
|
}
|
|
|
|
if ($this->urlPart === null) {
|
|
|
|
$this->assertTrue(false, 'Set the urlPart variable');
|
|
|
|
}
|
|
|
|
$this->url = $GLOBALS['unittestUrl'] . $this->urlPart;
|
|
|
|
|
2011-04-06 07:50:42 +00:00
|
|
|
//clean up before test
|
2011-05-19 05:38:35 +00:00
|
|
|
$configFile = $GLOBALS['datadir'] . '/config.testing-tmp.php';
|
|
|
|
if (file_exists($configFile)) {
|
|
|
|
unlink($configFile);
|
2011-04-06 07:50:42 +00:00
|
|
|
}
|
|
|
|
|
2010-03-28 18:06:07 +00:00
|
|
|
$this->us = SemanticScuttle_Service_Factory::get('User');
|
2010-03-28 18:07:18 +00:00
|
|
|
$this->us->deleteAll();
|
2010-03-28 18:06:07 +00:00
|
|
|
$this->bs = SemanticScuttle_Service_Factory::get('Bookmark');
|
|
|
|
$this->bs->deleteAll();
|
2010-03-28 18:07:18 +00:00
|
|
|
$this->b2t = SemanticScuttle_Service_Factory::get('Bookmark2Tag');
|
|
|
|
$this->b2t->deleteAll();
|
2010-03-28 18:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-04-18 17:20:58 +00:00
|
|
|
* Creates and returns a HTTP GET request object.
|
2010-09-28 22:10:53 +00:00
|
|
|
* Uses $this->url plus $urlSuffix as request URL.
|
2010-03-28 18:06:07 +00:00
|
|
|
*
|
2010-03-28 18:07:18 +00:00
|
|
|
* @param string $urlSuffix Suffix for the URL
|
2010-03-28 18:06:07 +00:00
|
|
|
*
|
|
|
|
* @return HTTP_Request2 HTTP request object
|
2011-05-20 05:02:52 +00:00
|
|
|
*/
|
|
|
|
protected function getRequest($urlSuffix = null)
|
|
|
|
{
|
|
|
|
$url = $this->getTestUrl($urlSuffix);
|
|
|
|
$req = new HTTP_Request2($url, HTTP_Request2::METHOD_GET);
|
|
|
|
|
|
|
|
return $req;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an URL from $this->url plus $urlSuffix and an appended
|
|
|
|
* unittestMode=1 parameter.
|
|
|
|
*
|
|
|
|
* @param string $urlSuffix Suffix for the URL
|
|
|
|
*
|
|
|
|
* @return string URL
|
2010-09-28 22:10:53 +00:00
|
|
|
*
|
|
|
|
* @uses $url
|
2010-03-28 18:06:07 +00:00
|
|
|
*/
|
2011-05-20 05:02:52 +00:00
|
|
|
protected function getTestUrl($urlSuffix = null)
|
2010-03-28 18:06:07 +00:00
|
|
|
{
|
2011-05-20 04:58:28 +00:00
|
|
|
$url = $this->url . $urlSuffix;
|
|
|
|
if (strpos($urlSuffix, '?') !== false) {
|
|
|
|
$url .= '&unittestMode=1';
|
|
|
|
} else {
|
|
|
|
$url .= '?unittestMode=1';
|
|
|
|
}
|
2011-05-20 05:02:52 +00:00
|
|
|
return $url;
|
2010-03-28 18:06:07 +00:00
|
|
|
}
|
|
|
|
|
2010-03-28 18:07:18 +00:00
|
|
|
|
2011-05-12 17:09:30 +00:00
|
|
|
/**
|
|
|
|
* Completes an URL that's missing the protocol.
|
|
|
|
* Useful when re-using URLs extracted from HTML
|
|
|
|
*
|
|
|
|
* @param string $url Potentially partial URL
|
|
|
|
*
|
|
|
|
* @return string Full URL
|
|
|
|
*/
|
|
|
|
protected function completeUrl($url)
|
|
|
|
{
|
|
|
|
if (substr($url, 0, 2) == '//') {
|
|
|
|
$url = 'http:' . $url;
|
|
|
|
}
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-28 18:07:18 +00:00
|
|
|
|
|
|
|
/**
|
2011-04-18 17:20:58 +00:00
|
|
|
* Creates a user and a HTTP GET request object and prepares
|
2010-09-28 22:10:53 +00:00
|
|
|
* the request object with authentication details, so that
|
|
|
|
* the user is logged in.
|
2010-03-28 18:07:18 +00:00
|
|
|
*
|
2011-03-25 18:26:21 +00:00
|
|
|
* Useful for HTTP API methods only, cannot be used with
|
|
|
|
* "normal" HTML pages since they do not support HTTP auth.
|
2011-05-02 07:03:35 +00:00
|
|
|
*
|
2010-03-28 18:07:18 +00:00
|
|
|
* @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
|
2010-09-28 22:10:53 +00:00
|
|
|
*
|
|
|
|
* @uses getRequest()
|
2011-05-02 07:03:35 +00:00
|
|
|
* @see getLoggedInRequest()
|
2010-03-28 18:07:18 +00:00
|
|
|
*/
|
|
|
|
protected function getAuthRequest($urlSuffix = null, $auth = true)
|
|
|
|
{
|
|
|
|
$req = $this->getRequest($urlSuffix);
|
|
|
|
if (is_array($auth)) {
|
|
|
|
list($username, $password) = $auth;
|
|
|
|
} else {
|
|
|
|
$username = 'testuser';
|
|
|
|
$password = 'testpassword';
|
|
|
|
}
|
|
|
|
$uid = $this->addUser($username, $password);
|
|
|
|
$req->setAuth(
|
|
|
|
$username, $password,
|
|
|
|
HTTP_Request2::AUTH_BASIC
|
|
|
|
);
|
|
|
|
return array($req, $uid);
|
|
|
|
}
|
|
|
|
|
2011-04-06 06:43:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a user and a HTTP_Request2 object, does a normal login
|
2011-04-18 17:20:58 +00:00
|
|
|
* and prepares the cookies for the HTTP GET request object so that
|
2011-04-06 06:43:42 +00:00
|
|
|
* the user is seen as logged in when requesting any HTML page.
|
|
|
|
*
|
|
|
|
* Useful for testing HTML pages or ajax URLs.
|
|
|
|
*
|
2011-06-08 05:00:24 +00:00
|
|
|
* @param string $urlSuffix Suffix for the URL
|
|
|
|
* @param mixed $auth If user authentication is needed (true/false)
|
|
|
|
* or array with username and password
|
2011-06-04 04:29:04 +00:00
|
|
|
* @param boolean $privateKey True if to add user with private key
|
2011-04-06 06:43:42 +00:00
|
|
|
*
|
|
|
|
* @return array(HTTP_Request2, integer) HTTP request object and user id
|
|
|
|
*
|
|
|
|
* @uses getRequest()
|
|
|
|
*/
|
2011-06-08 05:00:24 +00:00
|
|
|
protected function getLoggedInRequest(
|
2011-06-27 17:39:38 +00:00
|
|
|
$urlSuffix = null, $auth = true, $privateKey = null
|
2011-06-08 05:00:24 +00:00
|
|
|
) {
|
2011-04-06 06:43:42 +00:00
|
|
|
if (is_array($auth)) {
|
|
|
|
list($username, $password) = $auth;
|
|
|
|
} else {
|
|
|
|
$username = 'testuser';
|
|
|
|
$password = 'testpassword';
|
|
|
|
}
|
2011-06-27 17:39:38 +00:00
|
|
|
$uid = $this->addUser($username, $password, $privateKey);
|
2011-04-06 06:43:42 +00:00
|
|
|
|
|
|
|
$req = new HTTP_Request2(
|
2011-05-20 05:02:52 +00:00
|
|
|
$GLOBALS['unittestUrl'] . '/login.php?unittestMode=1',
|
2011-04-06 06:43:42 +00:00
|
|
|
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);
|
2011-06-27 17:39:38 +00:00
|
|
|
$req->setCookieJar($cookies);
|
2011-04-06 06:43:42 +00:00
|
|
|
|
|
|
|
return array($req, $uid);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-28 06:34:12 +00:00
|
|
|
/**
|
|
|
|
* 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')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-04-06 06:43:42 +00:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2011-06-04 04:29:04 +00:00
|
|
|
$str = '<' . "?php\n";
|
2011-04-06 06:43:42 +00:00
|
|
|
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',
|
2011-05-19 05:38:35 +00:00
|
|
|
file_put_contents($GLOBALS['datadir'] . '/config.testing-tmp.php', $str),
|
2011-04-06 06:43:42 +00:00
|
|
|
'Writing config.unittest.php failed'
|
|
|
|
);
|
|
|
|
}
|
2010-03-28 18:06:07 +00:00
|
|
|
}
|
2011-06-04 04:29:04 +00:00
|
|
|
?>
|