Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pedro-mendonca committed Jul 29, 2024
1 parent a09bb9f commit c34669b
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 12 deletions.
22 changes: 20 additions & 2 deletions bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

if [ $# -lt 3 ]; then
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [skip-database-creation]"
echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version] [gp-version] [skip-database-creation]"
exit 1
fi

Expand All @@ -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
Expand Down Expand Up @@ -51,6 +53,7 @@ else
fi
WP_TESTS_TAG="tags/$LATEST_VERSION"
fi

set -ex

install_wp() {
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<!-- Exclude paths. -->
<exclude-pattern>*/node_modules/*</exclude-pattern>
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>/tests/phpunit/bootstrap.php</exclude-pattern>

<!-- Only scan PHP files. -->
<arg name="extensions" value="php"/>
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
<directory suffix=".php">./includes</directory>
<file>gp-toolbox.php</file>
</include>
<exclude>
<directory suffix=".php">./gp-templates</directory>
</exclude>
</coverage>
</phpunit>
57 changes: 47 additions & 10 deletions tests/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
62 changes: 62 additions & 0 deletions tests/phpunit/includes/test-toolbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

Check failure on line 1 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Class file names should be based on the class name with "class-" prepended. Expected class-test-toolbox.php, but found test-toolbox.php.
/**
* Class Toolbox Test.
*
* @package GP_Toolbox
*/

use GP_Toolbox\Toolbox;


/**

Check warning on line 11 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Found precision alignment of 1 spaces.
* Toolbox test case.

Check warning on line 12 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Found precision alignment of 1 spaces.
*/

Check warning on line 13 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Found precision alignment of 1 spaces.
class Test_Toolbox extends GP_UnitTestCase {

Check warning on line 14 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Found precision alignment of 1 spaces.

Check failure on line 14 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 0 tabs, found 1 spaces


/**

Check failure on line 17 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
* Test add Tools and Dashboard items do side menu.

Check failure on line 18 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
*/

Check failure on line 19 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
public function test_nav_menu_items() {

Check failure on line 20 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed

$existent_items = array(
'/glotpress/profile/username/' => 'Profile',
'/glotpress/settings/' => 'Settings',
'/wp-login.php?action=logout' => 'Log out',

Check failure on line 25 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
);

$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,

Check warning on line 38 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 1 space(s) between "'user_id'" and double arrow, but found 5.
'action' => 'admin',

Check warning on line 39 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 2 space(s) between "'action'" and double arrow, but found 6.
)
);

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',

Check warning on line 50 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 13 space(s) between "'/glotpress/tools'" and double arrow, but found 1.
admin_url() => 'Dashboard',

Check warning on line 51 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Array double arrow not aligned correctly; expected 20 space(s) between ")" and double arrow, but found 1.
'/glotpress/profile/username/' => 'Profile',
'/glotpress/settings/' => 'Settings',
'/wp-login.php?action=logout' => 'Log out',

Check failure on line 54 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed
),
$new_nav_menu_items,

Check failure on line 57 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Empty lines are not allowed in multi-line function calls
);

}

Check failure on line 60 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Tabs must be used to indent lines; spaces are not allowed

}

Check warning on line 62 in tests/phpunit/includes/test-toolbox.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Found precision alignment of 1 spaces.

0 comments on commit c34669b

Please sign in to comment.