From 27f77dcb2e3944dcbea5871dc7126346631bc0b7 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 26 Mar 2019 16:37:04 +0800 Subject: [PATCH] User can delete issue comments --- app/Http/Controllers/Issues/CommentController.php | 25 ++++++++++++++++++++++ .../issues/partials/comment-section.blade.php | 8 +++++++ routes/web/projects.php | 1 + tests/Feature/Projects/IssueCommentsTest.php | 22 +++++++++++++++++++ 4 files changed, 56 insertions(+) diff --git a/app/Http/Controllers/Issues/CommentController.php b/app/Http/Controllers/Issues/CommentController.php index 1894e32..95dcdbc 100644 --- a/app/Http/Controllers/Issues/CommentController.php +++ b/app/Http/Controllers/Issues/CommentController.php @@ -54,4 +54,29 @@ class CommentController extends Controller return redirect()->route('projects.issues.show', [$issue->project, $issue]); } + + /** + * Remove the specified comment. + * + * @param \App\Entities\Projects\Issue $issue + * @param \\App\Entities\Projects\Comment $comment + * @return \Illuminate\Routing\Redirector + */ + public function destroy(Issue $issue, Comment $comment) + { + $this->authorize('delete', $comment); + + request()->validate([ + 'comment_id' => 'required|exists:comments,id', + ]); + + if (request('comment_id') == $comment->id && $comment->delete()) { + flash(__('comment.deleted'), 'warning'); + + return redirect()->route('projects.issues.show', [$issue->project, $issue]); + } + flash(__('comment.undeleted'), 'error'); + + return back(); + } } diff --git a/resources/views/projects/issues/partials/comment-section.blade.php b/resources/views/projects/issues/partials/comment-section.blade.php index 3cc9d69..73eec00 100644 --- a/resources/views/projects/issues/partials/comment-section.blade.php +++ b/resources/views/projects/issues/partials/comment-section.blade.php @@ -8,6 +8,14 @@ @can('update', $comment) {{ link_to_route('projects.issues.show', __('app.edit'), [$project, $issue, 'action' => 'comment-edit', 'comment_id' => $comment->id], ['id' => 'edit-comment-'.$comment->id, 'class' => 'small', 'title' => __('comment.edit')]) }} @endcan + @can('delete', $comment) + {!! FormField::delete( + ['route' => ['issues.comments.destroy', $issue, $comment], 'class' => ''], + '×', + ['class' => 'btn-link', 'id' => 'delete-comment-'.$comment->id], + ['comment_id' => $comment->id] + ) !!} + @endcan {!! nl2br($comment->body) !!} diff --git a/routes/web/projects.php b/routes/web/projects.php index c162b54..2f4ae02 100644 --- a/routes/web/projects.php +++ b/routes/web/projects.php @@ -111,3 +111,4 @@ Route::patch('issues/{issue}/options', 'Issues\OptionController@update')->name(' */ Route::post('issues/{issue}/comments', 'Issues\CommentController@store')->name('issues.comments.store'); Route::patch('issues/{issue}/comments/{comment}', 'Issues\CommentController@update')->name('issues.comments.update'); +Route::delete('issues/{issue}/comments/{comment}', 'Issues\CommentController@destroy')->name('issues.comments.destroy'); diff --git a/tests/Feature/Projects/IssueCommentsTest.php b/tests/Feature/Projects/IssueCommentsTest.php index a57cc29..a85c290 100644 --- a/tests/Feature/Projects/IssueCommentsTest.php +++ b/tests/Feature/Projects/IssueCommentsTest.php @@ -81,4 +81,26 @@ class IssueCommentsTest extends TestCase ]); } + /** @test */ + public function user_can_delete_an_issue_comment() + { + $this->adminUserSigningIn(); + $issue = factory(Issue::class)->create(); + $comment = factory(Comment::class)->create([ + 'commentable_type' => 'issues', + 'commentable_id' => $issue->id, + 'body' => 'This is issue comment.', + ]); + + $this->visitRoute('projects.issues.show', [$issue->project, $issue]); + $this->seeElement('button', ['id' => 'delete-comment-'.$comment->id]); + $this->press('delete-comment-'.$comment->id); + + $this->seePageIs(route('projects.issues.show', [$issue->project, $issue])); + $this->see(__('comment.deleted')); + + $this->dontSeeInDatabase('comments', [ + 'id' => $comment->id, + ]); + } }