/** * Handles a button click request. * * @return bool `true` if the request was successfully handled, `false` otherwise. */ public function handle(WP_REST_Request $request) { $headers = array(); if (!$this->auth_handler->verify_auth($request, 'button-click')) { return new WP_REST_Response('Invalid auth', 403, $headers); } $post_id = $request->get_param('post_id'); $content = $request->get_param('content'); if (empty($post_id) || empty($content)) { return new WP_REST_Response('Missing data', 400, $headers); } $comment_id = $this->comments_repository->add_vote_for_post($post_id, $content); $message = empty($comment_id) ? 'Could not register click' : $comment_id; $status = empty($comment_id) ? 400 : 200; return new WP_REST_Response($comment_id, $status, $headers); }
/** * @test * it should return 200 response if comment insertion succeeds */ public function it_should_return_200_response_if_comment_insertion_succeeds() { $this->auth_handler->verify_auth(Argument::any(), Argument::any())->willReturn(true); $this->comment_repository->add_vote_for_post(Argument::any(), Argument::any())->willReturn(112); $sut = $this->make_instance(); /** @var \WP_REST_Response $out */ $request = new \WP_REST_Request(); $request->set_param('post_id', 123); $request->set_param('content', 'some content'); $out = $sut->handle($request); $this->assertEquals(200, $out->status); $this->assertEquals(112, $out->data); }