From 75d22eaf3affa38663bc25ba8728f30341d8eaa8 Mon Sep 17 00:00:00 2001 From: Herbert Maschke Date: Thu, 20 Feb 2020 11:25:33 +0100 Subject: [PATCH 1/4] improve validation style of starts_from and ends_till in CartRuleRepository --- .../Http/Controllers/CartRuleController.php | 58 ++++++++++--------- .../src/Repositories/CartRuleRepository.php | 4 +- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/packages/Webkul/CartRule/src/Http/Controllers/CartRuleController.php b/packages/Webkul/CartRule/src/Http/Controllers/CartRuleController.php index 0e74172ac..726d60b5e 100644 --- a/packages/Webkul/CartRule/src/Http/Controllers/CartRuleController.php +++ b/packages/Webkul/CartRule/src/Http/Controllers/CartRuleController.php @@ -10,7 +10,7 @@ use Webkul\CartRule\Repositories\CartRuleCouponRepository; /** * Cart Rule controller * - * @author Jitendra Singh + * @author Jitendra Singh * @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com) */ class CartRuleController extends Controller @@ -39,8 +39,9 @@ class CartRuleController extends Controller /** * Create a new controller instance. * - * @param \Webkul\CartRule\Repositories\CartRuleRepository $cartRuleRepository - * @param \Webkul\CartRule\Repositories\CartRuleCouponRepository $cartRuleCouponRepository + * @param \Webkul\CartRule\Repositories\CartRuleRepository $cartRuleRepository + * @param \Webkul\CartRule\Repositories\CartRuleCouponRepository $cartRuleCouponRepository + * * @return void */ public function __construct( @@ -83,16 +84,16 @@ class CartRuleController extends Controller public function store() { $this->validate(request(), [ - 'name' => 'required', - 'channels' => 'required|array|min:1', - 'customer_groups' => 'required|array|min:1', - 'coupon_type' => 'required', + 'name' => 'required', + 'channels' => 'required|array|min:1', + 'customer_groups' => 'required|array|min:1', + 'coupon_type' => 'required', 'use_auto_generation' => 'required_if:coupon_type,==,1', - 'coupon_code' => 'required_if:use_auto_generation,==,0', - 'starts_from' => 'nullable|date', - 'ends_till' => 'nullable|date|after_or_equal:starts_from', - 'action_type' => 'required', - 'discount_amount' => 'required|numeric' + 'coupon_code' => 'required_if:use_auto_generation,==,0', + 'starts_from' => 'nullable|date', + 'ends_till' => 'nullable|date|after_or_equal:starts_from', + 'action_type' => 'required', + 'discount_amount' => 'required|numeric', ]); $data = request()->all(); @@ -112,6 +113,7 @@ class CartRuleController extends Controller * Show the form for editing the specified resource. * * @param int $id + * * @return \Illuminate\Http\Response */ public function edit($id) @@ -124,23 +126,24 @@ class CartRuleController extends Controller /** * Update the specified resource in storage. * - * @param \Illuminate\Http\Request $request - * @param int $id + * @param \Illuminate\Http\Request $request + * @param int $id + * * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $this->validate(request(), [ - 'name' => 'required', - 'channels' => 'required|array|min:1', - 'customer_groups' => 'required|array|min:1', - 'coupon_type' => 'required', + 'name' => 'required', + 'channels' => 'required|array|min:1', + 'customer_groups' => 'required|array|min:1', + 'coupon_type' => 'required', 'use_auto_generation' => 'required_if:coupon_type,==,1', - 'coupon_code' => 'required_if:use_auto_generation,==,0', - 'starts_from' => 'nullable|date', - 'ends_till' => 'nullable|date|after_or_equal:starts_from', - 'action_type' => 'required', - 'discount_amount' => 'required|numeric' + 'coupon_code' => 'required_if:use_auto_generation,==,0', + 'starts_from' => 'nullable|date', + 'ends_till' => 'nullable|date|after_or_equal:starts_from', + 'action_type' => 'required', + 'discount_amount' => 'required|numeric', ]); $cartRule = $this->cartRuleRepository->findOrFail($id); @@ -159,7 +162,8 @@ class CartRuleController extends Controller /** * Remove the specified resource from storage. * - * @param int $id + * @param int $id + * * @return \Illuminate\Http\Response */ public function destroy($id) @@ -176,7 +180,7 @@ class CartRuleController extends Controller session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Cart Rule'])); return response()->json(['message' => true], 200); - } catch(\Exception $e) { + } catch (\Exception $e) { session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Cart Rule'])); } @@ -191,9 +195,9 @@ class CartRuleController extends Controller public function generateCoupons() { $this->validate(request(), [ - 'coupon_qty' => 'required|integer|min:1', + 'coupon_qty' => 'required|integer|min:1', 'code_length' => 'required|integer|min:10', - 'code_format' => 'required' + 'code_format' => 'required', ]); if (! request('id')) diff --git a/packages/Webkul/CartRule/src/Repositories/CartRuleRepository.php b/packages/Webkul/CartRule/src/Repositories/CartRuleRepository.php index 80847ee67..5d15a8bf2 100755 --- a/packages/Webkul/CartRule/src/Repositories/CartRuleRepository.php +++ b/packages/Webkul/CartRule/src/Repositories/CartRuleRepository.php @@ -125,9 +125,9 @@ class CartRuleRepository extends Repository */ public function create(array $data) { - $data['starts_from'] = $data['starts_from'] ?: null; + $data['starts_from'] = isset($data['starts_from']) ? $data['starts_from'] : null; - $data['ends_till'] = $data['ends_till'] ?: null; + $data['ends_till'] = isset($data['ends_till']) ? $data['ends_till'] : null; $data['status'] = ! isset($data['status']) ? 0 : 1; From 8b5def97563b142c4139d9c736fb20f73abcdc5e Mon Sep 17 00:00:00 2001 From: Herbert Maschke Date: Thu, 20 Feb 2020 11:56:07 +0100 Subject: [PATCH 2/4] introduce CartRuleCest.php --- .env.example | 2 +- bin/test.sh | 8 ++++ tests/functional/CartRule/CartRuleCest.php | 49 ++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/functional/CartRule/CartRuleCest.php diff --git a/.env.example b/.env.example index d09d2b8fd..e710496ca 100644 --- a/.env.example +++ b/.env.example @@ -19,7 +19,7 @@ DB_PREFIX= BROADCAST_DRIVER=log CACHE_DRIVER=file SESSION_DRIVER=file -SESSION_LIFETIME=20 +SESSION_LIFETIME=120 QUEUE_DRIVER=sync REDIS_HOST=127.0.0.1 diff --git a/bin/test.sh b/bin/test.sh index 6f82ff04a..bf7c9962d 100755 --- a/bin/test.sh +++ b/bin/test.sh @@ -15,6 +15,14 @@ printf "### start preparation ###\n" printf ">> truncate and migrate database\n" php artisan migrate:fresh --env=testing --quiet + printf ">> cleaning laravel caches ###\n" + ${WORKPATH}/../php artisan view:clear + ${WORKPATH}/../php artisan route:clear + ${WORKPATH}/../php artisan cache:clear + ${WORKPATH}/../php artisan config:clear + + printf ">> cleaning previous tests ###\n" + ${WORKPATH}/../vendor/bin/codecept clean printf "### finish preparation ###\n" printf "### start tests ###\n" diff --git a/tests/functional/CartRule/CartRuleCest.php b/tests/functional/CartRule/CartRuleCest.php new file mode 100644 index 000000000..18191ff9b --- /dev/null +++ b/tests/functional/CartRule/CartRuleCest.php @@ -0,0 +1,49 @@ +loginAsAdmin(); + + $I->amOnAdminRoute('admin.cart-rules.index'); + + // we are dealing with vue.js so we can not do classical form filling + $I->sendAjaxPostRequest(route('admin.cart-rules.store'), [ + '_token' => csrf_token(), + 'name' => 'Demo Cart Rule', + + // the following fields are important to send with the POST request: + 'starts_from' => '', + 'ends_till' => '', + + 'use_auto_generation' => 0, + 'coupon_type' => 0, // no coupon + 'action_type' => 'by_percent', + 'coupon_code' => 'coupon', + 'discount_amount' => '10', + + 'channels' => [ + 'default', + ], + + 'customer_groups' => [ + 'guest', + ], + ]); + + $cartRule = $I->grabRecord(CartRule::class, [ + 'name' => 'Demo Cart Rule', + ]); + + $I->seeResponseCodeIsSuccessful(); + + } +} From 80724d24116e6009dbfa46799a1f0597f10b9ea4 Mon Sep 17 00:00:00 2001 From: Herbert Maschke Date: Thu, 20 Feb 2020 12:51:23 +0100 Subject: [PATCH 3/4] fix bin/test.sh --- bin/test.sh | 6 ------ 1 file changed, 6 deletions(-) diff --git a/bin/test.sh b/bin/test.sh index bf7c9962d..ef0e70d73 100755 --- a/bin/test.sh +++ b/bin/test.sh @@ -15,12 +15,6 @@ printf "### start preparation ###\n" printf ">> truncate and migrate database\n" php artisan migrate:fresh --env=testing --quiet - printf ">> cleaning laravel caches ###\n" - ${WORKPATH}/../php artisan view:clear - ${WORKPATH}/../php artisan route:clear - ${WORKPATH}/../php artisan cache:clear - ${WORKPATH}/../php artisan config:clear - printf ">> cleaning previous tests ###\n" ${WORKPATH}/../vendor/bin/codecept clean printf "### finish preparation ###\n" From f7219767edc0681d63b7ac9a93b3707daa1b7a83 Mon Sep 17 00:00:00 2001 From: Herbert Maschke Date: Thu, 20 Feb 2020 13:26:14 +0100 Subject: [PATCH 4/4] fix CartRuleRepository once more --- .../Webkul/CartRule/src/Repositories/CartRuleRepository.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/Webkul/CartRule/src/Repositories/CartRuleRepository.php b/packages/Webkul/CartRule/src/Repositories/CartRuleRepository.php index 5d15a8bf2..b7f7abd5d 100755 --- a/packages/Webkul/CartRule/src/Repositories/CartRuleRepository.php +++ b/packages/Webkul/CartRule/src/Repositories/CartRuleRepository.php @@ -125,9 +125,9 @@ class CartRuleRepository extends Repository */ public function create(array $data) { - $data['starts_from'] = isset($data['starts_from']) ? $data['starts_from'] : null; + $data['starts_from'] = isset($data['starts_from']) && $data['starts_from'] ? $data['starts_from'] : null; - $data['ends_till'] = isset($data['ends_till']) ? $data['ends_till'] : null; + $data['ends_till'] = isset($data['ends_till']) && $data['ends_till'] ? $data['ends_till'] : null; $data['status'] = ! isset($data['status']) ? 0 : 1;