diff --git a/changelog/fix-quiz-questions-with-falsy-answers b/changelog/fix-quiz-questions-with-falsy-answers new file mode 100644 index 0000000000..8e9e7b06d4 --- /dev/null +++ b/changelog/fix-quiz-questions-with-falsy-answers @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Support "0" or other falsy values as an answer for a quiz question diff --git a/includes/rest-api/class-sensei-rest-api-question-helpers-trait.php b/includes/rest-api/class-sensei-rest-api-question-helpers-trait.php index 518465007b..ed302898b4 100644 --- a/includes/rest-api/class-sensei-rest-api-question-helpers-trait.php +++ b/includes/rest-api/class-sensei-rest-api-question-helpers-trait.php @@ -353,7 +353,7 @@ private function get_multiple_choice_meta( array $question ): array { $meta['_answer_order'] = []; foreach ( $question['answer']['answers'] ?? [] as $option ) { - if ( empty( $option['label'] ) ) { + if ( ! isset( $option['label'] ) || '' === $option['label'] ) { continue; } @@ -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 ); @@ -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 ); diff --git a/tests/unit-tests/rest-api/test-class-sensei-rest-api-lesson-quiz-controller.php b/tests/unit-tests/rest-api/test-class-sensei-rest-api-lesson-quiz-controller.php index 57a34d7084..73d382e657 100644 --- a/tests/unit-tests/rest-api/test-class-sensei-rest-api-lesson-quiz-controller.php +++ b/tests/unit-tests/rest-api/test-class-sensei-rest-api-lesson-quiz-controller.php @@ -71,7 +71,6 @@ public function testGetSimple() { $controller = new Sensei_REST_API_Lesson_Quiz_Controller( '' ); $this->assertMeetsSchema( $controller->get_item_schema(), $response->get_data() ); - } /** @@ -684,6 +683,92 @@ public function testPostMultipleChoiceQuestion() { $this->assertEquals( '', $questions[1]->post_content ); } + /** + * Tests posting multiple choice question with answer "0" (falsy value). + */ + public function testSaveQuiz_WhenAnswersContainsFalsyValue_AnswerIsNotRemoved() { + // Arrange. + $this->login_as_teacher(); + + list( $lesson_id, $quiz_id ) = $this->create_lesson_with_quiz(); + + $body = [ + 'options' => [], + 'questions' => [ + [ + 'title' => 'Question', + 'type' => 'multiple-choice', + 'answer' => [ + 'answers' => [ + [ + 'label' => 'Right', + 'correct' => true, + ], + [ + 'label' => 'Wrong', + 'correct' => false, + ], + [ + 'label' => '0', + 'correct' => false, + ], + ], + ], + ], + ], + ]; + + // Act. + $this->send_post_request( $lesson_id, $body ); + $questions = Sensei()->quiz->get_questions( Sensei()->lesson->lesson_quizzes( $lesson_id ) ); + + // Assert. + $this->assertContains( '0', get_post_meta( $questions[0]->ID, '_question_wrong_answers', true ) ); + } + + /** + * Tests posting multiple choice question with empty string as an answer. + */ + public function testSaveQuiz_WhenAnswersContainsEmptyString_AnswerIsRemoved() { + // Arrange. + $this->login_as_teacher(); + + list( $lesson_id, $quiz_id ) = $this->create_lesson_with_quiz(); + + $body = [ + 'options' => [], + 'questions' => [ + [ + 'title' => 'Question', + 'type' => 'multiple-choice', + 'answer' => [ + 'answers' => [ + [ + 'label' => 'Right', + 'correct' => true, + ], + [ + 'label' => 'Wrong', + 'correct' => false, + ], + [ + 'label' => '', + 'correct' => false, + ], + ], + ], + ], + ], + ]; + + // Act. + $this->send_post_request( $lesson_id, $body ); + $questions = Sensei()->quiz->get_questions( Sensei()->lesson->lesson_quizzes( $lesson_id ) ); + + // Assert. + $this->assertCount( 1, get_post_meta( $questions[0]->ID, '_question_wrong_answers', true ) ); + } + /** * Tests editing true/false question properties. */