-
Notifications
You must be signed in to change notification settings - Fork 800
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
Sync: add a new method, do_only_first_initial_sync #21676
Merged
Merged
Changes from 5 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cec23fc
Sync: do not run an initial sync if an initial sync has already been run
kbrown9 9c7f164
Try to fix failing unit test.
kbrown9 8eb7b1a
Update unit test and update package version.
kbrown9 bbd288d
Merge remote-tracking branch 'origin/master' into update/add_method_f…
kbrown9 cb73696
Update the units tests to add jetpack_site_registered to the current …
kbrown9 795ca71
Add the do_only_first_initial_sync method
kbrown9 6fae378
* Remove the sync_allowed check because it's done later.
kbrown9 2cad7e8
Merge remote-tracking branch 'origin/master' into update/add_method_f…
kbrown9 148695d
[not verified] Remove unnecessary changes to the Jetpack unit tests f…
kbrown9 771a2b1
[not verified] Remove another unnecessary unit test change
kbrown9 97e4491
Remove unnecessary set_constant call from unit test.
kbrown9 7aa6b52
Update changelog file
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
4 changes: 4 additions & 0 deletions
4
projects/packages/sync/changelog/update-add_method_for_initial_sync_check
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: changed | ||
|
||
Actions: prevent an initial sync when not registering and an intial sync has already been started |
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
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
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,95 @@ | ||
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | ||
kbrown9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace Automattic\Jetpack\Sync; | ||
|
||
use Automattic\Jetpack\Constants; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* Unit tests for the Actions class. | ||
* | ||
* @package automattic/jetpack-sync | ||
*/ | ||
class Test_Actions extends TestCase { | ||
|
||
/** | ||
* Tests the should_do_initial_sync method. | ||
* | ||
* @param bool $doing_registered_action Whether the 'jetpack_site_registered' action is currently in progress. | ||
* @param mixed $started The 'jetpack_sync_full_status' option's 'started' field value. | ||
* @param mixed $finished The 'jetpack_sync_full_status' option's 'finished' field value. | ||
* @param bool $expected_result The value that Actions::should_do_initial_sync is expected to return. | ||
* | ||
* @dataProvider data_provider_test_should_do_initial_sync | ||
*/ | ||
public function test_should_do_intitial_sync( $doing_registered_action, $started, $finished, $expected_result ) { | ||
Constants::set_constant( 'JETPACK_DISABLE_RAW_OPTIONS', true ); | ||
$full_sync_option = array( | ||
'started' => $started, | ||
'finished' => $finished, | ||
'progress' => array(), | ||
'config' => array(), | ||
); | ||
|
||
\Jetpack_Options::update_raw_option( Modules\Full_Sync_Immediately::STATUS_OPTION, $full_sync_option ); | ||
|
||
global $wp_current_filter; | ||
$original_filter = $wp_current_filter; | ||
if ( $doing_registered_action ) { | ||
$wp_current_filter = array( 'jetpack_site_registered' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited | ||
} | ||
|
||
$result = Actions::should_do_initial_sync(); | ||
|
||
delete_option( Modules\Full_Sync_Immediately::STATUS_OPTION ); | ||
$wp_current_filter = $original_filter; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited | ||
|
||
$this->assertSame( $expected_result, $result ); | ||
} | ||
|
||
/** | ||
* Data provider for the test_should_do_initial_sync method. | ||
* | ||
* @return array The test data with the format: | ||
* [ | ||
* 'do_registered_action" => (bool) Whether the 'jetpack_site_registered' action is currently in progress. | ||
* 'started' => (mixed) The 'jetpack_sync_full_status' option's 'started' field value. | ||
* 'finished' => (mixed) The 'jetpack_sync_full_status' option's 'finished' field value. | ||
* 'expected_result' => (bool) The value that Actions::should_do_initial_sync is expected to return. | ||
* ] | ||
*/ | ||
public function data_provider_test_should_do_initial_sync() { | ||
return array( | ||
'registering, full sync started' => array( | ||
'do_registered_action' => true, | ||
'started' => time(), | ||
'finished' => false, | ||
'expected_result' => false, | ||
), | ||
'registering, full sync finished' => array( | ||
'do_registered_action' => true, | ||
'started' => time(), | ||
'finished' => time(), | ||
'expected_result' => true, | ||
), | ||
'registering, full sync not started' => array( | ||
'do_registered_action' => true, | ||
'started' => false, | ||
'finished' => false, | ||
'expected_result' => true, | ||
), | ||
'not registering, full sync started' => array( | ||
'do_registered_action' => false, | ||
'started' => time(), | ||
'finished' => time(), | ||
'expected_result' => false, | ||
), | ||
'not registering, full sync not started' => array( | ||
'do_registered_action' => false, | ||
'started' => false, | ||
'finished' => false, | ||
'expected_result' => true, | ||
), | ||
); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
projects/plugins/jetpack/changelog/update-add_method_for_initial_sync_check
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,5 @@ | ||
Significance: patch | ||
Type: other | ||
Comment: Fix a sync unit test | ||
|
||
|
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
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
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.
@kbrown9 can you outline why this check on 'jetpack_site_registered' is added? Logically it does make sense but it is a departure from existing functionality where the action 'jetpack_user_authorized' can also trigger do_initial_sync.
Also won't this check on is_started cause issues with sites that delete Jetpack than re-install?
Would it make sense to add flag of $strict or similar that would preserve existing flow and then add this if statement only if strict is true? This way WC Pay can call
do_initial_sync( true )
and get the flow they want but not change existing flows?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 like your proposal to add a flag. I think an even better approach would be to introduce a new method,
Actions::do_only_first_initial_sync
, which consumer plugins could call when they want an initial sync run only when one has never been run before.The
Actions::do_intial_sync
method is hooked to a few actions, and thejetpack_site_registered
action passes a few arguments to the callback functions. We specify thatActions::do_initial_sync
accepts 0 arguments from the action hook here, but if that ever needed to change, I think we would run into problems.I think introducing the new
Actions::do_only_first_initial_sync
method will be both clearer and safer than adding a flag to the existingActions::do_intial_sync
method. What do you think of this approach?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 agree a new method is cleaner and safer for usage. 👍 from me on this approach