Browse files

MDL-40264 fix essay handling of zero-string response

Fixes an inappropriate behavior of the is_complete_response() function
for the Essay question type, which caused the string "0" to be
inappropriately marked as an incomplete response.

Also adds a set of unit tests for the Essay question type's
is_complete_response.
  • Loading branch information...
1 parent d3cf194 commit 11f6680deec05ae45aa563c8956dbd9adf1a0f53 @ktemkin ktemkin committed Jun 20, 2013
Showing with 16 additions and 1 deletion.
  1. +1 −1 question/type/essay/question.php
  2. +15 −0 question/type/essay/tests/question_test.php
View
2 question/type/essay/question.php
@@ -84,7 +84,7 @@ public function get_correct_response() {
}
public function is_complete_response(array $response) {
- return !empty($response['answer']);
+ return array_key_exists('answer', $response) && ($response['answer'] !== '');
}
public function is_same_response(array $prevresponse, array $newresponse) {
View
15 question/type/essay/tests/question_test.php
@@ -137,4 +137,19 @@ public function test_is_same_response_with_template() {
array('answer' => ''),
array('answer' => '0')));
}
+
+ public function test_is_complete_response() {
+
+ $essay = test_question_maker::make_an_essay_question();
+ $essay->start_attempt(new question_attempt_step(), 1);
+
+ // The empty string should be considered an empty response, as should a lack of a response.
+ $this->assertFalse($essay->is_complete_response(array('answer' => '')));
+ $this->assertFalse($essay->is_complete_response(array()));
+
+ // Any nonempty string should be considered a complete response.
+ $this->assertTrue($essay->is_complete_response(array('answer' => 'A student response.')));
+ $this->assertTrue($essay->is_complete_response(array('answer' => '0 times.')));
+ $this->assertTrue($essay->is_complete_response(array('answer' => '0')));
+ }
}

0 comments on commit 11f6680

Please sign in to comment.