From 70e92e2b6d919c8b59a716d9164f6067bd1f887f Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Thu, 29 Oct 2020 13:17:04 +0530 Subject: [PATCH 1/5] Cancel Order Method Added --- tests/functional/Admin/Sales/OrderCest.php | 160 ++++++++++++++++++++- 1 file changed, 158 insertions(+), 2 deletions(-) diff --git a/tests/functional/Admin/Sales/OrderCest.php b/tests/functional/Admin/Sales/OrderCest.php index f48aa0aa9..2af683051 100644 --- a/tests/functional/Admin/Sales/OrderCest.php +++ b/tests/functional/Admin/Sales/OrderCest.php @@ -2,10 +2,16 @@ namespace Tests\Functional\Admin\Sales; - +use Faker\Factory; use FunctionalTester; +use Codeception\Util\Locator; use Webkul\Sales\Models\Order; - +use Webkul\Core\Models\Channel; +use Webkul\Customer\Models\Customer; +use Webkul\Sales\Models\OrderAddress; +use Webkul\Sales\Models\OrderPayment; +use Webkul\Checkout\Models\CartAddress; +use Webkul\Checkout\Models\CartPayment; class OrderCest { @@ -23,4 +29,154 @@ class OrderCest $I->see($order->id, '//script[@type="text/x-template"]'); $I->see($order->sub_total, '//script[@type="text/x-template"]'); } + + public function testCancelCashOnDeliveryOrder(FunctionalTester $I): void + { + /* place order for customer */ + $order = $this->placeCashOnDeliveryOrderForCustomer($I); + + /* go to admin page and login */ + $this->goToAdminLoginPageAndSignIn($I); + + /* go to order view page */ + $I->amOnPage(route('admin.sales.orders.view', $order->id)); + $I->seeCurrentRouteIs('admin.sales.orders.view'); + + /* now cancel order test */ + $I->see('Cancel', Locator::href(route('admin.sales.orders.cancel', $order->id))); + $I->click('Cancel', Locator::href(route('admin.sales.orders.cancel', $order->id))); + $I->seeCurrentRouteIs('admin.sales.orders.view'); + $I->see(0.00, '#due-amount-on-cancelled'); + } + + private function goToAdminLoginPageAndSignIn(FunctionalTester $I): void + { + $I->amOnRoute('admin.session.create'); + $I->fillField('email', 'admin@example.com'); + $I->fillField('password', 'admin123'); + $I->click('Sign In'); + $I->seeCurrentRouteIs('admin.dashboard.index'); + } + + private function placeCashOnDeliveryOrderForCustomer(FunctionalTester $I): object + { + $customer = $I->loginAsCustomer(); + + $faker = Factory::create(); + + $addressData = [ + 'city' => $faker->city, + 'company_name' => $faker->company, + 'country' => $faker->countryCode, + 'email' => $faker->email, + 'first_name' => $faker->firstName, + 'last_name' => $faker->lastName, + 'phone' => $faker->phoneNumber, + 'postcode' => $faker->postcode, + 'state' => $faker->state, + ]; + + $mocks = $I->prepareCart([ + 'customer' => $customer, + ]); + + $I->amOnRoute('shop.checkout.onepage.index'); + + $I->seeCurrentRouteIs('shop.checkout.onepage.index'); + + $I->sendAjaxPostRequest(route('shop.checkout.save-address'), [ + '_token' => csrf_token(), + 'billing' => array_merge($addressData, [ + 'address1' => ['900 Nobel Parkway'], + 'save_as_address' => true, + 'use_for_shipping' => true, + ]), + 'shipping' => array_merge($addressData, [ + 'address1' => ['900 Nobel Parkway'], + 'save_as_address' => true, + 'use_for_shipping' => true, + ]), + ]); + + $I->seeResponseCodeIsSuccessful(); + + $I->seeRecord(CartAddress::class, array_merge($addressData, [ + 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING, + 'cart_id' => $mocks['cart']->id, + 'customer_id' => $mocks['customer']->id, + ])); + + $I->seeRecord(CartAddress::class, array_merge($addressData, [ + 'address_type' => CartAddress::ADDRESS_TYPE_BILLING, + 'cart_id' => $mocks['cart']->id, + 'customer_id' => $mocks['customer']->id, + ])); + + $I->sendAjaxPostRequest(route('shop.checkout.save-shipping'), [ + '_token' => csrf_token(), + 'shipping_method' => 'free_free', + ]); + + $I->seeResponseCodeIsSuccessful(); + + $I->sendAjaxPostRequest(route('shop.checkout.save-payment'), [ + '_token' => csrf_token(), + 'payment' => [ + 'method' => 'cashondelivery', + ], + ]); + + $I->seeResponseCodeIsSuccessful(); + + $I->seeRecord(CartPayment::class, [ + 'method' => 'cashondelivery', + 'method_title' => null, + 'cart_id' => $mocks['cart']->id, + ]); + + $I->sendAjaxPostRequest(route('shop.checkout.save-order'), + ['_token' => csrf_token()] + ); + + $I->seeResponseCodeIsSuccessful(); + + $order = $I->grabRecord(Order::class, [ + 'status' => 'pending', + 'channel_name' => 'Default', + 'is_guest' => 0, + 'customer_first_name' => $customer->first_name, + 'customer_last_name' => $customer->last_name, + 'customer_email' => $customer->email, + 'shipping_method' => 'free_free', + 'shipping_title' => 'Free Shipping - Free Shipping', + 'shipping_description' => 'Free Shipping', + 'customer_type' => Customer::class, + 'channel_id' => 1, + 'channel_type' => Channel::class, + 'cart_id' => $mocks['cart']->id, + 'customer_id' => $customer->id, + 'total_item_count' => count($mocks['cartItems']), + 'total_qty_ordered' => $mocks['totalQtyOrdered'], + ]); + + $I->seeRecord(OrderAddress::class, array_merge($addressData, [ + 'order_id' => $order->id, + 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING, + 'customer_id' => $mocks['customer']->id, + ])); + + $I->seeRecord(OrderAddress::class, array_merge($addressData, [ + 'order_id' => $order->id, + 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING, + 'customer_id' => $mocks['customer']->id, + ])); + + $I->seeRecord(OrderPayment::class, [ + 'method' => 'cashondelivery', + 'method_title' => null, + 'order_id' => $order->id, + ]); + + return $order; + } } From ef74d42ff6ccc5ee05cb8103eb7cc9151a7a26a4 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Wed, 11 Nov 2020 19:49:22 +0530 Subject: [PATCH 2/5] Product Number To Copy Added --- .../src/Repositories/ProductRepository.php | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index 53559b2a6..a08f7374a 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -122,8 +122,8 @@ class ProductRepository extends Repository if ($categoryId) { $qb->where('product_categories.category_id', $categoryId); - } - + } + return $qb->get(); } @@ -334,7 +334,7 @@ class ProductRepository extends Repository */ public function getNewProducts() { - $count = core()->getConfigData('catalog.products.homepage.no_of_new_product_homepage'); + $count = core()->getConfigData('catalog.products.homepage.no_of_new_product_homepage'); $results = app(ProductFlatRepository::class)->scopeQuery(function ($query) { $channel = request()->get('channel') ?: (core()->getCurrentChannelCode() ?: core()->getDefaultChannelCode()); @@ -614,7 +614,7 @@ class ProductRepository extends Repository { $ids = []; - foreach (['name', 'sku', 'status', 'url_key'] as $code) { + foreach (['name', 'sku', 'product_number', 'status', 'url_key'] as $code) { $ids[$code] = Attribute::query()->where(['code' => $code])->firstOrFail()->id; } @@ -675,6 +675,18 @@ class ProductRepository extends Repository $newProductFlat->sku = $copiedProduct->sku; } + // change product number + if ($oldValue->attribute_id === $attributeIds['product_number']) { + $copyProductNumber = trans('admin::app.copy-of-slug'); + $copiedProductNumber = sprintf('%s%s-%s', + Str::startsWith($originalProduct->product_number, $copyProductNumber) ? '' : $copyProductNumber, + $originalProduct->product_number, + $randomSuffix + ); + $newValue->text_value = $copiedProductNumber; + $newProductFlat->name = $copiedProductNumber; + } + // force the copied product to be inactive so the admin can adjust it before release if ($oldValue->attribute_id === $attributeIds['status']) { $newValue->boolean_value = 0; From 70f9e1fd575a58792681ab7592f198526e1c09be Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Wed, 11 Nov 2020 19:53:07 +0530 Subject: [PATCH 3/5] Small Mistake Fixed --- packages/Webkul/Product/src/Repositories/ProductRepository.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/Webkul/Product/src/Repositories/ProductRepository.php b/packages/Webkul/Product/src/Repositories/ProductRepository.php index a08f7374a..01ff89d49 100755 --- a/packages/Webkul/Product/src/Repositories/ProductRepository.php +++ b/packages/Webkul/Product/src/Repositories/ProductRepository.php @@ -684,7 +684,7 @@ class ProductRepository extends Repository $randomSuffix ); $newValue->text_value = $copiedProductNumber; - $newProductFlat->name = $copiedProductNumber; + $newProductFlat->product_number = $copiedProductNumber; } // force the copied product to be inactive so the admin can adjust it before release From 7af392e210e59d3e4f47571584b2a1e19afb2923 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Wed, 11 Nov 2020 22:59:50 +0530 Subject: [PATCH 4/5] Test Case Updated For Null Check --- .../functional/Admin/Catalog/ProductCest.php | 57 ++++++++++++------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/tests/functional/Admin/Catalog/ProductCest.php b/tests/functional/Admin/Catalog/ProductCest.php index ef49cb1ce..3e52fbc1e 100644 --- a/tests/functional/Admin/Catalog/ProductCest.php +++ b/tests/functional/Admin/Catalog/ProductCest.php @@ -2,26 +2,44 @@ namespace Tests\Functional\Admin\Catalog; -use FunctionalTester; use Faker\Factory; -use Webkul\Attribute\Models\Attribute; -use Webkul\Attribute\Models\AttributeFamily; -use Webkul\Attribute\Models\AttributeOption; -use Webkul\Core\Helpers\Laravel5Helper; +use FunctionalTester; use Webkul\Core\Models\Locale; use Webkul\Product\Models\Product; +use Webkul\Attribute\Models\Attribute; +use Webkul\Core\Helpers\Laravel5Helper; +use Webkul\Attribute\Models\AttributeFamily; +use Webkul\Attribute\Models\AttributeOption; use Webkul\Product\Models\ProductAttributeValue; class ProductCest { - /** @var Factory $faker */ + /** + * Factory $faker + * + * @var Faker\Factory + */ private $faker; - /** @var Attribute $attributeBrand */ + /** + * Attribute $attributeBrand + * + * @var Webkul\Attribute\Models\Attribute + */ private $attributeBrand; - /** @var AttributeOption $attributeBrandDefaultOption */ + + /** + * AttributeOption $attributeBrandDefaultOption + * + * @var Webkul\Attribute\Models\AttributeOption + */ private $attributeBrandDefaultOption; - /** @var AttributeOption $attributeBrandOption */ + + /** + * AttributeOption $attributeBrandOption + * + * @var Webkul\Attribute\Models\AttributeOption + */ private $attributeBrandOption; public function _before(FunctionalTester $I): void @@ -47,12 +65,10 @@ class ProductCest ]; } - $this->attributeBrandDefaultOption = $I->have(AttributeOption::class, - $defaultAttributeOptionAttributes); + $this->attributeBrandDefaultOption = $I->have(AttributeOption::class, $defaultAttributeOptionAttributes); $this->attributeBrandOption = $I->have(AttributeOption::class, [ 'attribute_id' => $this->attributeBrand->id, ]); - } public function testIndex(FunctionalTester $I): void @@ -90,9 +106,7 @@ class ProductCest $I->fillField('sku', $sku); $I->click(__('admin::app.catalog.products.save-btn-title')); - $I->seeInSource('Product created successfully.'); - $I->seeCurrentRouteIs('admin.catalog.products.edit'); $productTitle = $this->faker->word; @@ -100,11 +114,9 @@ class ProductCest $I->fillField('name', $productTitle); $I->fillField('url_key', $productUrlKey); - $I->selectOption($this->attributeBrand->code, - $this->attributeBrandDefaultOption->id); + $I->selectOption($this->attributeBrand->code, $this->attributeBrandDefaultOption->id); $I->fillField('price', $this->faker->randomFloat(2)); $I->fillField('weight', $this->faker->randomDigit); - $I->fillField('short_description', $this->faker->paragraph(1, true)); $I->fillField('description', $this->faker->paragraph(5, true)); @@ -119,11 +131,18 @@ class ProductCest 'attribute_family_id' => $attributeFamily->id, ]); + $productAttribute = $I->grabRecord(ProductAttributeValue::class, [ + 'product_id' => $product->id, + 'attribute_id' => $this->attributeBrand->id, + 'integer_value' => $this->attributeBrandDefaultOption->id + ]); + $I->seeRecord(ProductAttributeValue::class, [ 'product_id' => $product->id, 'attribute_id' => $this->attributeBrand->id, - 'integer_value' => $this->attributeBrandDefaultOption->id, - 'text_value' => null, + 'integer_value' => $this->attributeBrandDefaultOption->id ]); + + $I->assertNull($productAttribute->text_value); } } From 5fc2e6c091046dca7fd80f3e05e3ce340037f932 Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Fri, 13 Nov 2020 00:47:39 +0530 Subject: [PATCH 5/5] Completed Cancel Cash On Delivery Test Cases --- tests/functional/Admin/Sales/OrderCest.php | 170 ++++++--------------- 1 file changed, 47 insertions(+), 123 deletions(-) diff --git a/tests/functional/Admin/Sales/OrderCest.php b/tests/functional/Admin/Sales/OrderCest.php index 2af683051..4cf63935c 100644 --- a/tests/functional/Admin/Sales/OrderCest.php +++ b/tests/functional/Admin/Sales/OrderCest.php @@ -6,25 +6,28 @@ use Faker\Factory; use FunctionalTester; use Codeception\Util\Locator; use Webkul\Sales\Models\Order; -use Webkul\Core\Models\Channel; -use Webkul\Customer\Models\Customer; +use Webkul\Sales\Models\OrderItem; use Webkul\Sales\Models\OrderAddress; use Webkul\Sales\Models\OrderPayment; -use Webkul\Checkout\Models\CartAddress; -use Webkul\Checkout\Models\CartPayment; +use Webkul\Core\Helpers\Laravel5Helper; class OrderCest { public function testIndex(FunctionalTester $I): void { + /* simple order no further association like address, shipping method, payment method, etc. */ $order = $I->have(Order::class); + /* login as admin */ $I->loginAsAdmin(); + + /* go to order view page */ $I->amOnAdminRoute('admin.dashboard.index'); $I->click(__('admin::app.layouts.sales'), '//*[contains(@class, "navbar-left")]'); $I->seeCurrentRouteIs('admin.sales.orders.index'); $I->click(__('admin::app.layouts.orders'), '//*[contains(@class, "aside-nav")]'); + /* now test index page */ $I->seeCurrentRouteIs('admin.sales.orders.index'); $I->see($order->id, '//script[@type="text/x-template"]'); $I->see($order->sub_total, '//script[@type="text/x-template"]'); @@ -32,39 +35,63 @@ class OrderCest public function testCancelCashOnDeliveryOrder(FunctionalTester $I): void { - /* place order for customer */ - $order = $this->placeCashOnDeliveryOrderForCustomer($I); + /* generate cash on delivery order */ + $order = $this->generateCashOnDeliveryOrder($I); - /* go to admin page and login */ - $this->goToAdminLoginPageAndSignIn($I); + /* login as admin */ + $I->loginAsAdmin(); /* go to order view page */ $I->amOnPage(route('admin.sales.orders.view', $order->id)); $I->seeCurrentRouteIs('admin.sales.orders.view'); - /* now cancel order test */ + /* now test cancel order */ $I->see('Cancel', Locator::href(route('admin.sales.orders.cancel', $order->id))); $I->click('Cancel', Locator::href(route('admin.sales.orders.cancel', $order->id))); $I->seeCurrentRouteIs('admin.sales.orders.view'); $I->see(0.00, '#due-amount-on-cancelled'); } - private function goToAdminLoginPageAndSignIn(FunctionalTester $I): void + private function generateCashOnDeliveryOrder(FunctionalTester $I) { - $I->amOnRoute('admin.session.create'); - $I->fillField('email', 'admin@example.com'); - $I->fillField('password', 'admin123'); - $I->click('Sign In'); - $I->seeCurrentRouteIs('admin.dashboard.index'); + $product = $I->haveProduct(Laravel5Helper::SIMPLE_PRODUCT, [ + 'productAttributes' => [], + 'productInventory' => [ + 'qty' => 5, + ], + 'attributeValues' => [ + 'status' => 1, + ], + ]); + + $order = $I->have(OrderItem::class, ['product_id' => $product->id])->order; + + $I->have(OrderAddress::class, $this->generateAddressData([ + 'order_id' => $order->id, + 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING, + 'customer_id' => $order->customer->id, + ])); + + $I->have(OrderAddress::class, $this->generateAddressData([ + 'order_id' => $order->id, + 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING, + 'customer_id' => $order->customer->id, + ])); + + $I->have(OrderPayment::class, [ + 'method' => 'cashondelivery', + 'method_title' => null, + 'order_id' => $order->id, + ]); + + return $order; } - private function placeCashOnDeliveryOrderForCustomer(FunctionalTester $I): object + private function generateAddressData(array $additionalData): array { - $customer = $I->loginAsCustomer(); - $faker = Factory::create(); - $addressData = [ + return array_merge([ 'city' => $faker->city, 'company_name' => $faker->company, 'country' => $faker->countryCode, @@ -74,109 +101,6 @@ class OrderCest 'phone' => $faker->phoneNumber, 'postcode' => $faker->postcode, 'state' => $faker->state, - ]; - - $mocks = $I->prepareCart([ - 'customer' => $customer, - ]); - - $I->amOnRoute('shop.checkout.onepage.index'); - - $I->seeCurrentRouteIs('shop.checkout.onepage.index'); - - $I->sendAjaxPostRequest(route('shop.checkout.save-address'), [ - '_token' => csrf_token(), - 'billing' => array_merge($addressData, [ - 'address1' => ['900 Nobel Parkway'], - 'save_as_address' => true, - 'use_for_shipping' => true, - ]), - 'shipping' => array_merge($addressData, [ - 'address1' => ['900 Nobel Parkway'], - 'save_as_address' => true, - 'use_for_shipping' => true, - ]), - ]); - - $I->seeResponseCodeIsSuccessful(); - - $I->seeRecord(CartAddress::class, array_merge($addressData, [ - 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING, - 'cart_id' => $mocks['cart']->id, - 'customer_id' => $mocks['customer']->id, - ])); - - $I->seeRecord(CartAddress::class, array_merge($addressData, [ - 'address_type' => CartAddress::ADDRESS_TYPE_BILLING, - 'cart_id' => $mocks['cart']->id, - 'customer_id' => $mocks['customer']->id, - ])); - - $I->sendAjaxPostRequest(route('shop.checkout.save-shipping'), [ - '_token' => csrf_token(), - 'shipping_method' => 'free_free', - ]); - - $I->seeResponseCodeIsSuccessful(); - - $I->sendAjaxPostRequest(route('shop.checkout.save-payment'), [ - '_token' => csrf_token(), - 'payment' => [ - 'method' => 'cashondelivery', - ], - ]); - - $I->seeResponseCodeIsSuccessful(); - - $I->seeRecord(CartPayment::class, [ - 'method' => 'cashondelivery', - 'method_title' => null, - 'cart_id' => $mocks['cart']->id, - ]); - - $I->sendAjaxPostRequest(route('shop.checkout.save-order'), - ['_token' => csrf_token()] - ); - - $I->seeResponseCodeIsSuccessful(); - - $order = $I->grabRecord(Order::class, [ - 'status' => 'pending', - 'channel_name' => 'Default', - 'is_guest' => 0, - 'customer_first_name' => $customer->first_name, - 'customer_last_name' => $customer->last_name, - 'customer_email' => $customer->email, - 'shipping_method' => 'free_free', - 'shipping_title' => 'Free Shipping - Free Shipping', - 'shipping_description' => 'Free Shipping', - 'customer_type' => Customer::class, - 'channel_id' => 1, - 'channel_type' => Channel::class, - 'cart_id' => $mocks['cart']->id, - 'customer_id' => $customer->id, - 'total_item_count' => count($mocks['cartItems']), - 'total_qty_ordered' => $mocks['totalQtyOrdered'], - ]); - - $I->seeRecord(OrderAddress::class, array_merge($addressData, [ - 'order_id' => $order->id, - 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING, - 'customer_id' => $mocks['customer']->id, - ])); - - $I->seeRecord(OrderAddress::class, array_merge($addressData, [ - 'order_id' => $order->id, - 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING, - 'customer_id' => $mocks['customer']->id, - ])); - - $I->seeRecord(OrderPayment::class, [ - 'method' => 'cashondelivery', - 'method_title' => null, - 'order_id' => $order->id, - ]); - - return $order; + ], $additionalData); } }