cherry-pick:

add new feature: allow unit test mode enabling via HTTP GET parameter
This commit is contained in:
Christian Weiske 2011-04-06 08:42:26 +02:00
parent 1a2b37cc34
commit c81566f5d8
3 changed files with 112 additions and 1 deletions

View file

@ -752,4 +752,12 @@ $authEmailSuffix = null;
*/ */
$unittestUrl = null; $unittestUrl = null;
/**
* Allow "unittestMode=1" in URLs.
* Should only be enabled on development systems
*
* @var boolean
*/
$allowUnittestMode = false;
?> ?>

View file

@ -39,6 +39,20 @@ set_include_path(
require_once $datadir . '/config.default.php'; require_once $datadir . '/config.default.php';
require_once $datadir . '/config.php'; require_once $datadir . '/config.php';
if (isset($_GET['unittestMode']) && $_GET['unittestMode'] == 1
) {
if ($allowUnittestMode !== true) {
header('HTTP/1.0 400 Bad Request');
die("Unittestmode is not allowed\n");
}
$unittestConfigFile = $datadir . '/config.unittest.php';
if (file_exists($unittestConfigFile)) {
require_once $unittestConfigFile;
}
define('HTTP_UNIT_TEST_MODE', true);
define('UNIT_TEST_MODE', true);
}
if (defined('UNIT_TEST_MODE')) { if (defined('UNIT_TEST_MODE')) {
//make local config vars global - needed for unit tests //make local config vars global - needed for unit tests
//run with phpunit //run with phpunit
@ -117,7 +131,7 @@ $tplVars['currentUser'] = $currentUser;
$tplVars['userservice'] = $userservice; $tplVars['userservice'] = $userservice;
// 6 // Force UTF-8 behaviour for server (cannot be moved into top.inc.php which is not included into every file) // 6 // Force UTF-8 behaviour for server (cannot be moved into top.inc.php which is not included into every file)
if (!defined('UNIT_TEST_MODE')) { if (!defined('UNIT_TEST_MODE') || defined('HTTP_UNIT_TEST_MODE')) {
//API files define that, so we need a way to support both of them //API files define that, so we need a way to support both of them
if (!isset($httpContentType)) { if (!isset($httpContentType)) {
$httpContentType = 'text/html'; $httpContentType = 'text/html';

View file

@ -22,6 +22,18 @@
*/ */
class TestBase extends PHPUnit_Framework_TestCase class TestBase extends PHPUnit_Framework_TestCase
{ {
/**
* Clean up after test
*/
public function tearDown()
{
if (file_exists($GLOBALS['datadir'] . '/config.unittest.php')) {
unlink($GLOBALS['datadir'] . '/config.unittest.php');
}
}
/** /**
* Create a new bookmark. * Create a new bookmark.
* *
@ -79,8 +91,25 @@ class TestBase extends PHPUnit_Framework_TestCase
* @param string $password Password * @param string $password Password
* *
* @return integer ID of user * @return integer ID of user
*
* @uses addUserData()
*/ */
protected function addUser($username = null, $password = null) 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'); $us = SemanticScuttle_Service_Factory::get('User');
$rand = rand(); $rand = rand();
@ -97,9 +126,69 @@ class TestBase extends PHPUnit_Framework_TestCase
$password, $password,
'unittest-' . $rand . '@example.org' 'unittest-' . $rand . '@example.org'
); );
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; return $uid;
} }
/**
* 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'
);
}
} }
?> ?>