Skip to content

Commit

Permalink
Merge ecd17e6 into b7e903f
Browse files Browse the repository at this point in the history
  • Loading branch information
dels07 committed Jul 7, 2019
2 parents b7e903f + ecd17e6 commit 06f9eff
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 54 deletions.
9 changes: 8 additions & 1 deletion app/Http/Requests/Categories/DeleteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function authorize()
public function rules()
{
return [
'category_id' => 'required',
'category_id' => 'required',
'delete_transactions' => 'boolean',
];
}

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

if ($this->has('delete_transactions')) {
$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
19 changes: 10 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,18 @@
{!! $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::checkbox('delete_transactions', 1, false, ['id' => 'delete_transactions']) !!}
{!! Form::label('delete_transactions', __('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
45 changes: 44 additions & 1 deletion 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 @@ -81,17 +82,59 @@ public function user_can_update_a_category()
}

/** @test */
public function user_can_delete_a_category()
public function user_can_delete_a_category_without_deleting_any_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,
], [
'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
36 changes: 34 additions & 2 deletions tests/Feature/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 @@ -70,10 +71,11 @@ public function user_can_edit_a_category_within_search_query()
}

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

$this->visit(route('categories.index', ['action' => 'edit', 'id' => $category->id]));
$this->click('del-category-'.$category->id);
Expand All @@ -83,10 +85,40 @@ public function user_can_delete_a_category()
'id' => $category->id,
]);

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

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

$this->dontSeeInDatabase('transactions', [
'category_id' => $category->id,
]);
}

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

$this->visit(route('categories.index', ['action' => 'delete', 'id' => $category->id]));

$this->submitForm(__('app.delete_confirm_button'), [
'category_id' => $category->id,
]);

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

$this->seeInDatabase('transactions', [
'id' => $transaction->id,
'category_id' => null,
]);
}
}
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));
}
}
3 changes: 2 additions & 1 deletion tests/Unit/Requests/Categories/DeleteRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function it_fails_for_empty_attributes()
private function getDeleteAttributes($overrides = [])
{
return array_merge([
'category_id' => '1',
'category_id' => '1',
'delete_transactions' => '1',
], $overrides);
}
}

0 comments on commit 06f9eff

Please sign in to comment.