From 8b3bca9705f6032623c8f48a76d777d3da7ebc6c Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Mon, 10 Jun 2024 17:20:02 -0300 Subject: [PATCH 1/3] Fix empty string check It was failing with falsy values --- ...sensei-rest-api-question-helpers-trait.php | 6 +- ...sensei-rest-api-lesson-quiz-controller.php | 82 +++++++++++++++++++ 2 files changed, 85 insertions(+), 3 deletions(-) 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..0647cda013 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 @@ -684,6 +684,88 @@ public function testPostMultipleChoiceQuestion() { $this->assertEquals( '', $questions[1]->post_content ); } + /** + * Tests posting multiple choice question with answer "0" (falsy value). + */ + public function testPostMultipleChoiceQuestion_WhenAnswersContainsFalsyValue_AnswerIsNotRemoved() { + $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, + ], + ], + ], + ], + ], + ]; + + $this->send_post_request( $lesson_id, $body ); + + $questions = Sensei()->quiz->get_questions( Sensei()->lesson->lesson_quizzes( $lesson_id ) ); + + $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 testPostMultipleChoiceQuestion_WhenAnswersContainsEmptyString_AnswerIsRemoved() { + $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, + ], + ], + ], + ], + ], + ]; + + $this->send_post_request( $lesson_id, $body ); + + $questions = Sensei()->quiz->get_questions( Sensei()->lesson->lesson_quizzes( $lesson_id ) ); + + $this->assertCount( 1, get_post_meta( $questions[0]->ID, '_question_wrong_answers', true ) ); + } + /** * Tests editing true/false question properties. */ From 85a6a05597388169aeb5e6c75762065f03704d78 Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Mon, 10 Jun 2024 17:21:17 -0300 Subject: [PATCH 2/3] Add changelog --- changelog/fix-quiz-questions-with-falsy-answers | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog/fix-quiz-questions-with-falsy-answers 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 From 126cb9522985b3ad9f897d7b2a831a2ed6b5015e Mon Sep 17 00:00:00 2001 From: Renatho De Carli Rosa Date: Wed, 12 Jun 2024 14:14:33 -0300 Subject: [PATCH 3/3] Update tests format --- ...class-sensei-rest-api-lesson-quiz-controller.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) 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 0647cda013..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() ); - } /** @@ -687,7 +686,8 @@ public function testPostMultipleChoiceQuestion() { /** * Tests posting multiple choice question with answer "0" (falsy value). */ - public function testPostMultipleChoiceQuestion_WhenAnswersContainsFalsyValue_AnswerIsNotRemoved() { + public function testSaveQuiz_WhenAnswersContainsFalsyValue_AnswerIsNotRemoved() { + // Arrange. $this->login_as_teacher(); list( $lesson_id, $quiz_id ) = $this->create_lesson_with_quiz(); @@ -718,17 +718,19 @@ public function testPostMultipleChoiceQuestion_WhenAnswersContainsFalsyValue_Ans ], ]; + // 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 testPostMultipleChoiceQuestion_WhenAnswersContainsEmptyString_AnswerIsRemoved() { + public function testSaveQuiz_WhenAnswersContainsEmptyString_AnswerIsRemoved() { + // Arrange. $this->login_as_teacher(); list( $lesson_id, $quiz_id ) = $this->create_lesson_with_quiz(); @@ -759,10 +761,11 @@ public function testPostMultipleChoiceQuestion_WhenAnswersContainsEmptyString_An ], ]; + // 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 ) ); }