From 3700271d4816fe95da996c92f3c367ad1a3b2870 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Thu, 19 Jan 2023 21:47:21 +0000 Subject: [PATCH 1/9] Query by post type argument --- inc/cli/class-migrate-command.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/inc/cli/class-migrate-command.php b/inc/cli/class-migrate-command.php index c8d5893..017303b 100644 --- a/inc/cli/class-migrate-command.php +++ b/inc/cli/class-migrate-command.php @@ -49,6 +49,12 @@ class Migrate_Command extends WP_CLI_Command { * - false * --- * + * [--post-type=] + * : Post type, or comma separated list of post types. + * --- + * default: post + * --- + * * ## EXAMPLES * * wp authorship migrate wp-authors --dry-run=true @@ -79,6 +85,9 @@ public function wp_authors( $args, $assoc_args ) : void { WP_CLI::warning( 'Overwriting of previous Authorship data is set to true.' ); } + $post_types = explode( ',', $assoc_args['post-type'] ); + WP_CLI::line( sprintf( 'Updating post types: %s', implode( ', ', $post_types ) ) ); + $tax_query = $overwrite ? [] : [ [ 'taxonomy' => 'authorship', @@ -93,6 +102,7 @@ public function wp_authors( $args, $assoc_args ) : void { $posts = get_posts( [ 'posts_per_page' => $posts_per_page, 'paged' => $paged, + 'post_type' => $post_types, 'post_status' => 'any', 'ignore_sticky_posts' => true, 'suppress_filters' => false, From 0d9e6e5ec0adbeddb1d5764307b01bc90a1adf84 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 4 Sep 2023 13:17:33 +0100 Subject: [PATCH 2/9] Add unit test for migration command --- tests/phpunit/test-cli.php | 63 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 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..4bdab67 --- /dev/null +++ b/tests/phpunit/test-cli.php @@ -0,0 +1,63 @@ +post; + + // Post. Owned by editor, attributed to nobody. + $post1 = $factory->create_and_get( [ + 'post_author' => self::$users['editor']->ID, + ] ); + + // Check initial authorship data unset. + $authorship_authors = \Authorship\get_authors( $post1 ); + $this->assertCount( 0, $authorship_authors ); + + $command = new CLI\Migrate_Command(); + $command->wp_authors( [], [ + 'dry-run' => false, + ] ); + + // Check migration command has correctly set the author. + $authorship_authors = \Authorship\get_authors( $post1 ); + $this->assertCount( 1, $authorship_authors ); + $this->assertSame( self::$users['editor']->ID, $authorship_authors[0] ); + } + + public function testMigratePostTypePage() : void { + $factory = self::factory()->post; + + // Page. Owned by editor, attributed to nobody. + $page1 = $factory->create_and_get( [ + 'post_author' => self::$users['editor']->ID, + 'post_type' => 'page', + ] ); + + // Check initial authorship data unset. + $authorship_authors = \Authorship\get_authors( $page1 ); + $this->assertCount( 0, $authorship_authors ); + + $command = new CLI\Migrate_Command(); + $command->wp_authors( [], [ + 'dry-run' => false, + 'post-type' => 'page', + ] ); + + // Check migration command has correctly set the author. + $authorship_authors = \Authorship\get_authors( $page1 ); + $this->assertCount( 1, $authorship_authors ); + $this->assertSame( self::$users['editor']->ID, $authorship_authors[0] ); + } +} From b4e8108021a6f9724132a69e8de35699fd0ea786 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 4 Sep 2023 13:18:14 +0100 Subject: [PATCH 3/9] Update docs --- tests/phpunit/test-cli.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/test-cli.php b/tests/phpunit/test-cli.php index 4bdab67..d443dd9 100644 --- a/tests/phpunit/test-cli.php +++ b/tests/phpunit/test-cli.php @@ -1,6 +1,6 @@ Date: Mon, 4 Sep 2023 13:26:49 +0100 Subject: [PATCH 4/9] Force unset authorship data --- tests/phpunit/test-cli.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/phpunit/test-cli.php b/tests/phpunit/test-cli.php index d443dd9..c77a7c4 100644 --- a/tests/phpunit/test-cli.php +++ b/tests/phpunit/test-cli.php @@ -11,6 +11,7 @@ use Authorship\CLI; use const Authorship\POSTS_PARAM; +use const Authorship\TAXONOMY; class TestCLI extends TestCase { public function testMigratePostTypePost() : void { @@ -21,6 +22,9 @@ public function testMigratePostTypePost() : void { 'post_author' => self::$users['editor']->ID, ] ); + // Unset author data. + wp_set_post_terms( $post1->ID, [], TAXONOMY ); + // Check initial authorship data unset. $authorship_authors = \Authorship\get_authors( $post1 ); $this->assertCount( 0, $authorship_authors ); @@ -45,6 +49,9 @@ public function testMigratePostTypePage() : void { 'post_type' => 'page', ] ); + // Unset author data. + wp_set_post_terms( $page1->ID, [], TAXONOMY ); + // Check initial authorship data unset. $authorship_authors = \Authorship\get_authors( $page1 ); $this->assertCount( 0, $authorship_authors ); From 671ecafd1eb56cef4636ba1faae9c5a8aacbde08 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 4 Sep 2023 13:32:20 +0100 Subject: [PATCH 5/9] Setup CLI --- tests/phpunit/test-cli.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/test-cli.php b/tests/phpunit/test-cli.php index c77a7c4..9b4d0de 100644 --- a/tests/phpunit/test-cli.php +++ b/tests/phpunit/test-cli.php @@ -14,6 +14,13 @@ use const Authorship\TAXONOMY; class TestCLI extends TestCase { + public function set_up() { + parent::set_up(); + require_once __DIR__ . '/inc/cli/namespace.php'; + require_once __DIR__ . '/inc/cli/class-migrate-command.php'; + CLI\bootstrap(); + } + public function testMigratePostTypePost() : void { $factory = self::factory()->post; @@ -29,7 +36,7 @@ public function testMigratePostTypePost() : void { $authorship_authors = \Authorship\get_authors( $post1 ); $this->assertCount( 0, $authorship_authors ); - $command = new CLI\Migrate_Command(); + $command = new CLI\Migrate_Command; $command->wp_authors( [], [ 'dry-run' => false, ] ); From a899054c03aafed44a93126201e1b61b5656a1d2 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 4 Sep 2023 14:21:33 +0100 Subject: [PATCH 6/9] Fix path --- tests/phpunit/test-cli.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/test-cli.php b/tests/phpunit/test-cli.php index 9b4d0de..5fcb826 100644 --- a/tests/phpunit/test-cli.php +++ b/tests/phpunit/test-cli.php @@ -16,8 +16,8 @@ class TestCLI extends TestCase { public function set_up() { parent::set_up(); - require_once __DIR__ . '/inc/cli/namespace.php'; - require_once __DIR__ . '/inc/cli/class-migrate-command.php'; + require_once dirname( __DIR__, 2 ) . '/inc/cli/namespace.php'; + require_once dirname( __DIR__, 2 ) . '/inc/cli/class-migrate-command.php'; CLI\bootstrap(); } From 4499eab51a4b3b913b3e333e8c0369fe94197ca9 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 4 Sep 2023 14:28:36 +0100 Subject: [PATCH 7/9] Compare ID to ID not Object --- tests/phpunit/test-cli.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/test-cli.php b/tests/phpunit/test-cli.php index 5fcb826..1838d99 100644 --- a/tests/phpunit/test-cli.php +++ b/tests/phpunit/test-cli.php @@ -44,7 +44,7 @@ public function testMigratePostTypePost() : void { // Check migration command has correctly set the author. $authorship_authors = \Authorship\get_authors( $post1 ); $this->assertCount( 1, $authorship_authors ); - $this->assertSame( self::$users['editor']->ID, $authorship_authors[0] ); + $this->assertSame( self::$users['editor']->ID, $authorship_authors[0]->ID ); } public function testMigratePostTypePage() : void { @@ -72,6 +72,6 @@ public function testMigratePostTypePage() : void { // Check migration command has correctly set the author. $authorship_authors = \Authorship\get_authors( $page1 ); $this->assertCount( 1, $authorship_authors ); - $this->assertSame( self::$users['editor']->ID, $authorship_authors[0] ); + $this->assertSame( self::$users['editor']->ID, $authorship_authors[0]->ID ); } } From f312a6e50c203edec9d99a58eb963ed4c214d227 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 4 Sep 2023 14:50:26 +0100 Subject: [PATCH 8/9] Set default initial value --- tests/phpunit/test-cli.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/test-cli.php b/tests/phpunit/test-cli.php index 1838d99..43e6de7 100644 --- a/tests/phpunit/test-cli.php +++ b/tests/phpunit/test-cli.php @@ -39,6 +39,7 @@ public function testMigratePostTypePost() : void { $command = new CLI\Migrate_Command; $command->wp_authors( [], [ 'dry-run' => false, + 'post_type' => 'post', // Note, have to set default values manually. ] ); // Check migration command has correctly set the author. @@ -66,7 +67,7 @@ public function testMigratePostTypePage() : void { $command = new CLI\Migrate_Command(); $command->wp_authors( [], [ 'dry-run' => false, - 'post-type' => 'page', + 'post-type' => 'post,page', ] ); // Check migration command has correctly set the author. From 947a468f80f6964d7601d49e3d5eae9d374f4cd1 Mon Sep 17 00:00:00 2001 From: Matthew Haines-Young Date: Mon, 4 Sep 2023 14:57:31 +0100 Subject: [PATCH 9/9] fix typo --- tests/phpunit/test-cli.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/test-cli.php b/tests/phpunit/test-cli.php index 43e6de7..42f7392 100644 --- a/tests/phpunit/test-cli.php +++ b/tests/phpunit/test-cli.php @@ -39,7 +39,7 @@ public function testMigratePostTypePost() : void { $command = new CLI\Migrate_Command; $command->wp_authors( [], [ 'dry-run' => false, - 'post_type' => 'post', // Note, have to set default values manually. + 'post-type' => 'post', // Note, have to set default values manually. ] ); // Check migration command has correctly set the author.