Skip to content

Commit

Permalink
Connection: Don't run package version tracker if not all packages wer…
Browse files Browse the repository at this point in the history
…e initialized (#35402)

* Connection: Don't run package version tracker if not all packages were initialized.

---------

Co-authored-by: Foteini Giannaropoulou <giannaropoulou.foteini@gmail.com>

Committed via a GitHub action: https://github.com/Automattic/jetpack/actions/runs/7754428015
  • Loading branch information
fgiannar authored and matticbot committed Feb 2, 2024
1 parent b6ce979 commit 23efe40
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/class-package-version-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ class Package_Version_Tracker {
* version tracking. If the package versions have changed, updates the option and notifies WPCOM.
*/
public function maybe_update_package_versions() {
// Do not run too early or all the modules may not be loaded.
if ( ! did_action( 'init' ) ) {
return;
}

// The version check is being rate limited.
if ( $this->is_rate_limiting() ) {
// The version check is being rate limited.
return;
}

Expand Down
31 changes: 31 additions & 0 deletions tests/php/test_package_version_tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,37 @@ function () {
$this->assertSame( self::PACKAGE_VERSIONS, get_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION ) );
}

/**
* 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'] );

\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' ) );

update_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION, self::PACKAGE_VERSIONS );

add_filter(
'jetpack_package_versions',
function () {
return self::CHANGED_VERSIONS;
}
);

( new Package_Version_Tracker() )->maybe_update_package_versions();

remove_filter( 'pre_http_request', array( $this, 'intercept_http_request_success' ) );

$this->assertFalse( $this->http_request_attempted );

$this->assertSame( self::PACKAGE_VERSIONS, get_option( Package_Version_Tracker::PACKAGE_VERSION_OPTION ) );
}

/**
* Tests the maybe_update_package_versions method when the HTTP request to WPCOM has already failed within last hour..
*/
Expand Down

0 comments on commit 23efe40

Please sign in to comment.