Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pagination issue with migrate wp authors CLI command. #121

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
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
6 changes: 5 additions & 1 deletion inc/cli/class-migrate-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
57 changes: 57 additions & 0 deletions tests/phpunit/test-cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Plugin CLI command tests.
*
* @package authorship
*/

declare( strict_types=1 );

namespace Authorship\Tests;

use Authorship\CLI;
use const Authorship\POSTS_PARAM;
use const Authorship\TAXONOMY;

class TestCLI extends TestCase {
public function set_up() {
parent::set_up();
require_once dirname( __DIR__, 2 ) . '/inc/cli/namespace.php';
require_once dirname( __DIR__, 2 ) . '/inc/cli/class-migrate-command.php';
CLI\bootstrap();
}

public function testMigrateWpAuthorsPagination() : void {
$factory = self::factory()->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 );
}
}