public function test_delete_success() { $r = self::factory()->response->create(); $response = new \WeBWorK\Server\Response($r); $this->assertTrue($response->exists()); $this->assertTrue($response->delete()); $response_2 = new \WeBWorK\Server\Vote($r); $this->assertFalse($response_2->exists()); }
public function create_item($request) { $params = $request->get_params(); $item_id = $params['item_id']; $raw_value = $params['value']; $value = null; if ('up' === $raw_value) { $value = 1; } elseif ('down' === $raw_value) { $value = -1; } $item = null; switch ($params['item_type']) { case 'question': $item = new \WeBWorK\Server\Question($item_id); break; case 'response': $item = new \WeBWorK\Server\Response($item_id); break; } $vote = new \WeBWorK\Server\Vote(); $vote->set_user_id(get_current_user_id()); $vote->set_item($item); $vote->populate(); // Don't allow duplicate votes. // This is not really RESTful. On a successful lookup, perform an update. $retval = false; if ($vote->exists() && $value === $vote->get_value()) { // do something } elseif ($value) { $vote->set_value($value); $retval = $vote->save(); } elseif ($vote->exists()) { $retval = $vote->delete(); } $response = rest_ensure_response($retval); if ($retval) { $response->set_status(201); } else { // We return 200 anyway. Not sure how to give good error feedback here. $response->set_status(200); } return $response; }
public function test_delete_success() { self::factory()->vote->create(array('user_id' => 5, 'item' => new MockVoteable(10), 'value' => 15)); $v = new \WeBWorK\Server\Vote(); $v->set_user_id(5); $v->set_item(new MockVoteable(10)); $v->populate(); $this->assertTrue($v->exists()); $this->assertTrue($v->delete()); $v2 = new \WeBWorK\Server\Vote(); $v2->set_user_id(5); $v2->set_item(new MockVoteable(10)); $v2->populate(); $this->assertFalse($v2->exists()); }