From c34669b7f2a204510540083fe330f6d47db2b173 Mon Sep 17 00:00:00 2001 From: pedro-mendonca Date: Mon, 29 Jul 2024 15:40:47 +0100 Subject: [PATCH] Add tests --- bin/install-wp-tests.sh | 22 ++++++++- phpcs.xml.dist | 1 + phpunit.xml.dist | 3 ++ tests/phpunit/bootstrap.php | 57 +++++++++++++++++++---- tests/phpunit/includes/test-toolbox.php | 62 +++++++++++++++++++++++++ 5 files changed, 133 insertions(+), 12 deletions(-) create mode 100644 tests/phpunit/includes/test-toolbox.php diff --git a/bin/install-wp-tests.sh b/bin/install-wp-tests.sh index 4c8c9f5..1c6a4c6 100644 --- a/bin/install-wp-tests.sh +++ b/bin/install-wp-tests.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash if [ $# -lt 3 ]; then - echo "usage: $0 [db-host] [wp-version] [skip-database-creation]" + echo "usage: $0 [db-host] [wp-version] [gp-version] [skip-database-creation]" exit 1 fi @@ -10,12 +10,14 @@ DB_USER=$2 DB_PASS=$3 DB_HOST=${4-localhost} WP_VERSION=${5-latest} -SKIP_DB_CREATE=${6-false} +GP_VERSION=${6-latest} +SKIP_DB_CREATE=${7-false} TMPDIR=${TMPDIR-/tmp} TMPDIR=$(echo $TMPDIR | sed -e "s/\/$//") WP_TESTS_DIR=${WP_TESTS_DIR-$TMPDIR/wordpress-tests-lib} WP_CORE_DIR=${WP_CORE_DIR-$TMPDIR/wordpress} +GP_TESTS_DIR="$WP_CORE_DIR/wp-content/plugins/glotpress/tests/phpunit" download() { if [ `which curl` ]; then @@ -51,6 +53,7 @@ else fi WP_TESTS_TAG="tags/$LATEST_VERSION" fi + set -ex install_wp() { @@ -176,6 +179,21 @@ install_db() { fi } +install_gp() { + if [[ $GP_VERSION == 'nightly' || $GP_VERSION == 'develop' || $GP_VERSION == 'trunk' ]]; then + local BRANCH_NAME='develop' + elif [[ $GP_VERSION == 'latest' ]]; then + local BRANCH_NAME='stable' + else + local BRANCH_NAME="$GP_VERSION" + fi + + # Set up GlotPress + git clone --branch "$BRANCH_NAME" --single-branch -q https://github.com/GlotPress/GlotPress.git "$WP_CORE_DIR/wp-content/plugins/glotpress" +} + + install_wp install_test_suite install_db +install_gp diff --git a/phpcs.xml.dist b/phpcs.xml.dist index a1fc8ed..da12cca 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -8,6 +8,7 @@ */node_modules/* */vendor/* + /tests/phpunit/bootstrap.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d85a5c8..af8eca5 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -21,5 +21,8 @@ ./includes gp-toolbox.php + + ./gp-templates + diff --git a/tests/phpunit/bootstrap.php b/tests/phpunit/bootstrap.php index a8d7326..acba3f4 100644 --- a/tests/phpunit/bootstrap.php +++ b/tests/phpunit/bootstrap.php @@ -5,34 +5,71 @@ * @package GP_Toolbox */ -$gp_toolbox_tests_dir = getenv( 'WP_TESTS_DIR' ); -if ( ! $gp_toolbox_tests_dir ) { - $gp_toolbox_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; +if ( ! defined( 'GP_TESTS_PERMALINK_STRUCTURE' ) ) { + define( 'GP_TESTS_PERMALINK_STRUCTURE', '/%postname%' ); +} + +$_tests_dir = getenv( 'WP_TESTS_DIR' ); +$_gp_tests_dir = getenv( 'GP_TESTS_DIR' ); + +if ( ! $_tests_dir ) { + $_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib'; +} + +if ( ! $_gp_tests_dir ) { + $_gp_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress/wp-content/plugins/glotpress/tests/phpunit'; } // Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file. -$gp_toolbox_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ); -if ( false !== $gp_toolbox_phpunit_polyfills_path ) { - define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $gp_toolbox_phpunit_polyfills_path ); // phpcs:ignore +$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ); +if ( false !== $_phpunit_polyfills_path ) { + define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path ); // phpcs:ignore } -if ( ! file_exists( "{$gp_toolbox_tests_dir}/includes/functions.php" ) ) { - echo "Could not find {$gp_toolbox_tests_dir}/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped +if ( ! file_exists( "{$_tests_dir}/includes/functions.php" ) ) { + echo "Could not find {$_tests_dir}/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped exit( 1 ); } +if ( ! file_exists( $_gp_tests_dir . '/bootstrap.php' ) ) { + die( "GlotPress test suite could not be found. Have you run bin/install-wp-tests.sh ?\n" ); +} + // Give access to tests_add_filter() function. -require_once "{$gp_toolbox_tests_dir}/includes/functions.php"; +require_once "{$_tests_dir}/includes/functions.php"; + /** * Manually load the plugin being tested. */ function gp_toolbox_manually_load_plugin() { + + // Load GlotPress. + require dirname( dirname( __DIR__ ) ) . '/../glotpress/glotpress.php'; + + // Load GP-Toolbox. require dirname( dirname( __DIR__ ) ) . '/gp-toolbox.php'; } tests_add_filter( 'muplugins_loaded', 'gp_toolbox_manually_load_plugin' ); +global $wp_tests_options; + +// So GlotPress doesn't bail early, see https://github.com/GlotPress/GlotPress-WP/blob/43bb5383e114835b09fc47c727d06e6d3ca8114e/glotpress.php#L142-L152. +$wp_tests_options['permalink_structure'] = '/%postname%'; + // Start up the WP testing environment. -require "{$gp_toolbox_tests_dir}/includes/bootstrap.php"; +require "{$_tests_dir}/includes/bootstrap.php"; + +require_once $_gp_tests_dir . '/lib/testcase.php'; +require_once $_gp_tests_dir . '/lib/testcase-route.php'; +require_once $_gp_tests_dir . '/lib/testcase-request.php'; + +/** + * Installs GlotPress tables. + */ +require_once ABSPATH . 'wp-admin/includes/upgrade.php'; +require_once $_gp_tests_dir . '/../../gp-includes/schema.php'; +require_once $_gp_tests_dir . '/../../gp-includes/install-upgrade.php'; +gp_upgrade_db(); diff --git a/tests/phpunit/includes/test-toolbox.php b/tests/phpunit/includes/test-toolbox.php new file mode 100644 index 0000000..7b58611 --- /dev/null +++ b/tests/phpunit/includes/test-toolbox.php @@ -0,0 +1,62 @@ + 'Profile', + '/glotpress/settings/' => 'Settings', + '/wp-login.php?action=logout' => 'Log out', + ); + + $new_nav_menu_items = Toolbox::nav_menu_items( $existent_items, 'main' ); + + // Don't add any menu items for non-administrators. + $this->assertSame( + $existent_items, + $new_nav_menu_items, + ); + + GP::$administrator_permission->create( + array( + 'user_id' => 1, + 'action' => 'admin', + ) + ); + + wp_set_current_user( '1' ); + + $new_nav_menu_items = Toolbox::nav_menu_items( $existent_items, 'side' ); + + // Add menu items for GlotPress administrators. + $this->assertSame( + array( + '/glotpress/tools' => 'Tools', + admin_url() => 'Dashboard', + '/glotpress/profile/username/' => 'Profile', + '/glotpress/settings/' => 'Settings', + '/wp-login.php?action=logout' => 'Log out', + ), + $new_nav_menu_items, + + ); + + } + + }