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

Fix empty string check in quiz question answers #7614

Merged
merged 4 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading