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

Keep current status for a question when it is updated #7603

Merged
merged 9 commits into from
Jul 30, 2024
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 @@ -119,15 +119,15 @@ private function save_category_question( $question ) {
],
];

$result = wp_insert_post( $post_args );
$result = wp_update_post( $post_args );

/**
* This action is triggered when a category question is created or updated by the lesson quiz REST endpoint.
*
* @since 3.9.0
* @hook sensei_rest_api_category_question_saved
*
* @param {int|WP_Error} $result Result of wp_insert_post. Post ID on success or WP_Error on failure.
* @param {int|WP_Error} $result Result of wp_update_post. Post ID on success or WP_Error on failure.
* @param {array} $question The question JSON arguments.
*/
do_action( 'sensei_rest_api_category_question_saved', $result, $question );
Expand Down Expand Up @@ -196,7 +196,7 @@ private function save_question( $question, $status = 'publish' ) {
$post_args['post_content'] = $question['description'];
}

$result = wp_insert_post( $post_args );
$result = wp_update_post( $post_args );

if ( ! $is_new && ! is_wp_error( $result ) ) {
$this->migrate_non_editor_question( $result, $question['type'] );
Expand All @@ -208,7 +208,7 @@ private function save_question( $question, $status = 'publish' ) {
* @since 3.9.0
* @hook sensei_rest_api_question_saved
*
* @param {int|WP_Error} $result Result of wp_insert_post. Post ID on success or WP_Error on failure.
* @param {int|WP_Error} $result Result of wp_update_post. Post ID on success or WP_Error on failure.
* @param {string} $question_type The question type.
* @param {array} $question The question JSON arguments.
*/
Expand Down Expand Up @@ -437,7 +437,7 @@ private function get_question( WP_Post $question ): array {
*
* @return array
*/
private function get_category_question( WP_Post $question ) : array {
private function get_category_question( WP_Post $question ): array {
$category = (int) get_post_meta( $question->ID, 'category', true );
$number = (int) get_post_meta( $question->ID, 'number', true );

Expand Down Expand Up @@ -494,7 +494,7 @@ private function get_question_common_properties( WP_Post $question ): array {
*
* @return array Media info. It includes the type, id, url and title.
*/
private function get_question_media( int $question_media_id, int $question_id ) : array {
private function get_question_media( int $question_media_id, int $question_id ): array {
$question_media = [];
$mimetype = get_post_mime_type( $question_media_id );
$attachment = get_post( $question_media_id );
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
Loading