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

feat(testing): Add co-authors-plus to unit testing #203

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ install_wp() {
download https://raw.githubusercontent.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
}

# Install plugins needed for the test suite.
install_contrib_plugins() {
# Also see set_up_contrib_plugins() in ./bootstrap.php in this repo for how to activate plugins in the test suite.
wget -nv -O /tmp/co-authors-plus.zip https://downloads.wordpress.org/plugin/co-authors-plus.zip
unzip -q -o /tmp/co-authors-plus.zip -d $WP_CORE_DIR/wp-content/plugins/
}

install_test_suite() {
# portable in-place argument for both GNU sed and Mac OSX sed
if [[ $(uname -s) == 'Darwin' ]]; then
Expand Down Expand Up @@ -176,5 +183,6 @@ install_db() {
}

install_wp
install_contrib_plugins
install_test_suite
install_db
43 changes: 43 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,46 @@
// Give access to tests_add_filter() function.
require_once "{$newspack_network_hub_test_dir}/includes/functions.php";

/**
* Include and activate the 3rd-party plugins we use.
*
* See install_contrib_plugins in bin/install-wp-tests.sh for the list of plugins we download.
*/
function set_up_contrib_plugins(): void {
// Add plugins to this array to have them loaded by the test suite.
// Note that it would load and activate the plugin in all tests, so use sparingly.
$plugins_active_in_tests = [
// The CAP plugin is used so much in our code that it is hard to test without it.
'co-authors-plus' => 'co-authors-plus/co-authors-plus.php',
];

$wordpress_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress';
foreach ( $plugins_active_in_tests as $plugin ) {
$plugin_file = "$wordpress_dir/wp-content/plugins/$plugin";

if ( ! file_exists( $plugin_file ) ) {
// Be very, very specific about what is wrong to save hours of error finding.
// phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.Security.EscapeOutput.HeredocOutputNotEscaped
echo <<<MESSAGE
----------------------------------------------
👋
This is an error message from your friendly function set_up_contrib_plugins() in bootstrap.php:
Could not find the plugin file: $plugin_file
Make sure the plugin gets downloaded in the install_contrib_plugins() function in bin/install-wp-tests.sh.
Make sure you have run ./bin/install-wp-tests.sh.
Sometimes deleting the WordPress test directory $wordpress_dir and then running ./bin/install-wp-tests.sh again helps.\n
----------------------------------------------
MESSAGE;
// phpcs:enable WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.Security.EscapeOutput.HeredocOutputNotEscaped
exit( 1 );
}
tests_add_filter( 'muplugins_loaded', fn() => require $plugin_file );
}

// This will activate the plugins in the test suite.
$GLOBALS['wp_tests_options']['active_plugins'] = $plugins_active_in_tests;
}

/**
* Manually load the plugin being tested.
*/
Expand All @@ -30,6 +70,9 @@ function newspack_network_hub_manually_load_plugin() {

tests_add_filter( 'muplugins_loaded', 'newspack_network_hub_manually_load_plugin' );

// Include and "activate" plugins needed.
set_up_contrib_plugins();

require_once __DIR__ . '/../vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';

// Start up the WP testing environment.
Expand Down