From 45b9b88d58e01e9065d62a847e1856c3d54e9612 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Tue, 2 May 2017 15:42:55 +0700 Subject: [PATCH] Added delete a product within search query --- app/Http/Controllers/ProductsController.php | 4 +++- resources/views/products/partials/forms.blade.php | 11 +++++++++-- tests/Feature/ManageProductsTest.php | 22 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/ProductsController.php b/app/Http/Controllers/ProductsController.php index df075fc..a23d1a4 100644 --- a/app/Http/Controllers/ProductsController.php +++ b/app/Http/Controllers/ProductsController.php @@ -62,9 +62,11 @@ class ProductsController extends Controller 'product_id' => 'required|exists:products,id', ]); + $routeParam = $request->only('q'); + if ($request->get('product_id') == $productId && Product::findOrFail($productId)->delete()) { flash(trans('product.deleted'), 'success'); - return redirect()->route('products.index'); + return redirect()->route('products.index', $routeParam); } flash(trans('product.undeleted'), 'error'); diff --git a/resources/views/products/partials/forms.blade.php b/resources/views/products/partials/forms.blade.php index 6dc149b..0f295ba 100644 --- a/resources/views/products/partials/forms.blade.php +++ b/resources/views/products/partials/forms.blade.php @@ -20,7 +20,9 @@
{!! FormField::price('cash_price', ['label' => trans('product.cash_price'), 'required' => true]) !!}
{!! FormField::price('credit_price', ['label' => trans('product.credit_price')]) !!}
- {{ Form::hidden('q', request('q')) }} + @if (request('q')) + {{ Form::hidden('q', request('q')) }} + @endif {!! Form::submit(trans('product.update'), ['class' => 'btn btn-success']) !!} {{ link_to_route('products.index', trans('app.cancel'), Request::only('q'), ['class' => 'btn btn-default']) }} {!! Form::close() !!} @@ -40,7 +42,12 @@ {{ trans('product.delete_confirm') }} diff --git a/tests/Feature/ManageProductsTest.php b/tests/Feature/ManageProductsTest.php index 1801e91..a4ff7b8 100644 --- a/tests/Feature/ManageProductsTest.php +++ b/tests/Feature/ManageProductsTest.php @@ -151,4 +151,26 @@ class ManageProductsTest extends BrowserKitTestCase 'id' => $product->id ]); } + + /** @test */ + public function user_can_delete_a_product_within_search_query() + { + $this->loginAsUser(); + $product = factory(Product::class)->create(['name' => 'Product 123']); + + $this->visit(route('products.index', ['q' => '123'])); + $this->click('del-product-' . $product->id); + + $this->seePageIs(route('products.index', ['action' => 'delete','id' => $product->id, 'q' => '123'])); + $this->seeInDatabase('products', [ + 'id' => $product->id + ]); + + $this->press(trans('app.delete_confirm_button')); + + $this->seePageIs(route('products.index', ['q' => '123'])); + $this->dontSeeInDatabase('products', [ + 'id' => $product->id + ]); + } }