10 changed files with 332 additions and 9 deletions
-
82app/Http/Controllers/Jobs/CommentsController.php
-
9app/Http/Controllers/JobsController.php
-
27app/Policies/Projects/JobPolicy.php
-
6resources/lang/id/comment.php
-
32resources/views/jobs/partials/comment-section.blade.php
-
39resources/views/jobs/show.blade.php
-
2routes/web.php
-
10routes/web/projects.php
-
104tests/Feature/Projects/JobCommentsTest.php
-
30tests/Unit/Policies/JobPolicyTest.php
@ -0,0 +1,82 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Controllers\Jobs; |
||||
|
|
||||
|
use Illuminate\Http\Request; |
||||
|
use App\Entities\Projects\Job; |
||||
|
use App\Entities\Projects\Comment; |
||||
|
use App\Http\Controllers\Controller; |
||||
|
|
||||
|
class CommentsController extends Controller |
||||
|
{ |
||||
|
/** |
||||
|
* Store a new comment in storage. |
||||
|
* |
||||
|
* @param \Illuminate\Http\Request $request |
||||
|
* @param \App\Entities\Projects\Job $job |
||||
|
* @return \Illuminate\Http\RedirectResponse |
||||
|
*/ |
||||
|
public function store(Request $request, Job $job) |
||||
|
{ |
||||
|
$this->authorize('comment-on', $job); |
||||
|
|
||||
|
$newComment = $request->validate([ |
||||
|
'body' => 'required|string|max:255', |
||||
|
]); |
||||
|
|
||||
|
$job->comments()->create([ |
||||
|
'body' => $newComment['body'], |
||||
|
'creator_id' => auth()->id(), |
||||
|
]); |
||||
|
|
||||
|
flash(__('comment.created'), 'success'); |
||||
|
|
||||
|
return back(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Update the specified comment. |
||||
|
* |
||||
|
* @param \Illuminate\Http\Request $request |
||||
|
* @param \App\Entities\Projects\Job $job |
||||
|
* @param \App\Entities\Jobs\Comment $comment |
||||
|
* @return \Illuminate\Http\Response |
||||
|
*/ |
||||
|
public function update(Request $request, Job $job, Comment $comment) |
||||
|
{ |
||||
|
$this->authorize('update', $comment); |
||||
|
|
||||
|
$commentData = $request->validate([ |
||||
|
'body' => 'required|string|max:255', |
||||
|
]); |
||||
|
$comment->update($commentData); |
||||
|
flash(__('comment.updated'), 'success'); |
||||
|
|
||||
|
return redirect()->route('jobs.show', [$job] + request(['page'])); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Remove the specified comment. |
||||
|
* |
||||
|
* @param \App\Entities\Jobs\Comment $comment |
||||
|
* @return \Illuminate\Routing\Redirector |
||||
|
*/ |
||||
|
public function destroy(Job $job, Comment $comment) |
||||
|
{ |
||||
|
$this->authorize('delete', $comment); |
||||
|
|
||||
|
request()->validate([ |
||||
|
'comment_id' => 'required|exists:comments,id', |
||||
|
]); |
||||
|
|
||||
|
if (request('comment_id') == $comment->id && $comment->delete()) { |
||||
|
$routeParam = [$job] + request(['page']); |
||||
|
flash(__('comment.deleted'), 'warning'); |
||||
|
|
||||
|
return redirect()->route('jobs.show', $routeParam); |
||||
|
} |
||||
|
flash(__('comment.undeleted'), 'error'); |
||||
|
|
||||
|
return back(); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
@can('comment-on', $job) |
||||
|
{{ Form::open(['route' => ['jobs.comments.store', $job]]) }} |
||||
|
<div class="row"> |
||||
|
<div class="col-md-9">{!! FormField::textarea('body', ['required' => true, 'label' => false, 'placeholder' => __('comment.create_text')]) !!}</div> |
||||
|
<div class="col-md-3"> |
||||
|
{{ Form::submit(__('comment.create'), ['class' => 'btn btn-success btn-block']) }}<br> |
||||
|
</div> |
||||
|
</div> |
||||
|
{{ Form::close() }} |
||||
|
@endcan |
||||
|
@foreach($comments as $comment) |
||||
|
<div class="alert alert-warning"> |
||||
|
<legend style="font-size: 14px;margin-bottom: 10px;"> |
||||
|
<span class="label label-default pull-right">{{ $comment->time_display }}</span> |
||||
|
<strong>{{ $comment->creator->name }}</strong> |
||||
|
</legend> |
||||
|
<div class="pull-right"> |
||||
|
@can('update', $comment) |
||||
|
{{ link_to_route('jobs.show', __('app.edit'), [$job, 'action' => 'comment-edit', 'comment_id' => $comment->id], ['id' => 'edit-comment-'.$comment->id, 'class' => 'small', 'title' => __('comment.edit')]) }} |
||||
|
@endcan |
||||
|
@can('delete', $comment) |
||||
|
{!! FormField::delete( |
||||
|
['route' => ['jobs.comments.destroy', $job, $comment], 'class' => ''], |
||||
|
'×', |
||||
|
['class' => 'btn-link', 'id' => 'delete-comment-'.$comment->id], |
||||
|
['comment_id' => $comment->id, 'page' => request('page')] |
||||
|
) !!} |
||||
|
@endcan |
||||
|
</div> |
||||
|
{!! nl2br($comment->body) !!} |
||||
|
</div> |
||||
|
@endforeach |
||||
@ -0,0 +1,104 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace Tests\Feature\Projects; |
||||
|
|
||||
|
use Tests\TestCase; |
||||
|
use App\Entities\Projects\Job; |
||||
|
use App\Entities\Projects\Comment; |
||||
|
|
||||
|
class JobCommentsTest extends TestCase |
||||
|
{ |
||||
|
/** @test */ |
||||
|
public function user_can_view_job_comments() |
||||
|
{ |
||||
|
$this->adminUserSigningIn(); |
||||
|
$job = factory(Job::class)->create(); |
||||
|
$comment = factory(Comment::class)->create([ |
||||
|
'commentable_type' => 'jobs', |
||||
|
'commentable_id' => $job->id, |
||||
|
'body' => 'This is job comment.', |
||||
|
]); |
||||
|
|
||||
|
$this->visitRoute('jobs.show', $job); |
||||
|
$this->seeRouteIs('jobs.show', $job); |
||||
|
|
||||
|
$this->seeText('This is job comment.'); |
||||
|
} |
||||
|
|
||||
|
/** @test */ |
||||
|
public function admin_can_add_comment_to_a_job() |
||||
|
{ |
||||
|
$admin = $this->adminUserSigningIn(); |
||||
|
$job = factory(Job::class)->create(); |
||||
|
|
||||
|
$this->visitRoute('jobs.show', $job); |
||||
|
|
||||
|
$this->submitForm(__('comment.create'), [ |
||||
|
'body' => 'Komentar pertama.', |
||||
|
]); |
||||
|
|
||||
|
$this->seePageIs(route('jobs.show', $job)); |
||||
|
$this->see(__('comment.created')); |
||||
|
|
||||
|
$this->seeInDatabase('comments', [ |
||||
|
'commentable_type' => 'jobs', |
||||
|
'commentable_id' => $job->id, |
||||
|
'body' => 'Komentar pertama.', |
||||
|
'creator_id' => $admin->id, |
||||
|
]); |
||||
|
} |
||||
|
|
||||
|
/** @test */ |
||||
|
public function user_can_edit_comment() |
||||
|
{ |
||||
|
$this->adminUserSigningIn(); |
||||
|
$job = factory(Job::class)->create(); |
||||
|
$comment = factory(Comment::class)->create([ |
||||
|
'commentable_type' => 'jobs', |
||||
|
'commentable_id' => $job->id, |
||||
|
'body' => 'This is job comment.', |
||||
|
]); |
||||
|
|
||||
|
$this->visitRoute('jobs.show', $job); |
||||
|
$this->seeElement('a', ['id' => 'edit-comment-'.$comment->id]); |
||||
|
$this->click('edit-comment-'.$comment->id); |
||||
|
$this->seeRouteIs('jobs.show', [$job, 'action' => 'comment-edit', 'comment_id' => $comment->id]); |
||||
|
|
||||
|
$this->submitForm(__('comment.update'), [ |
||||
|
'body' => 'Komentar pertama.', |
||||
|
]); |
||||
|
|
||||
|
$this->seePageIs(route('jobs.show', $job)); |
||||
|
$this->see(__('comment.updated')); |
||||
|
|
||||
|
$this->seeInDatabase('comments', [ |
||||
|
'id' => $comment->id, |
||||
|
'commentable_type' => 'jobs', |
||||
|
'commentable_id' => $job->id, |
||||
|
'body' => 'Komentar pertama.', |
||||
|
]); |
||||
|
} |
||||
|
|
||||
|
/** @test */ |
||||
|
public function user_can_delete_comment() |
||||
|
{ |
||||
|
$this->adminUserSigningIn(); |
||||
|
$job = factory(Job::class)->create(); |
||||
|
$comment = factory(Comment::class)->create([ |
||||
|
'commentable_type' => 'jobs', |
||||
|
'commentable_id' => $job->id, |
||||
|
'body' => 'This is job comment.', |
||||
|
]); |
||||
|
|
||||
|
$this->visitRoute('jobs.show', $job); |
||||
|
$this->seeElement('button', ['id' => 'delete-comment-'.$comment->id]); |
||||
|
$this->press('delete-comment-'.$comment->id); |
||||
|
|
||||
|
$this->seePageIs(route('jobs.show', $job)); |
||||
|
$this->see(__('comment.deleted')); |
||||
|
|
||||
|
$this->dontSeeInDatabase('comments', [ |
||||
|
'id' => $comment->id, |
||||
|
]); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue