fix: handle invalid package uploads
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace WPContentSync\Tests\Unit\Admin;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use WPContentSync\Settings\Settings;
|
||||
|
||||
class DashboardTemplateTest extends TestCase {
|
||||
protected function tearDown(): void {
|
||||
$_GET = array();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_it_renders_import_error_notices(): void {
|
||||
$_GET['wpcs_import_error'] = 'The selected file is not valid JSON.';
|
||||
|
||||
$output = $this->renderDashboard();
|
||||
|
||||
self::assertStringContainsString( 'notice-error', $output );
|
||||
self::assertStringContainsString( 'The selected file is not valid JSON.', $output );
|
||||
}
|
||||
|
||||
public function test_it_renders_import_success_notices(): void {
|
||||
$_GET['wpcs_imported'] = '1';
|
||||
|
||||
$output = $this->renderDashboard();
|
||||
|
||||
self::assertStringContainsString( 'notice-success', $output );
|
||||
self::assertStringContainsString( 'The package JSON file was validated successfully.', $output );
|
||||
}
|
||||
|
||||
private function renderDashboard(): string {
|
||||
$settings = Settings::fromArray( array() );
|
||||
|
||||
ob_start();
|
||||
include WPCS_PLUGIN_DIR . 'templates/admin/dashboard.php';
|
||||
|
||||
return (string) ob_get_clean();
|
||||
}
|
||||
}
|
||||
@@ -82,6 +82,20 @@ class FileImportControllerTest extends TestCase {
|
||||
self::assertStringContainsString( 'wpcs_imported=1', $GLOBALS['wpcs_redirect_location'] );
|
||||
}
|
||||
|
||||
public function test_it_redirects_with_error_for_invalid_uploaded_packages(): void {
|
||||
$file = $this->createTemporaryPackageFile( '{"schema_version":' );
|
||||
|
||||
$_FILES['wpcs_package_file'] = array(
|
||||
'tmp_name' => $file,
|
||||
'error' => UPLOAD_ERR_OK,
|
||||
);
|
||||
|
||||
$this->controller()->handleImport();
|
||||
|
||||
self::assertStringContainsString( 'wpcs_import_error=', $GLOBALS['wpcs_redirect_location'] );
|
||||
self::assertStringContainsString( 'not+valid+JSON', $GLOBALS['wpcs_redirect_location'] );
|
||||
}
|
||||
|
||||
private function controller(): FileImportController {
|
||||
return new FileImportController(
|
||||
new JsonFileTransport( new PackageValidator() ),
|
||||
|
||||
Reference in New Issue
Block a user