<?php
/**
 * Bootstrap the plugin unit testing environment.
 *
 * Support for:
 *
 * 1. `WP_DEVELOP_DIR` and `WP_TESTS_DIR` environment variables
 * 2. Plugin installed inside of WordPress.org developer checkout
 * 3. Tests checked out to /tmp
 *
 * @package CMB2
 */

define('WP_DISABLE_FATAL_ERROR_HANDLER', true);

if ( false !== getenv( 'WP_DEVELOP_DIR' ) ) {
	$GLOBALS['test_root'] = getenv( 'WP_DEVELOP_DIR' ). '/tests/phpunit';
} elseif ( false !== getenv( 'WP_TESTS_DIR' ) ) {
	$GLOBALS['test_root'] = getenv( 'WP_TESTS_DIR' );
} elseif ( file_exists( '../../../../tests/phpunit/includes/bootstrap.php' ) ) {
	$GLOBALS['test_root'] = '../../../../tests/phpunit';
} elseif ( file_exists( '/tmp/wordpress-tests-lib/includes/bootstrap.php' ) ) {
	$GLOBALS['test_root'] = '/tmp/wordpress-tests-lib';
}

// Ensure we have a valid test root directory
if ( empty( $GLOBALS['test_root'] ) || ! file_exists( $GLOBALS['test_root'] . '/includes/bootstrap.php' ) ) {
	echo "Error in bootstrap script: WordPress test suite not found.\n";
	echo "Please run: bash tests/bin/install-wp-tests.sh <db_name> <db_user> <db_pass> [db_host] [wp_version]\n";
	exit( 1 );
}

$plugin = dirname( dirname( __FILE__ ) );
define( 'CMB2_TEST_PLUGIN', $plugin );
define( 'CMB2_WP_CONTENT', dirname( dirname( $plugin ) ) );

require_once $plugin . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
require_once $GLOBALS['test_root'] . '/includes/functions.php';

/**
 * Activates the CMB2 plugin in WordPress so it can be tested.
 *
 */
function _tests_cmb2_manually_load_plugin() {
	global $test_root;

	define( 'CMB2_TESTDATA', dirname( __FILE__ ) . '/data' );
	if ( ! defined( 'WP_ADMIN' ) ) {
		define( 'WP_ADMIN', true );
	}

	if (
		( $rest_api = '/tmp/wordpress/wp-content/plugins/rest-api/plugin.php' )
		&& file_exists( $rest_api )
	) {
		require_once $rest_api;
	} elseif (
		( $rest_api = CMB2_WP_CONTENT . '/plugins/rest-api/plugin.php' )
		&& file_exists( $rest_api )
	) {
		require_once $rest_api;
	} elseif (
		( $rest_api = CMB2_WP_CONTENT . '/plugins/WP-API/plugin.php' )
		&& file_exists( $rest_api )
	) {
		require_once $rest_api;
	}

	require_once CMB2_TEST_PLUGIN . '/init.php';
}
tests_add_filter( 'muplugins_loaded', '_tests_cmb2_manually_load_plugin' );

require $GLOBALS['test_root'] . '/includes/bootstrap.php';

