diff --git a/.github/workflows/build-test-measure.yml b/.github/workflows/build-test-measure.yml index 285822326fd..b55afa81a4e 100644 --- a/.github/workflows/build-test-measure.yml +++ b/.github/workflows/build-test-measure.yml @@ -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/ @@ -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/cache@v3.0.11 @@ -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/cache@v3.0.11 @@ -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/cache@v3.0.11 @@ -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 @@ -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 @@ -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/setup-node@v3.5.1 + 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/cache@v3.0.11 + 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 }}" @@ -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/cache@v3.0.11 @@ -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/cache@v3.0.11 @@ -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 @@ -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 == '' }} diff --git a/.github/workflows/gutenberg-packages-update.yml b/.github/workflows/gutenberg-packages-update.yml index 1047af3596f..634dfc5795d 100644 --- a/.github/workflows/gutenberg-packages-update.yml +++ b/.github/workflows/gutenberg-packages-update.yml @@ -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 }} @@ -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' @@ -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 }} @@ -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 }} @@ -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 }} @@ -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 }} @@ -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 diff --git a/.wp-env.json b/.wp-env.json index 79e357d0fe5..16680193acb 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -1,5 +1,6 @@ { "core": "WordPress/WordPress#master", + "phpVersion": "7.4", "env": { "development": { "plugins": [ diff --git a/package.json b/package.json index cd13fa21479..70b25f6fbf5 100644 --- a/package.json +++ b/package.json @@ -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)", diff --git a/src/Admin/AfterActivationSiteScan.php b/src/Admin/AfterActivationSiteScan.php index 30593edeba9..1425505c1e8 100644 --- a/src/Admin/AfterActivationSiteScan.php +++ b/src/Admin/AfterActivationSiteScan.php @@ -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' ); } diff --git a/tests/php/src/Admin/AfterActivationSiteScanTest.php b/tests/php/src/Admin/AfterActivationSiteScanTest.php index 1095eda22bf..9b5152989b7 100644 --- a/tests/php/src/Admin/AfterActivationSiteScanTest.php +++ b/tests/php/src/Admin/AfterActivationSiteScanTest.php @@ -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; /** @@ -28,7 +29,7 @@ */ class AfterActivationSiteScanTest extends DependencyInjectedTestCase { - use PrivateAccess; + use PrivateAccess, MockAdminUser; /** * Test instance. @@ -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' ] ) ); @@ -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' ] ) ); diff --git a/tests/php/src/Admin/ReaderThemesTest.php b/tests/php/src/Admin/ReaderThemesTest.php index b38d9090f55..b8e8d4ef2e5 100644 --- a/tests/php/src/Admin/ReaderThemesTest.php +++ b/tests/php/src/Admin/ReaderThemesTest.php @@ -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; @@ -28,7 +29,7 @@ */ class ReaderThemesTest extends TestCase { - use ThemesApiRequestMocking, LoadsCoreThemes; + use ThemesApiRequestMocking, LoadsCoreThemes, MockAdminUser; /** * Test instance. @@ -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 ) ); @@ -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 ) ); diff --git a/tests/php/src/Admin/SiteHealthTest.php b/tests/php/src/Admin/SiteHealthTest.php index bdf9f54c562..ce1fc2a0a7b 100644 --- a/tests/php/src/Admin/SiteHealthTest.php +++ b/tests/php/src/Admin/SiteHealthTest.php @@ -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; @@ -28,6 +29,7 @@ class SiteHealthTest extends TestCase { use HomeUrlLoopbackRequestMocking; use PrivateAccess; + use MockAdminUser; /** * Whether external object cache is being used. @@ -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 { diff --git a/tests/php/src/Admin/SupportLinkTest.php b/tests/php/src/Admin/SupportLinkTest.php index 624a8de3c87..32ef491f078 100644 --- a/tests/php/src/Admin/SupportLinkTest.php +++ b/tests/php/src/Admin/SupportLinkTest.php @@ -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; /** @@ -23,6 +24,8 @@ */ class SupportLinkTest extends TestCase { + use MockAdminUser; + /** * Instance of SupportLink * @@ -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() ); } diff --git a/tests/php/src/Admin/SupportScreenTest.php b/tests/php/src/Admin/SupportScreenTest.php index c3f76fe86cb..ec28a045214 100644 --- a/tests/php/src/Admin/SupportScreenTest.php +++ b/tests/php/src/Admin/SupportScreenTest.php @@ -15,6 +15,7 @@ use AmpProject\AmpWP\Infrastructure\Registerable; use AmpProject\AmpWP\Infrastructure\Service; use AmpProject\AmpWP\Tests\DependencyInjectedTestCase; +use AmpProject\AmpWP\Tests\Helpers\MockAdminUser; use AmpProject\AmpWP\Tests\Helpers\HomeUrlLoopbackRequestMocking; /** @@ -25,7 +26,7 @@ */ class SupportScreenTest extends DependencyInjectedTestCase { - use HomeUrlLoopbackRequestMocking; + use HomeUrlLoopbackRequestMocking, MockAdminUser; /** * Instance of SupportMenu @@ -119,7 +120,8 @@ public function test_is_needed() { $this->assertFalse( SupportScreen::has_cap() ); // Mock the is_admin() with required user caps. - wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) ); + $this->mock_admin_user(); + $this->assertFalse( SupportScreen::is_needed() ); $this->assertTrue( SupportScreen::has_cap() ); diff --git a/tests/php/src/Admin/UserRESTEndpointExtensionTest.php b/tests/php/src/Admin/UserRESTEndpointExtensionTest.php index fd02e2933c0..ae2828504dc 100644 --- a/tests/php/src/Admin/UserRESTEndpointExtensionTest.php +++ b/tests/php/src/Admin/UserRESTEndpointExtensionTest.php @@ -9,6 +9,7 @@ use AMP_Theme_Support; use AmpProject\AmpWP\Admin\UserRESTEndpointExtension; +use AmpProject\AmpWP\Tests\Helpers\MockAdminUser; use AmpProject\AmpWP\Tests\TestCase; use WP_REST_Request; @@ -23,6 +24,8 @@ */ class UserRESTEndpointExtensionTest extends TestCase { + use MockAdminUser; + /** * Test instance. * @@ -68,7 +71,8 @@ public function test_update_review_panel_dismissed_for_template_mode() { $server = rest_get_server(); $this->user_rest_endpoint_extension->register_rest_field(); - $admin_user = self::factory()->user->create_and_get( [ 'role' => 'administrator' ] ); + $admin_user = $this->mock_admin_user(); + $editor_user = self::factory()->user->create_and_get( [ 'role' => 'editor' ] ); $this->assertSame( '', $this->user_rest_endpoint_extension->get_review_panel_dismissed_for_template_mode( [ 'id' => $admin_user->ID ] ) ); diff --git a/tests/php/src/Helpers/MockAdminUser.php b/tests/php/src/Helpers/MockAdminUser.php new file mode 100644 index 00000000000..45debbea1a1 --- /dev/null +++ b/tests/php/src/Helpers/MockAdminUser.php @@ -0,0 +1,35 @@ +user->create_and_get( [ 'role' => 'administrator' ] ); + + if ( is_multisite() ) { + grant_super_admin( $admin_user->ID ); + } + + wp_set_current_user( $admin_user->ID ); + + return $admin_user; + } +} diff --git a/tests/php/src/ReaderThemeLoaderTest.php b/tests/php/src/ReaderThemeLoaderTest.php index a0a583eade9..c58700aef9c 100644 --- a/tests/php/src/ReaderThemeLoaderTest.php +++ b/tests/php/src/ReaderThemeLoaderTest.php @@ -10,6 +10,7 @@ use AmpProject\AmpWP\Option; use AmpProject\AmpWP\ReaderThemeLoader; use AmpProject\AmpWP\Tests\Helpers\LoadsCoreThemes; +use AmpProject\AmpWP\Tests\Helpers\MockAdminUser; use WP_Customize_Manager; use WP_Customize_Panel; use WP_Theme; @@ -17,7 +18,7 @@ /** @coversDefaultClass \AmpProject\AmpWP\ReaderThemeLoader */ final class ReaderThemeLoaderTest extends DependencyInjectedTestCase { - use LoadsCoreThemes; + use LoadsCoreThemes, MockAdminUser; /** @var ReaderThemeLoader */ private $instance; @@ -151,12 +152,18 @@ public function test_filter_wp_prepare_themes_to_indicate_reader_theme() { AMP_Options_Manager::update_option( Option::READER_THEME, $reader_theme_slug ); $this->assertEquals( $active_theme_slug, get_stylesheet() ); $this->assertEquals( $reader_theme_slug, $this->instance->get_reader_theme()->get_stylesheet() ); - wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) ); + + $this->mock_admin_user(); // Note that this is added via filter and not called directly because the filtered value is keyed by theme slug, // but the return value of wp_prepare_themes_for_js() is keyed with numeric indices. $this->instance->register(); // This adds a `wp_prepare_themes_for_js` filter. - $themes = wp_prepare_themes_for_js(); + $themes = wp_prepare_themes_for_js( + [ + wp_get_theme( $active_theme_slug ), + wp_get_theme( $reader_theme_slug ), + ] + ); $this->assertEquals( $active_theme_slug, $themes[0]['id'] ); $this->assertStringNotContainsString( 'AMP', $themes[0]['description'] ); $this->assertArrayHasKey( 'delete', $themes[0]['actions'], 'The delete key is expected even though the theme is active because the delete option is hidden via the JS template.' ); diff --git a/tests/php/test-amp-helper-functions.php b/tests/php/test-amp-helper-functions.php index ebcc2d4a6e4..aafa5a37801 100644 --- a/tests/php/test-amp-helper-functions.php +++ b/tests/php/test-amp-helper-functions.php @@ -10,6 +10,7 @@ use AmpProject\AmpWP\Tests\Helpers\HandleValidation; use AmpProject\AmpWP\Tests\Helpers\LoadsCoreThemes; use AmpProject\AmpWP\Tests\Helpers\PrivateAccess; +use AmpProject\AmpWP\Tests\Helpers\MockAdminUser; use AmpProject\AmpWP\Tests\DependencyInjectedTestCase; use AmpProject\AmpWP\AmpSlugCustomizationWatcher; @@ -21,6 +22,7 @@ class Test_AMP_Helper_Functions extends DependencyInjectedTestCase { use HandleValidation; use LoadsCoreThemes; use PrivateAccess; + use MockAdminUser; /** * The mock Site Icon value to use in a filter. @@ -1963,7 +1965,7 @@ function ( $xpaths ) use ( $element_xpaths ) { remove_filter( 'amp_dev_mode_enabled', '__return_true' ); // Check that AMP_Dev_Mode_Sanitizer is registered once in dev mode, and now also with admin bar showing. - wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) ); + $this->mock_admin_user(); add_filter( 'amp_dev_mode_enabled', '__return_true' ); add_filter( 'show_admin_bar', '__return_true' ); $sanitizers = amp_get_content_sanitizers(); @@ -2576,6 +2578,11 @@ public function data_amp_has_paired_endpoint() { * @param bool $is_amp */ public function test_amp_has_paired_endpoint_go_to( $paired_url_structure, $suffix, $is_amp ) { + if ( is_multisite() ) { + // Switch blog in multisite to ensure paired endpoint is working with multiple subdirectory installations. + switch_to_blog( self::factory()->blog->create() ); + } + AMP_Options_Manager::update_option( Option::PAIRED_URL_STRUCTURE, $paired_url_structure ); add_filter( 'wp_redirect', '__return_empty_string' ); // Prevent ensure_proper_amp_location() from redirecting. diff --git a/tests/php/test-uninstall.php b/tests/php/test-uninstall.php index e2c4167c3da..8b27356731d 100644 --- a/tests/php/test-uninstall.php +++ b/tests/php/test-uninstall.php @@ -569,7 +569,17 @@ public function test_remove_plugin_data() { $num_queries_before = $wpdb->num_queries; require AMP__DIR__ . '/uninstall.php'; $this->flush_cache(); - $this->assertEquals( $num_queries_before, $wpdb->num_queries ); + + if ( is_multisite() ) { + /* + * Multisite has 2 additional queries: + * 1. Get sites query. + * 2. Switch to blog query. + */ + $this->assertEquals( $num_queries_before + 2, $wpdb->num_queries ); + } else { + $this->assertEquals( $num_queries_before, $wpdb->num_queries ); + } $this->assertNotEmpty( get_option( AMP_Options_Manager::OPTION_NAME, false ) );