diff --git a/includes/class-amp-theme-support.php b/includes/class-amp-theme-support.php index b9fe763e303..086e8d713e6 100644 --- a/includes/class-amp-theme-support.php +++ b/includes/class-amp-theme-support.php @@ -2476,14 +2476,13 @@ public static function setup_paired_browsing_client() { true ); - // Whitelist enqueued script for AMP dev mdoe so that it is not removed. + // Whitelist enqueued script for AMP dev mode so that it is not removed. // @todo Revisit with . add_filter( 'script_loader_tag', static function( $tag, $handle ) { if ( is_amp_endpoint() && self::has_dependency( wp_scripts(), 'amp-paired-browsing-client', $handle ) ) { - $attrs = [ AMP_Rule_Spec::DEV_MODE_ATTRIBUTE, 'async' ]; - $tag = preg_replace( '/(?<=)/i', ' ' . implode( ' ', $attrs ), $tag ); + $tag = preg_replace( '/(?<=)/i', ' ' . AMP_Rule_Spec::DEV_MODE_ATTRIBUTE, $tag ); } return $tag; }, diff --git a/tests/php/test-class-amp-theme-support.php b/tests/php/test-class-amp-theme-support.php index 77e3a2486c1..59f0d0782ce 100644 --- a/tests/php/test-class-amp-theme-support.php +++ b/tests/php/test-class-amp-theme-support.php @@ -2471,6 +2471,89 @@ public function test_enqueue_assets() { $this->assertContains( $style_slug, wp_styles()->queue ); } + /** + * Test the enqueuing in setup_paired_browsing_client(). + * + * @covers AMP_Theme_Support::setup_paired_browsing_client() + */ + public function test_setup_paired_browsing_client_enqueuing() { + $handle = 'amp-paired-browsing-client'; + + // The conditions aren't met, so this should not enqueue the script. + $_GET[ AMP_Theme_Support::PAIRED_BROWSING_QUERY_VAR ] = '1'; + AMP_Theme_Support::setup_paired_browsing_client(); + $this->assertFalse( wp_script_is( $handle ) ); + + // Only one condition is met, so this should still not enqueue the script. + unset( $_GET[ AMP_Theme_Support::PAIRED_BROWSING_QUERY_VAR ] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended + AMP_Theme_Support::setup_paired_browsing_client(); + $this->assertFalse( wp_script_is( $handle ) ); + + // Both of the conditions to enqueue are met. + add_filter( 'amp_dev_mode_enabled', '__return_true' ); + AMP_Theme_Support::setup_paired_browsing_client(); + $this->assertTrue( wp_script_is( $handle ) ); + } + + /** + * Gets the test data for test_setup_paired_browsing_client_filter(). + * + * @return array The test data. + */ + public function get_setup_paired_browsing_data() { + $original_script_tag = ''; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript + $script_tag_with_dev_mode = ''; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript + + return [ + 'no_parent_of_dependency' => [ + '', + $original_script_tag, + null, + ], + 'wrong_parent_of_dependency' => [ + 'different-handle-completely', + $original_script_tag, + null, + ], + 'correct_parent_of_dependency' => [ + 'amp-paired-browsing-client', + $original_script_tag, + $script_tag_with_dev_mode, + ], + ]; + } + + /** + * Test the filter in setup_paired_browsing_client(). + * + * @dataProvider get_setup_paired_browsing_data + * @covers AMP_Theme_Support::setup_paired_browsing_client() + * + * @param string $parent_of_dependency The script that has a dependency on the dependency handle. + * @param string $original_script_tag The