Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions features/import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,49 @@ Feature: Import content.
1
"""

@require-wp-5.2 @require-mysql
Scenario: Create an author mapping file and import with it
Given a WP install
And I run `wp plugin install --activate wordpress-importer`
And I run `wp site empty --yes`
And I run `wp user create jdoe jdoe@example.com --porcelain`
And save STDOUT as {JDOE_ID}
And I run `wp post create --post_title='Post by admin' --post_author=1 --post_status=publish`
And I run `wp post create --post_title='Post by jdoe' --post_author={JDOE_ID} --post_status=publish`

When I run `wp export --filename_format=authors.{n}.xml`
Then save STDOUT 'Writing to file %s' as {EXPORT_FILE}

When I run `wp site empty --yes`
And I run `wp user update 1 --user_email=admin@example.org --skip-email`
And I run `wp user update jdoe --user_email=jdoe@example.org --skip-email`
And I try `wp import {EXPORT_FILE} --authors=mapping.csv`
Then STDERR should be:
"""
Error: Please update author mapping file before continuing: mapping.csv
"""
And the return code should be 1
And the mapping.csv file should contain:
"""
old_user_login,new_user_login
"""
And the mapping.csv file should contain:
"""
admin,admin
"""
And the mapping.csv file should contain:
"""
jdoe,jdoe
"""

When I run `wp import {EXPORT_FILE} --authors=mapping.csv`
Then STDOUT should contain:
"""
Success: Finished importing from
"""

When I run `wp post list --author={JDOE_ID} --field=post_title`
Then STDOUT should be:
"""
Post by jdoe
"""
11 changes: 9 additions & 2 deletions src/Import_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

class Import_Command extends WP_CLI_Command {

private $blog_users = [];
/**
* Users of the current site, loaded on first use by suggest_user().
*
* Must stay null until then, as suggest_user() checks it with isset().
*
* @var \WP_User[]|null
*/
private $blog_users;

public $processed_posts = [];

Expand Down Expand Up @@ -729,7 +736,7 @@ private function suggest_user( $author_user_login, $author_user_email = '' ) {
$shortestavg[] = $lev;
}
// in case all usernames have a common pattern
if ( $shortest > ( array_sum( $shortestavg ) / count( $shortestavg ) ) ) {
if ( [] !== $shortestavg && $shortest > ( array_sum( $shortestavg ) / count( $shortestavg ) ) ) {
return '';
}

Expand Down
Loading