Skip to content

Commit

Permalink
Jetpack Sync: Ensure duplicate Sync modules are not loaded (#38503)
Browse files Browse the repository at this point in the history
* Jetpack Sync: Ensure duplicate Sync modules are removed
  • Loading branch information
fgiannar authored Jul 25, 2024
1 parent 1f03c74 commit 5526089
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions projects/packages/sync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ the following modules will be enabled no matter the configuration:
- `Automattic\\Jetpack\\Sync\\Modules\\Stats`
- `Automattic\\Jetpack\\Sync\\Modules\\Updates`

**Attention**: Sync currently only supports configuring the list of [default Sync modules](https://github.com/Automattic/jetpack/blob/trunk/projects/packages/sync/src/class-modules.php#L25). Any modules that Sync already loads conditionally, such as `WooCommerce` or `Search` are **NOT** configurable.

##### `jetpack_sync_options_whitelist` / `jetpack_sync_options_contentless`

**Controlled by the Sync Options Module, which is required.**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Jetpack Sync: Ensure duplicate Sync modules are not loaded
2 changes: 2 additions & 0 deletions projects/packages/sync/src/class-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ public static function initialize_modules() {
*/
$modules = apply_filters( 'jetpack_sync_modules', self::DEFAULT_SYNC_MODULES );

$modules = array_unique( $modules );

$modules = array_map( array( __CLASS__, 'load_module' ), $modules );

return array_map( array( __CLASS__, 'set_module_defaults' ), $modules );
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/sync/src/class-package-version.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
class Package_Version {

const PACKAGE_VERSION = '3.3.0';
const PACKAGE_VERSION = '3.3.1-alpha';

const PACKAGE_SLUG = 'sync';

Expand Down
63 changes: 63 additions & 0 deletions projects/packages/sync/tests/php/test-modules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName

namespace Automattic\Jetpack\Sync;

use WorDBless\BaseTestCase;

/**
* Unit tests for the Automattic\Jetpack\Sync\Modules class.
*
* @package automattic/jetpack-sync
*/
class Test_Modules extends BaseTestCase {

/**
* Runs before every test in this class.
*/
public function set_up() {
// Reset private static properties after each test.
$reflection_class = new \ReflectionClass( '\Automattic\Jetpack\Sync\Modules' );
try {
$reflection_class->setStaticPropertyValue( 'initialized_modules', null );
} catch ( \ReflectionException $e ) { // PHP 7 compat
$configured = $reflection_class->getProperty( 'initialized_modules' );
$configured->setAccessible( true );
$configured->setValue( null );
}
}

/**
* Runs after every test in this class.
*/
public function tear_down() {
remove_filter( 'jetpack_sync_modules', array( $this, 'add_posts_module' ) );
}

/**
* Tests get_modules with duplicate modules.
*/
public function test_get_modules_will_remove_duplicates() {
add_filter( 'jetpack_sync_modules', array( $this, 'add_posts_module' ) );

$modules = Modules::get_modules();
$module_classes = array();
foreach ( $modules as $module ) {
$module_classes[] = get_class( $module );
}

$this->assertSame( Modules::DEFAULT_SYNC_MODULES, $module_classes );
}

/**
* Adds Sync Posts module to Sync's module list.
*
* @param array $sync_modules The list of sync modules declared prior to this filter.
*
* @return array A list of sync modules that now includes Posts modules.
*/
public function add_posts_module( array $sync_modules ) {
$sync_modules[] = 'Automattic\\Jetpack\\Sync\\Modules\\Posts';

return $sync_modules;
}
}

0 comments on commit 5526089

Please sign in to comment.