Skip to content

Commit 8c8564e

Browse files
authored
Merge pull request #7321 from ampproject/add/multisite-phpunit-workflow
Add PHPUnit tests workflow for multisite
2 parents 9f6567c + 8f51deb commit 8c8564e

15 files changed

+186
-42
lines changed

.github/workflows/build-test-measure.yml

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ jobs:
7171
echo "Changed JS file count: $JS_FILE_COUNT"
7272
echo "Changed GHA workflow file count: $GHA_WORKFLOW_COUNT"
7373
74-
echo "::set-output name=count::$FILE_COUNT"
75-
echo "::set-output name=php-count::$PHP_FILE_COUNT"
76-
echo "::set-output name=css-count::$CSS_FILE_COUNT"
77-
echo "::set-output name=js-count::$JS_FILE_COUNT"
78-
echo "::set-output name=gha-workflow-count::$GHA_WORKFLOW_COUNT"
74+
echo "count=$FILE_COUNT" >> $GITHUB_OUTPUT
75+
echo "php-count=$PHP_FILE_COUNT" >> $GITHUB_OUTPUT
76+
echo "css-count=$CSS_FILE_COUNT" >> $GITHUB_OUTPUT
77+
echo "js-count=$JS_FILE_COUNT" >> $GITHUB_OUTPUT
78+
echo "gha-workflow-count=$GHA_WORKFLOW_COUNT" >> $GITHUB_OUTPUT
7979
env:
8080
# Ignore Paths:
8181
# - .github/
@@ -176,7 +176,7 @@ jobs:
176176

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

181181
- name: Configure Composer cache
182182
uses: actions/[email protected]
@@ -222,7 +222,7 @@ jobs:
222222

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

227227
- name: Configure Composer cache
228228
uses: actions/[email protected]
@@ -304,7 +304,7 @@ jobs:
304304

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

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

462462
- name: Configure Composer cache
463463
if: needs.pre-run.outputs.changed-php-count > 0
@@ -475,7 +475,7 @@ jobs:
475475
composer remove --dev phpdocumentor/reflection
476476
composer install --prefer-dist --ignore-platform-reqs --no-progress --no-interaction
477477
478-
# See https://github.com/wp-cli/wp-cli/issues/5484
478+
# See https://github.com/wp-cli/wp-cli/issues/5484
479479
- name: Remove conflicting Requests library
480480
if: needs.pre-run.outputs.changed-php-count > 0
481481
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
@@ -567,7 +567,76 @@ jobs:
567567
flags: php,unit
568568
fail_ci_if_error: true
569569

570-
#-----------------------------------------------------------------------------------------------------------------------
570+
#-----------------------------------------------------------------------------------------------------------------------
571+
572+
unit-test-multisite-php:
573+
name: 'Unit Tests Multisite: PHP 7.4, WP Latest'
574+
needs: pre-run
575+
runs-on: ubuntu-latest
576+
if: needs.pre-run.outputs.changed-php-count > 0
577+
strategy:
578+
matrix:
579+
testsuite: ['default', 'external-http']
580+
581+
steps:
582+
- name: Checkout
583+
uses: actions/checkout@v3
584+
585+
- name: Setup Node
586+
uses: actions/[email protected]
587+
with:
588+
node-version-file: '.nvmrc'
589+
cache: npm
590+
591+
- name: Get Composer Cache Directory
592+
id: composer-cache
593+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
594+
595+
- name: Configure Composer cache
596+
uses: actions/[email protected]
597+
with:
598+
path: ${{ steps.composer-cache.outputs.dir }}
599+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
600+
restore-keys: |
601+
${{ runner.os }}-composer-
602+
603+
- name: Install Node dependencies
604+
run: npm ci
605+
env:
606+
CI: true
607+
608+
- name: Install Composer dependencies
609+
run: |
610+
# phpdocumentor/reflection has to be removed as it makes use of an outdated dependency.
611+
composer remove --dev phpdocumentor/reflection
612+
composer install --prefer-dist --ignore-platform-reqs --no-progress --no-interaction
613+
614+
# See https://github.com/wp-cli/wp-cli/issues/5484
615+
- name: Remove conflicting Requests library
616+
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
617+
618+
- name: Update PHPUnit
619+
if: needs.pre-run.outputs.changed-php-count > 0
620+
run: |
621+
# We are using PHP 7.4 and WP Latest.
622+
echo "Installing latest version of PHPUnit"
623+
composer update --ignore-platform-reqs --no-interaction --no-scripts yoast/phpunit-polyfills --with-dependencies
624+
625+
- name: Build plugin
626+
run: npm run build:js
627+
628+
- name: Move amp-wp to amp
629+
run: cp -r "$PWD" "/tmp/amp"
630+
631+
- name: Start wp-env
632+
working-directory: /tmp/amp
633+
run: npm run wp-env start
634+
635+
- name: Run multisite unit tests
636+
working-directory: /tmp/amp
637+
run: npm run test:php:multisite ${{ matrix.testsuite == 'external-http' && '-- --testsuite external-http' || '' }}
638+
639+
#-----------------------------------------------------------------------------------------------------------------------
571640

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

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

646715
- name: Configure Composer cache
647716
uses: actions/[email protected]
@@ -741,8 +810,7 @@ jobs:
741810

742811
- name: Get Composer Cache Directory
743812
id: composer-cache
744-
run: |
745-
echo "::set-output name=dir::$(composer config cache-files-dir)"
813+
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
746814

747815
- name: Configure Composer cache
748816
uses: actions/[email protected]
@@ -774,11 +842,11 @@ jobs:
774842

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

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

783851
- name: Upload build as artifact
784852
uses: actions/upload-artifact@v3
@@ -847,7 +915,7 @@ jobs:
847915
- Download [development build](https://storage.googleapis.com/ampwp_github_artifacts/${{ github.ref }}/dev/amp.zip?${{ github.sha }})
848916
- Download [production build](https://storage.googleapis.com/ampwp_github_artifacts/${{ github.ref }}/prod/amp.zip?${{ github.sha }})"
849917
body="${body//$'\n'/'%0A'}"
850-
echo "::set-output name=body::$body"
918+
echo "body=$body" >> $GITHUB_OUTPUT
851919
852920
- name: Create comment on PR with links to plugin builds
853921
if: ${{ steps.find-comment.outputs.comment-id == '' }}

.github/workflows/gutenberg-packages-update.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
steps:
2929
- name: Get latest release version
3030
id: latest-release
31-
run: echo "::set-output name=version::$(gh api -X GET repos/wordpress/gutenberg/releases/latest --jq '.name')"
31+
run: echo "version=$(gh api -X GET repos/wordpress/gutenberg/releases/latest --jq '.name')" >> $GITHUB_OUTPUT
3232
env:
3333
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3434

@@ -40,7 +40,7 @@ jobs:
4040
if ! egrep -q '^[0-9][0-9]*(\.[0-9][0-9]*)*$' <<< "$LAST_VERSION"; then
4141
LAST_VERSION='0.0.0'
4242
fi
43-
echo "::set-output name=version::$(echo "$LAST_VERSION")"
43+
echo "version=$(echo "$LAST_VERSION")" >> $GITHUB_OUTPUT
4444
env:
4545
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4646
QUERY: 'repo:ampproject/amp-wp is:pr author:app/github-actions is:merged in:title Update Gutenberg packages after'
@@ -50,7 +50,7 @@ jobs:
5050
run: |
5151
echo "Last version: $LAST_VER"
5252
echo "Latest version: $LATEST_VER"
53-
echo "::set-output name=outdated::$(php -r 'echo json_encode(version_compare($argv[1], $argv[2], ">"));' "$LATEST_VER" "$LAST_VER")"
53+
echo "outdated=$(php -r 'echo json_encode(version_compare($argv[1], $argv[2], ">"));' "$LATEST_VER" "$LAST_VER")" >> $GITHUB_OUTPUT
5454
env:
5555
LAST_VER: ${{ steps.last-release.outputs.version }}
5656
LATEST_VER: ${{ steps.latest-release.outputs.version }}
@@ -66,7 +66,7 @@ jobs:
6666
id: latest-pr
6767
run: |
6868
PR_NUM=$(gh api -X GET search/issues -f q='${{ env.QUERY }}' -f sort='created' -f order='desc' --jq '.items.[0].number')
69-
echo "::set-output name=num::$(echo $PR_NUM)"
69+
echo "num=$(echo $PR_NUM)" >> $GITHUB_OUTPUT
7070
echo "Latest PR number: ${PR_NUM}"
7171
env:
7272
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -100,8 +100,8 @@ jobs:
100100
- name: Determine branch name
101101
id: branches
102102
run: |
103-
echo "::set-output name=base::$(echo ${GITHUB_REF#refs/heads/})"
104-
echo "::set-output name=head::$(echo "update/gutenberg-v$VERSION-packages")"
103+
echo "base=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_OUTPUT
104+
echo "head=$(echo "update/gutenberg-v$VERSION-packages")" >> $GITHUB_OUTPUT
105105
env:
106106
VERSION: ${{ needs.check-gutenberg-release.outputs.latest-version }}
107107

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

@@ -147,7 +147,7 @@ jobs:
147147
run: |
148148
# Get list of latest package versions.
149149
PACKAGES=$(npm outdated --parseable | cut -d':' -f 4 | grep @wordpress | paste -s -d' ' || echo 0)
150-
echo "::set-output name=list::$(echo "$PACKAGES")"
150+
echo "list=$(echo "$PACKAGES")" >> $GITHUB_OUTPUT
151151
152152
- name: Update packages
153153
if: steps.packages.outputs.list != 0

.wp-env.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"core": "WordPress/WordPress#master",
3+
"phpVersion": "7.4",
34
"env": {
45
"development": {
56
"plugins": [

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
"test:js:update-snapshots": "npm run test:js -- --updateSnapshot",
144144
"test:js:watch": "npm run test:js -- --watch",
145145
"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'",
146+
"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'",
146147
"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'",
147148
"test:php:help": "npm run test:php --args='--help'",
148149
"update-wordpress-packages": "npm i $(npm outdated --parseable | cut -d':' -f 4 | grep @wordpress)",

src/Admin/AfterActivationSiteScan.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ protected function get_amp_compatible_plugins_url() {
211211
* @return string URL to AMP compatible themes directory.
212212
*/
213213
protected function get_amp_compatible_themes_url() {
214-
if ( current_user_can( 'switch_themes' ) ) {
214+
if ( current_user_can( 'install_themes' ) ) {
215215
return admin_url( '/theme-install.php?browse=amp-compatible' );
216216
}
217217

tests/php/src/Admin/AfterActivationSiteScanTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use AmpProject\AmpWP\Services;
1616
use AmpProject\AmpWP\Tests\DependencyInjectedTestCase;
1717
use AmpProject\AmpWP\Tests\Helpers\PrivateAccess;
18+
use AmpProject\AmpWP\Tests\Helpers\MockAdminUser;
1819
use AMP_Validation_Manager;
1920

2021
/**
@@ -28,7 +29,7 @@
2829
*/
2930
class AfterActivationSiteScanTest extends DependencyInjectedTestCase {
3031

31-
use PrivateAccess;
32+
use PrivateAccess, MockAdminUser;
3233

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

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

293294
wp_set_current_user( self::factory()->user->create( [ 'role' => 'author' ] ) );

tests/php/src/Admin/ReaderThemesTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use AmpProject\AmpWP\Option;
1616
use AmpProject\AmpWP\Tests\Helpers\LoadsCoreThemes;
1717
use AmpProject\AmpWP\Tests\Helpers\ThemesApiRequestMocking;
18+
use AmpProject\AmpWP\Tests\Helpers\MockAdminUser;
1819
use AmpProject\AmpWP\Tests\TestCase;
1920
use Closure;
2021
use WP_Error;
@@ -28,7 +29,7 @@
2829
*/
2930
class ReaderThemesTest extends TestCase {
3031

31-
use ThemesApiRequestMocking, LoadsCoreThemes;
32+
use ThemesApiRequestMocking, LoadsCoreThemes, MockAdminUser;
3233

3334
/**
3435
* Test instance.
@@ -304,7 +305,8 @@ static function () {
304305
}
305306
);
306307

307-
wp_set_current_user( $this->factory()->user->create( [ 'role' => 'administrator' ] ) );
308+
$this->mock_admin_user();
309+
308310
$expected = $get_expected();
309311
$this->assertEquals( $expected, $this->reader_themes->get_theme_availability( $theme ) );
310312
$this->assertEquals( $can_install, $this->reader_themes->can_install_theme( $theme ) );
@@ -344,7 +346,7 @@ static function () {
344346
$this->assertFalse( $this->reader_themes->can_install_theme( $core_theme ) );
345347
$this->assertFalse( $this->reader_themes->can_install_theme( $neve_theme ) );
346348

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

tests/php/src/Admin/SiteHealthTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use AmpProject\AmpWP\QueryVar;
1616
use AmpProject\AmpWP\Tests\Helpers\HomeUrlLoopbackRequestMocking;
1717
use AmpProject\AmpWP\Tests\Helpers\PrivateAccess;
18+
use AmpProject\AmpWP\Tests\Helpers\MockAdminUser;
1819
use AmpProject\AmpWP\Tests\TestCase;
1920
use WP_REST_Server;
2021
use WP_Error;
@@ -28,6 +29,7 @@ class SiteHealthTest extends TestCase {
2829

2930
use HomeUrlLoopbackRequestMocking;
3031
use PrivateAccess;
32+
use MockAdminUser;
3133

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

141143
// Prior to WordPress 5.2, the view_site_health_checks cap didn't exist because Site Health didn't exist.
142-
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
144+
$this->mock_admin_user();
145+
143146
if ( version_compare( get_bloginfo( 'version' ), '5.2', '>=' ) ) {
144147
$this->assertTrue( call_user_func( $route['permission_callback'] ) );
145148
} else {

tests/php/src/Admin/SupportLinkTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use AMP_Validation_Manager;
1414
use AMP_Theme_Support;
1515
use AMP_Validated_URL_Post_Type;
16+
use AmpProject\AmpWP\Tests\Helpers\MockAdminUser;
1617
use AmpProject\AmpWP\Tests\TestCase;
1718

1819
/**
@@ -23,6 +24,8 @@
2324
*/
2425
class SupportLinkTest extends TestCase {
2526

27+
use MockAdminUser;
28+
2629
/**
2730
* Instance of SupportLink
2831
*
@@ -65,7 +68,7 @@ public function test_is_needed() {
6568
$this->assertFalse( SupportLink::is_needed() );
6669

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

7073
$this->assertTrue( SupportLink::is_needed() );
7174
}

0 commit comments

Comments
 (0)