OrdersTest.php 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  1. <?php
  2. use Illuminate\Support\Arr;
  3. use Webkul\Checkout\Models\Cart;
  4. use Webkul\Checkout\Models\CartAddress;
  5. use Webkul\Checkout\Models\CartItem;
  6. use Webkul\Checkout\Models\CartPayment;
  7. use Webkul\Checkout\Models\CartShippingRate;
  8. use Webkul\Customer\Models\CompareItem;
  9. use Webkul\Customer\Models\Customer;
  10. use Webkul\Customer\Models\CustomerAddress;
  11. use Webkul\Customer\Models\Wishlist;
  12. use Webkul\Faker\Helpers\Customer as CustomerFaker;
  13. use Webkul\Faker\Helpers\Product as ProductFaker;
  14. use Webkul\Sales\Models\Order;
  15. use Webkul\Sales\Models\OrderAddress;
  16. use Webkul\Sales\Models\OrderItem;
  17. use Webkul\Sales\Models\OrderPayment;
  18. use function Pest\Laravel\deleteJson;
  19. use function Pest\Laravel\getJson;
  20. use function Pest\Laravel\postJson;
  21. use function Pest\Laravel\putJson;
  22. it('should search the customers via email or name', function () {
  23. // Arrange.
  24. $customer = (new CustomerFaker)->factory()->create();
  25. // Act and Assert.
  26. $this->loginAsAdmin();
  27. getJson(route('admin.customers.customers.search'), [
  28. 'query' => fake()->randomElement([$customer->name, $customer->email]),
  29. ])
  30. ->assertOk()
  31. ->assertJsonPath('data.0.id', $customer->id)
  32. ->assertJsonPath('data.0.first_name', $customer->first_name)
  33. ->assertJsonPath('data.0.last_name', $customer->last_name)
  34. ->assertJsonPath('data.0.gender', $customer->gender)
  35. ->assertJsonPath('data.0.status', $customer->status)
  36. ->assertJsonPath('data.0.customer_group_id', $customer->customer_group_id)
  37. ->assertJsonPath('data.0.email', $customer->email);
  38. });
  39. it('should create the customer if none exists when creating an order', function () {
  40. // Arrange.
  41. $customer = (new CustomerFaker)->factory()->create();
  42. $cart = Cart::factory()->create([
  43. 'customer_id' => $customer->id,
  44. 'customer_first_name' => $customer->first_name,
  45. 'customer_last_name' => $customer->last_name,
  46. 'customer_email' => $customer->email,
  47. 'is_guest' => 0,
  48. 'is_active' => 0,
  49. 'items_count' => null,
  50. ]);
  51. // Act and Assert.
  52. $this->loginAsAdmin();
  53. getJson(route('admin.sales.orders.create', $cart->id))
  54. ->assertOk()
  55. ->assertSeeText(trans('admin::app.sales.orders.create.title', ['name' => $cart->customer->name]))
  56. ->assertSeeText(trans('admin::app.sales.orders.create.cart.items.add-product'))
  57. ->assertSeeText(trans('admin::app.sales.orders.create.wishlist-items.title'))
  58. ->assertSeeText(trans('admin::app.sales.orders.create.compare-items.title'))
  59. ->assertSeeText(trans('admin::app.sales.orders.create.recent-order-items.title'))
  60. ->assertSeeText(trans('admin::app.sales.orders.create.cart.items.title'));
  61. });
  62. it('should search the products via name for adding products to the cart', function () {
  63. // Arrange.
  64. $product = (new ProductFaker([
  65. 'attributes' => [
  66. 5 => 'new',
  67. ],
  68. 'attribute_value' => [
  69. 'new' => [
  70. 'boolean_value' => true,
  71. ],
  72. ],
  73. ]))
  74. ->getSimpleProductFactory()
  75. ->create();
  76. // Act and Assert.
  77. $this->loginAsAdmin();
  78. getJson(route('admin.catalog.products.search', [
  79. 'query' => $product->name,
  80. ]))
  81. ->assertOk()
  82. ->assertJsonPath('data.0.id', $product->id)
  83. ->assertJsonPath('data.0.name', $product->name)
  84. ->assertJsonPath('data.0.type', $product->type)
  85. ->assertJsonPath('data.0.sku', $product->sku);
  86. });
  87. it('should add product to the cart after search the product', function () {
  88. // Arrange.
  89. $product = (new ProductFaker([
  90. 'attributes' => [
  91. 5 => 'new',
  92. ],
  93. 'attribute_value' => [
  94. 'new' => [
  95. 'boolean_value' => true,
  96. ],
  97. ],
  98. ]))
  99. ->getSimpleProductFactory()
  100. ->create();
  101. $customer = (new CustomerFaker)->factory()->create();
  102. $cart = Cart::factory()->create([
  103. 'customer_id' => $customer->id,
  104. 'customer_first_name' => $customer->first_name,
  105. 'customer_last_name' => $customer->last_name,
  106. 'customer_email' => $customer->email,
  107. 'is_guest' => 0,
  108. 'is_active' => 0,
  109. 'items_count' => null,
  110. ]);
  111. // Act and Assert.
  112. $this->loginAsAdmin();
  113. $response = postJson(route('admin.sales.cart.items.store', $cart->id), $data = [
  114. 'product_id' => $product->id,
  115. 'quantity' => rand(1, 10),
  116. ])
  117. ->assertOk()
  118. ->assertJsonPath('data.id', $cart->id)
  119. ->assertJsonPath('data.is_guest', $cart->is_guest)
  120. ->assertJsonPath('data.customer_id', $cart->customer_id)
  121. ->assertJsonPath('data.items_count', 1)
  122. ->assertJsonPath('data.items_qty', $data['quantity']);
  123. $this->assertPrice($price = $product->price * $data['quantity'], $response->json('data.grand_total'));
  124. $this->assertPrice($price, $response->json('data.sub_total'));
  125. $this->assertModelWise([
  126. CartItem::class => [
  127. [
  128. 'cart_id' => $cart->id,
  129. 'product_id' => $data['product_id'],
  130. 'sku' => $product->sku,
  131. 'type' => $product->type,
  132. 'name' => $product->name,
  133. 'price' => $product->price,
  134. 'total' => $price,
  135. ],
  136. ],
  137. ]);
  138. });
  139. it('should update the cart item after add product to the cart', function () {
  140. // Arrange.
  141. $product = (new ProductFaker([
  142. 'attributes' => [
  143. 5 => 'new',
  144. ],
  145. 'attribute_value' => [
  146. 'new' => [
  147. 'boolean_value' => true,
  148. ],
  149. ],
  150. ]))
  151. ->getSimpleProductFactory()
  152. ->create();
  153. $customer = (new CustomerFaker)->factory()->create();
  154. $cart = Cart::factory()->create([
  155. 'customer_id' => $customer->id,
  156. 'customer_first_name' => $customer->first_name,
  157. 'customer_last_name' => $customer->last_name,
  158. 'customer_email' => $customer->email,
  159. 'is_guest' => 0,
  160. 'is_active' => 0,
  161. 'items_count' => null,
  162. ]);
  163. $additional = [
  164. 'product_id' => $product->id,
  165. 'rating' => '0',
  166. 'is_buy_now' => '0',
  167. 'quantity' => '1',
  168. ];
  169. $cartItem = CartItem::factory()->create([
  170. 'cart_id' => $cart->id,
  171. 'product_id' => $product->id,
  172. 'sku' => $product->sku,
  173. 'quantity' => $additional['quantity'],
  174. 'name' => $product->name,
  175. 'price' => $convertedPrice = core()->convertPrice($product->price),
  176. 'base_price' => $product->price,
  177. 'total' => $convertedPrice * $additional['quantity'],
  178. 'base_total' => ($product->price) * $additional['quantity'],
  179. 'weight' => $product->weight ?? 0,
  180. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  181. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  182. 'type' => $product->type,
  183. 'additional' => $additional,
  184. ]);
  185. // Act and Assert.
  186. $this->loginAsAdmin();
  187. putJson(route('admin.sales.cart.items.update', $cart->id), [
  188. 'qty' => [
  189. $cartItem->id => $qty = rand(2, 10),
  190. ],
  191. ])
  192. ->assertOk()
  193. ->assertSeeText(trans('admin::app.sales.orders.create.cart.success-update'));
  194. $cart->refresh();
  195. $cartItem->refresh();
  196. $this->assertPrice($product->price * $qty, $cart->grand_total);
  197. $this->assertPrice($product->price * $qty, $cart->sub_total);
  198. $this->assertModelWise([
  199. Cart::class => [
  200. [
  201. 'customer_id' => $customer->id,
  202. 'items_count' => 1,
  203. 'items_qty' => number_format($qty, 4),
  204. ],
  205. ],
  206. CartItem::class => [
  207. [
  208. 'id' => $cartItem->id,
  209. 'cart_id' => $cart->id,
  210. 'product_id' => $product->id,
  211. 'sku' => $product->sku,
  212. 'type' => $product->type,
  213. 'name' => $product->name,
  214. 'price' => $product->price,
  215. 'total' => $product->price * $qty,
  216. ],
  217. ],
  218. ]);
  219. });
  220. it('should fails the validation error if billing and shipping address is not provided', function () {
  221. // Arrange.
  222. $product = (new ProductFaker([
  223. 'attributes' => [
  224. 5 => 'new',
  225. ],
  226. 'attribute_value' => [
  227. 'new' => [
  228. 'boolean_value' => true,
  229. ],
  230. ],
  231. ]))
  232. ->getSimpleProductFactory()
  233. ->create();
  234. $customer = (new CustomerFaker)->factory()->create();
  235. $cart = Cart::factory()->create([
  236. 'customer_id' => $customer->id,
  237. 'customer_first_name' => $customer->first_name,
  238. 'customer_last_name' => $customer->last_name,
  239. 'customer_email' => $customer->email,
  240. 'is_guest' => 0,
  241. 'is_active' => 0,
  242. 'items_count' => null,
  243. ]);
  244. $additional = [
  245. 'product_id' => $product->id,
  246. 'rating' => '0',
  247. 'is_buy_now' => '0',
  248. 'quantity' => '1',
  249. ];
  250. CartItem::factory()->create([
  251. 'cart_id' => $cart->id,
  252. 'product_id' => $product->id,
  253. 'sku' => $product->sku,
  254. 'quantity' => $additional['quantity'],
  255. 'name' => $product->name,
  256. 'price' => $convertedPrice = core()->convertPrice($product->price),
  257. 'base_price' => $product->price,
  258. 'total' => $convertedPrice * $additional['quantity'],
  259. 'base_total' => ($product->price) * $additional['quantity'],
  260. 'weight' => $product->weight ?? 0,
  261. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  262. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  263. 'type' => $product->type,
  264. 'additional' => $additional,
  265. ]);
  266. cart()->setCart($cart);
  267. cart()->collectTotals();
  268. // Act and Assert.
  269. $this->loginAsAdmin();
  270. postJson(route('admin.sales.cart.addresses.store', $cart->id), [
  271. 'billing' => [],
  272. 'shipping' => [],
  273. ])
  274. ->assertJsonValidationErrorFor('billing.first_name')
  275. ->assertJsonValidationErrorFor('billing.last_name')
  276. ->assertJsonValidationErrorFor('billing.email')
  277. ->assertJsonValidationErrorFor('billing.address')
  278. ->assertJsonValidationErrorFor('billing.city')
  279. ->assertJsonValidationErrorFor('billing.phone')
  280. ->assertJsonValidationErrorFor('shipping.first_name')
  281. ->assertJsonValidationErrorFor('shipping.last_name')
  282. ->assertJsonValidationErrorFor('shipping.email')
  283. ->assertJsonValidationErrorFor('shipping.address')
  284. ->assertJsonValidationErrorFor('shipping.city')
  285. ->assertJsonValidationErrorFor('shipping.phone')
  286. ->assertUnprocessable();
  287. });
  288. it('should add billing address after add item to the cart', function () {
  289. // Arrange.
  290. $product = (new ProductFaker([
  291. 'attributes' => [
  292. 5 => 'new',
  293. ],
  294. 'attribute_value' => [
  295. 'new' => [
  296. 'boolean_value' => true,
  297. ],
  298. ],
  299. ]))
  300. ->getSimpleProductFactory()
  301. ->create();
  302. $customer = (new CustomerFaker)->factory()->create();
  303. $cart = Cart::factory()->create([
  304. 'customer_id' => $customer->id,
  305. 'customer_first_name' => $customer->first_name,
  306. 'customer_last_name' => $customer->last_name,
  307. 'customer_email' => $customer->email,
  308. 'is_guest' => 0,
  309. 'is_active' => 0,
  310. 'items_count' => null,
  311. ]);
  312. $additional = [
  313. 'product_id' => $product->id,
  314. 'rating' => '0',
  315. 'is_buy_now' => '0',
  316. 'quantity' => '1',
  317. ];
  318. CartItem::factory()->create([
  319. 'cart_id' => $cart->id,
  320. 'product_id' => $product->id,
  321. 'sku' => $product->sku,
  322. 'quantity' => $additional['quantity'],
  323. 'name' => $product->name,
  324. 'price' => $convertedPrice = core()->convertPrice($product->price),
  325. 'base_price' => $product->price,
  326. 'total' => $convertedPrice * $additional['quantity'],
  327. 'base_total' => ($product->price) * $additional['quantity'],
  328. 'weight' => $product->weight ?? 0,
  329. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  330. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  331. 'type' => $product->type,
  332. 'additional' => $additional,
  333. ]);
  334. $customerAddress = CustomerAddress::factory()->create()->toArray();
  335. cart()->setCart($cart);
  336. cart()->collectTotals();
  337. // Act and Assert.
  338. $this->loginAsAdmin();
  339. $response = postJson(route('admin.sales.cart.addresses.store', $cart->id), [
  340. 'billing' => $billingAddress = [
  341. ...$customerAddress,
  342. 'use_for_shipping' => 1,
  343. 'address' => [fake()->address()],
  344. ],
  345. ])
  346. ->assertOk()
  347. ->assertJsonPath('redirect', false)
  348. ->assertJsonPath('data.shippingMethods.flatrate.carrier_title', 'Flat Rate')
  349. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.carrier', 'flatrate')
  350. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.carrier_title', 'Flat Rate')
  351. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.method', 'flatrate_flatrate')
  352. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.method_title', 'Flat Rate')
  353. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.price', 10)
  354. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.base_price', 10)
  355. ->assertJsonPath('data.shippingMethods.free.carrier_title', 'Free Shipping')
  356. ->assertJsonPath('data.shippingMethods.free.rates.0.carrier', 'free')
  357. ->assertJsonPath('data.shippingMethods.free.rates.0.carrier_title', 'Free Shipping')
  358. ->assertJsonPath('data.shippingMethods.free.carrier_title', 'Free Shipping')
  359. ->assertJsonPath('data.shippingMethods.free.rates.0.method', 'free_free')
  360. ->assertJsonPath('data.shippingMethods.free.rates.0.method_title', 'Free Shipping')
  361. ->assertJsonPath('data.shippingMethods.free.rates.0.price', 0)
  362. ->assertJsonPath('data.shippingMethods.free.rates.0.base_price', 0);
  363. $response->assertJsonPath('data.shippingMethods.flatrate.rates.0.cart_address_id', $cart->shipping_address->id);
  364. $response->assertJsonPath('data.shippingMethods.free.rates.0.cart_address_id', $cart->shipping_address->id);
  365. $this->assertModelWise([
  366. CartAddress::class => [
  367. [
  368. 'address' => implode("\n", $billingAddress['address']),
  369. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  370. 'cart_id' => $cart->id,
  371. 'use_for_shipping' => $billingAddress['use_for_shipping'],
  372. ...Arr::only($billingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  373. ],
  374. ],
  375. CartAddress::class => [
  376. [
  377. 'address' => implode("\n", $billingAddress['address']),
  378. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  379. 'cart_id' => $cart->id,
  380. ...Arr::only($billingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  381. ],
  382. ],
  383. ]);
  384. });
  385. it('should add billing and shipping address after add item to the cart', function () {
  386. // Arrange.
  387. $product = (new ProductFaker([
  388. 'attributes' => [
  389. 5 => 'new',
  390. ],
  391. 'attribute_value' => [
  392. 'new' => [
  393. 'boolean_value' => true,
  394. ],
  395. ],
  396. ]))
  397. ->getSimpleProductFactory()
  398. ->create();
  399. $customer = (new CustomerFaker)->factory()->create();
  400. $cart = Cart::factory()->create([
  401. 'customer_id' => $customer->id,
  402. 'customer_first_name' => $customer->first_name,
  403. 'customer_last_name' => $customer->last_name,
  404. 'customer_email' => $customer->email,
  405. 'is_guest' => 0,
  406. 'is_active' => 0,
  407. 'items_count' => null,
  408. ]);
  409. $additional = [
  410. 'product_id' => $product->id,
  411. 'rating' => '0',
  412. 'is_buy_now' => '0',
  413. 'quantity' => '1',
  414. ];
  415. CartItem::factory()->create([
  416. 'cart_id' => $cart->id,
  417. 'product_id' => $product->id,
  418. 'sku' => $product->sku,
  419. 'quantity' => $additional['quantity'],
  420. 'name' => $product->name,
  421. 'price' => $convertedPrice = core()->convertPrice($product->price),
  422. 'base_price' => $product->price,
  423. 'total' => $convertedPrice * $additional['quantity'],
  424. 'base_total' => ($product->price) * $additional['quantity'],
  425. 'weight' => $product->weight ?? 0,
  426. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  427. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  428. 'type' => $product->type,
  429. 'additional' => $additional,
  430. ]);
  431. $customerAddress = CustomerAddress::factory()->create()->toArray();
  432. cart()->setCart($cart);
  433. cart()->collectTotals();
  434. // Act and Assert.
  435. $this->loginAsAdmin();
  436. $response = postJson(route('admin.sales.cart.addresses.store', $cart->id), [
  437. 'billing' => $billingAddress = [
  438. ...$customerAddress,
  439. 'use_for_shipping' => 0,
  440. 'address' => [fake()->address()],
  441. ],
  442. 'shipping' => $shippingAddress = [
  443. ...$customerAddress,
  444. 'address' => [fake()->address()],
  445. ],
  446. ])
  447. ->assertOk()
  448. ->assertJsonPath('redirect', false)
  449. ->assertJsonPath('data.shippingMethods.flatrate.carrier_title', 'Flat Rate')
  450. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.carrier', 'flatrate')
  451. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.carrier_title', 'Flat Rate')
  452. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.method', 'flatrate_flatrate')
  453. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.method_title', 'Flat Rate')
  454. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.price', 10)
  455. ->assertJsonPath('data.shippingMethods.flatrate.rates.0.base_price', 10)
  456. ->assertJsonPath('data.shippingMethods.free.carrier_title', 'Free Shipping')
  457. ->assertJsonPath('data.shippingMethods.free.rates.0.carrier', 'free')
  458. ->assertJsonPath('data.shippingMethods.free.rates.0.carrier_title', 'Free Shipping')
  459. ->assertJsonPath('data.shippingMethods.free.carrier_title', 'Free Shipping')
  460. ->assertJsonPath('data.shippingMethods.free.rates.0.method', 'free_free')
  461. ->assertJsonPath('data.shippingMethods.free.rates.0.method_title', 'Free Shipping')
  462. ->assertJsonPath('data.shippingMethods.free.rates.0.price', 0)
  463. ->assertJsonPath('data.shippingMethods.free.rates.0.base_price', 0);
  464. $response->assertJsonPath('data.shippingMethods.flatrate.rates.0.cart_address_id', $cart->shipping_address->id);
  465. $response->assertJsonPath('data.shippingMethods.free.rates.0.cart_address_id', $cart->shipping_address->id);
  466. $this->assertModelWise([
  467. CartAddress::class => [
  468. [
  469. 'address' => implode("\n", $billingAddress['address']),
  470. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  471. 'cart_id' => $cart->id,
  472. 'use_for_shipping' => $billingAddress['use_for_shipping'],
  473. ...Arr::only($billingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  474. ],
  475. ],
  476. CartAddress::class => [
  477. [
  478. 'address' => implode("\n", $shippingAddress['address']),
  479. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  480. 'cart_id' => $cart->id,
  481. ...Arr::only($shippingAddress, ['first_name', 'last_name', 'company_name', 'city', 'state', 'country', 'email', 'postcode', 'phone']),
  482. ],
  483. ],
  484. ]);
  485. });
  486. it('should the shipping rates after storing address', function () {
  487. // Arrange.
  488. $product = (new ProductFaker([
  489. 'attributes' => [
  490. 5 => 'new',
  491. 26 => 'guest_checkout',
  492. ],
  493. 'attribute_value' => [
  494. 'new' => [
  495. 'boolean_value' => true,
  496. ],
  497. 'guest_checkout' => [
  498. 'boolean_value' => true,
  499. ],
  500. ],
  501. ]))
  502. ->getSimpleProductFactory()
  503. ->create();
  504. $customer = (new CustomerFaker)->factory()->create();
  505. $cart = Cart::factory()->create([
  506. 'customer_id' => $customer->id,
  507. 'customer_first_name' => $customer->first_name,
  508. 'customer_last_name' => $customer->last_name,
  509. 'customer_email' => $customer->email,
  510. 'is_guest' => 0,
  511. 'is_active' => 0,
  512. 'items_count' => null,
  513. ]);
  514. $additional = [
  515. 'product_id' => $product->id,
  516. 'rating' => '0',
  517. 'is_buy_now' => '0',
  518. 'quantity' => '1',
  519. ];
  520. CartItem::factory()->create([
  521. 'cart_id' => $cart->id,
  522. 'product_id' => $product->id,
  523. 'sku' => $product->sku,
  524. 'quantity' => $additional['quantity'],
  525. 'name' => $product->name,
  526. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  527. 'base_price' => $price,
  528. 'total' => $convertedPrice * $additional['quantity'],
  529. 'base_total' => $price * $additional['quantity'],
  530. 'weight' => $product->weight ?? 0,
  531. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  532. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  533. 'type' => $product->type,
  534. 'additional' => $additional,
  535. ]);
  536. CartAddress::factory()->create([
  537. 'cart_id' => $cart->id,
  538. 'customer_Id' => $customer->id,
  539. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  540. ]);
  541. CartAddress::factory()->create([
  542. 'cart_id' => $cart->id,
  543. 'customer_Id' => $customer->id,
  544. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  545. ]);
  546. cart()->setCart($cart);
  547. // Act and Assert.
  548. $this->loginAsAdmin();
  549. postJson(route('admin.sales.cart.shipping_methods.store', $cart->id), [
  550. 'shipping_method' => 'free_free',
  551. ])
  552. ->assertOk()
  553. ->assertJsonPath('payment_methods.0.method', 'cashondelivery')
  554. ->assertJsonPath('payment_methods.0.method_title', 'Cash On Delivery')
  555. ->assertJsonPath('payment_methods.0.description', 'Cash On Delivery')
  556. ->assertJsonPath('payment_methods.0.sort', 1)
  557. ->assertJsonPath('payment_methods.1.method', 'moneytransfer')
  558. ->assertJsonPath('payment_methods.1.method_title', 'Money Transfer')
  559. ->assertJsonPath('payment_methods.1.description', 'Money Transfer')
  560. ->assertJsonPath('payment_methods.1.sort', 2)
  561. ->assertJsonPath('payment_methods.2.method', 'paypal_standard')
  562. ->assertJsonPath('payment_methods.2.method_title', 'PayPal Standard')
  563. ->assertJsonPath('payment_methods.2.description', 'PayPal Standard')
  564. ->assertJsonPath('payment_methods.2.sort', 3)
  565. ->assertJsonPath('payment_methods.3.method', 'paypal_smart_button')
  566. ->assertJsonPath('payment_methods.3.method_title', 'PayPal Smart Button')
  567. ->assertJsonPath('payment_methods.3.description', 'PayPal')
  568. ->assertJsonPath('payment_methods.3.sort', 4);
  569. });
  570. it('should store the payment method after storing the shipping method', function () {
  571. // Arrange.
  572. $product = (new ProductFaker([
  573. 'attributes' => [
  574. 5 => 'new',
  575. 26 => 'guest_checkout',
  576. ],
  577. 'attribute_value' => [
  578. 'new' => [
  579. 'boolean_value' => true,
  580. ],
  581. 'guest_checkout' => [
  582. 'boolean_value' => true,
  583. ],
  584. ],
  585. ]))
  586. ->getSimpleProductFactory()
  587. ->create();
  588. $customer = (new CustomerFaker)->factory()->create();
  589. $cart = Cart::factory()->create([
  590. 'customer_id' => $customer->id,
  591. 'customer_first_name' => $customer->first_name,
  592. 'customer_last_name' => $customer->last_name,
  593. 'customer_email' => $customer->email,
  594. 'is_guest' => 0,
  595. 'is_active' => 0,
  596. 'items_count' => null,
  597. ]);
  598. $additional = [
  599. 'product_id' => $product->id,
  600. 'rating' => '0',
  601. 'is_buy_now' => '0',
  602. 'quantity' => '1',
  603. ];
  604. $cartItem = CartItem::factory()->create([
  605. 'cart_id' => $cart->id,
  606. 'product_id' => $product->id,
  607. 'sku' => $product->sku,
  608. 'quantity' => $additional['quantity'],
  609. 'name' => $product->name,
  610. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  611. 'base_price' => $price,
  612. 'total' => $convertedPrice * $additional['quantity'],
  613. 'base_total' => $price * $additional['quantity'],
  614. 'weight' => $product->weight ?? 0,
  615. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  616. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  617. 'type' => $product->type,
  618. 'additional' => $additional,
  619. ]);
  620. $cartBillingAddress = CartAddress::factory()->create([
  621. 'cart_id' => $cart->id,
  622. 'customer_id' => $customer->id,
  623. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  624. ]);
  625. $cartShippingAddress = CartAddress::factory()->create([
  626. 'cart_id' => $cart->id,
  627. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  628. ]);
  629. cart()->setCart($cart);
  630. // Act and Assert.
  631. $this->loginAsAdmin();
  632. $response = postJson(route('admin.sales.cart.payment_methods.store', $cart->id), [
  633. 'payment' => [
  634. 'description' => 'Cash On Delivery',
  635. 'method' => 'cashondelivery',
  636. 'method_title' => 'Cash On Delivery',
  637. 'sort' => 1,
  638. ],
  639. ])
  640. ->assertJsonPath('cart.id', $cart->id)
  641. ->assertJsonPath('cart.is_guest', 0)
  642. ->assertJsonPath('cart.items_count', 1)
  643. ->assertJsonPath('cart.customer_id', $customer->id)
  644. ->assertJsonPath('cart.items_count', 1)
  645. ->assertJsonPath('cart.items_qty', 1)
  646. ->assertJsonPath('cart.items.0.id', $cartItem->id)
  647. ->assertJsonPath('cart.items.0.quantity', 1)
  648. ->assertJsonPath('cart.items.0.type', $cartItem->type)
  649. ->assertJsonPath('cart.items.0.name', $cartItem->name)
  650. ->assertJsonPath('cart.items.0.price', $cartItem->price)
  651. ->assertJsonPath('cart.items.0.formatted_price', core()->formatPrice($cartItem->price))
  652. ->assertJsonPath('cart.items.0.formatted_total', core()->formatPrice($cartItem->total))
  653. ->assertJsonPath('cart.items.0.options', array_values($cartItem->additional['attributes'] ?? []))
  654. ->assertJsonPath('cart.billing_address.id', $cartBillingAddress->id)
  655. ->assertJsonPath('cart.billing_address.address_type', $cartBillingAddress->address_type)
  656. ->assertJsonPath('cart.billing_address.parent_address_id', $cartBillingAddress->parent_address_id)
  657. ->assertJsonPath('cart.billing_address.customer_id', $cartBillingAddress->customer_id)
  658. ->assertJsonPath('cart.billing_address.cart_id', $cart->id)
  659. ->assertJsonPath('cart.billing_address.order_id', null)
  660. ->assertJsonPath('cart.billing_address.first_name', $cartBillingAddress->first_name)
  661. ->assertJsonPath('cart.billing_address.last_name', $cartBillingAddress->last_name)
  662. ->assertJsonPath('cart.billing_address.gender', $cartBillingAddress->gender)
  663. ->assertJsonPath('cart.billing_address.company_name', $cartBillingAddress->company_name)
  664. ->assertJsonPath('cart.billing_address.address', explode("\n", $cartBillingAddress->address))
  665. ->assertJsonPath('cart.billing_address.city', $cartBillingAddress->city)
  666. ->assertJsonPath('cart.billing_address.state', $cartBillingAddress->state)
  667. ->assertJsonPath('cart.billing_address.country', $cartBillingAddress->country)
  668. ->assertJsonPath('cart.billing_address.postcode', $cartBillingAddress->postcode)
  669. ->assertJsonPath('cart.billing_address.email', $cartBillingAddress->email)
  670. ->assertJsonPath('cart.billing_address.phone', $cartBillingAddress->phone)
  671. ->assertJsonPath('cart.billing_address.vat_id', $cartBillingAddress->vat_id)
  672. ->assertJsonPath('cart.shipping_address.id', $cartShippingAddress->id)
  673. ->assertJsonPath('cart.shipping_address.address_type', $cartShippingAddress->address_type)
  674. ->assertJsonPath('cart.shipping_address.parent_address_id', $cartShippingAddress->parent_address_id)
  675. ->assertJsonPath('cart.shipping_address.customer_id', $cartShippingAddress->customer_id)
  676. ->assertJsonPath('cart.shipping_address.cart_id', $cart->id)
  677. ->assertJsonPath('cart.shipping_address.order_id', null)
  678. ->assertJsonPath('cart.shipping_address.first_name', $cartShippingAddress->first_name)
  679. ->assertJsonPath('cart.shipping_address.last_name', $cartShippingAddress->last_name)
  680. ->assertJsonPath('cart.shipping_address.gender', $cartShippingAddress->gender)
  681. ->assertJsonPath('cart.shipping_address.company_name', $cartShippingAddress->company_name)
  682. ->assertJsonPath('cart.shipping_address.address', explode("\n", $cartShippingAddress->address))
  683. ->assertJsonPath('cart.shipping_address.city', $cartShippingAddress->city)
  684. ->assertJsonPath('cart.shipping_address.state', $cartShippingAddress->state)
  685. ->assertJsonPath('cart.shipping_address.country', $cartShippingAddress->country)
  686. ->assertJsonPath('cart.shipping_address.postcode', $cartShippingAddress->postcode)
  687. ->assertJsonPath('cart.shipping_address.email', $cartShippingAddress->email)
  688. ->assertJsonPath('cart.shipping_address.phone', $cartShippingAddress->phone)
  689. ->assertJsonPath('cart.shipping_address.vat_id', $cartShippingAddress->vat_id)
  690. ->assertJsonPath('cart.have_stockable_items', true)
  691. ->assertJsonPath('cart.payment_method', 'cashondelivery')
  692. ->assertJsonPath('cart.payment_method_title', 'Cash On Delivery')
  693. ->assertOk();
  694. $this->assertPrice($product->price, $response['cart']['grand_total']);
  695. $this->assertPrice($cartItem->total, $response['cart']['items']['0']['total']);
  696. $this->assertPrice($product->price, $response['cart']['sub_total']);
  697. });
  698. it('should place order via admin', function () {
  699. // Arrange.
  700. $product = (new ProductFaker([
  701. 'attributes' => [
  702. 5 => 'new',
  703. 26 => 'guest_checkout',
  704. ],
  705. 'attribute_value' => [
  706. 'new' => [
  707. 'boolean_value' => true,
  708. ],
  709. 'guest_checkout' => [
  710. 'boolean_value' => true,
  711. ],
  712. ],
  713. ]))
  714. ->getSimpleProductFactory()
  715. ->create();
  716. $customer = (new CustomerFaker)->factory()->create();
  717. $cart = Cart::factory()->create([
  718. 'customer_id' => $customer->id,
  719. 'customer_first_name' => $customer->first_name,
  720. 'customer_last_name' => $customer->last_name,
  721. 'customer_email' => $customer->email,
  722. 'is_guest' => 0,
  723. 'shipping_method' => 'free_free',
  724. 'is_active' => 0,
  725. ]);
  726. $additional = [
  727. 'product_id' => $product->id,
  728. 'rating' => '0',
  729. 'is_buy_now' => '0',
  730. 'quantity' => '1',
  731. ];
  732. CartItem::factory()->create([
  733. 'cart_id' => $cart->id,
  734. 'product_id' => $product->id,
  735. 'sku' => $product->sku,
  736. 'quantity' => $additional['quantity'],
  737. 'name' => $product->name,
  738. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  739. 'base_price' => $price,
  740. 'total' => $convertedPrice * $additional['quantity'],
  741. 'base_total' => $price * $additional['quantity'],
  742. 'weight' => $product->weight ?? 0,
  743. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  744. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  745. 'type' => $product->type,
  746. 'additional' => $additional,
  747. ]);
  748. CartAddress::factory()->create([
  749. 'cart_id' => $cart->id,
  750. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  751. ]);
  752. $cartShippingAddress = CartAddress::factory()->create([
  753. 'cart_id' => $cart->id,
  754. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  755. ]);
  756. CartPayment::factory()->create([
  757. 'cart_id' => $cart->id,
  758. 'method' => $paymentMethod = 'cashondelivery',
  759. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  760. ]);
  761. CartShippingRate::factory()->create([
  762. 'carrier' => 'free',
  763. 'carrier_title' => 'Free shipping',
  764. 'method' => 'free_free',
  765. 'method_title' => 'Free Shipping',
  766. 'method_description' => 'Free Shipping',
  767. 'cart_address_id' => $cartShippingAddress->id,
  768. 'cart_id' => $cart->id,
  769. ]);
  770. cart()->setCart($cart);
  771. cart()->collectTotals();
  772. // Act and Assert.
  773. $this->loginAsAdmin();
  774. postJson(route('admin.sales.orders.store', $cart->id))
  775. ->assertOk()
  776. ->assertJsonPath('data.redirect', true);
  777. $this->assertDatabaseMissing('cart', [
  778. 'id' => $cart->id,
  779. ]);
  780. });
  781. it('should lists the all wishlist items related to the customer', function () {
  782. // Arrange.
  783. $products = (new ProductFaker([
  784. 'attributes' => [
  785. 5 => 'new',
  786. ],
  787. 'attribute_value' => [
  788. 'new' => [
  789. 'boolean_value' => true,
  790. ],
  791. ],
  792. ]))
  793. ->getSimpleProductFactory()
  794. ->count(5)
  795. ->create();
  796. $customer = (new CustomerFaker)->factory()->create();
  797. $wishlists = [];
  798. foreach ($products as $product) {
  799. $wishlists[] = Wishlist::factory()->create([
  800. 'channel_id' => core()->getDefaultChannel()->id,
  801. 'product_id' => $product->id,
  802. 'customer_id' => $customer->id,
  803. ]);
  804. }
  805. // Act and assert.
  806. $this->loginAsAdmin();
  807. $response = getJson(route('admin.customers.customers.wishlist.items', $customer->id));
  808. foreach ($wishlists as $key => $wishlist) {
  809. $response->assertJsonPath('data.'.$key.'.id', $wishlist->id)
  810. ->assertJsonPath('data.'.$key.'.product.id', $wishlist->product_id)
  811. ->assertJsonPath('data.'.$key.'.product.name', $wishlist->product->name)
  812. ->assertJsonPath('data.'.$key.'.product.sku', $wishlist->product->sku)
  813. ->assertJsonPath('data.'.$key.'.product.type', $wishlist->product->type)
  814. ->assertJsonPath('data.'.$key.'.product.price', $wishlist->product->price)
  815. ->assertJsonPath('data.'.$key.'.product.formatted_price', core()->formatPrice($wishlist->product->price));
  816. }
  817. });
  818. it('should remove item from the wishlist', function () {
  819. // Arrange.
  820. $product = (new ProductFaker([
  821. 'attributes' => [
  822. 5 => 'new',
  823. ],
  824. 'attribute_value' => [
  825. 'new' => [
  826. 'boolean_value' => true,
  827. ],
  828. ],
  829. ]))
  830. ->getSimpleProductFactory()
  831. ->create();
  832. $customer = (new CustomerFaker)->factory()->create();
  833. $wishlist = Wishlist::factory()->create([
  834. 'channel_id' => core()->getDefaultChannel()->id,
  835. 'product_id' => $product->id,
  836. 'customer_id' => $customer->id,
  837. ]);
  838. // Act and assert.
  839. $this->loginAsAdmin();
  840. deleteJson(route('admin.customers.customers.wishlist.items.delete', $customer->id), [
  841. 'data' => [
  842. 'item_id' => $wishlist->id,
  843. ],
  844. ]);
  845. $this->assertDatabaseMissing('wishlist', [
  846. 'id' => $wishlist->id,
  847. ]);
  848. });
  849. it('should add a simple product wishlisted item to the cart', function () {
  850. // Arrange.
  851. $product = (new ProductFaker([
  852. 'attributes' => [
  853. 5 => 'new',
  854. ],
  855. 'attribute_value' => [
  856. 'new' => [
  857. 'boolean_value' => true,
  858. ],
  859. ],
  860. ]))
  861. ->getSimpleProductFactory()
  862. ->create();
  863. $customer = (new CustomerFaker)->factory()->create();
  864. $cart = Cart::factory()->create([
  865. 'customer_id' => $customer->id,
  866. 'customer_first_name' => $customer->first_name,
  867. 'customer_last_name' => $customer->last_name,
  868. 'customer_email' => $customer->email,
  869. 'is_guest' => 0,
  870. ]);
  871. $wishlist = Wishlist::factory()->create([
  872. 'channel_id' => core()->getDefaultChannel()->id,
  873. 'product_id' => $product->id,
  874. 'customer_id' => $customer->id,
  875. ]);
  876. // Act and assert.
  877. $this->loginAsAdmin();
  878. $response = postJson(route('admin.sales.cart.items.store', $cart->id), [
  879. 'data' => [
  880. 'item_id' => $wishlist->id,
  881. ],
  882. 'product_id' => $product->id,
  883. 'quantity' => 1,
  884. ])
  885. ->assertJsonPath('data.customer_id', $customer->id)
  886. ->assertJsonPath('data.items_count', 1)
  887. ->assertJsonPath('data.items_qty', 1)
  888. ->assertJsonPath('data.items.0.id', $cart->items->first()->id)
  889. ->assertJsonPath('data.items.0.cart_id', $cart->id)
  890. ->assertJsonPath('data.items.0.product_id', $product->id)
  891. ->assertJsonPath('data.items.0.sku', $product->sku)
  892. ->assertJsonPath('data.items.0.type', 'simple')
  893. ->assertJsonPath('data.have_stockable_items', true)
  894. ->assertJsonPath('message', trans('admin::app.sales.orders.create.cart.success-add-to-cart'));
  895. $this->assertPrice($product->price, $response->json('data.items.0.price'));
  896. $this->assertPrice($product->price, $response->json('data.items.0.total'));
  897. $this->assertPrice($product->price, $response->json('data.grand_total'));
  898. $this->assertPrice($product->price, $response->json('data.sub_total'));
  899. });
  900. it('should add a configurable product wishlisted item to the cart', function () {
  901. // Arrange.
  902. $product = (new ProductFaker([
  903. 'attributes' => [
  904. 5 => 'new',
  905. ],
  906. 'attribute_value' => [
  907. 'new' => [
  908. 'boolean_value' => true,
  909. ],
  910. ],
  911. ]))
  912. ->getConfigurableProductFactory()
  913. ->create();
  914. $customer = (new CustomerFaker)->factory()->create();
  915. $cart = Cart::factory()->create([
  916. 'customer_id' => $customer->id,
  917. 'customer_first_name' => $customer->first_name,
  918. 'customer_last_name' => $customer->last_name,
  919. 'customer_email' => $customer->email,
  920. 'is_guest' => 0,
  921. ]);
  922. $wishlist = Wishlist::factory()->create([
  923. 'channel_id' => core()->getDefaultChannel()->id,
  924. 'product_id' => $product->id,
  925. 'customer_id' => $customer->id,
  926. ]);
  927. $childProduct = $product->variants()->first();
  928. // Act and assert.
  929. $this->loginAsAdmin();
  930. $response = postJson(route('admin.sales.cart.items.store', $cart->id), [
  931. 'data' => [
  932. 'item_id' => $wishlist->id,
  933. ],
  934. 'selected_configurable_option' => $childProduct->id,
  935. 'product_id' => $product->id,
  936. 'is_buy_now' => '0',
  937. 'rating' => '0',
  938. 'quantity' => '1',
  939. 'super_attribute' => [
  940. 23 => '1',
  941. 24 => '7',
  942. ],
  943. ])
  944. ->assertJsonPath('data.customer_id', $customer->id)
  945. ->assertJsonPath('data.items_count', 1)
  946. ->assertJsonPath('data.items_qty', 1)
  947. ->assertJsonPath('data.items.0.id', $cart->items->first()->id)
  948. ->assertJsonPath('data.items.0.cart_id', $cart->id)
  949. ->assertJsonPath('data.items.0.product_id', $product->id)
  950. ->assertJsonPath('data.items.0.sku', $product->sku)
  951. ->assertJsonPath('data.items.0.type', 'configurable')
  952. ->assertJsonPath('data.have_stockable_items', true)
  953. ->assertJsonPath('message', trans('admin::app.sales.orders.create.cart.success-add-to-cart'));
  954. $this->assertPrice($childProduct->price, $response->json('data.items.0.price'));
  955. $this->assertPrice($childProduct->price, $response->json('data.items.0.total'));
  956. $this->assertPrice($childProduct->price, $response->json('data.grand_total'));
  957. $this->assertPrice($childProduct->price, $response->json('data.sub_total'));
  958. });
  959. it('should return all the compare items related to the customer', function () {
  960. // Arrange.
  961. $products = (new ProductFaker([
  962. 'attributes' => [
  963. 5 => 'new',
  964. ],
  965. 'attribute_value' => [
  966. 'new' => [
  967. 'boolean_value' => true,
  968. ],
  969. ],
  970. ]))
  971. ->getSimpleProductFactory()
  972. ->count(5)
  973. ->create();
  974. $customer = (new CustomerFaker)->factory()->create();
  975. $compares = [];
  976. foreach ($products as $product) {
  977. $compares[] = CompareItem::factory()->create([
  978. 'product_id' => $product->id,
  979. 'customer_id' => $customer->id,
  980. ]);
  981. }
  982. // Act and assert.
  983. $this->loginAsAdmin();
  984. $response = getJson(route('admin.customers.customers.compare.items', $customer->id));
  985. foreach ($compares as $key => $compare) {
  986. $response->assertJsonPath('data.'.$key.'.id', $compare->id)
  987. ->assertJsonPath('data.'.$key.'.product.id', $compare->product_id)
  988. ->assertJsonPath('data.'.$key.'.product.name', $compare->product->name)
  989. ->assertJsonPath('data.'.$key.'.product.sku', $compare->product->sku)
  990. ->assertJsonPath('data.'.$key.'.product.type', $compare->product->type)
  991. ->assertJsonPath('data.'.$key.'.product.price', $compare->product->price)
  992. ->assertJsonPath('data.'.$key.'.product.formatted_price', core()->formatPrice($compare->product->price));
  993. }
  994. });
  995. it('should remove compare items from the compared list item', function () {
  996. // Arrange.
  997. $product = (new ProductFaker([
  998. 'attributes' => [
  999. 5 => 'new',
  1000. ],
  1001. 'attribute_value' => [
  1002. 'new' => [
  1003. 'boolean_value' => true,
  1004. ],
  1005. ],
  1006. ]))
  1007. ->getSimpleProductFactory()
  1008. ->create();
  1009. $customer = (new CustomerFaker)->factory()->create();
  1010. $compare = CompareItem::factory()->create([
  1011. 'product_id' => $product->id,
  1012. 'customer_id' => $customer->id,
  1013. ]);
  1014. // Act and assert.
  1015. $this->loginAsAdmin();
  1016. deleteJson(route('admin.customers.customers.compare.items.delete', $customer->id), [
  1017. 'item_id' => $compare->id,
  1018. ]);
  1019. $this->assertDatabaseMissing('compare_items', [
  1020. 'id' => $compare->id,
  1021. 'product_id' => $compare->product_id,
  1022. 'customer_id' => $compare->customer_id,
  1023. ]);
  1024. });
  1025. it('show add a simple product to the cart from compared items', function () {
  1026. // Arrange.
  1027. $product = (new ProductFaker([
  1028. 'attributes' => [
  1029. 5 => 'new',
  1030. ],
  1031. 'attribute_value' => [
  1032. 'new' => [
  1033. 'boolean_value' => true,
  1034. ],
  1035. ],
  1036. ]))
  1037. ->getSimpleProductFactory()
  1038. ->create();
  1039. $customer = (new CustomerFaker)->factory()->create();
  1040. $cart = Cart::factory()->create([
  1041. 'customer_id' => $customer->id,
  1042. 'customer_first_name' => $customer->first_name,
  1043. 'customer_last_name' => $customer->last_name,
  1044. 'customer_email' => $customer->email,
  1045. 'is_guest' => 0,
  1046. ]);
  1047. $compare = CompareItem::factory()->create([
  1048. 'product_id' => $product->id,
  1049. 'customer_id' => $customer->id,
  1050. ]);
  1051. // Act and assert.
  1052. $this->loginAsAdmin();
  1053. $response = postJson(route('admin.sales.cart.items.store', $cart->id), [
  1054. 'data' => [
  1055. 'item_id' => $compare->id,
  1056. ],
  1057. 'product_id' => $product->id,
  1058. 'quantity' => 1,
  1059. ])
  1060. ->assertJsonPath('data.customer_id', $customer->id)
  1061. ->assertJsonPath('data.items_count', 1)
  1062. ->assertJsonPath('data.items_qty', 1)
  1063. ->assertJsonPath('data.items.0.id', $cart->items->first()->id)
  1064. ->assertJsonPath('data.items.0.cart_id', $cart->id)
  1065. ->assertJsonPath('data.items.0.product_id', $product->id)
  1066. ->assertJsonPath('data.items.0.sku', $product->sku)
  1067. ->assertJsonPath('data.items.0.type', 'simple')
  1068. ->assertJsonPath('data.have_stockable_items', true)
  1069. ->assertJsonPath('message', trans('admin::app.sales.orders.create.cart.success-add-to-cart'));
  1070. $this->assertPrice($product->price, $response->json('data.items.0.price'));
  1071. $this->assertPrice($product->price, $response->json('data.items.0.total'));
  1072. $this->assertPrice($product->price, $response->json('data.grand_total'));
  1073. $this->assertPrice($product->price, $response->json('data.sub_total'));
  1074. });
  1075. it('show add a configurable product to the cart from compared items', function () {
  1076. // Arrange.
  1077. $product = (new ProductFaker([
  1078. 'attributes' => [
  1079. 5 => 'new',
  1080. ],
  1081. 'attribute_value' => [
  1082. 'new' => [
  1083. 'boolean_value' => true,
  1084. ],
  1085. ],
  1086. ]))
  1087. ->getConfigurableProductFactory()
  1088. ->create();
  1089. $customer = (new CustomerFaker)->factory()->create();
  1090. $cart = Cart::factory()->create([
  1091. 'customer_id' => $customer->id,
  1092. 'customer_first_name' => $customer->first_name,
  1093. 'customer_last_name' => $customer->last_name,
  1094. 'customer_email' => $customer->email,
  1095. 'is_guest' => 0,
  1096. ]);
  1097. $compare = CompareItem::factory()->create([
  1098. 'product_id' => $product->id,
  1099. 'customer_id' => $customer->id,
  1100. ]);
  1101. $childProduct = $product->variants()->first();
  1102. // Act and assert.
  1103. $this->loginAsAdmin();
  1104. $response = postJson(route('admin.sales.cart.items.store', $cart->id), [
  1105. 'data' => [
  1106. 'item_id' => $compare->id,
  1107. ],
  1108. 'selected_configurable_option' => $childProduct->id,
  1109. 'product_id' => $product->id,
  1110. 'is_buy_now' => '0',
  1111. 'rating' => '0',
  1112. 'quantity' => '1',
  1113. 'super_attribute' => [
  1114. 23 => '1',
  1115. 24 => '7',
  1116. ],
  1117. ])
  1118. ->assertJsonPath('data.customer_id', $customer->id)
  1119. ->assertJsonPath('data.items_count', 1)
  1120. ->assertJsonPath('data.items_qty', 1)
  1121. ->assertJsonPath('data.items.0.id', $cart->items->first()->id)
  1122. ->assertJsonPath('data.items.0.cart_id', $cart->id)
  1123. ->assertJsonPath('data.items.0.product_id', $product->id)
  1124. ->assertJsonPath('data.items.0.sku', $product->sku)
  1125. ->assertJsonPath('data.items.0.type', 'configurable')
  1126. ->assertJsonPath('data.have_stockable_items', true)
  1127. ->assertJsonPath('message', trans('admin::app.sales.orders.create.cart.success-add-to-cart'));
  1128. $this->assertPrice($childProduct->price, $response->json('data.items.0.price'));
  1129. $this->assertPrice($childProduct->price, $response->json('data.items.0.total'));
  1130. $this->assertPrice($childProduct->price, $response->json('data.grand_total'));
  1131. $this->assertPrice($childProduct->price, $response->json('data.sub_total'));
  1132. });
  1133. it('should return the list of the recent orders', function () {
  1134. // Arrange.
  1135. $product = (new ProductFaker([
  1136. 'attributes' => [
  1137. 5 => 'new',
  1138. ],
  1139. 'attribute_value' => [
  1140. 'new' => [
  1141. 'boolean_value' => true,
  1142. ],
  1143. ],
  1144. ]))
  1145. ->getSimpleProductFactory()
  1146. ->create();
  1147. $customer = Customer::factory()->create();
  1148. $cart = Cart::factory()->create([
  1149. 'customer_id' => $customer->id,
  1150. 'customer_first_name' => $customer->first_name,
  1151. 'customer_last_name' => $customer->last_name,
  1152. 'customer_email' => $customer->email,
  1153. 'is_guest' => 0,
  1154. ]);
  1155. $additional = [
  1156. 'product_id' => $product->id,
  1157. 'rating' => '0',
  1158. 'is_buy_now' => '0',
  1159. 'quantity' => '1',
  1160. ];
  1161. CartItem::factory()->create([
  1162. 'cart_id' => $cart->id,
  1163. 'product_id' => $product->id,
  1164. 'sku' => $product->sku,
  1165. 'quantity' => $additional['quantity'],
  1166. 'name' => $product->name,
  1167. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  1168. 'base_price' => $price,
  1169. 'total' => $convertedPrice * $additional['quantity'],
  1170. 'base_total' => $price * $additional['quantity'],
  1171. 'weight' => $product->weight ?? 0,
  1172. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1173. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1174. 'type' => $product->type,
  1175. 'additional' => $additional,
  1176. ]);
  1177. CustomerAddress::factory()->create([
  1178. 'cart_id' => $cart->id,
  1179. 'customer_id' => $customer->id,
  1180. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  1181. ]);
  1182. CartAddress::factory()->create([
  1183. 'cart_id' => $cart->id,
  1184. 'customer_id' => $customer->id,
  1185. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1186. ]);
  1187. $cartShippingAddress = CartAddress::factory()->create([
  1188. 'cart_id' => $cart->id,
  1189. 'customer_id' => $customer->id,
  1190. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1191. ]);
  1192. CartPayment::factory()->create([
  1193. 'cart_id' => $cart->id,
  1194. 'method' => $paymentMethod = 'cashondelivery',
  1195. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  1196. ]);
  1197. CartShippingRate::factory()->create([
  1198. 'carrier' => 'free',
  1199. 'carrier_title' => 'Free shipping',
  1200. 'method' => 'free_free',
  1201. 'method_title' => 'Free Shipping',
  1202. 'method_description' => 'Free Shipping',
  1203. 'cart_address_id' => $cartShippingAddress->id,
  1204. ]);
  1205. $order = Order::factory()->create([
  1206. 'cart_id' => $cart->id,
  1207. 'customer_id' => $customer->id,
  1208. 'customer_email' => $customer->email,
  1209. 'customer_first_name' => $customer->first_name,
  1210. 'customer_last_name' => $customer->last_name,
  1211. ]);
  1212. OrderItem::factory()->create([
  1213. 'product_id' => $product->id,
  1214. 'order_id' => $order->id,
  1215. 'sku' => $product->sku,
  1216. 'type' => $product->type,
  1217. 'name' => $product->name,
  1218. ]);
  1219. OrderAddress::factory()->create([
  1220. 'cart_id' => $cart->id,
  1221. 'customer_id' => $customer->id,
  1222. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  1223. ]);
  1224. OrderAddress::factory()->create([
  1225. 'cart_id' => $cart->id,
  1226. 'customer_id' => $customer->id,
  1227. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  1228. ]);
  1229. OrderPayment::factory()->create([
  1230. 'order_id' => $order->id,
  1231. ]);
  1232. // Act and assert.
  1233. $this->loginAsAdmin();
  1234. $response = getJson(route('admin.customers.customers.orders.recent_items', $cart->customer_id))
  1235. ->assertJsonPath('data.0.id', $order->id)
  1236. ->assertJsonPath('data.0.order_id', $order->id)
  1237. ->assertJsonPath('data.0.product.id', $product->id)
  1238. ->assertJsonPath('data.0.product.sku', $product->sku)
  1239. ->assertJsonPath('data.0.product.name', $product->name);
  1240. $this->assertPrice($product->price, $response->json('data.0.product.price'));
  1241. });