feat: apply packages during imports

This commit is contained in:
Keith Solomon
2026-05-01 15:23:25 -05:00
parent e9f4b17229
commit 825e89b55f
6 changed files with 355 additions and 48 deletions
+108 -8
View File
@@ -8,9 +8,17 @@
namespace WPContentSync\Tests\Unit\Rest;
use PHPUnit\Framework\TestCase;
use WPContentSync\Content\ContentHandlerInterface;
use WPContentSync\Content\ContentHandlerRegistry;
use WPContentSync\Logging\LoggerInterface;
use WPContentSync\Package\PackageChecksum;
use WPContentSync\Package\PackageValidator;
use WPContentSync\Rest\RestPackageController;
use WPContentSync\Settings\SettingsRepository;
use WPContentSync\Sync\SyncContext;
use WPContentSync\Sync\SyncEngine;
use WPContentSync\Sync\SyncResult;
use WPContentSync\Sync\SyncStateRepository;
class RestPackageControllerTest extends TestCase {
protected function setUp(): void {
@@ -20,13 +28,21 @@ class RestPackageControllerTest extends TestCase {
}
protected function tearDown(): void {
unset( $GLOBALS['wpcs_rest_routes'], $GLOBALS['wpcs_current_user_can'], $GLOBALS['wpcs_test_actions'] );
unset(
$GLOBALS['wpcs_rest_routes'],
$GLOBALS['wpcs_current_user_can'],
$GLOBALS['wpcs_test_actions'],
$GLOBALS['wpcs_test_options'],
$GLOBALS['wpcs_test_option_autoloads'],
$GLOBALS['wpcs_test_transients'],
$GLOBALS['wpcs_test_transient_expiration']
);
parent::tearDown();
}
public function test_it_hooks_route_registration_to_rest_api_init(): void {
$controller = new RestPackageController( new PackageValidator() );
$controller = $this->controller();
$controller->register();
self::assertSame(
@@ -36,7 +52,7 @@ class RestPackageControllerTest extends TestCase {
}
public function test_it_registers_status_and_package_routes(): void {
$controller = new RestPackageController( new PackageValidator() );
$controller = $this->controller();
$controller->registerRoutes();
self::assertArrayHasKey( 'wp-content-sync/v1/status', $GLOBALS['wpcs_rest_routes'] );
@@ -45,13 +61,13 @@ class RestPackageControllerTest extends TestCase {
public function test_it_requires_manage_options_permission(): void {
$GLOBALS['wpcs_current_user_can']['manage_options'] = false;
$controller = new RestPackageController( new PackageValidator() );
$controller = $this->controller();
self::assertFalse( $controller->canReceivePackage() );
}
public function test_it_returns_status_payload(): void {
$controller = new RestPackageController( new PackageValidator() );
$controller = $this->controller();
self::assertSame(
array(
@@ -64,7 +80,7 @@ class RestPackageControllerTest extends TestCase {
}
public function test_it_accepts_valid_packages(): void {
$controller = new RestPackageController( new PackageValidator() );
$controller = $this->controller();
self::assertSame(
$this->acceptedResponse(),
@@ -77,7 +93,7 @@ class RestPackageControllerTest extends TestCase {
}
public function test_it_accepts_rest_request_like_objects(): void {
$controller = new RestPackageController( new PackageValidator() );
$controller = $this->controller();
$request = new class(
array(
'package' => $this->validPackage(),
@@ -105,7 +121,7 @@ class RestPackageControllerTest extends TestCase {
}
public function test_it_rejects_invalid_package_shapes(): void {
$controller = new RestPackageController( new PackageValidator() );
$controller = $this->controller();
self::assertSame(
array(
@@ -116,6 +132,36 @@ class RestPackageControllerTest extends TestCase {
);
}
public function test_it_returns_rejected_response_when_sync_import_fails(): void {
$controller = $this->controller( SyncResult::failure( array( 'Posts failed.' ) ) );
self::assertSame(
array(
'accepted' => false,
'schema_version' => '1.0',
'manifest' => array(
'posts' => 0,
'terms' => 0,
'media' => 0,
'custom_post_types' => 0,
),
'import' => array(
'successful' => false,
'created' => 0,
'updated' => 0,
'skipped' => 0,
'conflicts' => 0,
'errors' => array( 'Posts failed.' ),
),
),
$controller->receivePackage(
array(
'package' => $this->validPackage(),
)
)
);
}
/**
* @return array<string, mixed>
*/
@@ -129,9 +175,63 @@ class RestPackageControllerTest extends TestCase {
'media' => 0,
'custom_post_types' => 0,
),
'import' => array(
'successful' => true,
'created' => 0,
'updated' => 0,
'skipped' => 0,
'conflicts' => 0,
'errors' => array(),
),
);
}
private function controller( ?SyncResult $result = null ): RestPackageController {
return new RestPackageController(
new PackageValidator(),
$this->syncEngine( $result ?? SyncResult::success() )
);
}
private function syncEngine( SyncResult $result ): SyncEngine {
return new SyncEngine(
new ContentHandlerRegistry( array( $this->handler( $result ) ) ),
new SyncStateRepository(),
new SettingsRepository(),
$this->logger()
);
}
private function handler( SyncResult $result ): ContentHandlerInterface {
return new class( $result ) implements ContentHandlerInterface {
private SyncResult $result;
public function __construct( SyncResult $result ) {
$this->result = $result;
}
public function bucket(): string {
return 'posts';
}
public function importRecords( array $records, SyncContext $context ): SyncResult {
return $this->result;
}
};
}
private function logger(): LoggerInterface {
return new class() implements LoggerInterface {
public function error( string $message, array $context = array() ): void {}
public function warning( string $message, array $context = array() ): void {}
public function info( string $message, array $context = array() ): void {}
public function debug( string $message, array $context = array() ): void {}
};
}
/**
* @return array<string, mixed>
*/