Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the Package_Version_Tracker class #20365

Merged
merged 14 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions projects/packages/backup/actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@

// Register REST routes.
add_action( 'rest_api_init', array( 'Automattic\\Jetpack\\Backup\\REST_Controller', 'register_rest_routes' ) );

// Set up package version hook.
add_filter( 'jetpack_package_versions', 'Automattic\\Jetpack\\Backup\\Package_Version::send_package_version_to_tracker' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Add package version tracking.
14 changes: 14 additions & 0 deletions projects/packages/backup/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,18 @@ class Package_Version {

const PACKAGE_VERSION = '1.1.2-alpha';

const PACKAGE_SLUG = 'backup';

/**
* Adds the package slug and version to the package version tracker's data.
*
* @param array $package_versions The package version array.
*
* @return array The packge version array.
*/
public static function send_package_version_to_tracker( $package_versions ) {
$package_versions[ self::PACKAGE_SLUG ] = self::PACKAGE_VERSION;
return $package_versions;
}

}
46 changes: 46 additions & 0 deletions projects/packages/backup/tests/php/test-package-version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName

namespace Automattic\Jetpack\Backup;

use PHPUnit\Framework\TestCase;

/**
* Unit tests for the Package_Version class.
*
* @package automattic/jetpack-backup
*/
class Test_Package_Version extends TestCase {

/**
* Tests that the backup package version is added to the package verions array obtained by the
* Package_Version_Tracker.
*/
public function test_send_package_version_to_tracker_empty_array() {
$expected = array(
Package_Version::PACKAGE_SLUG => Package_Version::PACKAGE_VERSION,
);

add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' );

$this->assertSame( $expected, apply_filters( 'jetpack_package_versions', array() ) );
}

/**
* Tests that the backup package version is added to the package verions array obtained by the
* Package_Version_Tracker.
*/
public function test_send_package_version_to_tracker_existing_array() {
$existing_array = array(
'test-package-slug' => '1.0.0',
);

$expected = array_merge(
$existing_array,
array( Package_Version::PACKAGE_SLUG => Package_Version::PACKAGE_VERSION )
);

add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' );

$this->assertSame( $expected, apply_filters( 'jetpack_package_versions', $existing_array ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Add package version tracking.
4 changes: 4 additions & 0 deletions projects/packages/connection/src/class-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public static function configure() {

if ( $manager->is_connected() ) {
add_filter( 'xmlrpc_methods', array( $manager, 'public_xmlrpc_methods' ) );
add_filter( 'plugins_loaded', array( new Package_Version_Tracker(), 'maybe_update_package_versions' ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
add_filter( 'plugins_loaded', array( new Package_Version_Tracker(), 'maybe_update_package_versions' ) );
add_filter( 'plugins_loaded', __NAMESPACE__ . '\Package_Version_Tracker::maybe_update_package_versions' );

(since we made the method static).

Btw, while in this context the static method makes sense I'd personally prefer the non-static approach as it makes unit testing so much easier (eg the previous mockable approach was beautiful) and in case we want to add more functionality to this class it won't restrict us to make every method we add static.
However, this is just a personal preference - no need to change your logic!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was on the fence about the static methods. Since you have a preference, I changed them back to non-static. I agree that the non-static approach could make future changes significantly easier to test.

I also brought back the mocked Package_Version_Tracker::update_package_versions_option method in test_maybe_update_package_versions, and added a new test, test_maybe_update_package_versions_success, which tests the case when the HTTP request to WPCOM is successful.

}

add_action( 'rest_api_init', array( $manager, 'initialize_rest_api_registration_connector' ) );
Expand All @@ -108,6 +109,9 @@ public static function configure() {
add_filter( 'jetpack_heartbeat_stats_array', array( $manager, 'add_stats_to_heartbeat' ) );

Webhooks::init( $manager );

// Set up package version hook.
add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' );
}

/**
Expand Down
83 changes: 83 additions & 0 deletions projects/packages/connection/src/class-package-version-tracker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* The Package_Version_Tracker class.
*
* @package automattic/jetpack-connection
*/

namespace Automattic\Jetpack\Connection;

/**
* The Package_Version_Tracker class.
*/
class Package_Version_Tracker {

const PACKAGE_VERSION_OPTION = 'jetpack_package_versions';

/**
* Uses the jetpack_package_versions filter to obtain the package versions from packages that need
* version tracking. If the package versions have changed, updates the option and notifies WPCOM.
*/
public function maybe_update_package_versions() {
/**
* Obtains the package versions.
*
* @since $$next_version$$
*
* @param array An associative array of Jetpack package slugs and their corresponding versions as key/value pairs.
*/
$filter_versions = apply_filters( 'jetpack_package_versions', array() );

if ( ! is_array( $filter_versions ) ) {
return;
}

$option_versions = get_option( self::PACKAGE_VERSION_OPTION, array() );

foreach ( $filter_versions as $package => $version ) {
if ( ! is_string( $package ) || ! is_string( $version ) ) {
unset( $filter_versions[ $package ] );
}
}

if ( ! is_array( $option_versions )
|| count( array_diff_assoc( $filter_versions, $option_versions ) )
|| count( array_diff_assoc( $option_versions, $filter_versions ) )
) {
$this->update_package_versions_option( $filter_versions );
}
}

/**
* Updates the package versions:
* - 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 ) {
$site_id = \Jetpack_Options::get_option( 'id' );

$body = wp_json_encode(
array(
'package_versions' => $package_versions,
)
);

$response = Client::wpcom_json_api_request_as_blog(
sprintf( '/sites/%d/jetpack-package-versions', $site_id ),
'2',
array(
'headers' => array( 'content-type' => 'application/json' ),
'method' => 'POST',
),
$body,
'wpcom'
);

if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
update_option( self::PACKAGE_VERSION_OPTION, $package_versions );
}
}

}
16 changes: 15 additions & 1 deletion projects/packages/connection/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,19 @@
*/
class Package_Version {

const PACKAGE_VERSION = '1.30.1';
const PACKAGE_VERSION = '1.30.2-alpha';

const PACKAGE_SLUG = 'connection';

/**
* Adds the package slug and version to the package version tracker's data.
*
* @param array $package_versions The package version array.
*
* @return array The packge version array.
*/
public static function send_package_version_to_tracker( $package_versions ) {
$package_versions[ self::PACKAGE_SLUG ] = self::PACKAGE_VERSION;
return $package_versions;
}
}
46 changes: 46 additions & 0 deletions projects/packages/connection/tests/php/test-package-version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName

namespace Automattic\Jetpack\Connection;

use PHPUnit\Framework\TestCase;

/**
* Unit tests for the Package_Version class.
*
* @package automattic/jetpack-connection
*/
class Test_Package_Version extends TestCase {

/**
* Tests that the connction package version is added to the package verions array obtained by the
* Package_Version_Tracker.
*/
public function test_send_package_version_to_tracker_empty_array() {
$expected = array(
Package_Version::PACKAGE_SLUG => Package_Version::PACKAGE_VERSION,
);

add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' );

$this->assertSame( $expected, apply_filters( 'jetpack_package_versions', array() ) );
}

/**
* Tests that the connection package version is added to the package verions array obtained by the
* Package_Version_Tracker.
*/
public function test_send_package_version_to_tracker_existing_array() {
$existing_array = array(
'test-package-slug' => '1.0.0',
);

$expected = array_merge(
$existing_array,
array( Package_Version::PACKAGE_SLUG => Package_Version::PACKAGE_VERSION )
);

add_filter( 'jetpack_package_versions', __NAMESPACE__ . '\Package_Version::send_package_version_to_tracker' );

$this->assertSame( $expected, apply_filters( 'jetpack_package_versions', $existing_array ) );
}
}
Loading