Skip to content

Commit

Permalink
Merge pull request #7614 from Automattic/fix/quiz-questions-with-fals…
Browse files Browse the repository at this point in the history
…y-answers

Fix empty string check in quiz question answers
  • Loading branch information
renatho authored Jun 12, 2024
2 parents 982fbd4 + 126cb95 commit e0f34bf
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 4 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-quiz-questions-with-falsy-answers
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Support "0" or other falsy values as an answer for a quiz question
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

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 @@ -71,7 +71,6 @@ public function testGetSimple() {

$controller = new Sensei_REST_API_Lesson_Quiz_Controller( '' );
$this->assertMeetsSchema( $controller->get_item_schema(), $response->get_data() );

}

/**
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit e0f34bf

Please sign in to comment.