Repository: Automattic/sensei

Issue:
Multiple-choice Quiz Question with all correct answers does not show in the frontend
<!-- Thanks for contributing to Sensei LMS! Pick a clear title ("Lesson: Show complexity in individual lessons") and proceed. -->

### Steps to Reproduce
1. On the core SenseiLMS plugin (Version 4.25.1), create a free course and a lesson.
2. Add a Quiz block to the lesson, and add two or more multiple choice questions
3. For one of the multiple choice questions, mark all answers as correct, as shown below:

<img width="1171" height="716" alt="Image" src="https://github.com/user-attachments/assets/97e6fdf2-73b2-46c7-a25d-6fe625ca3dc6" />

5. See in the frontend, all questions show except the one where all answers are supposed to be correct:

<img width="1413" height="712" alt="Image" src="https://github.com/user-attachments/assets/c162db75-c2fb-4eca-a9c5-94d502a635b8" />

<img width="1085" height="552" alt="Image" src="https://github.com/user-attachments/assets/0330b651-6487-41c1-9b46-844dbba0c56b" />

### What I Expected
All quiz questions would show in the frontend

### What Happened Instead
Multiple choice quiz question with all correct answers didn't show

### PHP / WordPress / Sensei LMS version
- WordPress 6.8.3 running [Course](https://sensei-paid-test.mystagingwebsite.com/wp-admin/themes.php) theme.
- PHP version	8.1.33

### Browser / OS version
- Firefox 144.0.2

### Screenshot / Video
See above

### Context / Source
<!-- Optional: share your unique context to help us understand your perspective. -->
Reported by user in https://wordpress.org/support/topic/quiz-question-missing-on-front-end-when-all-answers-are-correct/#post-18732508. 

<!--
PLEASE NOTE
- These comments won't show up when you submit the issue.
- Everything is optional, but try to add as many details as possible.

Contributing docs:
https://github.com/Automattic/sensei/blob/trunk/CONTRIBUTING.md

Helpful tips for screenshots:
https://en.support.wordpress.com/make-a-screenshot/
-->


Discussion:
@uogecko: Any timeline for resolution of this issue?@donnapep: Hi @uogecko. A fix for this will ship in the next release a little later this month.

Solution patch:
diff --git a/changelog/fix-some-questions-not-showing b/changelog/fix-some-questions-not-showing
new file mode 100644
index 000000000..b85f7a5cb
--- /dev/null
+++ b/changelog/fix-some-questions-not-showing
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fixed
+
+Allow multiple choice quiz questions with no wrong answers
diff --git a/includes/class-sensei-quiz.php b/includes/class-sensei-quiz.php
index 8dce7c988..10e6a9156 100755
--- a/includes/class-sensei-quiz.php
+++ b/includes/class-sensei-quiz.php
@@ -2207,11 +2207,16 @@ class Sensei_Quiz {
 		$right_answers = get_post_meta( $question_id, '_question_right_answer', true );
 		$wrong_answers = get_post_meta( $question_id, '_question_wrong_answers', true );
 
-		// Multiple choice question is incomplete if there isn't at least one right and one wrong answer.
-		if ( ! is_array( $right_answers ) || count( $right_answers ) < 1 || ! is_array( $wrong_answers ) || count( $wrong_answers ) < 1 ) {
+		// Multiple choice question is incomplete if there isn't at least one right answer. Wrong answers are optional - it's valid to have all answers be correct.
+		if ( ! is_array( $right_answers ) || count( $right_answers ) < 1 ) {
 			return false;
 		}
-		// Wrong or right answers values can't be whitespace.
+
+		if ( ! is_array( $wrong_answers ) ) {
+			$wrong_answers = array();
+		}
+
+		// Wrong or right answer values can't be whitespace.
 		return ! count(
 			array_filter(
 				array_merge( $right_answers, $wrong_answers ),



Test patch:
diff --git a/tests/unit-tests/test-class-quiz.php b/tests/unit-tests/test-class-quiz.php
index 8a32d8533..f56353e20 100644
--- a/tests/unit-tests/test-class-quiz.php
+++ b/tests/unit-tests/test-class-quiz.php
@@ -197,6 +197,43 @@ class Sensei_Class_Quiz_Test extends WP_UnitTestCase {
 		$this->assertCount( 2, $questions_filtered );
 	}
 
+	/**
+	 * Testing that multiple choice questions with all correct answers are not filtered out.
+	 *
+	 * This test verifies that questions where all answer options are correct
+	 * (i.e., no wrong answers) are still considered complete and rendered on the frontend.
+	 */
+	public function testGetQuestionsFiltersIncompleteQuestionsAllCorrectAnswers() {
+		$lesson_id = $this->factory->lesson->create();
+		$quiz_id   = $this->factory->maybe_create_quiz_for_lesson( $lesson_id );
+
+		// Create a question with multiple right answers and no wrong answers (all answers are correct).
+		$question_id = $this->factory->question->create(
+			[
+				'quiz_id'                => $quiz_id,
+				'question_type'          => 'multiple-choice',
+				'question_right_answers' => [ 'Answer A', 'Answer B', 'Answer C' ],
+				'question_wrong_answers' => [],
+			]
+		);
+
+		// Create a normal question with both right and wrong answers for comparison.
+		$normal_question_id = $this->factory->question->create(
+			[
+				'quiz_id'       => $quiz_id,
+				'question_type' => 'multiple-choice',
+			]
+		);
+
+		// When filtering incomplete questions, both should be included.
+		$questions_filtered = Sensei()->quiz->get_questions( $quiz_id, 'any', 'meta_value_num title', 'ASC', true );
+		$question_ids       = wp_list_pluck( $questions_filtered, 'ID' );
+
+		$this->assertContains( $question_id, $question_ids, 'Question with all correct answers should not be filtered out' );
+		$this->assertContains( $normal_question_id, $question_ids, 'Normal question should not be filtered out' );
+		$this->assertCount( 2, $questions_filtered, 'Both questions should be included when filtering' );
+	}
+
 	/**
 	 * This test Woothemes_Sensei()->quiz->save_user_answers.
 	 */