diff --git a/inc/cli/class-migrate-command.php b/inc/cli/class-migrate-command.php index c8d5893..6f281f6 100644 --- a/inc/cli/class-migrate-command.php +++ b/inc/cli/class-migrate-command.php @@ -128,7 +128,11 @@ public function wp_authors( $args, $assoc_args ) : void { WP_CLI::line( sprintf( 'Processed %d posts, pausing for a breath...', $count ) ); sleep( 2 ); - $paged++; + // A normal run, don't need to paginate due to NOT EXISTS tax query. + // But for dry run or when overwriting, need to increment paged. + if ( $dry_run || $overwrite ) { + $paged++; + } } while ( count( $posts ) ); if ( true === $dry_run ) { diff --git a/tests/phpunit/test-cli.php b/tests/phpunit/test-cli.php new file mode 100644 index 0000000..9d98560 --- /dev/null +++ b/tests/phpunit/test-cli.php @@ -0,0 +1,57 @@ +post; + $posts = []; + + for ( $i = 0; $i < 200; $i++ ) { + $posts[] = $factory->create_and_get( [ + 'post_author' => self::$users['admin']->ID, + POSTS_PARAM => [ + self::$users['editor']->ID, + ], + ] ); + } + + $paged_post = $posts[100]; // Any post from second page of results. + $authorship_authors = \Authorship\get_authors( $paged_post ); + + // Asset initial authorship authors set correctly. + $this->assertCount( 1, $authorship_authors ); + $this->assertSame( self::$users['editor']->ID, $authorship_authors[0]->ID ); + + // Migrate, overwriting authorship data with WP Author data. + $command = new CLI\Migrate_Command; + $command->wp_authors( [], [ + 'dry-run' => false, + 'overwrite' => true, + 'post-type' => 'post', // Must set default value manually. + ] ); + + // Verify author data migrated correctly. + $authorship_authors = \Authorship\get_authors( $paged_post->ID ); + $this->assertCount( 1, $authorship_authors ); + $this->assertSame( self::$users['admin']->ID, $authorship_authors[0]->ID ); + } +}