/** * @test * it should render the comments count display if there are idlikethis comments associated with * the post */ public function it_should_render_the_comments_count_display_if_there_are_idlikethis_comments_associated_with_the_post() { $post = $this->factory()->post->create_and_get(); $comments = $this->factory()->comment->create_many(10); $this->commments_repository->get_votes_for_post($post)->willReturn(['first idea' => array_splice($comments, 0, 5), 'second idea' => $comments]); $this->texts_provider->get_comments_title_text()->shouldBeCalled(); $this->render_engine->render(Argument::any(), Argument::any())->shouldBeCalled(); $sut = $this->make_instance(); $sut->render($post, []); }
/** * @test * it should return 200 response if comment consolidation succeeds */ public function it_should_return_200_response_if_comment_consolidation_succeeds() { $this->auth_handler->verify_auth(Argument::any(), Argument::any())->willReturn(true); $this->comments_repository->consolidate_votes_for_post(Argument::any())->willReturn(true); $sut = $this->make_instance(); /** @var \WP_REST_Response $out */ $request = new \WP_REST_Request(); $request->set_param('post_id', 123); $out = $sut->handle($request); $this->assertEquals(200, $out->status); $this->assertEquals(true, $out->data); }
/** * Handles a 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, 'reset-all')) { return new WP_REST_Response('Invalid auth', 403, $headers); } $post_id = $request->get_param('post_id'); if (empty($post_id)) { return new WP_REST_Response('Missing post ID', 400, $headers); } $exit = $this->comments_repository->reset_votes_for_post($post_id); $status = $exit === false ? 400 : 200; return new WP_REST_Response($exit, $status, $headers); }
/** * 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 show the consolidate all comments control if there are idlikethis comments associated to the post */ public function it_should_show_the_consolidate_all_comments_control_if_there_are_idlikethis_comments_associated_to_the_post() { $post = $this->factory()->post->create_and_get(); $comments_ids = $this->factory()->comment->create_many(3, ['comment_post_ID' => $post->ID, 'comment_type' => 'idlikethis']); $this->comments_repository->get_votes_for_post($post)->willReturn(['some idea' => $comments_ids]); $this->context->get_post_id()->willReturn($post->ID); $this->texts_provider->get_comments_title_text()->shouldBeCalled(); $this->texts_provider->get_reset_all_text()->willReturn('foo'); $this->texts_provider->get_consolidate_all_text()->shouldBeCalled(); $sut = $this->make_instance(); $sut->render($post, []); }
/** * Echoes the meta box markup to the page. * * @param string|WP_Post|array $object * @param array|mixed $box The meta box registration description. * @return void */ public function render($object, $box) { $comments = $this->comments_repository->get_votes_for_post($object); if (empty($comments)) { $this->template_data['has_comments'] = false; $this->template_data['header_text'] = $this->texts->get_empty_comments_text(); } else { $this->template_data['has_comments'] = true; $this->template_date['header_text'] = $this->texts->get_comments_title_text(); $this->template_data['reset_all_text'] = $this->texts->get_reset_all_text(); $this->template_data['consolidate_all_text'] = $this->texts->get_consolidate_all_text(); $this->template_data['post_id'] = $this->context->get_post_id(); } echo $this->render_engine->render($this->template_slug, $this->template_data); }
/** * Echoes the meta box markup to the page. * * @param string|WP_Post|array $object * @param array|mixed $box The meta box registration description. * @return void */ public function render($object, $box) { $comments = $this->comments_repository->get_votes_for_post($object); if (empty($comments)) { $this->template_data['has_comments'] = false; $this->template_data['header_text'] = $this->texts->get_empty_comments_text(); } else { $this->template_data['has_comments'] = true; $this->template_data['header_text'] = $this->texts->get_comments_title_text(); array_walk($comments, array($this, 'count_comments')); arsort($comments); $this->template_data['rows'] = $comments; } echo $this->render_engine->render($this->template_slug, $this->template_data); }