-
Notifications
You must be signed in to change notification settings - Fork 805
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8424af8
Add the jetpack_package_versions filter and the Package_Version_Track…
kbrown9 f7f4d0e
Add unit tests.
kbrown9 6edc194
Merge remote-tracking branch 'origin/master' into update/package_vers…
kbrown9 8f75e30
Update versions.
kbrown9 17ecd1e
Better handling of situation where versions are not strings.
kbrown9 99bb5e1
Merge remote-tracking branch 'origin/master' into update/package_vers…
kbrown9 a35cd3b
Updates:
kbrown9 5fcd1f7
Respond to review comments:
kbrown9 b468619
Update projects/packages/connection/src/class-package-version-tracker…
kbrown9 d8a2d56
Update sync package version.
kbrown9 243f34f
Merge remote-tracking branch 'origin/master' into update/package_vers…
kbrown9 b577ee0
Merge remote-tracking branch 'origin/master' into update/package_vers…
kbrown9 4222bd8
Fix unit test description.
kbrown9 ef1d9ea
Merge remote-tracking branch 'origin/master' into update/package_vers…
kbrown9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
projects/packages/backup/changelog/update-package_version_filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: added | ||
|
||
Add package version tracking. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
projects/packages/backup/tests/php/test-package-version.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
projects/packages/connection/changelog/update-package_version_filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: added | ||
|
||
Add package version tracking. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
projects/packages/connection/src/class-package-version-tracker.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
projects/packages/connection/tests/php/test-package-version.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ) ); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(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!
There was a problem hiding this comment.
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 intest_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.