Skip to content

Commit

Permalink
Merge pull request #7321 from ampproject/add/multisite-phpunit-workflow
Browse files Browse the repository at this point in the history
Add PHPUnit tests workflow for multisite
  • Loading branch information
westonruter committed Nov 24, 2022
2 parents 9f6567c + 8f51deb commit 8c8564e
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 42 deletions.
102 changes: 85 additions & 17 deletions .github/workflows/build-test-measure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ jobs:
echo "Changed JS file count: $JS_FILE_COUNT"
echo "Changed GHA workflow file count: $GHA_WORKFLOW_COUNT"
echo "::set-output name=count::$FILE_COUNT"
echo "::set-output name=php-count::$PHP_FILE_COUNT"
echo "::set-output name=css-count::$CSS_FILE_COUNT"
echo "::set-output name=js-count::$JS_FILE_COUNT"
echo "::set-output name=gha-workflow-count::$GHA_WORKFLOW_COUNT"
echo "count=$FILE_COUNT" >> $GITHUB_OUTPUT
echo "php-count=$PHP_FILE_COUNT" >> $GITHUB_OUTPUT
echo "css-count=$CSS_FILE_COUNT" >> $GITHUB_OUTPUT
echo "js-count=$JS_FILE_COUNT" >> $GITHUB_OUTPUT
echo "gha-workflow-count=$GHA_WORKFLOW_COUNT" >> $GITHUB_OUTPUT
env:
# Ignore Paths:
# - .github/
Expand Down Expand Up @@ -176,7 +176,7 @@ jobs:

- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Configure Composer cache
uses: actions/[email protected]
Expand Down Expand Up @@ -222,7 +222,7 @@ jobs:

- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Configure Composer cache
uses: actions/[email protected]
Expand Down Expand Up @@ -304,7 +304,7 @@ jobs:

- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Configure Composer cache
uses: actions/[email protected]
Expand Down Expand Up @@ -457,7 +457,7 @@ jobs:
- name: Get Composer Cache Directory
if: needs.pre-run.outputs.changed-php-count > 0
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Configure Composer cache
if: needs.pre-run.outputs.changed-php-count > 0
Expand All @@ -475,7 +475,7 @@ jobs:
composer remove --dev phpdocumentor/reflection
composer install --prefer-dist --ignore-platform-reqs --no-progress --no-interaction
# See https://github.com/wp-cli/wp-cli/issues/5484
# See https://github.com/wp-cli/wp-cli/issues/5484
- name: Remove conflicting Requests library
if: needs.pre-run.outputs.changed-php-count > 0
run: composer remove --dev --ignore-platform-reqs --no-interaction --no-scripts roave/security-advisories wp-cli/export-command wp-cli/extension-command wp-cli/wp-cli wp-cli/wp-cli-tests
Expand Down Expand Up @@ -567,7 +567,76 @@ jobs:
flags: php,unit
fail_ci_if_error: true

#-----------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------

unit-test-multisite-php:
name: 'Unit Tests Multisite: PHP 7.4, WP Latest'
needs: pre-run
runs-on: ubuntu-latest
if: needs.pre-run.outputs.changed-php-count > 0
strategy:
matrix:
testsuite: ['default', 'external-http']

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Node
uses: actions/[email protected]
with:
node-version-file: '.nvmrc'
cache: npm

- name: Get Composer Cache Directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Configure Composer cache
uses: actions/[email protected]
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-composer-
- name: Install Node dependencies
run: npm ci
env:
CI: true

- name: Install Composer dependencies
run: |
# phpdocumentor/reflection has to be removed as it makes use of an outdated dependency.
composer remove --dev phpdocumentor/reflection
composer install --prefer-dist --ignore-platform-reqs --no-progress --no-interaction
# See https://github.com/wp-cli/wp-cli/issues/5484
- name: Remove conflicting Requests library
run: composer remove --dev --ignore-platform-reqs --no-interaction --no-scripts roave/security-advisories wp-cli/export-command wp-cli/extension-command wp-cli/wp-cli wp-cli/wp-cli-tests

- name: Update PHPUnit
if: needs.pre-run.outputs.changed-php-count > 0
run: |
# We are using PHP 7.4 and WP Latest.
echo "Installing latest version of PHPUnit"
composer update --ignore-platform-reqs --no-interaction --no-scripts yoast/phpunit-polyfills --with-dependencies
- name: Build plugin
run: npm run build:js

- name: Move amp-wp to amp
run: cp -r "$PWD" "/tmp/amp"

- name: Start wp-env
working-directory: /tmp/amp
run: npm run wp-env start

- name: Run multisite unit tests
working-directory: /tmp/amp
run: npm run test:php:multisite ${{ matrix.testsuite == 'external-http' && '-- --testsuite external-http' || '' }}

#-----------------------------------------------------------------------------------------------------------------------

feature-test-php:
name: "Feature test${{ matrix.coverage && ' (with coverage)' || '' }}: PHP ${{ matrix.php }}, WP ${{ matrix.wp }}"
Expand Down Expand Up @@ -641,7 +710,7 @@ jobs:

- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Configure Composer cache
uses: actions/[email protected]
Expand Down Expand Up @@ -741,8 +810,7 @@ jobs:

- name: Get Composer Cache Directory
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Configure Composer cache
uses: actions/[email protected]
Expand Down Expand Up @@ -774,11 +842,11 @@ jobs:

- name: Retrieve branch name
id: retrieve-branch-name
run: echo "::set-output name=branch_name::$(REF=${GITHUB_HEAD_REF:-$GITHUB_REF} && echo ${REF#refs/heads/} | sed 's/\//-/g')"
run: echo "branch_name=$(REF=${GITHUB_HEAD_REF:-$GITHUB_REF} && echo ${REF#refs/heads/} | sed 's/\//-/g')" >> $GITHUB_OUTPUT

- name: Retrieve git SHA-8 string
id: retrieve-git-sha-8
run: echo "::set-output name=sha8::$(echo ${GITHUB_SHA} | cut -c1-8)"
run: echo "sha8=$(echo ${GITHUB_SHA} | cut -c1-8)" >> $GITHUB_OUTPUT

- name: Upload build as artifact
uses: actions/upload-artifact@v3
Expand Down Expand Up @@ -847,7 +915,7 @@ jobs:
- Download [development build](https://storage.googleapis.com/ampwp_github_artifacts/${{ github.ref }}/dev/amp.zip?${{ github.sha }})
- Download [production build](https://storage.googleapis.com/ampwp_github_artifacts/${{ github.ref }}/prod/amp.zip?${{ github.sha }})"
body="${body//$'\n'/'%0A'}"
echo "::set-output name=body::$body"
echo "body=$body" >> $GITHUB_OUTPUT
- name: Create comment on PR with links to plugin builds
if: ${{ steps.find-comment.outputs.comment-id == '' }}
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/gutenberg-packages-update.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
steps:
- name: Get latest release version
id: latest-release
run: echo "::set-output name=version::$(gh api -X GET repos/wordpress/gutenberg/releases/latest --jq '.name')"
run: echo "version=$(gh api -X GET repos/wordpress/gutenberg/releases/latest --jq '.name')" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand All @@ -40,7 +40,7 @@ jobs:
if ! egrep -q '^[0-9][0-9]*(\.[0-9][0-9]*)*$' <<< "$LAST_VERSION"; then
LAST_VERSION='0.0.0'
fi
echo "::set-output name=version::$(echo "$LAST_VERSION")"
echo "version=$(echo "$LAST_VERSION")" >> $GITHUB_OUTPUT
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
QUERY: 'repo:ampproject/amp-wp is:pr author:app/github-actions is:merged in:title Update Gutenberg packages after'
Expand All @@ -50,7 +50,7 @@ jobs:
run: |
echo "Last version: $LAST_VER"
echo "Latest version: $LATEST_VER"
echo "::set-output name=outdated::$(php -r 'echo json_encode(version_compare($argv[1], $argv[2], ">"));' "$LATEST_VER" "$LAST_VER")"
echo "outdated=$(php -r 'echo json_encode(version_compare($argv[1], $argv[2], ">"));' "$LATEST_VER" "$LAST_VER")" >> $GITHUB_OUTPUT
env:
LAST_VER: ${{ steps.last-release.outputs.version }}
LATEST_VER: ${{ steps.latest-release.outputs.version }}
Expand All @@ -66,7 +66,7 @@ jobs:
id: latest-pr
run: |
PR_NUM=$(gh api -X GET search/issues -f q='${{ env.QUERY }}' -f sort='created' -f order='desc' --jq '.items.[0].number')
echo "::set-output name=num::$(echo $PR_NUM)"
echo "num=$(echo $PR_NUM)" >> $GITHUB_OUTPUT
echo "Latest PR number: ${PR_NUM}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -100,8 +100,8 @@ jobs:
- name: Determine branch name
id: branches
run: |
echo "::set-output name=base::$(echo ${GITHUB_REF#refs/heads/})"
echo "::set-output name=head::$(echo "update/gutenberg-v$VERSION-packages")"
echo "base=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_OUTPUT
echo "head=$(echo "update/gutenberg-v$VERSION-packages")" >> $GITHUB_OUTPUT
env:
VERSION: ${{ needs.check-gutenberg-release.outputs.latest-version }}

Expand All @@ -118,7 +118,7 @@ jobs:
- name: Check if remote branch exists
id: remote-branch
run: echo ::set-output name=exists::$([[ -z $(git ls-remote --heads origin "$HEAD_BRANCH" ) ]] && echo "0" || echo "1")
run: echo "exists=$([[ -z $(git ls-remote --heads origin "$HEAD_BRANCH" ) ]] && echo "0" || echo "1")" >> $GITHUB_OUTPUT
env:
HEAD_BRANCH: ${{ steps.branches.outputs.head }}

Expand Down Expand Up @@ -147,7 +147,7 @@ jobs:
run: |
# Get list of latest package versions.
PACKAGES=$(npm outdated --parseable | cut -d':' -f 4 | grep @wordpress | paste -s -d' ' || echo 0)
echo "::set-output name=list::$(echo "$PACKAGES")"
echo "list=$(echo "$PACKAGES")" >> $GITHUB_OUTPUT
- name: Update packages
if: steps.packages.outputs.list != 0
Expand Down
1 change: 1 addition & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"core": "WordPress/WordPress#master",
"phpVersion": "7.4",
"env": {
"development": {
"plugins": [
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
"test:js:update-snapshots": "npm run test:js -- --updateSnapshot",
"test:js:watch": "npm run test:js -- --watch",
"test:php": "wp-env run phpunit 'env WP_PHPUNIT__TESTS_CONFIG=/wordpress-phpunit/wp-tests-config.php WORDPRESS_TABLE_PREFIX=wptests_ WP_TESTS_DIR=/var/www/wordpress-develop/tests/phpunit /var/www/html/wp-content/plugins/amp/vendor/bin/phpunit -c /var/www/html/wp-content/plugins/amp/phpunit.xml.dist $npm_config_args'",
"test:php:multisite": "wp-env run phpunit 'env WP_MULTISITE=1 WP_PHPUNIT__TESTS_CONFIG=/wordpress-phpunit/wp-tests-config.php WORDPRESS_TABLE_PREFIX=wptests_ WP_TESTS_DIR=/var/www/wordpress-develop/tests/phpunit /var/www/html/wp-content/plugins/amp/vendor/bin/phpunit -c /var/www/html/wp-content/plugins/amp/phpunit.xml.dist $npm_config_args'",
"test:php:xdebug": "wp-env run tests-wordpress 'env PHP_IDE_CONFIG=serverName=localhost WORDPRESS_TABLE_PREFIX=wptests_ WP_TESTS_DIR=/var/www/wordpress-develop/tests/phpunit WP_PHPUNIT__TESTS_CONFIG=/wordpress-phpunit/wp-tests-config.php /var/www/html/wp-content/plugins/amp/vendor/bin/phpunit -c /var/www/html/wp-content/plugins/amp/phpunit.xml.dist $npm_config_args'",
"test:php:help": "npm run test:php --args='--help'",
"update-wordpress-packages": "npm i $(npm outdated --parseable | cut -d':' -f 4 | grep @wordpress)",
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/AfterActivationSiteScan.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected function get_amp_compatible_plugins_url() {
* @return string URL to AMP compatible themes directory.
*/
protected function get_amp_compatible_themes_url() {
if ( current_user_can( 'switch_themes' ) ) {
if ( current_user_can( 'install_themes' ) ) {
return admin_url( '/theme-install.php?browse=amp-compatible' );
}

Expand Down
7 changes: 4 additions & 3 deletions tests/php/src/Admin/AfterActivationSiteScanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use AmpProject\AmpWP\Services;
use AmpProject\AmpWP\Tests\DependencyInjectedTestCase;
use AmpProject\AmpWP\Tests\Helpers\PrivateAccess;
use AmpProject\AmpWP\Tests\Helpers\MockAdminUser;
use AMP_Validation_Manager;

/**
Expand All @@ -28,7 +29,7 @@
*/
class AfterActivationSiteScanTest extends DependencyInjectedTestCase {

use PrivateAccess;
use PrivateAccess, MockAdminUser;

/**
* Test instance.
Expand Down Expand Up @@ -276,7 +277,7 @@ function ( $caps, $cap ) {
* @covers ::get_amp_compatible_plugins_url
*/
public function test_get_amp_compatible_plugins_url() {
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
$this->mock_admin_user();
$this->assertStringContainsString( '/plugin-install.php?tab=amp-compatible', $this->call_private_method( $this->after_activation_site_scan, 'get_amp_compatible_plugins_url' ) );

wp_set_current_user( self::factory()->user->create( [ 'role' => 'author' ] ) );
Expand All @@ -287,7 +288,7 @@ public function test_get_amp_compatible_plugins_url() {
* @covers ::get_amp_compatible_themes_url
*/
public function test_get_amp_compatible_themes_url() {
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
$this->mock_admin_user();
$this->assertStringContainsString( '/theme-install.php?browse=amp-compatible', $this->call_private_method( $this->after_activation_site_scan, 'get_amp_compatible_themes_url' ) );

wp_set_current_user( self::factory()->user->create( [ 'role' => 'author' ] ) );
Expand Down
8 changes: 5 additions & 3 deletions tests/php/src/Admin/ReaderThemesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use AmpProject\AmpWP\Option;
use AmpProject\AmpWP\Tests\Helpers\LoadsCoreThemes;
use AmpProject\AmpWP\Tests\Helpers\ThemesApiRequestMocking;
use AmpProject\AmpWP\Tests\Helpers\MockAdminUser;
use AmpProject\AmpWP\Tests\TestCase;
use Closure;
use WP_Error;
Expand All @@ -28,7 +29,7 @@
*/
class ReaderThemesTest extends TestCase {

use ThemesApiRequestMocking, LoadsCoreThemes;
use ThemesApiRequestMocking, LoadsCoreThemes, MockAdminUser;

/**
* Test instance.
Expand Down Expand Up @@ -304,7 +305,8 @@ static function () {
}
);

wp_set_current_user( $this->factory()->user->create( [ 'role' => 'administrator' ] ) );
$this->mock_admin_user();

$expected = $get_expected();
$this->assertEquals( $expected, $this->reader_themes->get_theme_availability( $theme ) );
$this->assertEquals( $can_install, $this->reader_themes->can_install_theme( $theme ) );
Expand Down Expand Up @@ -344,7 +346,7 @@ static function () {
$this->assertFalse( $this->reader_themes->can_install_theme( $core_theme ) );
$this->assertFalse( $this->reader_themes->can_install_theme( $neve_theme ) );

wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
$this->mock_admin_user();
$this->assertTrue( $this->reader_themes->can_install_theme( $core_theme ) );
$this->assertTrue( $this->reader_themes->can_install_theme( $neve_theme ) );

Expand Down
5 changes: 4 additions & 1 deletion tests/php/src/Admin/SiteHealthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use AmpProject\AmpWP\QueryVar;
use AmpProject\AmpWP\Tests\Helpers\HomeUrlLoopbackRequestMocking;
use AmpProject\AmpWP\Tests\Helpers\PrivateAccess;
use AmpProject\AmpWP\Tests\Helpers\MockAdminUser;
use AmpProject\AmpWP\Tests\TestCase;
use WP_REST_Server;
use WP_Error;
Expand All @@ -28,6 +29,7 @@ class SiteHealthTest extends TestCase {

use HomeUrlLoopbackRequestMocking;
use PrivateAccess;
use MockAdminUser;

/**
* Whether external object cache is being used.
Expand Down Expand Up @@ -139,7 +141,8 @@ public function test_register_async_test_endpoints() {
$this->assertFalse( call_user_func( $route['permission_callback'] ) );

// Prior to WordPress 5.2, the view_site_health_checks cap didn't exist because Site Health didn't exist.
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
$this->mock_admin_user();

if ( version_compare( get_bloginfo( 'version' ), '5.2', '>=' ) ) {
$this->assertTrue( call_user_func( $route['permission_callback'] ) );
} else {
Expand Down
5 changes: 4 additions & 1 deletion tests/php/src/Admin/SupportLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use AMP_Validation_Manager;
use AMP_Theme_Support;
use AMP_Validated_URL_Post_Type;
use AmpProject\AmpWP\Tests\Helpers\MockAdminUser;
use AmpProject\AmpWP\Tests\TestCase;

/**
Expand All @@ -23,6 +24,8 @@
*/
class SupportLinkTest extends TestCase {

use MockAdminUser;

/**
* Instance of SupportLink
*
Expand Down Expand Up @@ -65,7 +68,7 @@ public function test_is_needed() {
$this->assertFalse( SupportLink::is_needed() );

// Test 2: Test with admin user.
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
$this->mock_admin_user();

$this->assertTrue( SupportLink::is_needed() );
}
Expand Down
Loading

0 comments on commit 8c8564e

Please sign in to comment.