Skip to content

Commit

Permalink
Merge pull request #122 from humanmade/migrate-authors-post-type
Browse files Browse the repository at this point in the history
WP authors migration command - post type argument.
  • Loading branch information
mikelittle authored Sep 14, 2023
2 parents 5cc59f0 + 947a468 commit bca9c79
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
10 changes: 10 additions & 0 deletions inc/cli/class-migrate-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class Migrate_Command extends WP_CLI_Command {
* - false
* ---
*
* [--post-type=<post-type>]
* : Post type, or comma separated list of post types.
* ---
* default: post
* ---
*
* ## EXAMPLES
*
* wp authorship migrate wp-authors --dry-run=true
Expand Down Expand Up @@ -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',
Expand All @@ -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,
Expand Down
78 changes: 78 additions & 0 deletions tests/phpunit/test-cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?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 testMigratePostTypePost() : void {
$factory = self::factory()->post;

// Post. Owned by editor, attributed to nobody.
$post1 = $factory->create_and_get( [
'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 );

$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.
$authorship_authors = \Authorship\get_authors( $post1 );
$this->assertCount( 1, $authorship_authors );
$this->assertSame( self::$users['editor']->ID, $authorship_authors[0]->ID );
}

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',
] );

// 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 );

$command = new CLI\Migrate_Command();
$command->wp_authors( [], [
'dry-run' => false,
'post-type' => 'post,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]->ID );
}
}

0 comments on commit bca9c79

Please sign in to comment.