Skip to content

Commit

Permalink
PHPUnit tests: new MantisCoreBase class
Browse files Browse the repository at this point in the history
To be used as a base, inherited by MantisBT core API test units.

Provides a couple of helper methods
- dbConnect() (re)opens DB connection, as PHPUnit appears to kill it
  after each test case execution. This allows test cases that need the
  DB to reopen it easily.
- login() performs a "script login" based on the configured test suite
  user (as defined in bootstrap file), falls back to anonymous user if
  not defined, and asserts that user is successfully logged in.
  • Loading branch information
dregad committed Jun 14, 2019
1 parent 9c66d39 commit dc37aa0
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 33 deletions.
7 changes: 2 additions & 5 deletions tests/Mantis/ConfigParserTest.php
Expand Up @@ -26,10 +26,7 @@
/**
* Includes
*/
require_once dirname( dirname( __FILE__ ) ) . '/TestConfig.php';

# Mantis Core required for class autoloader and constants
require_mantis_core();
require_once 'MantisCoreBase.php';

use PHPUnit_Framework_Constraint_IsType as PHPUnit_Type;

Expand All @@ -44,7 +41,7 @@
* @package Tests
* @subpackage ConfigParser
*/
class MantisConfigParserTest extends PHPUnit_Framework_TestCase {
class MantisConfigParserTest extends MantisCoreBase {

/**
* Test with empty string or null
Expand Down
6 changes: 3 additions & 3 deletions tests/Mantis/EnumTest.php
Expand Up @@ -26,16 +26,16 @@
/**
* Includes
*/
require_once dirname( dirname( __FILE__ ) ) . '/TestConfig.php';

require_once 'MantisCoreBase.php';
require_once 'MantisEnum.class.php';

/**
* Test cases for MantisEnum class.
* @package Tests
* @subpackage Enum
*/
class MantisEnumTest extends PHPUnit_Framework_TestCase {
class MantisEnumTest extends MantisCoreBase {

const ACCESS_LEVELS_ENUM = '10:viewer,25:reporter,40:updater,55:developer,70:manager,90:administrator';
const ACCESS_LEVELS_ENUM_EXTRA = '10:viewer,25:reporter,40:updater,55:developer,70:manager,90:administrator,100:missing';
const ACCESS_LEVELS_LOCALIZED_ENUM = '10:viewer_x,25:reporter_x,40:updater_x,55:developer_x,70:manager_x,90:administrator_x,95:extra_x';
Expand Down
7 changes: 2 additions & 5 deletions tests/Mantis/HelperTest.php
Expand Up @@ -24,17 +24,14 @@
*/

# Includes
require_once dirname( dirname( __FILE__ ) ) . '/TestConfig.php';

# MantisBT Core API
require_mantis_core();
require_once 'MantisCoreBase.php';

/**
* Helper API tests
* @package Tests
* @subpackage String
*/
class MantisHelperTest extends PHPUnit_Framework_TestCase {
class MantisHelperTest extends MantisCoreBase {

/**
* Custom assertion to evaluate whether the given exception was a
Expand Down
108 changes: 108 additions & 0 deletions tests/Mantis/MantisCoreBase.php
@@ -0,0 +1,108 @@
<?php
# MantisBT - A PHP based bugtracking system

# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.

/**
* MantisBT Prepare API test cases
*
* @package Tests
* @subpackage MantisCoreTests
* @copyright Copyright 2019 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/

# Includes
require_once dirname( dirname( __FILE__ ) ) . '/TestConfig.php';

# MantisBT Core API
require_mantis_core();

abstract class MantisCoreBase extends PHPUnit_Framework_TestCase {

/**
* @var string Username
*/
protected static $userName = 'administrator';

/**
* @var string Password
*/
protected static $password = 'root';

/**
* MantisCore tests setup
*/
public static function setUpBeforeClass() {
parent::setUpBeforeClass();

if( array_key_exists( 'MANTIS_TESTSUITE_USERNAME', $GLOBALS ) ) {
self::$userName = $GLOBALS['MANTIS_TESTSUITE_USERNAME'];
}

if( array_key_exists( 'MANTIS_TESTSUITE_PASSWORD', $GLOBALS ) ) {
self::$password = $GLOBALS['MANTIS_TESTSUITE_PASSWORD'];
}
}

/**
* Login as defined test suite user, with fall-back to anonymous user.
*
* Some tests require a logged-in user to function properly.
*
* @param boolean $p_anonymous true to login anonymously,
* false (default) to login as test suite user
*/
public static function login( $p_anonymous = false ) {
$t_msg = '';
if( !$p_anonymous ) {
$t_logged_in = auth_attempt_script_login( self::$userName, self::$password );
$t_user = sprintf( "'%s' or ", self::$userName );
} else {
$t_user = '';
$t_logged_in = false;
}
if( !$t_logged_in ) {
# Login failed, try again as anonymous user
$t_logged_in = auth_attempt_script_login( null );
$t_msg = sprintf(
'Login as %s failed - must be logged in to perform test',
$t_user . 'Anonymous User'
);
}
self::assertTrue( $t_logged_in, $t_msg );
}

/**
* Utility function to establish DB connection.
*
* PHPUnit seems to kill the connection after each test case execution;
* this allows individual test cases that need the DB to reopen it easily.
*/
public static function dbConnect() {
global $g_hostname, $g_db_username, $g_db_password, $g_database_name,
$g_use_persistent_connections;

db_connect(
config_get_global( 'dsn', false ),
$g_hostname,
$g_db_username,
$g_db_password,
$g_database_name,
$g_use_persistent_connections == ON
);
}

}

7 changes: 2 additions & 5 deletions tests/Mantis/MentionParsingTest.php
Expand Up @@ -26,18 +26,15 @@
/**
* Includes
*/
require_once dirname( dirname( __FILE__ ) ) . '/TestConfig.php';

require_mantis_core();

require_once 'MantisCoreBase.php';
require_api( 'mention_api.php' );

/**
* Test cases for parsing functionality in mention API.
* @package Tests
* @subpackage Mention
*/
class MentionParsingTest extends PHPUnit_Framework_TestCase {
class MentionParsingTest extends MantisCoreBase {

/**
* Tests user mentions
Expand Down
7 changes: 2 additions & 5 deletions tests/Mantis/PluginTest.php
Expand Up @@ -24,17 +24,14 @@
*/

# Includes
require_once dirname( dirname( __FILE__ ) ) . '/TestConfig.php';

# MantisBT Core API
require_mantis_core();
require_once 'MantisCoreBase.php';

/**
* Helper API tests
* @package Tests
* @subpackage String
*/
class MantisPluginTest extends PHPUnit_Framework_TestCase {
class MantisPluginTest extends MantisCoreBase {

const MANTISCORE = 'MantisCore';

Expand Down
7 changes: 2 additions & 5 deletions tests/Mantis/PrepareTest.php
Expand Up @@ -24,15 +24,12 @@
*/

# Includes
require_once dirname( dirname( __FILE__ ) ) . '/TestConfig.php';

# MantisBT Core API
require_mantis_core();
require_once 'MantisCoreBase.php';

/**
* MantisBT Prepare API test cases
*/
class MantisPrepareTest extends PHPUnit_Framework_TestCase {
class MantisPrepareTest extends MantisCoreBase {
const EMAIL = 'test@example.com';

/**
Expand Down
7 changes: 2 additions & 5 deletions tests/Mantis/StringTest.php
Expand Up @@ -24,10 +24,7 @@
*/

# Includes
require_once dirname( dirname( __FILE__ ) ) . '/TestConfig.php';

# MantisBT Core API
require_mantis_core();
require_once 'MantisCoreBase.php';

/**
* Mantis string handling test cases
Expand All @@ -36,7 +33,7 @@
* @copyright Copyright 2002 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/
class MantisStringTest extends PHPUnit_Framework_TestCase {
class MantisStringTest extends MantisCoreBase {

/**
* Tests string_sanitize_url()
Expand Down

0 comments on commit dc37aa0

Please sign in to comment.