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

WP authors migration command - post type argument. #122

Merged
merged 10 commits into from
Sep 14, 2023
Merged
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
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 );
}
}