From 86ce6a93046a86dc38a9301f508150f01d00799a Mon Sep 17 00:00:00 2001 From: Tim Chambers Date: Thu, 24 Mar 2016 12:29:40 -0700 Subject: [PATCH] Correct question set results scoring per #8 Previously the response was not sending the question_id and answer_id to the backend API, resulting in no valid response. --- VERSION | 2 +- lib/ApiResource.php | 8 +-- tests/QuestionSetTest.php | 105 ++++++++++++++++++++++++++++++++++---- 3 files changed, 101 insertions(+), 14 deletions(-) diff --git a/VERSION b/VERSION index 1454f6e..4d54dad 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.0.1 +4.0.2 diff --git a/lib/ApiResource.php b/lib/ApiResource.php index ba41b97..054ad44 100644 --- a/lib/ApiResource.php +++ b/lib/ApiResource.php @@ -181,13 +181,13 @@ protected function _score($answers) $url = static::instanceUrl() . '/score'; // Weird request requires us to build the cURL request manually - $params = ''; + $params = array(); foreach ($answers as $key => $value) { - $params = $params . 'answers[][{$key}]={$value}&'; + $params[] = "answers[][question_id]={$value['question_id']}"; + $params[] = "answers[][answer_id]={$value['answer_id']}"; } - rtrim($params, '&'); - $response = static::_makeRequest('post', $url, $params); + $response = static::_makeRequest('post', $url, implode('&', $params)); return Util\Util::convertToBlockScoreObject($response); } } \ No newline at end of file diff --git a/tests/QuestionSetTest.php b/tests/QuestionSetTest.php index dfb6338..83244d0 100644 --- a/tests/QuestionSetTest.php +++ b/tests/QuestionSetTest.php @@ -4,6 +4,15 @@ class QuestionSetTest extends TestCase { + private static $qs_answers = array( + 'Which one of the following addresses is associated with you?' => '309 Colver Rd', + 'Which one of the following area codes is associated with you?' => '812', + 'Which one of the following counties is associated with you?' => 'Jasper', + 'Which one of the following zip codes is associated with you?' => '49230', + 'What state was your SSN issued in?' => 'None Of The Above', + 'Which one of the following adult individuals is most closely associated with you?' => 'None Of The Above', + ); + public function testUrl() { $this->assertSame(QuestionSet::classUrl(), '/question_sets'); @@ -16,7 +25,7 @@ public function testClassType() $this->assertTrue($qs instanceof QuestionSet); $this->assertTrue($qs instanceof Object); } - + public function testCreateQuestionSet() { $person = self::createTestPerson(); @@ -25,7 +34,7 @@ public function testCreateQuestionSet() $this->assertSame('None Of The Above',$i->answers[4]->answer); } } - + public function testAllQuestionSet() { $person = self::createTestPerson(); @@ -35,7 +44,7 @@ public function testAllQuestionSet() $all_qs = $person->question_sets->all(); $this->assertSame($qs->id, $all_qs[0]->id); } - + public function testRetrieveQuestionSet() { $person = self::createTestPerson(); @@ -45,21 +54,99 @@ public function testRetrieveQuestionSet() $this->assertSame($retrieved_qs->$key, $value); } } - + public function testScoringQuestionSet() { $person = self::createTestPerson(); $qs = $person->question_sets->create(); + + // Compute the correct answer to the first question + $first_question = $qs->questions[0]; + $first_question_answer = self::$qs_answers[$first_question->question]; + $first_question_answer_id = 0; + + foreach ($first_question->answers as $answer) { + if ($answer->answer == $first_question_answer) { + $first_question_answer_id = $answer->id; + } + } + $answers = array( - array('question_id' => 1, 'answer_id' => 2), - array('question_id' => 2, 'answer_id' => 2), - array('question_id' => 3, 'answer_id' => 2), - array('question_id' => 4, 'answer_id' => 2), - array('question_id' => 5, 'answer_id' => 2), + array('question_id' => 1, 'answer_id' => $first_question_answer_id), + array('question_id' => 2, 'answer_id' => 6), + array('question_id' => 3, 'answer_id' => 6), + array('question_id' => 4, 'answer_id' => 6), + array('question_id' => 5, 'answer_id' => 6), ); + $score = $qs->score($answers); + + // Test whether questions returned are the same foreach ($qs as $key => $value) { $this->assertSame($score->questions->$key, $value); } + + // Test whether we scored a 20%! + $this->assertSame($score->score, 20.0); + } + + public function testScoringQuestionSet2() + { + $person = self::createTestPerson(); + $qs = $person->question_sets->create(); + + // Compute the correct answer to all the questions! + $answer_ids = array(); + + foreach ($qs->questions as $question) { + $question_answer = self::$qs_answers[$question->question]; + foreach ($question->answers as $answer) { + if ($answer->answer == $question_answer) { + array_push($answer_ids, $answer->id); + } + } + } + + $answers = array( + array('question_id' => 1, 'answer_id' => $answer_ids[0]), + array('question_id' => 2, 'answer_id' => $answer_ids[1]), + array('question_id' => 3, 'answer_id' => $answer_ids[2]), + array('question_id' => 4, 'answer_id' => $answer_ids[3]), + array('question_id' => 5, 'answer_id' => $answer_ids[4]), + ); + + $score = $qs->score($answers); + + // Test whether questions returned are the same + foreach ($qs as $key => $value) { + $this->assertSame($score->questions->$key, $value); + } + + // Test whether we scored a 100%! + $this->assertSame($score->score, 100.0); + } + + public function testScoringQuestionSet3() + { + $person = self::createTestPerson(); + $qs = $person->question_sets->create(); + + $answers = array( + array('question_id' => 1, 'answer_id' => 6), + array('question_id' => 2, 'answer_id' => 6), + array('question_id' => 3, 'answer_id' => 6), + array('question_id' => 4, 'answer_id' => 6), + array('question_id' => 5, 'answer_id' => 6), + ); + + $score = $qs->score($answers); + + // Test whether questions returned are the same + foreach ($qs as $key => $value) { + $this->assertSame($score->questions->$key, $value); + } + + // Test whether we scored a 0%! + $this->assertSame($score->score, 0.0); } } \ No newline at end of file