Skip to content

Commit

Permalink
Keep current status for a question when it is updated (#7603)
Browse files Browse the repository at this point in the history
  • Loading branch information
merkushin committed Jul 30, 2024
1 parent eb1630b commit f4f5cae
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-question-status-reset
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Keep current status for a question when it is updated
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private function save_category_question( $question ) {
$post_title = sprintf( esc_html__( '%1$s Question(s) from %2$s', 'sensei-lms' ), $question_number, $question_category->name );

$post_args = [
'ID' => $question_id,
'ID' => (int) $question_id,
'post_title' => $post_title,
'post_status' => 'publish',
'post_type' => 'multiple_question',
Expand All @@ -119,7 +119,7 @@ private function save_category_question( $question ) {
],
];

$result = wp_insert_post( $post_args );
$result = $question_id ? wp_update_post( $post_args ) : wp_insert_post( $post_args );

/**
* This action is triggered when a category question is created or updated by the lesson quiz REST endpoint.
Expand Down Expand Up @@ -175,8 +175,8 @@ private function save_question( $question, $status = 'publish' ) {
$is_new = null === $question_id;

$post_args = [
'ID' => $question_id,
'post_title' => $question['title'],
'ID' => (int) $question_id,
'post_title' => (string) $question['title'],
'post_type' => 'question',
'meta_input' => $this->get_question_meta( $question ),
'tax_input' => [
Expand All @@ -185,7 +185,7 @@ private function save_question( $question, $status = 'publish' ) {
];

if ( $status ) {
$post_args['post_status'] = $status;
$post_args['post_status'] = (string) $status;
}

// Force publish the question if it's part of a quiz.
Expand All @@ -194,10 +194,12 @@ private function save_question( $question, $status = 'publish' ) {
}

if ( isset( $question['description'] ) ) {
$post_args['post_content'] = $question['description'];
$post_args['post_content'] = (string) $question['description'];
} else {
$post_args['post_content'] = '';
}

$result = wp_insert_post( $post_args );
$result = $question_id ? wp_update_post( $post_args ) : wp_insert_post( $post_args );

if ( ! $is_new && ! is_wp_error( $result ) ) {
$this->migrate_non_editor_question( $result, $question['type'] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,32 @@ public function testQuestionUpdateToDraft() {
$this->assertEquals( 'draft', get_post_status( $question_id ) );
}

public function testQuestionUpdate_WhenPublishedQuestionAndNoStatusInPayload_DoesnChangeStatusToDraft() {
/* Arrange. */
$this->login_as_admin();

$question_id = $this->factory->question->create();

$this->save_question_post(
$question_id,
[
'status' => 'publish',
'content' => '<!-- wp:sensei-lms/quiz-question {"title":"Test"} --><!-- /wp:sensei-lms/quiz-question -->',
]
);

/* Act. */
$this->save_question_post(
$question_id,
[
'content' => '<!-- wp:sensei-lms/quiz-question {"title":"Test"} --><!-- /wp:sensei-lms/quiz-question -->',
]
);

/* Assert. */
$this->assertEquals( 'publish', get_post_status( $question_id ) );
}

public function testQuestionWithCategoryUpdateToDraft() {
$this->login_as_admin();

Expand Down

0 comments on commit f4f5cae

Please sign in to comment.