feat: apply packages during imports
This commit is contained in:
@@ -4,15 +4,25 @@ namespace WPContentSync\Tests\Unit\Admin;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use WPContentSync\Admin\FileImportController;
|
||||
use WPContentSync\Content\ContentHandlerInterface;
|
||||
use WPContentSync\Content\ContentHandlerRegistry;
|
||||
use WPContentSync\Logging\LoggerInterface;
|
||||
use WPContentSync\Package\PackageChecksum;
|
||||
use WPContentSync\Package\PackageValidator;
|
||||
use WPContentSync\Settings\SettingsRepository;
|
||||
use WPContentSync\Sync\SyncContext;
|
||||
use WPContentSync\Sync\SyncEngine;
|
||||
use WPContentSync\Sync\SyncResult;
|
||||
use WPContentSync\Sync\SyncStateRepository;
|
||||
use WPContentSync\Transport\JsonFileTransport;
|
||||
|
||||
class FileImportControllerTest extends TestCase {
|
||||
/** @var array<int, string> */
|
||||
private array $temporary_files = array();
|
||||
|
||||
/** @var array<int, array<string, mixed>> */
|
||||
private array $logs = array();
|
||||
|
||||
protected function tearDown(): void {
|
||||
foreach ( $this->temporary_files as $file ) {
|
||||
if ( is_file( $file ) ) {
|
||||
@@ -21,8 +31,17 @@ class FileImportControllerTest extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
unset( $GLOBALS['wpcs_current_user_can'], $GLOBALS['wpcs_nonce_valid'], $GLOBALS['wpcs_redirect_location'] );
|
||||
$_FILES = array();
|
||||
unset(
|
||||
$GLOBALS['wpcs_current_user_can'],
|
||||
$GLOBALS['wpcs_nonce_valid'],
|
||||
$GLOBALS['wpcs_redirect_location'],
|
||||
$GLOBALS['wpcs_test_options'],
|
||||
$GLOBALS['wpcs_test_option_autoloads'],
|
||||
$GLOBALS['wpcs_test_transients'],
|
||||
$GLOBALS['wpcs_test_transient_expiration']
|
||||
);
|
||||
$_FILES = array();
|
||||
$this->logs = array();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
@@ -69,7 +88,7 @@ class FileImportControllerTest extends TestCase {
|
||||
$controller->handleImport();
|
||||
}
|
||||
|
||||
public function test_it_imports_valid_uploaded_packages_without_mutating_content(): void {
|
||||
public function test_it_imports_valid_uploaded_packages_with_sync_engine(): void {
|
||||
$file = $this->createTemporaryPackageFile( $this->validJson() );
|
||||
|
||||
$_FILES['wpcs_package_file'] = array(
|
||||
@@ -80,6 +99,22 @@ class FileImportControllerTest extends TestCase {
|
||||
$this->controller()->handleImport();
|
||||
|
||||
self::assertStringContainsString( 'wpcs_imported=1', $GLOBALS['wpcs_redirect_location'] );
|
||||
self::assertSame( 'Imported content package.', $this->logs[2]['message'] );
|
||||
self::assertSame( 0, $this->logs[2]['context']['created'] );
|
||||
}
|
||||
|
||||
public function test_it_redirects_with_error_when_sync_engine_import_fails(): void {
|
||||
$file = $this->createTemporaryPackageFile( $this->validJson() );
|
||||
|
||||
$_FILES['wpcs_package_file'] = array(
|
||||
'tmp_name' => $file,
|
||||
'error' => UPLOAD_ERR_OK,
|
||||
);
|
||||
|
||||
$this->controller( SyncResult::failure( array( 'Posts failed.' ) ) )->handleImport();
|
||||
|
||||
self::assertStringContainsString( 'wpcs_import_error=', $GLOBALS['wpcs_redirect_location'] );
|
||||
self::assertStringContainsString( 'Posts+failed.', $GLOBALS['wpcs_redirect_location'] );
|
||||
}
|
||||
|
||||
public function test_it_redirects_with_error_for_invalid_uploaded_packages(): void {
|
||||
@@ -96,33 +131,84 @@ class FileImportControllerTest extends TestCase {
|
||||
self::assertStringContainsString( 'not+valid+JSON', $GLOBALS['wpcs_redirect_location'] );
|
||||
}
|
||||
|
||||
private function controller(): FileImportController {
|
||||
private function controller( ?SyncResult $result = null ): FileImportController {
|
||||
$logger = $this->logger();
|
||||
|
||||
return new FileImportController(
|
||||
new JsonFileTransport( new PackageValidator() ),
|
||||
new class() implements LoggerInterface {
|
||||
/**
|
||||
* @param array<string, mixed> $context Context.
|
||||
*/
|
||||
public function error( string $message, array $context = array() ): void {}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $context Context.
|
||||
*/
|
||||
public function warning( string $message, array $context = array() ): void {}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $context Context.
|
||||
*/
|
||||
public function info( string $message, array $context = array() ): void {}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $context Context.
|
||||
*/
|
||||
public function debug( string $message, array $context = array() ): void {}
|
||||
}
|
||||
$logger,
|
||||
$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( $this->logs ) implements LoggerInterface {
|
||||
/** @var array<int, array<string, mixed>> */
|
||||
private array $logs;
|
||||
|
||||
/**
|
||||
* @param array<int, array<string, mixed>> $logs Logs.
|
||||
*/
|
||||
public function __construct( array &$logs ) {
|
||||
$this->logs = &$logs;
|
||||
}
|
||||
|
||||
public function error( string $message, array $context = array() ): void {
|
||||
$this->record( 'error', $message, $context );
|
||||
}
|
||||
|
||||
public function warning( string $message, array $context = array() ): void {
|
||||
$this->record( 'warning', $message, $context );
|
||||
}
|
||||
|
||||
public function info( string $message, array $context = array() ): void {
|
||||
$this->record( 'info', $message, $context );
|
||||
}
|
||||
|
||||
public function debug( string $message, array $context = array() ): void {
|
||||
$this->record( 'debug', $message, $context );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $context Context.
|
||||
*/
|
||||
private function record( string $level, string $message, array $context ): void {
|
||||
$this->logs[] = array(
|
||||
'level' => $level,
|
||||
'message' => $message,
|
||||
'context' => $context,
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private function validJson(): string {
|
||||
$records = array(
|
||||
'posts' => array(),
|
||||
|
||||
Reference in New Issue
Block a user