Merge pull request #2521 from Haendlerbund/improve-cart-item-validation

Automated test for Cart Rule Creation in Admin Backend
This commit is contained in:
Jitendra Singh 2020-02-21 11:29:34 +05:30 committed by GitHub
commit f9c608bbda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 85 additions and 30 deletions

View File

@ -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

View File

@ -15,6 +15,8 @@ printf "### start preparation ###\n"
printf ">> truncate and migrate database\n"
php artisan migrate:fresh --env=testing --quiet
printf ">> cleaning previous tests ###\n"
${WORKPATH}/../vendor/bin/codecept clean
printf "### finish preparation ###\n"
printf "### start tests ###\n"

View File

@ -10,7 +10,7 @@ use Webkul\CartRule\Repositories\CartRuleCouponRepository;
/**
* Cart Rule controller
*
* @author Jitendra Singh <jitendra@webkul.com>
* @author Jitendra Singh <jitendra@webkul.com>
* @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'))

View File

@ -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'] ? $data['starts_from'] : null;
$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;

View File

@ -0,0 +1,49 @@
<?php
use Webkul\Product\Models\ProductAttributeValue;
use Webkul\Product\Models\ProductFlat;
use Webkul\CartRule\Models\CartRule;
use Haendlerbund\SalesforceApi\Models\HbCartRule;
class CartRuleCest
{
public function testCartRuleCreation(FunctionalTester $I)
{
$I->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();
}
}