From dc23c865bf40f619d4b55c0eddae32a3bd1e3d5a Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Thu, 19 Jan 2023 18:42:47 +0000 Subject: [PATCH 1/5] Fix migrate authors query pagination --- inc/cli/class-migrate-command.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/inc/cli/class-migrate-command.php b/inc/cli/class-migrate-command.php index c8d5893..c43a81c 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++; + // When not overwriting, or during a dry run, + // need to skip pagination due to NOT EXISTS tax query. + if ( $dry_run || $overwrite ) { + $paged++; + } } while ( count( $posts ) ); if ( true === $dry_run ) { From baedf57b7f627989ac483f8a0236d49bcea89d6b Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Fri, 3 Mar 2023 17:27:43 +0000 Subject: [PATCH 2/5] Update comment --- inc/cli/class-migrate-command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/cli/class-migrate-command.php b/inc/cli/class-migrate-command.php index c43a81c..6f281f6 100644 --- a/inc/cli/class-migrate-command.php +++ b/inc/cli/class-migrate-command.php @@ -128,8 +128,8 @@ public function wp_authors( $args, $assoc_args ) : void { WP_CLI::line( sprintf( 'Processed %d posts, pausing for a breath...', $count ) ); sleep( 2 ); - // When not overwriting, or during a dry run, - // need to skip pagination due to NOT EXISTS tax query. + // 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++; } From 51504c25d1670c6df33dcef55e2b731797fb2aa0 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 4 Sep 2023 14:47:42 +0100 Subject: [PATCH 3/5] Add unit test for pagination when overwriting --- tests/phpunit/test-cli.php | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/phpunit/test-cli.php diff --git a/tests/phpunit/test-cli.php b/tests/phpunit/test-cli.php new file mode 100644 index 0000000..0ad6d73 --- /dev/null +++ b/tests/phpunit/test-cli.php @@ -0,0 +1,58 @@ +post; + $post_ids = []; + + for ( $i = 0; $i < 200; $i++ ) { + $post = $factory->create_and_get( [ + 'post_author' => self::$users['admin']->ID, + POSTS_PARAM => [ + self::$users['editor']->ID, + ], + ] ); + + $post_ids[] = $post->ID; + } + + $paged_post_id = $post_ids[100]; + $authorship_authors = \Authorship\get_authors( $paged_post_id ); + + // 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, + ] ); + + // 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 ); + } +} From f4fd257665301c6750cca8d0ed75929e30c38c39 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 4 Sep 2023 14:52:41 +0100 Subject: [PATCH 4/5] Fix correct post ID vs post object --- tests/phpunit/test-cli.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/test-cli.php b/tests/phpunit/test-cli.php index 0ad6d73..795e034 100644 --- a/tests/phpunit/test-cli.php +++ b/tests/phpunit/test-cli.php @@ -23,21 +23,19 @@ public function set_up() { public function testMigrateWpAuthorsPagination() : void { $factory = self::factory()->post; - $post_ids = []; + $posts = []; for ( $i = 0; $i < 200; $i++ ) { - $post = $factory->create_and_get( [ + $posts[] = $factory->create_and_get( [ 'post_author' => self::$users['admin']->ID, POSTS_PARAM => [ self::$users['editor']->ID, ], ] ); - - $post_ids[] = $post->ID; } - $paged_post_id = $post_ids[100]; - $authorship_authors = \Authorship\get_authors( $paged_post_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 ); @@ -51,7 +49,7 @@ public function testMigrateWpAuthorsPagination() : void { ] ); // Verify author data migrated correctly. - $authorship_authors = \Authorship\get_authors( $paged_post_id ); + $authorship_authors = \Authorship\get_authors( $paged_post->ID ); $this->assertCount( 1, $authorship_authors ); $this->assertSame( self::$users['admin']->ID, $authorship_authors[0]->ID ); } From 3f768a6aeaf69520b1cef49cfebea54218d1deff Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 4 Sep 2023 14:54:37 +0100 Subject: [PATCH 5/5] Set default value --- tests/phpunit/test-cli.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/test-cli.php b/tests/phpunit/test-cli.php index 795e034..9d98560 100644 --- a/tests/phpunit/test-cli.php +++ b/tests/phpunit/test-cli.php @@ -46,6 +46,7 @@ public function testMigrateWpAuthorsPagination() : void { $command->wp_authors( [], [ 'dry-run' => false, 'overwrite' => true, + 'post-type' => 'post', // Must set default value manually. ] ); // Verify author data migrated correctly.