From d5bcf66d2bcc828273bfcb6271ce8e31aecfaa62 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Mon, 7 Oct 2024 12:42:29 -0700 Subject: [PATCH] Only add AMP scripts to runtime cache in Standard Mode --- includes/class-amp-service-worker.php | 2 +- tests/php/test-class-amp-service-worker.php | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/includes/class-amp-service-worker.php b/includes/class-amp-service-worker.php index 624e097c1bb..a175b3a7b37 100644 --- a/includes/class-amp-service-worker.php +++ b/includes/class-amp-service-worker.php @@ -54,7 +54,7 @@ public static function init() { ); } - if ( $enabled_options['cdn_script_caching'] ) { + if ( $enabled_options['cdn_script_caching'] && amp_is_canonical() ) { add_action( 'wp_front_service_worker', [ __CLASS__, 'add_cdn_script_caching' ] ); } if ( $enabled_options['image_caching'] ) { diff --git a/tests/php/test-class-amp-service-worker.php b/tests/php/test-class-amp-service-worker.php index a70c57818b7..d4fe17ee885 100644 --- a/tests/php/test-class-amp-service-worker.php +++ b/tests/php/test-class-amp-service-worker.php @@ -27,23 +27,40 @@ public function set_up() { } } + public function data_to_test_default_init_hooks() { + return [ + 'standard' => [ + 'template_mode' => 'standard', + ], + 'legacy' => [ + 'template_mode' => 'reader', + ], + ]; + } + /** * Test default hooks in init. * * @covers \AMP_Service_Worker::init() + * @dataProvider data_to_test_default_init_hooks */ - public function test_default_init_hooks() { + public function test_default_init_hooks( $template_mode ) { remove_all_filters( 'query_vars' ); remove_all_actions( 'parse_request' ); remove_all_actions( 'wp' ); remove_all_actions( 'wp_front_service_worker' ); + AMP_Options_Manager::update_option( Option::THEME_SUPPORT, $template_mode ); AMP_Service_Worker::init(); $this->assertSame( 10, has_filter( 'query_vars', [ 'AMP_Service_Worker', 'add_query_var' ] ) ); $this->assertSame( 10, has_action( 'parse_request', [ 'AMP_Service_Worker', 'handle_service_worker_iframe_install' ] ) ); $this->assertSame( 10, has_action( 'wp', [ 'AMP_Service_Worker', 'add_install_hooks' ] ) ); - $this->assertSame( 10, has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_cdn_script_caching' ] ) ); + if ( amp_is_canonical() ) { + $this->assertEquals( 10, has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_cdn_script_caching' ] ) ); + } else { + $this->assertFalse( has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_cdn_script_caching' ] ) ); + } $this->assertFalse( has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_image_caching' ] ) ); $this->assertFalse( has_action( 'wp_front_service_worker', [ 'AMP_Service_Worker', 'add_google_fonts_caching' ] ) ); }