public function test_successful_save_for_new_item()
 {
     $response = new \WeBWorK\Server\Response();
     $response->set_content('foo');
     $response->set_question_id(20);
     $response->set_is_answer(false);
     $saved = $response->save();
     $this->assertTrue($saved);
     $new_response = new \WeBWorK\Server\Response($response->get_id());
     $this->assertSame('foo', $new_response->get_content());
     $this->assertSame(20, $new_response->get_question_id());
     $this->assertFalse($new_response->get_is_answer());
 }
 public function create_object($args)
 {
     $args['post_type'] = 'webwork_response';
     $post_id = parent::create_object($args);
     if (!$post_id || is_wp_error($post_id)) {
         return $post_id;
     }
     $response = new \WeBWorK\Server\Response($post_id);
     $is_answer = isset($args['is_answer']) ? (bool) $args['is_answer'] : false;
     $response->set_is_answer($is_answer);
     $question_id = isset($args['question_id']) ? (int) $args['question_id'] : false;
     if ($question_id) {
         $response->set_question_id($question_id);
     }
     if (isset($args['post_date'])) {
         $response->set_post_date($args['post_date']);
     }
     $response->save();
     return $post_id;
 }
 public function update_item($request)
 {
     $retval = false;
     $params = $request->get_params();
     if (isset($params['id']) && isset($params['is_answer'])) {
         $response = new \WeBWorK\Server\Response($params['id']);
         if ($response->exists()) {
             $response->set_is_answer($params['is_answer']);
             $response->save();
             $q = new \WeBWork\Server\Question\Query(array('question_id' => $response->get_question_id()));
             $retval = $q->get_for_endpoint();
         }
     }
     $response = rest_ensure_response($retval);
     if ($retval) {
         $response->set_status(200);
     } else {
         $response->set_status(500);
     }
     return $response;
 }
 public function update_item($request)
 {
     $retval = false;
     $params = $request->get_params();
     if (isset($params['id']) && isset($params['is_answer'])) {
         $response = new \WeBWorK\Server\Response($params['id']);
         if ($response->exists()) {
             $response->set_is_answer($params['is_answer']);
             $retval = $response->save();
         }
     }
     $response = rest_ensure_response($retval);
     if ($retval) {
         $response->set_status(200);
     } else {
         $response->set_status(500);
     }
     return $response;
 }
 public function test_get_has_answer_cache_should_be_busted_after_response_save()
 {
     $q = self::factory()->question->create_and_get();
     $r = new \WeBWorK\Server\Response();
     $r->set_question_id($q->get_id());
     $r->save();
     $this->assertFalse($q->get_has_answer());
     $r->set_is_answer(true);
     $r->save();
     $this->assertTrue($q->get_has_answer());
 }