Skip to content

Commit

Permalink
Merge 9f25f36 into b7e903f
Browse files Browse the repository at this point in the history
  • Loading branch information
dels07 committed Jun 29, 2019
2 parents b7e903f + 9f25f36 commit 49ae5cf
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 51 deletions.
7 changes: 7 additions & 0 deletions app/Http/Requests/Categories/DeleteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function rules()
{
return [
'category_id' => 'required',
'delete_transactions' => 'required',
];
}

Expand All @@ -37,6 +38,12 @@ public function delete()
{
$category = $this->route('category');

if ($this->get('delete_transactions') == 1) {
$category->transactions()->delete();
} else {
$category->transactions()->update(['category_id' => null]);
}

if ($this->get('category_id') == $category->id) {
return $category->delete();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Policies/CategoryPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ public function update(User $user, Category $category)
public function delete(User $user, Category $category)
{
// Update $user authorization to delete $category here.
return $user->id == $category->creator_id && $category->transactions->isEmpty();
return $user->id == $category->creator_id;
}
}
23 changes: 12 additions & 11 deletions resources/lang/en/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
'back_to_index' => 'Back to Category List',

// Actions
'create' => 'Create new Category',
'created' => 'A new Category has been created.',
'show' => 'View Category Detail',
'edit' => 'Edit Category',
'update' => 'Update Category',
'updated' => 'Category data has been updated.',
'delete' => 'Delete Category',
'delete_confirm' => 'Are you sure to delete this Category?',
'deleted' => 'Category has been deleted.',
'undeleted' => 'Category not deleted.',
'undeleteable' => 'Category data cannot be deleted.',
'create' => 'Create new Category',
'created' => 'A new Category has been created.',
'show' => 'View Category Detail',
'edit' => 'Edit Category',
'update' => 'Update Category',
'updated' => 'Category data has been updated.',
'delete' => 'Delete Category',
'delete_confirm' => 'Are you sure to delete this Category?',
'deleted' => 'Category has been deleted.',
'undeleted' => 'Category not deleted.',
'undeleteable' => 'Category data cannot be deleted.',
'delete_transactions' => 'Delete all transactions belong to this category',

// Attributes
'name' => 'Category Name',
Expand Down
23 changes: 12 additions & 11 deletions resources/lang/id/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
'back_to_index' => 'Kembali ke daftar Kategori',

// Actions
'create' => 'Input Kategori Baru',
'created' => 'Input Kategori baru telah berhasil.',
'show' => 'Lihat Detail Kategori',
'edit' => 'Edit Kategori',
'update' => 'Update Kategori',
'updated' => 'Update data Kategori telah berhasil.',
'delete' => 'Hapus Kategori',
'delete_confirm' => 'Anda yakin akan menghapus Kategori ini?',
'deleted' => 'Hapus data Kategori telah berhasil.',
'undeleted' => 'Data Kategori gagal dihapus.',
'undeleteable' => 'Data Kategori tidak dapat dihapus.',
'create' => 'Input Kategori Baru',
'created' => 'Input Kategori baru telah berhasil.',
'show' => 'Lihat Detail Kategori',
'edit' => 'Edit Kategori',
'update' => 'Update Kategori',
'updated' => 'Update data Kategori telah berhasil.',
'delete' => 'Hapus Kategori',
'delete_confirm' => 'Anda yakin akan menghapus Kategori ini?',
'deleted' => 'Hapus data Kategori telah berhasil.',
'undeleted' => 'Data Kategori gagal dihapus.',
'undeleteable' => 'Data Kategori tidak dapat dihapus.',
'delete_transactions' => 'Hapus semua Transaksi pada Kategori ini',

// Attributes
'name' => 'Nama Kategori',
Expand Down
20 changes: 11 additions & 9 deletions resources/views/categories/forms.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
{{ link_to_route('categories.index', '×', [], ['class' => 'close']) }}
<h4 class="modal-title">{{ __('category.delete') }} {{ $editableCategory->type }}</h4>
</div>
{!! Form::open(['url' => route('categories.destroy', $editableCategory), 'method' => 'DELETE']) !!}
<div class="modal-body">
<label class="control-label">{{ __('category.name') }}</label>
<p>{!! $editableCategory->name_label !!}</p>
Expand All @@ -102,18 +103,19 @@
{!! $errors->first('category_id', '<span class="form-error small">:message</span>') !!}
</div>
<hr style="margin:0">
<div class="modal-body">{{ __('app.delete_confirm') }}</div>
<div class="modal-body">
{!! Form::hidden('category_id', $editableCategory->id) !!}
{!! Form::hidden('delete_transactions', '0') !!}
{!! Form::checkbox('delete_transactions', '1', false) !!}
{!! Form::label(__('category.delete_transactions')) !!}
<br>
{{ __('app.delete_confirm') }}
</div>
<div class="modal-footer">
{!! FormField::delete(
['route' => ['categories.destroy', $editableCategory], 'class' => ''],
__('app.delete_confirm_button'),
['class'=>'btn btn-danger'],
[
'category_id' => $editableCategory->id,
]
) !!}
{!! Form::submit(__('app.delete_confirm_button'), ['class' => 'btn btn-danger']) !!}
{{ link_to_route('categories.index', __('app.cancel'), [], ['class' => 'btn btn-default']) }}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
Expand Down
44 changes: 44 additions & 0 deletions tests/Feature/Api/ManageCategoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Category;
use Tests\TestCase;
use App\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ManageCategoriesTest extends TestCase
Expand Down Expand Up @@ -85,13 +86,56 @@ public function user_can_delete_a_category()
{
$user = $this->createUser();
$category = factory(Category::class)->create(['creator_id' => $user->id]);
$transaction = factory(Transaction::class)->create([
'category_id' => $category->id,
'creator_id' => $user->id,
]);

$this->deleteJson(route('api.categories.destroy', $category), [
'category_id' => $category->id,
'delete_transactions' => 0,
], [
'Authorization' => 'Bearer '.$user->api_token,
]);

// check for related transactions
$this->seeInDatabase('transactions', [
'id' => $transaction->id,
'category_id' => null,
]);

$this->dontSeeInDatabase('categories', [
'id' => $category->id,
]);

$this->seeStatusCode(200);
$this->seeJson([
'message' => __('category.deleted'),
]);
}

/** @test */
public function user_can_delete_a_category_and_transactions()
{
$user = $this->createUser();
$category = factory(Category::class)->create(['creator_id' => $user->id]);
$transaction = factory(Transaction::class)->create([
'category_id' => $category->id,
'creator_id' => $user->id,
]);

$this->deleteJson(route('api.categories.destroy', $category), [
'category_id' => $category->id,
'delete_transactions' => 1,
], [
'Authorization' => 'Bearer '.$user->api_token,
]);

// check for related transactions
$this->dontSeeInDatabase('transactions', [
'id' => $transaction->id,
]);

$this->dontSeeInDatabase('categories', [
'id' => $category->id,
]);
Expand Down
4 changes: 3 additions & 1 deletion tests/Feature/ManageCategoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public function user_can_delete_a_category()
'id' => $category->id,
]);

$this->press(trans('app.delete_confirm_button'));
$this->submitForm(__('app.delete_confirm_button'), [
'delete_transactions' => 1,
]);

$this->dontSeeInDatabase('categories', [
'id' => $category->id,
Expand Down
17 changes: 0 additions & 17 deletions tests/Unit/Policies/CategoryPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Category;
use Tests\TestCase;
use App\Transaction;
use Illuminate\Foundation\Testing\RefreshDatabase;

class CategoryPolicyTest extends TestCase
Expand Down Expand Up @@ -50,20 +49,4 @@ public function user_can_only_delete_their_own_category()
$this->assertTrue($user->can('delete', $category));
$this->assertFalse($user->can('delete', $othersCategory));
}

/** @test */
public function user_cannot_delete_used_category()
{
$user = $this->loginAsUser();
$usedCategory = factory(Category::class)->create(['creator_id' => $user->id]);
$transaction = factory(Transaction::class)->create([
'creator_id' => $user->id,
'category_id' => $usedCategory->id,
]);

$unusedCategory = factory(Category::class)->create(['creator_id' => $user->id]);

$this->assertFalse($user->can('delete', $usedCategory));
$this->assertTrue($user->can('delete', $unusedCategory));
}
}
4 changes: 3 additions & 1 deletion tests/Unit/Requests/Categories/DeleteRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ public function it_pass_for_required_attributes()
public function it_fails_for_empty_attributes()
{
$this->assertValidationFails(new CategoryDeleteRequest(), [], function ($errors) {
$this->assertCount(1, $errors);
$this->assertCount(2, $errors);
$this->assertEquals(__('validation.required'), $errors->first('category_id'));
$this->assertEquals(__('validation.required'), $errors->first('delete_transactions'));
});
}

private function getDeleteAttributes($overrides = [])
{
return array_merge([
'category_id' => '1',
'delete_transactions' => '1',
], $overrides);
}
}

0 comments on commit 49ae5cf

Please sign in to comment.