Skip to content

Commit

Permalink
Jetpack Sync: Add 'jetpack_package_versions' to Sync option whitelist (
Browse files Browse the repository at this point in the history
…#35409)

* Jetpack Sync: Add jetpack_package_versions to Sync options
  • Loading branch information
fgiannar authored Feb 5, 2024
1 parent a9797ca commit 65706e8
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Jetpack Connection: Add jetpack_package_versions to Sync
34 changes: 32 additions & 2 deletions projects/packages/connection/src/class-package-version-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 );

Expand All @@ -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 );

Expand All @@ -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 );

Expand All @@ -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',
Expand All @@ -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 );

Expand All @@ -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 );
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: changed

Jetpack Connection: Add jetpack_package_versions to Sync
1 change: 1 addition & 0 deletions projects/packages/sync/src/class-data-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Data_Settings {
* Connection related options
*/
'jetpack_connection_active_plugins',
'jetpack_package_versions',
/**
* Generic site options
*/
Expand Down
1 change: 1 addition & 0 deletions projects/packages/sync/src/class-defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ class Defaults {
'wpcom_subscription_emails_use_excerpt',
'jetpack_verbum_subscription_modal',
'jetpack_blocks_disabled',
'jetpack_package_versions',
);

/**
Expand Down
5 changes: 5 additions & 0 deletions projects/packages/sync/tests/php/data-test-data-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Update Sync related unit tests


Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down

0 comments on commit 65706e8

Please sign in to comment.