diff --git a/features/import.feature b/features/import.feature index 9dc099a4..bc640983 100644 --- a/features/import.feature +++ b/features/import.feature @@ -400,3 +400,61 @@ Feature: Import content. https://newsite.com/ """ + @require-wp-5.2 @require-mysql + Scenario: Specifying a non-existent importer class produces an error + Given a WP install + And I run `wp plugin install wordpress-importer --activate` + + When I run `wp export` + Then save STDOUT 'Writing to file %s' as {EXPORT_FILE} + + When I try `wp import {EXPORT_FILE} --authors=skip --importer=NonExistentImporterClass` + Then STDERR should contain: + """ + Error: Importer class 'NonExistentImporterClass' does not exist. + """ + And the return code should be 1 + + @require-wp-5.2 @require-mysql + Scenario: Specifying an importer class that is not a subclass of WP_Import produces an error + Given a WP install + And I run `wp plugin install wordpress-importer --activate` + + When I run `wp export` + Then save STDOUT 'Writing to file %s' as {EXPORT_FILE} + + When I try `wp import {EXPORT_FILE} --authors=skip --importer=WP_CLI_Command` + Then STDERR should contain: + """ + Error: Importer class 'WP_CLI_Command' must be a subclass of WP_Import. + """ + And the return code should be 1 + + @require-wp-5.2 @require-mysql + Scenario: Specifying a valid custom importer subclass succeeds + Given a WP install + And I run `wp plugin install wordpress-importer --activate` + And a wp-content/mu-plugins/custom-importer.php file: + """ + ] + * : Use a custom importer class instead of the default WP_Import. The class must exist and be a subclass of WP_Import. + * * ## EXAMPLES * * # Import content from a WXR file @@ -45,6 +48,7 @@ public function __invoke( $args, $assoc_args ) { 'authors' => null, 'skip' => [], 'rewrite_urls' => null, + 'importer' => 'WP_Import', ); $assoc_args = wp_parse_args( $assoc_args, $defaults ); @@ -57,6 +61,16 @@ public function __invoke( $args, $assoc_args ) { WP_CLI::error( $importer ); } + $importer_class = $assoc_args['importer']; + if ( 'WP_Import' !== $importer_class ) { + if ( ! class_exists( $importer_class, false ) ) { + WP_CLI::error( "Importer class '$importer_class' does not exist." ); + } + if ( ! is_subclass_of( $importer_class, 'WP_Import' ) ) { + WP_CLI::error( "Importer class '$importer_class' must be a subclass of WP_Import." ); + } + } + $this->add_wxr_filters(); WP_CLI::log( 'Starting the import process...' ); @@ -117,7 +131,9 @@ public function __invoke( $args, $assoc_args ) { */ private function import_wxr( $file, $args ) { - $wp_import = new WP_Import(); + $importer_class = $args['importer']; + /** @var WP_Import $wp_import */ + $wp_import = new $importer_class(); $wp_import->processed_posts = $this->processed_posts; $import_data = $wp_import->parse( $file );