Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing PHPUnit tests to Travis CI #1540

Merged
merged 18 commits into from Aug 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 17 additions & 5 deletions core/api_token_api.php
Expand Up @@ -84,20 +84,32 @@ function api_token_hash( $p_token ) {
}

/**
* Ensure that the specified token name is unique to the user, otherwise,
* prompt the user with an error.
* Checks that the specified token name is unique to the user.
*
* @param string $p_token_name The token name.
* @param string $p_user_id The user id.
* @param string $p_user_id The user id.
*
* @return bool True if unique, False if token already exists
*/
function api_token_name_ensure_unique( $p_token_name, $p_user_id ) {
function api_token_name_is_unique( $p_token_name, $p_user_id ) {
db_param_push();
$t_query = 'SELECT * FROM {api_token} WHERE user_id=' . db_param() . ' AND name=' . db_param();
$t_result = db_query( $t_query, array( $p_user_id, $p_token_name ) );

$t_row = db_fetch_array( $t_result );

if ( $t_row ) {
return $t_row === false;
}

/**
* Ensure that the specified token name is unique to the user, otherwise,
* prompt the user with an error.
*
* @param string $p_token_name The token name.
* @param string $p_user_id The user id.
*/
function api_token_name_ensure_unique( $p_token_name, $p_user_id ) {
if ( !api_token_name_is_unique( $p_token_name, $p_user_id ) ) {
error_parameters( $p_token_name );
trigger_error( ERROR_API_TOKEN_NAME_NOT_UNIQUE, ERROR );
}
Expand Down
2 changes: 1 addition & 1 deletion core/tag_api.php
Expand Up @@ -499,7 +499,7 @@ function tag_get_name( $p_tag_id ) {
/**
* Return a tag row for the given name.
* @param string $p_name The tag name to retrieve from the database.
* @return array|boolean Tag row
* @return array|false Tag row
*/
function tag_get_by_name( $p_name ) {
db_param_push();
Expand Down
23 changes: 22 additions & 1 deletion scripts/travis_before_script.sh
Expand Up @@ -23,11 +23,24 @@ MANTIS_DB_NAME=bugtracker
MANTIS_BOOTSTRAP=tests/bootstrap.php
MANTIS_CONFIG=config/config_inc.php

TIMESTAMP=$(date "+%s")

SQL_CREATE_DB="CREATE DATABASE $MANTIS_DB_NAME;"
SQL_CREATE_PROJECT="INSERT INTO mantis_project_table
(name, inherit_global, description)
VALUES
('Test Project',true,'Travis-CI Test Project');"
SQL_CREATE_VERSIONS="INSERT INTO mantis_project_version_table
(project_id, version, description, released, obsolete, date_order)
VALUES
(1, '1.0.0', 'Obsolete version', true, true, $(($TIMESTAMP - 120))),
(1, '1.1.0', 'Released version', true, false, $(($TIMESTAMP - 60))),
(1, '2.0.0', 'Future version', false, false, $TIMESTAMP);"
SQL_CREATE_TAGS="INSERT INTO mantis_tag_table
(user_id, name, description, date_created, date_updated)
VALUES
(0, 'modern-ui', '', $TIMESTAMP, $TIMESTAMP),
(0, 'patch', '', $TIMESTAMP, $TIMESTAMP);"


# -----------------------------------------------------------------------------
Expand Down Expand Up @@ -142,16 +155,23 @@ curl --data "${query_string:1}" http://$HOSTNAME:$PORT/admin/install.php
# -----------------------------------------------------------------------------
step "Post-installation steps"

echo "Creating project"
echo "Creating project, versions and tags"
$DB_CMD "$SQL_CREATE_PROJECT" $DB_CMD_SCHEMA
$DB_CMD "$SQL_CREATE_VERSIONS" $DB_CMD_SCHEMA
$DB_CMD "$SQL_CREATE_TAGS" $DB_CMD_SCHEMA

echo "Creating API Token"
TOKEN=$($myphp tests/travis_create_api_token.php)

# enable SOAP tests
echo "Creating PHPUnit Bootstrap file"
cat <<-EOF >> $MANTIS_BOOTSTRAP
<?php
\$GLOBALS['MANTIS_TESTSUITE_SOAP_ENABLED'] = true;
\$GLOBALS['MANTIS_TESTSUITE_SOAP_HOST'] = 'http://$HOSTNAME:$PORT/api/soap/mantisconnect.php?wsdl';
\$GLOBALS['MANTIS_TESTSUITE_REST_ENABLED'] = true;
\$GLOBALS['MANTIS_TESTSUITE_REST_HOST'] = 'http://$HOSTNAME:$PORT/api/rest/';
\$GLOBALS['MANTIS_TESTSUITE_API_TOKEN'] = '$TOKEN';
EOF

echo "Adding custom configuration options"
Expand All @@ -162,6 +182,7 @@ cat <<-EOF >> $MANTIS_CONFIG
\$g_allow_no_category = ON;
\$g_due_date_update_threshold = DEVELOPER;
\$g_due_date_view_threshold = DEVELOPER;
\$g_enable_product_build = ON;
\$g_enable_project_documentation = ON;
\$g_time_tracking_enabled = ON;
EOF
Expand Down
2 changes: 2 additions & 0 deletions tests/AllTests.php
Expand Up @@ -29,6 +29,7 @@

require_once 'Mantis/AllTests.php';
require_once 'soap/AllTests.php';
require_once 'rest/AllTests.php';

/**
* All tests
Expand All @@ -44,6 +45,7 @@ public static function suite() {

$t_suite->addTest( MantisAllTests::suite() );
$t_suite->addTest( SoapAllTests::suite() );
$t_suite->addTest( RestAllTests::suite() );

return $t_suite;
}
Expand Down
31 changes: 30 additions & 1 deletion tests/rest/RestBase.php
Expand Up @@ -23,9 +23,16 @@
* @link http://www.mantisbt.org
*/

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

# MantisBT Core API
require_mantis_core();

require_once( __DIR__ . '/../../vendor/autoload.php' );
require_once ( __DIR__ . '/../../core/constant_inc.php' );


/**
* Base class for REST API test cases
*
Expand Down Expand Up @@ -128,7 +135,7 @@ protected function delete( $p_relative_path, $p_query_string ) {

/**
* @param string $p_relative_path The relative path under `/api/rest/` e.g. `/issues`.
* @param string $p_payload The payload object, it will be json encoded before sending.
* @param mixed $p_payload The payload object, it will be json encoded before sending.
* @return mixed|string The response object.
*/
protected function post( $p_relative_path, $p_payload ) {
Expand Down Expand Up @@ -183,4 +190,26 @@ protected function getIssueToAdd( $p_test_case ) {
protected function deleteAfterRun( $p_issue_id ) {
$this->issueIdsToDelete[] = $p_issue_id;
}

/**
* 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.
*
* @todo Copied from MantisCoreBase class - see if code duplication can be avoided
*/
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
);
}
}