Issues (4868)

api/tests/LoggedInTest.php (4 issues)

1
<?php
2
/**
3
 * EGroupware Api: Test base class for when you need an Egroupware session for
4
 * the class or test
5
 *
6
 * @link http://www.stylite.de
7
 * @package api
8
 * @subpackage test
9
 * @author Nathan Gray
10
 * @copyright (c) 2016 Nathan Gray
11
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
12
 * @version $Id$
13
 */
14
15
namespace EGroupware\Api;
16
17
use PHPUnit\Framework\TestCase as TestCase;
18
use EGroupware\Api;
19
20
/**
21
 * Base class for tests, loads the egroupware environment
22
 *
23
 * It's not best practice to require the session for every test, but so much
24
 * is reliant on the session that it's just easier.  By "not best practice",
25
 * I mean this is pretty bad, but better than manual testing.
26
 *
27
 * Each test case (extending class) should get its own session, but session is
28
 * shared between tests inside.
29
 *
30
 * The login information is pulled from doc/phpunit.xml.  Run tests from the
31
 * command line using 'phpunit -c doc'
32
 */
33
abstract class LoggedInTest extends TestCase
34
{
35
	/**
36
	 * Start session once before each test case
37
	 */
38
	public static function setUpBeforeClass() : void
39
	{
40
		try
41
		{
42
			// These globals pulled from the test config (phpunit.xml)
43
			static::load_egw($GLOBALS['EGW_USER'],$GLOBALS['EGW_PASSWORD'], $GLOBALS['EGW_DOMAIN']);
44
45
			if($GLOBALS['egw']->db)
46
			{
47
				$GLOBALS['egw']->db->connect();
48
			}
49
			else
50
			{
51
				static::markTestSkipped('No $GLOBALS[egw]->db');
52
				die();
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
53
			}
54
55
			// Re-init config, since it doesn't get handled by loading Egw
56
			Api\Config::init_static();
57
			Api\Vfs::init_static();
58
		}
59
		catch(Exception $e)
60
		{
61
			static::markTestSkipped('Unable to connect to Egroupware - ' . $e->getMessage());
62
			return;
63
		}
64
	}
65
66
	protected function assertPreConditions() : void
67
	{
68
		// Do some checks to make sure things we expect are there
69
		$this->assertTrue(static::sanity_check(), 'Unable to connect to Egroupware - failed sanity check');
70
	}
71
72
	/**
73
	 * End session when done - every test class gets its own session
74
	 */
75
	public static function tearDownAfterClass() : void
76
	{
77
		if($GLOBALS['egw'])
78
		{
79
			if($GLOBALS['egw']->session)
80
			{
81
				$GLOBALS['egw']->session->destroy(
82
					$GLOBALS['egw']->session->sessionid,
83
					$GLOBALS['egw']->session->kp3
84
				);
85
			}
86
			if($GLOBALS['egw']->acl)
87
			{
88
				$GLOBALS['egw']->acl = null;
89
			}
90
			if($GLOBALS['egw']->accounts)
91
			{
92
				$GLOBALS['egw']->accounts = null;
93
			}
94
			if($GLOBALS['egw']->applications)
95
			{
96
				$GLOBALS['egw']->applications = null;
97
			}
98
			if($GLOBALS['egw']->db)
99
			{
100
				$GLOBALS['egw']->db->disconnect();
101
			}
102
			unset($GLOBALS['egw']);
103
		}
104
105
		Api\Session::egw_setcookie('sessionid');
106
		Api\Session::egw_setcookie('kp3');
107
		unset($GLOBALS['egw_info']);
108
		unset($GLOBALS['_SESSION']);
109
		$_SESSION = array();
110
	}
111
112
	/**
113
	* Start the eGW session, skips the test if there are problems connecting
114
	*
115
	* @param string $user
116
	* @param string $passwd
117
	* @param string $domain
118
	*/
119
	public static function load_egw($user,$passwd,$domain='default',$info=array())
120
	{
121
		$_REQUEST['domain'] = $domain;
122
		$GLOBALS['egw_login_data'] = array(
123
			'login'  => $user,
124
			'passwd' => $passwd,
125
			'passwd_type' => 'text',
126
		);
127
128
		if (ini_get('session.save_handler') == 'files' && !is_writable(ini_get('session.save_path')) && is_dir('/tmp') && is_writable('/tmp'))
129
		{
130
			ini_set('session.save_path','/tmp');	// regular users may have no rights to apache's session dir
131
		}
132
133
		if(!$info)
134
		{
135
			$info = array(
136
				'flags' => array(
137
					'currentapp' => 'api',
138
					'noheader' => true,
139
					'autocreate_session_callback' => __CLASS__ .'::create_session',
140
					'no_exception_handler' => 'cli',
141
					'noapi' => false,
142
				)
143
			);
144
		}
145
		$GLOBALS['egw_info'] = $info;
146
147
		$ob_level = ob_get_level();
148
		try
149
		{
150
			include(realpath(__DIR__ . '/../../header.inc.php'));
151
		}
152
		catch (Exception $e)
153
		{
154
			// Something went wrong setting up egw environment - don't try
155
			// to do the test
156
			static::markTestSkipped($e->getMessage());
157
			return;
158
		}
159
160
		require_once realpath(__DIR__.'/../src/loader/common.php');	// autoloader & check_load_extension
161
162
		// egw is normally created when a file is loaded using require_once
163
		if(empty($GLOBALS['egw']) || !is_a($GLOBALS['egw'], 'EGroupware\Api\Egw\Base'))
164
		{
165
			// From Api/src/loader.php
166
			$GLOBALS['egw_info']['user']['domain'] = Api\Session::search_instance(
167
				isset($_POST['login']) ? $_POST['login'] : (isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : $_SERVER['REMOTE_USER']),
168
				Api\Session::get_request('domain'),$GLOBALS['egw_info']['server']['default_domain'],
169
				array($_SERVER['HTTP_HOST'], $_SERVER['SERVER_NAME']),$GLOBALS['egw_domain']);
170
171
			$GLOBALS['egw_info']['server']['db_host'] = $GLOBALS['egw_domain'][$GLOBALS['egw_info']['user']['domain']]['db_host'];
172
			$GLOBALS['egw_info']['server']['db_port'] = $GLOBALS['egw_domain'][$GLOBALS['egw_info']['user']['domain']]['db_port'];
173
			$GLOBALS['egw_info']['server']['db_name'] = $GLOBALS['egw_domain'][$GLOBALS['egw_info']['user']['domain']]['db_name'];
174
			$GLOBALS['egw_info']['server']['db_user'] = $GLOBALS['egw_domain'][$GLOBALS['egw_info']['user']['domain']]['db_user'];
175
			$GLOBALS['egw_info']['server']['db_pass'] = $GLOBALS['egw_domain'][$GLOBALS['egw_info']['user']['domain']]['db_pass'];
176
			$GLOBALS['egw_info']['server']['db_type'] = $GLOBALS['egw_domain'][$GLOBALS['egw_info']['user']['domain']]['db_type'];
177
178
			$GLOBALS['egw'] = new Api\Egw(array_keys($GLOBALS['egw_domain']));
179
		}
180
181
		// load up the $GLOBALS['egw_info']['server'] array
182
		$GLOBALS['egw_info']['server'] += Config::read('phpgwapi');
183
		// Make sure user is properly set
184
		$GLOBALS['egw_info']['user'] = $GLOBALS['egw']->session->read_repositories();
185
186
		// Disable asyc while we test
187
		$GLOBALS['egw_info']['server']['asyncservice'] = 'off';
188
189
		while(ob_get_level() > $ob_level)
190
		{
191
			ob_end_flush();
192
		}
193
	}
194
195
196
	/**
197
	* callback to authenticate with the user/pw specified on the commandline
198
	*
199
	* @param array &$account account_info with keys 'login', 'passwd' and optional 'passwd_type'
200
	* @return boolean/string true if we allow the access and account is set, a sessionid or false otherwise
0 ignored issues
show
Documentation Bug introduced by
The doc comment boolean/string at position 0 could not be parsed: Unknown type name 'boolean/string' at position 0 in boolean/string.
Loading history...
201
	*/
202
	public static function create_session(&$account)
0 ignored issues
show
The parameter $account is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

202
	public static function create_session(/** @scrutinizer ignore-unused */ &$account)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
203
	{
204
		if (!($sessionid = $GLOBALS['egw']->session->create($GLOBALS['egw_login_data'])))
205
		{
206
			if(!($reason = $GLOBALS['egw']->session->reason) && $GLOBALS['egw']->session->account_id)
207
			{
208
				$GLOBALS['egw']->session->sessionid = 'CLI';
209
				return 'CLI';
210
			}
211
			die($reason ? $reason : "Wrong account or password - run tests with 'phpunit -c doc/phpunit.xml' or 'phpunit <test_dir> -c doc/phpunit.xml'\n\n");
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
212
		}
213
		unset($GLOBALS['egw_login_data']);
214
		return $sessionid;
215
	}
216
217
	/**
218
	 * Do some checks to make sure things we expect are there
219
	 */
220
	protected static function sanity_check()
221
	{
222
		// Check that the apps are loaded
223
		if(!array_key_exists('apps', $GLOBALS['egw_info']) || count($GLOBALS['egw_info']['apps']) == 0)
224
		{
225
			return false;
226
		}
227
228
		// Check that the user is loaded
229
		if(!array_key_exists('user', $GLOBALS['egw_info']) || !$GLOBALS['egw_info']['user']['account_id'])
230
		{
231
			return false;
232
		}
233
234
		return true;
235
	}
236
}