diff --git a/projects/packages/connection/changelog/update-jetpack-package-versions-add-to-sync b/projects/packages/connection/changelog/update-jetpack-package-versions-add-to-sync new file mode 100644 index 0000000000000..f857ded66fb6d --- /dev/null +++ b/projects/packages/connection/changelog/update-jetpack-package-versions-add-to-sync @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Jetpack Connection: Add jetpack_package_versions to Sync diff --git a/projects/packages/connection/src/class-package-version-tracker.php b/projects/packages/connection/src/class-package-version-tracker.php index 1e54313a18491..2d1b374336a08 100644 --- a/projects/packages/connection/src/class-package-version-tracker.php +++ b/projects/packages/connection/src/class-package-version-tracker.php @@ -85,13 +85,43 @@ public function maybe_update_package_versions() { } /** - * Updates the package versions: + * Updates the package versions option. + * + * @param array $package_versions The package versions. + */ + protected function update_package_versions_option( $package_versions ) { + if ( ! $this->is_sync_enabled() ) { + $this->update_package_versions_via_remote_request( $package_versions ); + return; + } + + update_option( self::PACKAGE_VERSION_OPTION, $package_versions ); + } + + /** + * Whether Jetpack Sync is enabled. + * + * @return boolean true if Sync is present and enabled, false otherwise + */ + protected function is_sync_enabled() { + if ( class_exists( 'Automattic\Jetpack\Sync\Settings' ) && \Automattic\Jetpack\Sync\Settings::is_sync_enabled() ) { + + return true; + } + + return false; + } + + /** + * Fallback for updating the package versions via a remote request when Sync is not present. + * + * Updates the package versions as follows: * - Sends the updated package versions to wpcom. * - Updates the 'jetpack_package_versions' option. * * @param array $package_versions The package versions. */ - protected function update_package_versions_option( $package_versions ) { + protected function update_package_versions_via_remote_request( $package_versions ) { $connection = new Manager(); if ( ! $connection->is_connected() ) { return; diff --git a/projects/packages/connection/tests/php/test_package_version_tracker.php b/projects/packages/connection/tests/php/test_package_version_tracker.php index 8050bb7552b15..c35b126515397 100644 --- a/projects/packages/connection/tests/php/test_package_version_tracker.php +++ b/projects/packages/connection/tests/php/test_package_version_tracker.php @@ -54,6 +54,10 @@ public function set_up() { * @after */ public function tear_down() { + global $wp_actions; + // Restore `init` in global $wp_actions. + $wp_actions['init'] = true; + $this->http_request_attempted = false; Constants::clear_constants(); WorDBless_Options::init()->clear_options(); @@ -237,13 +241,15 @@ public function jetpack_maybe_update_package_versions_data_provider() { } /** - * Tests the maybe_update_package_versions method when the HTTP request to WPCOM succeeds. + * Tests the maybe_update_package_versions method with rate limit applied. */ - public function test_maybe_update_package_versions_success() { - \Jetpack_Options::update_option( 'blog_token', 'asdasd.123123' ); - \Jetpack_Options::update_option( 'id', 1234 ); + public function test_maybe_update_package_versions_with_rate_limit() { + // Rate limit. + set_transient( Package_Version_Tracker::RATE_LIMITER_KEY, time() ); - add_filter( 'pre_http_request', array( $this, 'intercept_http_request_success' ) ); + $tracker = $this->getMockBuilder( 'Automattic\Jetpack\Connection\Package_Version_Tracker' ) + ->setMethods( array( 'update_package_versions_option' ) ) + ->getMock(); update_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION, self::PACKAGE_VERSIONS ); @@ -254,20 +260,25 @@ function () { } ); - ( new Package_Version_Tracker() )->maybe_update_package_versions(); + $tracker->expects( $this->never() ) + ->method( 'update_package_versions_option' ); - remove_filter( 'pre_http_request', array( $this, 'intercept_http_request_success' ) ); - - $this->assertTrue( $this->http_request_attempted ); + $tracker->maybe_update_package_versions(); - $this->assertSame( self::CHANGED_VERSIONS, get_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION ) ); + $this->assertSame( self::PACKAGE_VERSIONS, get_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION ) ); } - /** - * Tests the maybe_update_package_versions method when the site is not connected. - */ - public function test_maybe_update_package_versions_not_connected() { - add_filter( 'pre_http_request', array( $this, 'intercept_http_request_failure' ) ); + /** + * Tests the maybe_update_package_versions method when the `init` hook is not fired. + */ + public function test_maybe_update_package_versions_with_init_hook_not_fired() { + global $wp_actions; + // Remove `init` from global $wp_actions. + unset( $wp_actions['init'] ); + + $tracker = $this->getMockBuilder( 'Automattic\Jetpack\Connection\Package_Version_Tracker' ) + ->setMethods( array( 'update_package_versions_option' ) ) + ->getMock(); update_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION, self::PACKAGE_VERSIONS ); @@ -278,27 +289,22 @@ function () { } ); - ( new Package_Version_Tracker() )->maybe_update_package_versions(); - - remove_filter( 'pre_http_request', array( $this, 'intercept_http_request_failure' ) ); + $tracker->expects( $this->never() ) + ->method( 'update_package_versions_option' ); - $this->assertFalse( $this->http_request_attempted ); + $tracker->maybe_update_package_versions(); $this->assertSame( self::PACKAGE_VERSIONS, get_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION ) ); - - $failed_request_cached = get_transient( Package_Version_Tracker::CACHED_FAILED_REQUEST_KEY ); - - $this->assertFalse( $failed_request_cached ); } /** - * Tests the maybe_update_package_versions method when the HTTP request to WPCOM fails. + * Tests the maybe_update_package_versions method with Sync disabled when the HTTP request to WPCOM succeeds. */ - public function test_maybe_update_package_versions_failure() { + public function test_maybe_update_package_versions_with_sync_disabled_remote_request_success() { \Jetpack_Options::update_option( 'blog_token', 'asdasd.123123' ); \Jetpack_Options::update_option( 'id', 1234 ); - add_filter( 'pre_http_request', array( $this, 'intercept_http_request_failure' ) ); + add_filter( 'pre_http_request', array( $this, 'intercept_http_request_success' ) ); update_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION, self::PACKAGE_VERSIONS ); @@ -311,28 +317,20 @@ function () { ( new Package_Version_Tracker() )->maybe_update_package_versions(); - remove_filter( 'pre_http_request', array( $this, 'intercept_http_request_failure' ) ); + remove_filter( 'pre_http_request', array( $this, 'intercept_http_request_success' ) ); $this->assertTrue( $this->http_request_attempted ); - $this->assertSame( self::PACKAGE_VERSIONS, get_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION ) ); - - $failed_request_cached = get_transient( Package_Version_Tracker::CACHED_FAILED_REQUEST_KEY ); - - $this->assertNotFalse( $failed_request_cached ); + $this->assertSame( self::CHANGED_VERSIONS, get_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION ) ); } /** - * Tests the maybe_update_package_versions method with rate limit applied. + * Tests the maybe_update_package_versions method with Sync disabled when the site is not connected. */ - public function test_maybe_update_package_versions_with_rate_limit() { - \Jetpack_Options::update_option( 'blog_token', 'asdasd.123123' ); - \Jetpack_Options::update_option( 'id', 1234 ); - - add_filter( 'pre_http_request', array( $this, 'intercept_http_request_success' ) ); + public function test_maybe_update_package_versions_with_sync_disabled_not_connected() { + add_filter( 'pre_http_request', array( $this, 'intercept_http_request_failure' ) ); update_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION, self::PACKAGE_VERSIONS ); - set_transient( Package_Version_Tracker::RATE_LIMITER_KEY, time() ); add_filter( 'jetpack_package_versions', @@ -343,25 +341,25 @@ function () { ( new Package_Version_Tracker() )->maybe_update_package_versions(); - remove_filter( 'pre_http_request', array( $this, 'intercept_http_request_success' ) ); + remove_filter( 'pre_http_request', array( $this, 'intercept_http_request_failure' ) ); $this->assertFalse( $this->http_request_attempted ); $this->assertSame( self::PACKAGE_VERSIONS, get_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION ) ); + + $failed_request_cached = get_transient( Package_Version_Tracker::CACHED_FAILED_REQUEST_KEY ); + + $this->assertFalse( $failed_request_cached ); } /** - * Tests the maybe_update_package_versions method when the `init` hook is not fired. + * Tests the maybe_update_package_versions method with Sync disabled when the HTTP request to WPCOM fails. */ - public function test_maybe_update_package_versions_with_init_hook_not_fired() { - global $wp_actions; - // Remove `init` from global $wp_actions. - unset( $wp_actions['init'] ); - + public function test_maybe_update_package_versions_with_sync_disabled_remote_request_failure() { \Jetpack_Options::update_option( 'blog_token', 'asdasd.123123' ); \Jetpack_Options::update_option( 'id', 1234 ); - add_filter( 'pre_http_request', array( $this, 'intercept_http_request_success' ) ); + add_filter( 'pre_http_request', array( $this, 'intercept_http_request_failure' ) ); update_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION, self::PACKAGE_VERSIONS ); @@ -374,11 +372,15 @@ function () { ( new Package_Version_Tracker() )->maybe_update_package_versions(); - remove_filter( 'pre_http_request', array( $this, 'intercept_http_request_success' ) ); + remove_filter( 'pre_http_request', array( $this, 'intercept_http_request_failure' ) ); - $this->assertFalse( $this->http_request_attempted ); + $this->assertTrue( $this->http_request_attempted ); $this->assertSame( self::PACKAGE_VERSIONS, get_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION ) ); + + $failed_request_cached = get_transient( Package_Version_Tracker::CACHED_FAILED_REQUEST_KEY ); + + $this->assertNotFalse( $failed_request_cached ); } /** @@ -410,6 +412,35 @@ function () { $this->assertSame( self::PACKAGE_VERSIONS, get_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION ) ); } + /** + * Tests the maybe_update_package_versions method with Sync disabled when the HTTP request to WPCOM succeeds. + */ + public function test_maybe_update_package_versions_with_sync_enabled() { + $tracker = $this->getMockBuilder( 'Automattic\Jetpack\Connection\Package_Version_Tracker' ) + ->setMethods( array( 'update_package_versions_via_remote_request', 'is_sync_enabled' ) ) + ->getMock(); + + update_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION, self::PACKAGE_VERSIONS ); + + add_filter( + 'jetpack_package_versions', + function () { + return self::CHANGED_VERSIONS; + } + ); + + $tracker->expects( $this->once() ) + ->method( 'is_sync_enabled' ) + ->willReturn( true ); + + $tracker->expects( $this->never() ) + ->method( 'update_package_versions_via_remote_request' ); + + $tracker->maybe_update_package_versions(); + + $this->assertSame( self::CHANGED_VERSIONS, get_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION ) ); + } + /** * Intercept the API request sent to WP.com, and mock success response. * diff --git a/projects/packages/sync/changelog/update-jetpack-package-versions-add-to-sync b/projects/packages/sync/changelog/update-jetpack-package-versions-add-to-sync new file mode 100644 index 0000000000000..f857ded66fb6d --- /dev/null +++ b/projects/packages/sync/changelog/update-jetpack-package-versions-add-to-sync @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Jetpack Connection: Add jetpack_package_versions to Sync diff --git a/projects/packages/sync/src/class-data-settings.php b/projects/packages/sync/src/class-data-settings.php index c2f9288cb5a4e..7ce5e2d2cd412 100644 --- a/projects/packages/sync/src/class-data-settings.php +++ b/projects/packages/sync/src/class-data-settings.php @@ -73,6 +73,7 @@ class Data_Settings { * Connection related options */ 'jetpack_connection_active_plugins', + 'jetpack_package_versions', /** * Generic site options */ diff --git a/projects/packages/sync/src/class-defaults.php b/projects/packages/sync/src/class-defaults.php index 90b6e28240633..69336ca8fdf92 100644 --- a/projects/packages/sync/src/class-defaults.php +++ b/projects/packages/sync/src/class-defaults.php @@ -193,6 +193,7 @@ class Defaults { 'wpcom_subscription_emails_use_excerpt', 'jetpack_verbum_subscription_modal', 'jetpack_blocks_disabled', + 'jetpack_package_versions', ); /** diff --git a/projects/packages/sync/tests/php/data-test-data-settings.php b/projects/packages/sync/tests/php/data-test-data-settings.php index b82fb67cf2584..0803a941871c2 100644 --- a/projects/packages/sync/tests/php/data-test-data-settings.php +++ b/projects/packages/sync/tests/php/data-test-data-settings.php @@ -234,6 +234,7 @@ public static function data_test_3() { 'jetpack_sync_settings_dedicated_sync_enabled', 'jetpack_sync_settings_custom_queue_table_enabled', 'jetpack_connection_active_plugins', + 'jetpack_package_versions', 'blog_charset', 'blog_public', 'blogdescription', @@ -521,6 +522,7 @@ public static function data_test_7() { 'jetpack_sync_settings_dedicated_sync_enabled', 'jetpack_sync_settings_custom_queue_table_enabled', 'jetpack_connection_active_plugins', + 'jetpack_package_versions', 'blog_charset', 'blog_public', 'blogdescription', @@ -573,6 +575,7 @@ public static function data_test_7_1() { 'jetpack_sync_settings_dedicated_sync_enabled', 'jetpack_sync_settings_custom_queue_table_enabled', 'jetpack_connection_active_plugins', + 'jetpack_package_versions', 'blog_charset', 'blog_public', 'blogdescription', @@ -628,6 +631,7 @@ public static function data_test_7_2() { 'jetpack_sync_settings_dedicated_sync_enabled', 'jetpack_sync_settings_custom_queue_table_enabled', 'jetpack_connection_active_plugins', + 'jetpack_package_versions', 'blog_charset', 'blog_public', 'blogdescription', @@ -863,6 +867,7 @@ public static function data_test_10() { 'jetpack_sync_settings_dedicated_sync_enabled', 'jetpack_sync_settings_custom_queue_table_enabled', 'jetpack_connection_active_plugins', + 'jetpack_package_versions', 'blog_charset', 'blog_public', 'blogdescription', diff --git a/projects/plugins/jetpack/changelog/update-jetpack-package-versions-add-to-sync b/projects/plugins/jetpack/changelog/update-jetpack-package-versions-add-to-sync new file mode 100644 index 0000000000000..f91b639f1e17b --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-jetpack-package-versions-add-to-sync @@ -0,0 +1,5 @@ +Significance: patch +Type: other +Comment: Update Sync related unit tests + + diff --git a/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-options.php b/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-options.php index 4720ea1bb49ae..7c70473302d6f 100644 --- a/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-options.php +++ b/projects/plugins/jetpack/tests/php/sync/test_class.jetpack-sync-options.php @@ -245,6 +245,7 @@ public function test_sync_default_options() { 'jetpack_verbum_subscription_modal' => true, 'jetpack_blocks_disabled' => false, 'wpcom_ai_site_prompt' => '', + 'jetpack_package_versions' => array(), ); $theme_mod_key = 'theme_mods_' . get_option( 'stylesheet' );