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

Add progress validation command #7160

Merged
merged 20 commits into from
Nov 6, 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
13 changes: 4 additions & 9 deletions includes/class-sensei-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* @package sensei
*/

defined( 'ABSPATH' ) || exit;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.

Check warning on line 9 in includes/class-sensei-cli.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-cli.php#L8-L9

Added lines #L8 - L9 were not covered by tests
}

/**
* CLI class.
Expand All @@ -17,21 +19,14 @@
* Class constructor.
*/
public function __construct() {
$this->load();
$this->register();
}

/**
* Load the command files.
*/
private function load() {
require_once dirname( __FILE__ ) . '/cli/class-sensei-db-seed-command.php';
}

/**
* Register the CLI commands.
*/
private function register() {
WP_CLI::add_command( 'sensei db seed', Sensei_DB_Seed_Command::class );
WP_CLI::add_command( 'sensei validate progress', Sensei_Validate_Progress_Command::class );

Check warning on line 30 in includes/class-sensei-cli.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-cli.php#L30

Added line #L30 was not covered by tests
}
}
4 changes: 1 addition & 3 deletions includes/class-sensei-course.php
Original file line number Diff line number Diff line change
Expand Up @@ -2302,9 +2302,7 @@ public function load_user_courses_content( $user = false ) {
* Returns a list of all courses
*
* @since 1.8.0
* @return array $courses{
* @type $course WP_Post
* }
* @return WP_Post[]
*/
public static function get_all_courses() {

Expand Down
4 changes: 3 additions & 1 deletion includes/cli/class-sensei-db-seed-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
* @package sensei
*/

defined( 'ABSPATH' ) || exit;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

Check warning on line 11 in includes/cli/class-sensei-db-seed-command.php

View check run for this annotation

Codecov / codecov/patch

includes/cli/class-sensei-db-seed-command.php#L8-L11

Added lines #L8 - L11 were not covered by tests
/**
* WP-CLI command that helps with seeding the database.
*
Expand Down
66 changes: 66 additions & 0 deletions includes/cli/class-sensei-validate-progress-command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Sensei_Validate_Progress_Command class file.
*
* @package sensei
*/

use Sensei\Internal\Migration\Validations\Progress_Validation;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* WP-CLI command that validates the progress data.
*
* @since $$next-version$$
*/
class Sensei_Validate_Progress_Command {
/**
* Seed the database.
*
* @since $$next-version$$
*
* @param array $args Command arguments.
* @param array $assoc_args Command arguments with names.
*/
public function __invoke( array $args = [], array $assoc_args = [] ) {
$progress_validation = new Progress_Validation();

Check warning on line 29 in includes/cli/class-sensei-validate-progress-command.php

View check run for this annotation

Codecov / codecov/patch

includes/cli/class-sensei-validate-progress-command.php#L28-L29

Added lines #L28 - L29 were not covered by tests

$progress_validation->run();

Check warning on line 31 in includes/cli/class-sensei-validate-progress-command.php

View check run for this annotation

Codecov / codecov/patch

includes/cli/class-sensei-validate-progress-command.php#L31

Added line #L31 was not covered by tests

if ( ! $progress_validation->has_errors() ) {
WP_CLI::success( 'Progress data is valid.' );
return;

Check warning on line 35 in includes/cli/class-sensei-validate-progress-command.php

View check run for this annotation

Codecov / codecov/patch

includes/cli/class-sensei-validate-progress-command.php#L33-L35

Added lines #L33 - L35 were not covered by tests
}

$this->output_validation_errors( $progress_validation );

Check warning on line 38 in includes/cli/class-sensei-validate-progress-command.php

View check run for this annotation

Codecov / codecov/patch

includes/cli/class-sensei-validate-progress-command.php#L38

Added line #L38 was not covered by tests

WP_CLI::error( 'Progress data is not valid.' );

Check warning on line 40 in includes/cli/class-sensei-validate-progress-command.php

View check run for this annotation

Codecov / codecov/patch

includes/cli/class-sensei-validate-progress-command.php#L40

Added line #L40 was not covered by tests
}

/**
* Output the validation errors.
*
* @since $$next-version$$
*
* @param Progress_Validation $progress_validation Progress validation.
*/
private function output_validation_errors( Progress_Validation $progress_validation ) {
foreach ( $progress_validation->get_errors() as $error ) {
WP_CLI::warning( $error->get_message() );

Check warning on line 52 in includes/cli/class-sensei-validate-progress-command.php

View check run for this annotation

Codecov / codecov/patch

includes/cli/class-sensei-validate-progress-command.php#L50-L52

Added lines #L50 - L52 were not covered by tests

if ( $error->has_data() ) {
$error_data = $error->get_data();
$error_data = is_array( $error_data[0] ) ? $error_data : [ $error_data ];

Check warning on line 56 in includes/cli/class-sensei-validate-progress-command.php

View check run for this annotation

Codecov / codecov/patch

includes/cli/class-sensei-validate-progress-command.php#L54-L56

Added lines #L54 - L56 were not covered by tests

WP_CLI\Utils\format_items(
'table',
$error_data,
array_keys( $error_data[0] )
);

Check warning on line 62 in includes/cli/class-sensei-validate-progress-command.php

View check run for this annotation

Codecov / codecov/patch

includes/cli/class-sensei-validate-progress-command.php#L58-L62

Added lines #L58 - L62 were not covered by tests
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private function get_comments_and_meta( int $after_comment_id, bool $dry_run ):
// At the moment we don't care about post meta for course progress.
if ( 'sensei_lesson_status' === $progress_comment->comment_type ) {
// Map the post ID to the comment ID. Is used later to map post meta to the comment ID.
$post_ids[ $progress_comment->comment_post_ID ] = $progress_comment->comment_ID;
$post_ids[ $progress_comment->comment_post_ID ][] = $progress_comment->comment_ID;
m1r0 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -188,8 +188,10 @@ private function get_comments_and_meta( int $after_comment_id, bool $dry_run ):
$mapped_meta[ $comment_id ]['status'] = $comment_status;
}
} else {
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
$mapped_meta[ $comment_id ][ $meta->meta_key ] = $meta->meta_value;
foreach ( $post_ids[ $meta->post_id ] as $comment_id ) {
// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
$mapped_meta[ $comment_id ][ $meta->meta_key ] = $meta->meta_value;
}
}
}

Expand Down
Loading
Loading