2017-02-06 21:04:32 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* EGroupware Api: Test base class for when you need an Egroupware session for
|
|
|
|
* the class or test
|
|
|
|
*
|
|
|
|
* @link http://www.stylite.de
|
|
|
|
* @package api
|
|
|
|
* @subpackage test
|
|
|
|
* @author Nathan Gray
|
|
|
|
* @copyright (c) 2016 Nathan Gray
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace EGroupware\Api;
|
|
|
|
|
2017-08-18 11:45:10 +02:00
|
|
|
use PHPUnit\Framework\TestCase as TestCase;
|
2017-02-06 21:04:32 +01:00
|
|
|
use EGroupware\Api;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for tests, loads the egroupware environment
|
|
|
|
*
|
|
|
|
* It's not best practice to require the session for every test, but so much
|
|
|
|
* is reliant on the session that it's just easier. By "not best practice",
|
|
|
|
* I mean this is pretty bad, but better than manual testing.
|
|
|
|
*
|
|
|
|
* Each test case (extending class) should get its own session, but session is
|
|
|
|
* shared between tests inside.
|
|
|
|
*
|
|
|
|
* The login information is pulled from doc/phpunit.xml. Run tests from the
|
|
|
|
* command line using 'phpunit -c doc'
|
|
|
|
*/
|
|
|
|
abstract class LoggedInTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Start session once before each test case
|
|
|
|
*/
|
|
|
|
public static function setUpBeforeClass()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// These globals pulled from the test config (phpunit.xml)
|
|
|
|
static::load_egw($GLOBALS['EGW_USER'],$GLOBALS['EGW_PASSWORD'], $GLOBALS['EGW_DOMAIN']);
|
|
|
|
|
|
|
|
if($GLOBALS['egw']->db)
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->db->connect();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static::markTestSkipped('No $GLOBALS[egw]->db');
|
|
|
|
die();
|
|
|
|
}
|
2017-02-08 00:02:06 +01:00
|
|
|
|
|
|
|
// Re-init config, since it doesn't get handled by loading Egw
|
|
|
|
Api\Config::init_static();
|
2018-02-15 17:33:01 +01:00
|
|
|
Api\Vfs::init_static();
|
2017-02-06 21:04:32 +01:00
|
|
|
}
|
|
|
|
catch(Exception $e)
|
|
|
|
{
|
2017-02-08 00:02:06 +01:00
|
|
|
static::markTestSkipped('Unable to connect to Egroupware - ' . $e->getMessage());
|
|
|
|
return;
|
2017-02-07 17:11:37 +01:00
|
|
|
}
|
2017-10-18 11:35:49 +02:00
|
|
|
}
|
2017-02-07 17:11:37 +01:00
|
|
|
|
2017-10-18 11:35:49 +02:00
|
|
|
public function assertPreConditions()
|
|
|
|
{
|
2017-02-07 17:11:37 +01:00
|
|
|
// Do some checks to make sure things we expect are there
|
2017-10-18 11:35:49 +02:00
|
|
|
$this->assertTrue(static::sanity_check(), 'Unable to connect to Egroupware - failed sanity check');
|
2017-02-06 21:04:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-18 11:35:49 +02:00
|
|
|
* End session when done - every test class gets its own session
|
2017-02-06 21:04:32 +01:00
|
|
|
*/
|
|
|
|
public static function tearDownAfterClass()
|
|
|
|
{
|
|
|
|
if($GLOBALS['egw'])
|
|
|
|
{
|
|
|
|
if($GLOBALS['egw']->session)
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->session->destroy(
|
|
|
|
$GLOBALS['egw']->session->sessionid,
|
|
|
|
$GLOBALS['egw']->session->kp3
|
|
|
|
);
|
|
|
|
}
|
2017-04-04 18:31:30 +02:00
|
|
|
if($GLOBALS['egw']->acl)
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->acl = null;
|
|
|
|
}
|
2017-02-06 21:04:32 +01:00
|
|
|
if($GLOBALS['egw']->accounts)
|
|
|
|
{
|
2017-04-04 18:31:30 +02:00
|
|
|
$GLOBALS['egw']->accounts = null;
|
2017-02-06 21:04:32 +01:00
|
|
|
}
|
|
|
|
if($GLOBALS['egw']->applications)
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->applications = null;
|
|
|
|
}
|
2018-09-05 17:53:57 +02:00
|
|
|
if($GLOBALS['egw']->db)
|
|
|
|
{
|
|
|
|
$GLOBALS['egw']->db->disconnect();
|
|
|
|
}
|
2017-02-06 21:04:32 +01:00
|
|
|
unset($GLOBALS['egw']);
|
|
|
|
}
|
2018-02-15 17:33:01 +01:00
|
|
|
|
|
|
|
Api\Session::egw_setcookie('sessionid');
|
|
|
|
Api\Session::egw_setcookie('kp3');
|
2017-02-06 21:04:32 +01:00
|
|
|
unset($GLOBALS['egw_info']);
|
|
|
|
unset($GLOBALS['_SESSION']);
|
|
|
|
$_SESSION = array();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-08 00:02:06 +01:00
|
|
|
* Start the eGW session, skips the test if there are problems connecting
|
2017-02-06 21:04:32 +01:00
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @param string $passwd
|
|
|
|
* @param string $domain
|
|
|
|
*/
|
2018-02-07 18:22:53 +01:00
|
|
|
public static function load_egw($user,$passwd,$domain='default',$info=array())
|
2017-02-06 21:04:32 +01:00
|
|
|
{
|
|
|
|
$_REQUEST['domain'] = $domain;
|
|
|
|
$GLOBALS['egw_login_data'] = array(
|
|
|
|
'login' => $user,
|
|
|
|
'passwd' => $passwd,
|
|
|
|
'passwd_type' => 'text',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (ini_get('session.save_handler') == 'files' && !is_writable(ini_get('session.save_path')) && is_dir('/tmp') && is_writable('/tmp'))
|
|
|
|
{
|
|
|
|
ini_set('session.save_path','/tmp'); // regular users may have no rights to apache's session dir
|
|
|
|
}
|
|
|
|
|
2018-02-07 18:22:53 +01:00
|
|
|
if(!$info)
|
|
|
|
{
|
|
|
|
$info = array(
|
|
|
|
'flags' => array(
|
|
|
|
'currentapp' => 'api',
|
|
|
|
'noheader' => true,
|
|
|
|
'autocreate_session_callback' => __CLASS__ .'::create_session',
|
|
|
|
'no_exception_handler' => 'cli',
|
|
|
|
'noapi' => false,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$GLOBALS['egw_info'] = $info;
|
2017-02-06 21:04:32 +01:00
|
|
|
|
2018-02-15 17:33:01 +01:00
|
|
|
$ob_level = ob_get_level();
|
2017-02-08 00:02:06 +01:00
|
|
|
try
|
|
|
|
{
|
2017-10-18 15:43:35 +02:00
|
|
|
include(realpath(__DIR__ . '/../../header.inc.php'));
|
2017-02-08 00:02:06 +01:00
|
|
|
}
|
|
|
|
catch (Exception $e)
|
|
|
|
{
|
|
|
|
// Something went wrong setting up egw environment - don't try
|
|
|
|
// to do the test
|
|
|
|
static::markTestSkipped($e->getMessage());
|
|
|
|
return;
|
|
|
|
}
|
2017-02-06 21:04:32 +01:00
|
|
|
|
2017-10-18 15:43:35 +02:00
|
|
|
require_once realpath(__DIR__.'/../src/loader/common.php'); // autoloader & check_load_extension
|
2017-02-06 21:04:32 +01:00
|
|
|
|
|
|
|
// egw is normally created when a file is loaded using require_once
|
2017-10-18 09:09:41 +02:00
|
|
|
if(empty($GLOBALS['egw']) || !is_a($GLOBALS['egw'], 'EGroupware\Api\Egw\Base'))
|
2017-02-06 21:04:32 +01:00
|
|
|
{
|
|
|
|
// From Api/src/loader.php
|
|
|
|
$GLOBALS['egw_info']['user']['domain'] = Api\Session::search_instance(
|
|
|
|
isset($_POST['login']) ? $_POST['login'] : (isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : $_SERVER['REMOTE_USER']),
|
|
|
|
Api\Session::get_request('domain'),$GLOBALS['egw_info']['server']['default_domain'],
|
|
|
|
array($_SERVER['HTTP_HOST'], $_SERVER['SERVER_NAME']),$GLOBALS['egw_domain']);
|
|
|
|
|
|
|
|
$GLOBALS['egw_info']['server']['db_host'] = $GLOBALS['egw_domain'][$GLOBALS['egw_info']['user']['domain']]['db_host'];
|
|
|
|
$GLOBALS['egw_info']['server']['db_port'] = $GLOBALS['egw_domain'][$GLOBALS['egw_info']['user']['domain']]['db_port'];
|
|
|
|
$GLOBALS['egw_info']['server']['db_name'] = $GLOBALS['egw_domain'][$GLOBALS['egw_info']['user']['domain']]['db_name'];
|
|
|
|
$GLOBALS['egw_info']['server']['db_user'] = $GLOBALS['egw_domain'][$GLOBALS['egw_info']['user']['domain']]['db_user'];
|
|
|
|
$GLOBALS['egw_info']['server']['db_pass'] = $GLOBALS['egw_domain'][$GLOBALS['egw_info']['user']['domain']]['db_pass'];
|
|
|
|
$GLOBALS['egw_info']['server']['db_type'] = $GLOBALS['egw_domain'][$GLOBALS['egw_info']['user']['domain']]['db_type'];
|
|
|
|
|
|
|
|
$GLOBALS['egw'] = new Api\Egw(array_keys($GLOBALS['egw_domain']));
|
|
|
|
}
|
2017-08-18 11:45:10 +02:00
|
|
|
|
2018-03-20 22:44:27 +01:00
|
|
|
// load up the $GLOBALS['egw_info']['server'] array
|
|
|
|
$GLOBALS['egw_info']['server'] += Config::read('phpgwapi');
|
2018-02-20 23:32:37 +01:00
|
|
|
// Make sure user is properly set
|
|
|
|
$GLOBALS['egw_info']['user'] = $GLOBALS['egw']->session->read_repositories();
|
|
|
|
|
2017-02-07 17:11:37 +01:00
|
|
|
// Disable asyc while we test
|
|
|
|
$GLOBALS['egw_info']['server']['asyncservice'] = 'off';
|
2018-02-15 17:33:01 +01:00
|
|
|
|
|
|
|
while(ob_get_level() > $ob_level)
|
|
|
|
{
|
2018-03-22 18:09:09 +01:00
|
|
|
ob_end_flush();
|
2018-02-15 17:33:01 +01:00
|
|
|
}
|
2017-02-06 21:04:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* callback to authenticate with the user/pw specified on the commandline
|
|
|
|
*
|
|
|
|
* @param array &$account account_info with keys 'login', 'passwd' and optional 'passwd_type'
|
|
|
|
* @return boolean/string true if we allow the access and account is set, a sessionid or false otherwise
|
|
|
|
*/
|
|
|
|
public static function create_session(&$account)
|
|
|
|
{
|
|
|
|
if (!($sessionid = $GLOBALS['egw']->session->create($GLOBALS['egw_login_data'])))
|
|
|
|
{
|
|
|
|
die("Wrong account or password - run tests with 'phpunit -c doc/phpunit.xml' or 'phpunit <test_dir> -c doc/phpunit.xml'\n\n");
|
|
|
|
}
|
|
|
|
unset($GLOBALS['egw_login_data']);
|
|
|
|
return $sessionid;
|
|
|
|
}
|
2017-02-07 17:11:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Do some checks to make sure things we expect are there
|
|
|
|
*/
|
|
|
|
protected static function sanity_check()
|
|
|
|
{
|
|
|
|
// Check that the apps are loaded
|
|
|
|
if(!array_key_exists('apps', $GLOBALS['egw_info']) || count($GLOBALS['egw_info']['apps']) == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the user is loaded
|
|
|
|
if(!array_key_exists('user', $GLOBALS['egw_info']) || !$GLOBALS['egw_info']['user']['account_id'])
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-06 21:04:32 +01:00
|
|
|
}
|