11 changed files with 350 additions and 2 deletions
-
82app/Http/Controllers/UsersController.php
-
2app/User.php
-
14database/factories/ModelFactory.php
-
28resources/lang/id/auth.php
-
28resources/lang/id/user.php
-
1resources/views/layouts/partials/top-nav.blade.php
-
38resources/views/users/index.blade.php
-
38resources/views/users/partials/forms.blade.php
-
5routes/web.php
-
2tests/Feature/Cart/SearchProductsTest.php
-
114tests/Feature/ManageUsersTest.php
@ -0,0 +1,82 @@ |
|||
<?php |
|||
|
|||
namespace App\Http\Controllers; |
|||
|
|||
use App\User; |
|||
use Illuminate\Http\Request; |
|||
|
|||
class UsersController extends Controller |
|||
{ |
|||
public function index(Request $request) |
|||
{ |
|||
$editableUser = null; |
|||
$users = User::get(); |
|||
|
|||
if (in_array($request->get('action'), ['edit', 'delete']) && $request->has('id')) { |
|||
$editableUser = User::find($request->get('id')); |
|||
} |
|||
|
|||
return view('users.index', compact('users', 'editableUser')); |
|||
} |
|||
|
|||
public function store(Request $request) |
|||
{ |
|||
$this->validate($request, [ |
|||
'name' => 'required|max:60', |
|||
'username' => 'required|max:30', |
|||
'password' => 'nullable|between:5,15', |
|||
]); |
|||
|
|||
$newUserData = $request->only('name','username'); |
|||
|
|||
if ($request->has('password')) |
|||
$newUserData['password'] = $request->get('password'); |
|||
else |
|||
$newUserData['password'] = 'rahasia'; |
|||
|
|||
|
|||
$user = User::create($newUserData); |
|||
|
|||
flash(trans('user.created'), 'success'); |
|||
|
|||
return redirect()->route('users.index'); |
|||
} |
|||
|
|||
public function update(Request $request, $userId) |
|||
{ |
|||
$this->validate($request, [ |
|||
'name' => 'required|max:60', |
|||
'username' => 'required|max:30|unique:users,username,' . $request->segment(2), |
|||
'password' => 'nullable|between:5,15', |
|||
]); |
|||
|
|||
$userData = $request->only('name','username'); |
|||
if ($request->has('password')) |
|||
$userData['password'] = $request->get('password'); |
|||
|
|||
User::findOrFail($userId)->update($userData); |
|||
|
|||
flash(trans('user.updated'), 'success'); |
|||
|
|||
return redirect()->route('users.index'); |
|||
} |
|||
|
|||
public function destroy(Request $request, User $user) |
|||
{ |
|||
$this->validate($request, [ |
|||
'user_id' => 'required|exists:users,id|not_exists:transactions,user_id', |
|||
], [ |
|||
'user_id.not_exists' => trans('user.undeleted'), |
|||
]); |
|||
|
|||
if ($request->get('user_id') == $user->id && $user->delete()) { |
|||
flash(trans('user.deleted'), 'success'); |
|||
|
|||
return redirect()->route('users.index'); |
|||
} |
|||
|
|||
flash(trans('user.undeleted'), 'error'); |
|||
|
|||
return back(); |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
<?php |
|||
|
|||
return [ |
|||
|
|||
'failed' => 'Identitas tersebut tidak cocok dengan data kami.', |
|||
'throttle' => 'Terlalu banyak usaha masuk. Silahkan coba lagi dalam :seconds detik.', |
|||
'username' => 'Username', |
|||
'password' => 'Password', |
|||
'login' => 'Login', |
|||
'remember_me' => 'Ingat Login Saya', |
|||
'logout' => 'Keluar', |
|||
'profile' => 'Profil Saya', |
|||
'register' => 'Buat Akun Baru', |
|||
'have_an_account' => 'Saya sudah punya Akun', |
|||
'need_account' => 'Belum punya Akun?', |
|||
'change_password' => 'Ganti Password', |
|||
'forgot_password' => 'Lupa Password?', |
|||
'reset_password' => 'Reset Password', |
|||
'password_confirmation' => 'Ulangi Password', |
|||
'old_password' => 'Password Lama', |
|||
'new_password' => 'Password Baru', |
|||
'new_password_confirmation' => 'Ulangi Password Baru', |
|||
'send_reset_password_link' => 'Kirim Link Reset Password', |
|||
'old_password_failed' => 'Password lama tidak cocok!', |
|||
'old_password_success' => 'Password berhasil diubah!', |
|||
'welcome_please_login' => 'Selamat datang, silakan login', |
|||
'welcome' => 'Selamat datang kembali :name.', |
|||
]; |
|||
@ -0,0 +1,28 @@ |
|||
<?php |
|||
|
|||
return [ |
|||
// Labels
|
|||
'uset' => 'User', |
|||
'list' => 'Daftar User', |
|||
'search' => 'Cari User', |
|||
'not_found' => 'User tidak ditemukan', |
|||
'empty' => 'Belum ada User', |
|||
'back_to_index' => 'Kembali ke daftar User', |
|||
|
|||
// Actions
|
|||
'create' => 'Input User Baru', |
|||
'created' => 'Input User baru telah berhasil.', |
|||
'show' => 'Detail User', |
|||
'edit' => 'Edit User', |
|||
'update' => 'Update User', |
|||
'updated' => 'Update data User telah berhasil.', |
|||
'delete' => 'Hapus User', |
|||
'delete_confirm' => 'Anda yakin akan menghapus User ini?', |
|||
'deleted' => 'Hapus data User telah berhasil.', |
|||
'undeleted' => 'Data User gagal dihapus.', |
|||
'undeleteable' => 'Data User tidak dapat dihapus.', |
|||
|
|||
// Attributes
|
|||
'name' => 'Nama User', |
|||
'username' => 'Username', |
|||
]; |
|||
@ -0,0 +1,38 @@ |
|||
@extends('layouts.app') |
|||
|
|||
@section('title', trans('user.list')) |
|||
|
|||
@section('content') |
|||
<h3 class="page-header">{{ trans('user.list') }}</h3> |
|||
|
|||
<div class="row"> |
|||
<div class="col-md-8"> |
|||
<div class="panel panel-default table-responsive"> |
|||
<table class="table table-condensed"> |
|||
<thead> |
|||
<tr> |
|||
<th class="text-center">{{ trans('app.table_no') }}</th> |
|||
<th>{{ trans('user.name') }}</th> |
|||
<th>{{ trans('user.username') }}</th> |
|||
<th class="text-center">{{ trans('app.action') }}</th> |
|||
</tr> |
|||
</thead> |
|||
<tbody> |
|||
@foreach($users as $key => $user) |
|||
<tr> |
|||
<td class="text-center">{{ 1 + $key }}</td> |
|||
<td>{{ $user->name }}</td> |
|||
<td>{{ $user->username }}</td> |
|||
<td class="text-center"> |
|||
{!! link_to_route('users.index', trans('app.edit'), ['action' => 'edit', 'id' => $user->id], ['id' => 'edit-user-' . $user->id]) !!} | |
|||
{!! link_to_route('users.index', trans('app.delete'), ['action' => 'delete', 'id' => $user->id], ['id' => 'del-user-' . $user->id]) !!} |
|||
</td> |
|||
</tr> |
|||
@endforeach |
|||
</tbody> |
|||
</table> |
|||
</div> |
|||
</div> |
|||
<div class="col-md-4">@include('users.partials.forms')</div> |
|||
</div> |
|||
@endsection |
|||
@ -0,0 +1,38 @@ |
|||
@if (! Request::has('action')) |
|||
{{ link_to_route('users.index', trans('user.create'), ['action' => 'create'], ['class' => 'btn btn-success pull-right']) }} |
|||
@endif |
|||
@if (Request::get('action') == 'create') |
|||
{!! Form::open(['route' => 'users.store']) !!} |
|||
{!! FormField::text('name', ['label' => trans('user.name'), 'required' => true]) !!} |
|||
{!! FormField::text('username', ['label' => trans('user.username'), 'required' => true]) !!} |
|||
{!! FormField::password('password', ['label' => trans('auth.password'), 'info' => ['text' => 'Kosongkan jika menggunakan password default: <strong>rahasia</strong>']]) !!} |
|||
{!! Form::submit(trans('user.create'), ['class' => 'btn btn-success']) !!} |
|||
{{ link_to_route('users.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} |
|||
{!! Form::close() !!} |
|||
@endif |
|||
@if (Request::get('action') == 'edit' && $editableUser) |
|||
{!! Form::model($editableUser, ['route' => ['users.update', $editableUser->id],'method' => 'patch']) !!} |
|||
{!! FormField::text('name', ['label' => trans('user.name'), 'required' => true]) !!} |
|||
{!! FormField::text('username', ['label' => trans('user.username'), 'required' => true]) !!} |
|||
{!! FormField::password('password', ['label' => trans('auth.password'), 'info' => ['text' => 'Isi untuk mengganti password.']]) !!} |
|||
{!! Form::submit(trans('user.update'), ['class' => 'btn btn-success']) !!} |
|||
{{ link_to_route('users.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} |
|||
{!! Form::close() !!} |
|||
@endif |
|||
@if (Request::get('action') == 'delete' && $editableUser) |
|||
<div class="panel panel-default"> |
|||
<div class="panel-heading"><h3 class="panel-title">{{ trans('user.delete') }}</h3></div> |
|||
<div class="panel-body"> |
|||
<label class="control-label">{{ trans('user.name') }}</label> |
|||
<p>{{ $editableUser->name }}</p> |
|||
<p>{{ $editableUser->username }}</p> |
|||
{!! $errors->first('user_id', '<span class="form-error small">:message</span>') !!} |
|||
<hr> |
|||
{{ trans('user.delete_confirm') }} |
|||
</div> |
|||
<div class="panel-footer"> |
|||
{!! FormField::delete(['route'=>['users.destroy',$editableUser->id]], trans('app.delete_confirm_button'), ['class'=>'btn btn-danger'], ['user_id'=>$editableUser->id]) !!} |
|||
{{ link_to_route('users.index', trans('app.cancel'), [], ['class' => 'btn btn-default']) }} |
|||
</div> |
|||
</div> |
|||
@endif |
|||
@ -0,0 +1,114 @@ |
|||
<?php |
|||
|
|||
namespace Tests\Feature; |
|||
|
|||
use App\User; |
|||
use App\Transaction; |
|||
use Illuminate\Foundation\Testing\DatabaseMigrations; |
|||
use Tests\BrowserKitTestCase; |
|||
|
|||
class ManageUsersTest extends BrowserKitTestCase |
|||
{ |
|||
use DatabaseMigrations; |
|||
|
|||
/** @test */ |
|||
public function user_can_see_user_list() |
|||
{ |
|||
$user1 = factory(User::class)->create(['name' => 'Testing 123']); |
|||
$user2 = factory(User::class)->create(['name' => 'Testing 456']); |
|||
|
|||
$this->loginAsUser(); |
|||
$this->visit(route('users.index')); |
|||
$this->see($user1->name); |
|||
$this->see($user2->name); |
|||
} |
|||
|
|||
/** @test */ |
|||
public function user_can_create_a_user() |
|||
{ |
|||
$this->loginAsUser(); |
|||
$this->visit(route('users.index')); |
|||
|
|||
$this->click(trans('user.create')); |
|||
$this->seePageIs(route('users.index', ['action' => 'create'])); |
|||
|
|||
$this->type('User 1', 'name'); |
|||
$this->type('username', 'username'); |
|||
$this->type('rahasia', 'password'); |
|||
$this->press(trans('user.create')); |
|||
|
|||
$this->see(trans('user.created')); |
|||
$this->seePageIs(route('users.index')); |
|||
|
|||
$this->seeInDatabase('users', [ |
|||
'name' => 'User 1', |
|||
'username' => 'username', |
|||
]); |
|||
} |
|||
|
|||
/** @test */ |
|||
public function user_can_edit_a_user() |
|||
{ |
|||
$this->loginAsUser(); |
|||
$user = factory(User::class)->create(); |
|||
|
|||
$this->visit(route('users.index')); |
|||
$this->click('edit-user-'.$user->id); |
|||
$this->seePageIs(route('users.index', ['action' => 'edit', 'id' => $user->id])); |
|||
|
|||
$this->type('User 1', 'name'); |
|||
$this->type('username', 'username'); |
|||
$this->press(trans('user.update')); |
|||
|
|||
$this->see(trans('user.updated')); |
|||
$this->seePageIs(route('users.index')); |
|||
|
|||
$this->seeInDatabase('users', [ |
|||
'id' => $user->id, |
|||
'name' => 'User 1', |
|||
'username' => 'username', |
|||
]); |
|||
} |
|||
|
|||
/** @test */ |
|||
public function user_can_delete_a_user() |
|||
{ |
|||
$this->loginAsUser(); |
|||
$user = factory(User::class)->create(); |
|||
|
|||
$this->visit(route('users.index')); |
|||
$this->click('del-user-'.$user->id); |
|||
$this->seePageIs(route('users.index', ['action' => 'delete', 'id' => $user->id])); |
|||
|
|||
$this->seeInDatabase('users', [ |
|||
'id' => $user->id, |
|||
]); |
|||
|
|||
$this->press(trans('app.delete_confirm_button')); |
|||
|
|||
$this->dontSeeInDatabase('users', [ |
|||
'id' => $user->id, |
|||
]); |
|||
} |
|||
|
|||
/** @test */ |
|||
public function user_can_not_delete_a_user_that_has_product() |
|||
{ |
|||
$this->loginAsUser(); |
|||
$product = factory(Transaction::class)->create(); |
|||
$userId = $product->user_id; |
|||
|
|||
$this->visit(route('users.index')); |
|||
$this->click('del-user-'.$userId); |
|||
$this->seePageIs(route('users.index', ['action' => 'delete', 'id' => $userId])); |
|||
|
|||
$this->press(trans('app.delete_confirm_button')); |
|||
|
|||
$this->see(trans('user.undeleted')); |
|||
$this->seePageIs(route('users.index', ['action' => 'delete', 'id' => $userId])); |
|||
|
|||
$this->seeInDatabase('users', [ |
|||
'id' => $userId, |
|||
]); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue