OrdersTest.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. <?php
  2. use Illuminate\Support\Facades\Mail;
  3. use Webkul\Admin\Mail\Order\CanceledNotification as AdminOrderCanceledNotification;
  4. use Webkul\Checkout\Models\Cart;
  5. use Webkul\Checkout\Models\CartAddress;
  6. use Webkul\Checkout\Models\CartItem;
  7. use Webkul\Checkout\Models\CartPayment;
  8. use Webkul\Checkout\Models\CartShippingRate;
  9. use Webkul\Core\Models\CoreConfig;
  10. use Webkul\Customer\Models\Customer;
  11. use Webkul\Customer\Models\CustomerAddress;
  12. use Webkul\Faker\Helpers\Product as ProductFaker;
  13. use Webkul\Sales\Models\Order;
  14. use Webkul\Sales\Models\OrderAddress;
  15. use Webkul\Sales\Models\OrderComment;
  16. use Webkul\Sales\Models\OrderItem;
  17. use Webkul\Sales\Models\OrderPayment;
  18. use Webkul\Shop\Mail\Order\CanceledNotification as ShopOrderCanceledNotification;
  19. use Webkul\Shop\Mail\Order\CommentedNotification;
  20. use function Pest\Laravel\get;
  21. use function Pest\Laravel\postJson;
  22. it('should return the index page of Orders page', function () {
  23. // Act and Assert.
  24. $this->loginAsAdmin();
  25. get(route('admin.sales.orders.index'))
  26. ->assertOk()
  27. ->assertSeeText(trans('admin::app.sales.orders.index.title'));
  28. });
  29. it('should return the view page of order', function () {
  30. // Arrange.
  31. $product = (new ProductFaker([
  32. 'attributes' => [
  33. 5 => 'new',
  34. ],
  35. 'attribute_value' => [
  36. 'new' => [
  37. 'boolean_value' => true,
  38. ],
  39. ],
  40. ]))
  41. ->getSimpleProductFactory()
  42. ->create();
  43. $customer = Customer::factory()->create();
  44. $cart = Cart::factory()->create([
  45. 'customer_id' => $customer->id,
  46. 'customer_first_name' => $customer->first_name,
  47. 'customer_last_name' => $customer->last_name,
  48. 'customer_email' => $customer->email,
  49. 'is_guest' => 0,
  50. ]);
  51. $additional = [
  52. 'product_id' => $product->id,
  53. 'rating' => '0',
  54. 'is_buy_now' => '0',
  55. 'quantity' => '1',
  56. ];
  57. $cartItem = CartItem::factory()->create([
  58. 'cart_id' => $cart->id,
  59. 'product_id' => $product->id,
  60. 'sku' => $product->sku,
  61. 'quantity' => $additional['quantity'],
  62. 'name' => $product->name,
  63. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  64. 'base_price' => $price,
  65. 'total' => $convertedPrice * $additional['quantity'],
  66. 'base_total' => $price * $additional['quantity'],
  67. 'weight' => $product->weight ?? 0,
  68. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  69. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  70. 'type' => $product->type,
  71. 'additional' => $additional,
  72. ]);
  73. $customerAddress = CustomerAddress::factory()->create([
  74. 'cart_id' => $cart->id,
  75. 'customer_id' => $customer->id,
  76. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  77. ]);
  78. $cartBillingAddress = CartAddress::factory()->create([
  79. 'cart_id' => $cart->id,
  80. 'customer_id' => $customer->id,
  81. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  82. ]);
  83. $cartShippingAddress = CartAddress::factory()->create([
  84. 'cart_id' => $cart->id,
  85. 'customer_id' => $customer->id,
  86. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  87. ]);
  88. $cartPayment = CartPayment::factory()->create([
  89. 'cart_id' => $cart->id,
  90. 'method' => $paymentMethod = 'cashondelivery',
  91. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  92. ]);
  93. $cartShippingRate = CartShippingRate::factory()->create([
  94. 'carrier' => 'free',
  95. 'carrier_title' => 'Free shipping',
  96. 'method' => 'free_free',
  97. 'method_title' => 'Free Shipping',
  98. 'method_description' => 'Free Shipping',
  99. 'cart_address_id' => $cartShippingAddress->id,
  100. ]);
  101. $order = Order::factory()->create([
  102. 'cart_id' => $cart->id,
  103. 'customer_id' => $customer->id,
  104. 'customer_email' => $customer->email,
  105. 'customer_first_name' => $customer->first_name,
  106. 'customer_last_name' => $customer->last_name,
  107. ]);
  108. $orderItem = OrderItem::factory()->create([
  109. 'product_id' => $product->id,
  110. 'order_id' => $order->id,
  111. 'sku' => $product->sku,
  112. 'type' => $product->type,
  113. 'name' => $product->name,
  114. ]);
  115. $orderBillingAddress = OrderAddress::factory()->create([
  116. 'cart_id' => $cart->id,
  117. 'customer_id' => $customer->id,
  118. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  119. ]);
  120. $orderShippingAddress = OrderAddress::factory()->create([
  121. 'cart_id' => $cart->id,
  122. 'customer_id' => $customer->id,
  123. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  124. ]);
  125. $orderPayment = OrderPayment::factory()->create([
  126. 'order_id' => $order->id,
  127. ]);
  128. // Act and Assert.
  129. $this->loginAsAdmin();
  130. get(route('admin.sales.orders.view', $order->id))
  131. ->assertOk()
  132. ->assertSeeText(trans('admin::app.sales.orders.view.'.$order->status))
  133. ->assertSeeText(trans('admin::app.sales.orders.view.title', ['order_id' => $order->increment_id]))
  134. ->assertSeeText(trans('admin::app.sales.orders.view.summary-tax'))
  135. ->assertSeeText(trans('admin::app.sales.orders.view.summary-grand-total'))
  136. ->assertSeeText(trans('admin::app.sales.orders.view.comments'));
  137. $cart->refresh();
  138. $cartItem->refresh();
  139. $cartBillingAddress->refresh();
  140. $cartShippingAddress->refresh();
  141. $orderBillingAddress->refresh();
  142. $orderShippingAddress->refresh();
  143. $order->refresh();
  144. $orderItem->refresh();
  145. $this->assertModelWise([
  146. Cart::class => [
  147. $this->prepareCart($cart),
  148. ],
  149. CartItem::class => [
  150. $this->prepareCartItem($cartItem),
  151. ],
  152. CartPayment::class => [
  153. $this->prepareCartPayment($cartPayment),
  154. ],
  155. CartAddress::class => [
  156. $this->prepareAddress($cartBillingAddress),
  157. ],
  158. CartAddress::class => [
  159. $this->prepareAddress($cartShippingAddress),
  160. ],
  161. CartShippingRate::class => [
  162. $this->prepareCartShippingRate($cartShippingRate),
  163. ],
  164. CustomerAddress::class => [
  165. $this->prepareAddress($customerAddress),
  166. ],
  167. Order::class => [
  168. $this->prepareOrder($order),
  169. ],
  170. OrderItem::class => [
  171. $this->prepareOrderItem($orderItem),
  172. ],
  173. OrderAddress::class => [
  174. $this->prepareAddress($orderBillingAddress),
  175. $this->prepareAddress($orderShippingAddress),
  176. ],
  177. OrderPayment::class => [
  178. $this->prepareOrderPayment($orderPayment),
  179. ],
  180. ]);
  181. });
  182. it('should cancel the order', function () {
  183. // Arrange.
  184. $product = (new ProductFaker([
  185. 'attributes' => [
  186. 5 => 'new',
  187. ],
  188. 'attribute_value' => [
  189. 'new' => [
  190. 'boolean_value' => true,
  191. ],
  192. ],
  193. ]))
  194. ->getSimpleProductFactory()
  195. ->create();
  196. $customer = Customer::factory()->create();
  197. $cart = Cart::factory()->create([
  198. 'customer_id' => $customer->id,
  199. 'customer_first_name' => $customer->first_name,
  200. 'customer_last_name' => $customer->last_name,
  201. 'customer_email' => $customer->email,
  202. 'is_guest' => 0,
  203. ]);
  204. $additional = [
  205. 'product_id' => $product->id,
  206. 'rating' => '0',
  207. 'is_buy_now' => '0',
  208. 'quantity' => '1',
  209. ];
  210. $cartItem = CartItem::factory()->create([
  211. 'cart_id' => $cart->id,
  212. 'product_id' => $product->id,
  213. 'sku' => $product->sku,
  214. 'quantity' => $additional['quantity'],
  215. 'name' => $product->name,
  216. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  217. 'base_price' => $price,
  218. 'total' => $convertedPrice * $additional['quantity'],
  219. 'base_total' => $price * $additional['quantity'],
  220. 'weight' => $product->weight ?? 0,
  221. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  222. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  223. 'type' => $product->type,
  224. 'additional' => $additional,
  225. ]);
  226. $customerAddress = CustomerAddress::factory()->create([
  227. 'cart_id' => $cart->id,
  228. 'customer_id' => $customer->id,
  229. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  230. ]);
  231. $cartBillingAddress = CartAddress::factory()->create([
  232. 'cart_id' => $cart->id,
  233. 'customer_id' => $customer->id,
  234. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  235. ]);
  236. $cartShippingAddress = CartAddress::factory()->create([
  237. 'cart_id' => $cart->id,
  238. 'customer_id' => $customer->id,
  239. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  240. ]);
  241. $cartPayment = CartPayment::factory()->create([
  242. 'cart_id' => $cart->id,
  243. 'method' => $paymentMethod = 'cashondelivery',
  244. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  245. ]);
  246. $cartShippingRate = CartShippingRate::factory()->create([
  247. 'carrier' => 'free',
  248. 'carrier_title' => 'Free shipping',
  249. 'method' => 'free_free',
  250. 'method_title' => 'Free Shipping',
  251. 'method_description' => 'Free Shipping',
  252. 'cart_address_id' => $cartShippingAddress->id,
  253. ]);
  254. $order = Order::factory()->create([
  255. 'cart_id' => $cart->id,
  256. 'customer_id' => $customer->id,
  257. 'customer_email' => $customer->email,
  258. 'customer_first_name' => $customer->first_name,
  259. 'customer_last_name' => $customer->last_name,
  260. ]);
  261. $orderItem = OrderItem::factory()->create([
  262. 'product_id' => $product->id,
  263. 'order_id' => $order->id,
  264. 'sku' => $product->sku,
  265. 'type' => $product->type,
  266. 'name' => $product->name,
  267. ]);
  268. $orderBillingAddress = OrderAddress::factory()->create([
  269. 'cart_id' => $cart->id,
  270. 'customer_id' => $customer->id,
  271. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  272. ]);
  273. $orderShippingAddress = OrderAddress::factory()->create([
  274. 'cart_id' => $cart->id,
  275. 'customer_id' => $customer->id,
  276. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  277. ]);
  278. $orderPayment = OrderPayment::factory()->create([
  279. 'order_id' => $order->id,
  280. ]);
  281. // Act and Assert.
  282. $this->loginAsAdmin();
  283. postJson(route('admin.sales.orders.cancel', $order->id))
  284. ->assertRedirect(route('admin.sales.orders.view', $order->id))
  285. ->isRedirection();
  286. $cart->refresh();
  287. $cartItem->refresh();
  288. $cartBillingAddress->refresh();
  289. $cartShippingAddress->refresh();
  290. $orderBillingAddress->refresh();
  291. $orderShippingAddress->refresh();
  292. $order->refresh();
  293. $orderItem->refresh();
  294. $this->assertModelWise([
  295. Cart::class => [
  296. $this->prepareCart($cart),
  297. ],
  298. CartItem::class => [
  299. $this->prepareCartItem($cartItem),
  300. ],
  301. CartPayment::class => [
  302. $this->prepareCartPayment($cartPayment),
  303. ],
  304. CartAddress::class => [
  305. $this->prepareAddress($cartBillingAddress),
  306. ],
  307. CartAddress::class => [
  308. $this->prepareAddress($cartShippingAddress),
  309. ],
  310. CartShippingRate::class => [
  311. $this->prepareCartShippingRate($cartShippingRate),
  312. ],
  313. CustomerAddress::class => [
  314. $this->prepareAddress($customerAddress),
  315. ],
  316. Order::class => [
  317. $this->prepareOrder($order),
  318. ],
  319. OrderItem::class => [
  320. $this->prepareOrderItem($orderItem),
  321. ],
  322. OrderAddress::class => [
  323. $this->prepareAddress($orderBillingAddress),
  324. $this->prepareAddress($orderShippingAddress),
  325. ],
  326. OrderPayment::class => [
  327. $this->prepareOrderPayment($orderPayment),
  328. ],
  329. ]);
  330. });
  331. it('should cancel the order and send the notification to the customer and admin', function () {
  332. // Arrange.
  333. Mail::fake();
  334. CoreConfig::factory()->create([
  335. 'code' => 'emails.general.notifications.emails.general.notifications.cancel_order',
  336. 'value' => 1,
  337. ]);
  338. CoreConfig::where('code', 'emails.general.notifications.emails.general.notifications.cancel_order_mail_to_admin')->update([
  339. 'value' => 1,
  340. ]);
  341. $product = (new ProductFaker([
  342. 'attributes' => [
  343. 5 => 'new',
  344. ],
  345. 'attribute_value' => [
  346. 'new' => [
  347. 'boolean_value' => true,
  348. ],
  349. ],
  350. ]))
  351. ->getSimpleProductFactory()
  352. ->create();
  353. $customer = Customer::factory()->create();
  354. $cart = Cart::factory()->create([
  355. 'customer_id' => $customer->id,
  356. 'customer_first_name' => $customer->first_name,
  357. 'customer_last_name' => $customer->last_name,
  358. 'customer_email' => $customer->email,
  359. 'is_guest' => 0,
  360. ]);
  361. $additional = [
  362. 'product_id' => $product->id,
  363. 'rating' => '0',
  364. 'is_buy_now' => '0',
  365. 'quantity' => '1',
  366. ];
  367. $cartItem = CartItem::factory()->create([
  368. 'cart_id' => $cart->id,
  369. 'product_id' => $product->id,
  370. 'sku' => $product->sku,
  371. 'quantity' => $additional['quantity'],
  372. 'name' => $product->name,
  373. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  374. 'base_price' => $price,
  375. 'total' => $convertedPrice * $additional['quantity'],
  376. 'base_total' => $price * $additional['quantity'],
  377. 'weight' => $product->weight ?? 0,
  378. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  379. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  380. 'type' => $product->type,
  381. 'additional' => $additional,
  382. ]);
  383. $customerAddress = CustomerAddress::factory()->create([
  384. 'cart_id' => $cart->id,
  385. 'customer_id' => $customer->id,
  386. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  387. ]);
  388. $cartBillingAddress = CartAddress::factory()->create([
  389. 'cart_id' => $cart->id,
  390. 'customer_id' => $customer->id,
  391. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  392. ]);
  393. $cartShippingAddress = CartAddress::factory()->create([
  394. 'cart_id' => $cart->id,
  395. 'customer_id' => $customer->id,
  396. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  397. ]);
  398. $cartPayment = CartPayment::factory()->create([
  399. 'cart_id' => $cart->id,
  400. 'method' => $paymentMethod = 'cashondelivery',
  401. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  402. ]);
  403. $cartShippingRate = CartShippingRate::factory()->create([
  404. 'carrier' => 'free',
  405. 'carrier_title' => 'Free shipping',
  406. 'method' => 'free_free',
  407. 'method_title' => 'Free Shipping',
  408. 'method_description' => 'Free Shipping',
  409. 'cart_address_id' => $cartShippingAddress->id,
  410. ]);
  411. $order = Order::factory()->create([
  412. 'cart_id' => $cart->id,
  413. 'customer_id' => $customer->id,
  414. 'customer_email' => $customer->email,
  415. 'customer_first_name' => $customer->first_name,
  416. 'customer_last_name' => $customer->last_name,
  417. ]);
  418. $orderItem = OrderItem::factory()->create([
  419. 'product_id' => $product->id,
  420. 'order_id' => $order->id,
  421. 'sku' => $product->sku,
  422. 'type' => $product->type,
  423. 'name' => $product->name,
  424. ]);
  425. $orderBillingAddress = OrderAddress::factory()->create([
  426. 'cart_id' => $cart->id,
  427. 'customer_id' => $customer->id,
  428. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  429. ]);
  430. $orderShippingAddress = OrderAddress::factory()->create([
  431. 'cart_id' => $cart->id,
  432. 'customer_id' => $customer->id,
  433. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  434. ]);
  435. $orderPayment = OrderPayment::factory()->create([
  436. 'order_id' => $order->id,
  437. ]);
  438. // Act and Assert.
  439. $this->loginAsAdmin();
  440. postJson(route('admin.sales.orders.cancel', $order->id))
  441. ->assertRedirect(route('admin.sales.orders.view', $order->id))
  442. ->isRedirection();
  443. $cart->refresh();
  444. $cartItem->refresh();
  445. $cartBillingAddress->refresh();
  446. $cartShippingAddress->refresh();
  447. $orderBillingAddress->refresh();
  448. $orderShippingAddress->refresh();
  449. $order->refresh();
  450. $orderItem->refresh();
  451. $this->assertModelWise([
  452. Cart::class => [
  453. $this->prepareCart($cart),
  454. ],
  455. CartItem::class => [
  456. $this->prepareCartItem($cartItem),
  457. ],
  458. CartPayment::class => [
  459. $this->prepareCartPayment($cartPayment),
  460. ],
  461. CartAddress::class => [
  462. $this->prepareAddress($cartBillingAddress),
  463. ],
  464. CartAddress::class => [
  465. $this->prepareAddress($cartShippingAddress),
  466. ],
  467. CartShippingRate::class => [
  468. $this->prepareCartShippingRate($cartShippingRate),
  469. ],
  470. CustomerAddress::class => [
  471. $this->prepareAddress($customerAddress),
  472. ],
  473. Order::class => [
  474. $this->prepareOrder($order),
  475. ],
  476. OrderItem::class => [
  477. $this->prepareOrderItem($orderItem),
  478. ],
  479. OrderAddress::class => [
  480. $this->prepareAddress($orderBillingAddress),
  481. $this->prepareAddress($orderShippingAddress),
  482. ],
  483. OrderPayment::class => [
  484. $this->prepareOrderPayment($orderPayment),
  485. ],
  486. ]);
  487. Mail::assertQueued(ShopOrderCanceledNotification::class);
  488. Mail::assertQueued(AdminOrderCanceledNotification::class);
  489. Mail::assertQueuedCount(2);
  490. });
  491. it('should give validation error when store the comment to the order', function () {
  492. // Arrange.
  493. $product = (new ProductFaker([
  494. 'attributes' => [
  495. 5 => 'new',
  496. ],
  497. 'attribute_value' => [
  498. 'new' => [
  499. 'boolean_value' => true,
  500. ],
  501. ],
  502. ]))
  503. ->getSimpleProductFactory()
  504. ->create();
  505. $customer = Customer::factory()->create();
  506. $cart = Cart::factory()->create([
  507. 'customer_id' => $customer->id,
  508. 'customer_first_name' => $customer->first_name,
  509. 'customer_last_name' => $customer->last_name,
  510. 'customer_email' => $customer->email,
  511. 'is_guest' => 0,
  512. ]);
  513. $additional = [
  514. 'product_id' => $product->id,
  515. 'rating' => '0',
  516. 'is_buy_now' => '0',
  517. 'quantity' => '1',
  518. ];
  519. $cartItem = CartItem::factory()->create([
  520. 'cart_id' => $cart->id,
  521. 'product_id' => $product->id,
  522. 'sku' => $product->sku,
  523. 'quantity' => $additional['quantity'],
  524. 'name' => $product->name,
  525. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  526. 'base_price' => $price,
  527. 'total' => $convertedPrice * $additional['quantity'],
  528. 'base_total' => $price * $additional['quantity'],
  529. 'weight' => $product->weight ?? 0,
  530. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  531. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  532. 'type' => $product->type,
  533. 'additional' => $additional,
  534. ]);
  535. $customerAddress = CustomerAddress::factory()->create([
  536. 'cart_id' => $cart->id,
  537. 'customer_id' => $customer->id,
  538. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  539. ]);
  540. $cartBillingAddress = CartAddress::factory()->create([
  541. 'cart_id' => $cart->id,
  542. 'customer_id' => $customer->id,
  543. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  544. ]);
  545. $cartShippingAddress = CartAddress::factory()->create([
  546. 'cart_id' => $cart->id,
  547. 'customer_id' => $customer->id,
  548. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  549. ]);
  550. $cartPayment = CartPayment::factory()->create([
  551. 'cart_id' => $cart->id,
  552. 'method' => $paymentMethod = 'cashondelivery',
  553. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  554. ]);
  555. $cartShippingRate = CartShippingRate::factory()->create([
  556. 'carrier' => 'free',
  557. 'carrier_title' => 'Free shipping',
  558. 'method' => 'free_free',
  559. 'method_title' => 'Free Shipping',
  560. 'method_description' => 'Free Shipping',
  561. 'cart_address_id' => $cartShippingAddress->id,
  562. ]);
  563. $order = Order::factory()->create([
  564. 'cart_id' => $cart->id,
  565. 'customer_id' => $customer->id,
  566. 'customer_email' => $customer->email,
  567. 'customer_first_name' => $customer->first_name,
  568. 'customer_last_name' => $customer->last_name,
  569. ]);
  570. $orderItem = OrderItem::factory()->create([
  571. 'product_id' => $product->id,
  572. 'order_id' => $order->id,
  573. 'sku' => $product->sku,
  574. 'type' => $product->type,
  575. 'name' => $product->name,
  576. ]);
  577. $orderBillingAddress = OrderAddress::factory()->create([
  578. 'cart_id' => $cart->id,
  579. 'customer_id' => $customer->id,
  580. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  581. ]);
  582. $orderShippingAddress = OrderAddress::factory()->create([
  583. 'cart_id' => $cart->id,
  584. 'customer_id' => $customer->id,
  585. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  586. ]);
  587. $orderPayment = OrderPayment::factory()->create([
  588. 'order_id' => $order->id,
  589. ]);
  590. // Act and Assert.
  591. $this->loginAsAdmin();
  592. postJson(route('admin.sales.orders.comment', $order->id))
  593. ->assertJsonValidationErrorFor('comment')
  594. ->assertUnprocessable();
  595. $cart->refresh();
  596. $cartItem->refresh();
  597. $cartBillingAddress->refresh();
  598. $cartShippingAddress->refresh();
  599. $orderBillingAddress->refresh();
  600. $orderShippingAddress->refresh();
  601. $order->refresh();
  602. $orderItem->refresh();
  603. $this->assertModelWise([
  604. Cart::class => [
  605. $this->prepareCart($cart),
  606. ],
  607. CartItem::class => [
  608. $this->prepareCartItem($cartItem),
  609. ],
  610. CartPayment::class => [
  611. $this->prepareCartPayment($cartPayment),
  612. ],
  613. CartAddress::class => [
  614. $this->prepareAddress($cartBillingAddress),
  615. ],
  616. CartAddress::class => [
  617. $this->prepareAddress($cartShippingAddress),
  618. ],
  619. CartShippingRate::class => [
  620. $this->prepareCartShippingRate($cartShippingRate),
  621. ],
  622. CustomerAddress::class => [
  623. $this->prepareAddress($customerAddress),
  624. ],
  625. Order::class => [
  626. $this->prepareOrder($order),
  627. ],
  628. OrderItem::class => [
  629. $this->prepareOrderItem($orderItem),
  630. ],
  631. OrderAddress::class => [
  632. $this->prepareAddress($orderBillingAddress),
  633. $this->prepareAddress($orderShippingAddress),
  634. ],
  635. OrderPayment::class => [
  636. $this->prepareOrderPayment($orderPayment),
  637. ],
  638. ]);
  639. });
  640. it('should comment to the order', function () {
  641. // Arrange.
  642. $product = (new ProductFaker([
  643. 'attributes' => [
  644. 5 => 'new',
  645. ],
  646. 'attribute_value' => [
  647. 'new' => [
  648. 'boolean_value' => true,
  649. ],
  650. ],
  651. ]))
  652. ->getSimpleProductFactory()
  653. ->create();
  654. $customer = Customer::factory()->create();
  655. $cart = Cart::factory()->create([
  656. 'customer_id' => $customer->id,
  657. 'customer_first_name' => $customer->first_name,
  658. 'customer_last_name' => $customer->last_name,
  659. 'customer_email' => $customer->email,
  660. 'is_guest' => 0,
  661. ]);
  662. $additional = [
  663. 'product_id' => $product->id,
  664. 'rating' => '0',
  665. 'is_buy_now' => '0',
  666. 'quantity' => '1',
  667. ];
  668. $cartItem = CartItem::factory()->create([
  669. 'cart_id' => $cart->id,
  670. 'product_id' => $product->id,
  671. 'sku' => $product->sku,
  672. 'quantity' => $additional['quantity'],
  673. 'name' => $product->name,
  674. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  675. 'base_price' => $price,
  676. 'total' => $convertedPrice * $additional['quantity'],
  677. 'base_total' => $price * $additional['quantity'],
  678. 'weight' => $product->weight ?? 0,
  679. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  680. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  681. 'type' => $product->type,
  682. 'additional' => $additional,
  683. ]);
  684. $customerAddress = CustomerAddress::factory()->create([
  685. 'cart_id' => $cart->id,
  686. 'customer_id' => $customer->id,
  687. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  688. ]);
  689. $cartBillingAddress = CartAddress::factory()->create([
  690. 'cart_id' => $cart->id,
  691. 'customer_id' => $customer->id,
  692. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  693. ]);
  694. $cartShippingAddress = CartAddress::factory()->create([
  695. 'cart_id' => $cart->id,
  696. 'customer_id' => $customer->id,
  697. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  698. ]);
  699. $cartPayment = CartPayment::factory()->create([
  700. 'cart_id' => $cart->id,
  701. 'method' => $paymentMethod = 'cashondelivery',
  702. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  703. ]);
  704. $cartShippingRate = CartShippingRate::factory()->create([
  705. 'carrier' => 'free',
  706. 'carrier_title' => 'Free shipping',
  707. 'method' => 'free_free',
  708. 'method_title' => 'Free Shipping',
  709. 'method_description' => 'Free Shipping',
  710. 'cart_address_id' => $cartShippingAddress->id,
  711. ]);
  712. $order = Order::factory()->create([
  713. 'cart_id' => $cart->id,
  714. 'customer_id' => $customer->id,
  715. 'customer_email' => $customer->email,
  716. 'customer_first_name' => $customer->first_name,
  717. 'customer_last_name' => $customer->last_name,
  718. ]);
  719. $orderItem = OrderItem::factory()->create([
  720. 'product_id' => $product->id,
  721. 'order_id' => $order->id,
  722. 'sku' => $product->sku,
  723. 'type' => $product->type,
  724. 'name' => $product->name,
  725. ]);
  726. $orderBillingAddress = OrderAddress::factory()->create([
  727. 'cart_id' => $cart->id,
  728. 'customer_id' => $customer->id,
  729. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  730. ]);
  731. $orderShippingAddress = OrderAddress::factory()->create([
  732. 'cart_id' => $cart->id,
  733. 'customer_id' => $customer->id,
  734. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  735. ]);
  736. $orderPayment = OrderPayment::factory()->create([
  737. 'order_id' => $order->id,
  738. ]);
  739. // Act and Assert.
  740. $this->loginAsAdmin();
  741. postJson(route('admin.sales.orders.comment', $order->id), [
  742. 'comment' => $comment = fake()->word(),
  743. ])
  744. ->assertRedirect(route('admin.sales.orders.view', $order->id))
  745. ->isRedirection();
  746. $cart->refresh();
  747. $cartItem->refresh();
  748. $cartBillingAddress->refresh();
  749. $cartShippingAddress->refresh();
  750. $orderBillingAddress->refresh();
  751. $orderShippingAddress->refresh();
  752. $order->refresh();
  753. $orderItem->refresh();
  754. $this->assertModelWise([
  755. Cart::class => [
  756. $this->prepareCart($cart),
  757. ],
  758. CartItem::class => [
  759. $this->prepareCartItem($cartItem),
  760. ],
  761. CartPayment::class => [
  762. $this->prepareCartPayment($cartPayment),
  763. ],
  764. CartAddress::class => [
  765. $this->prepareAddress($cartBillingAddress),
  766. ],
  767. CartAddress::class => [
  768. $this->prepareAddress($cartShippingAddress),
  769. ],
  770. CartShippingRate::class => [
  771. $this->prepareCartShippingRate($cartShippingRate),
  772. ],
  773. CustomerAddress::class => [
  774. $this->prepareAddress($customerAddress),
  775. ],
  776. Order::class => [
  777. $this->prepareOrder($order),
  778. ],
  779. OrderItem::class => [
  780. $this->prepareOrderItem($orderItem),
  781. ],
  782. OrderAddress::class => [
  783. $this->prepareAddress($orderBillingAddress),
  784. $this->prepareAddress($orderShippingAddress),
  785. ],
  786. OrderPayment::class => [
  787. $this->prepareOrderPayment($orderPayment),
  788. ],
  789. OrderComment::class => [
  790. [
  791. 'order_id' => $order->id,
  792. 'comment' => $comment,
  793. ],
  794. ],
  795. ]);
  796. });
  797. it('should comment to the order and send mail to the customer', function () {
  798. // Arrange.
  799. Mail::fake();
  800. $product = (new ProductFaker([
  801. 'attributes' => [
  802. 5 => 'new',
  803. ],
  804. 'attribute_value' => [
  805. 'new' => [
  806. 'boolean_value' => true,
  807. ],
  808. ],
  809. ]))
  810. ->getSimpleProductFactory()
  811. ->create();
  812. $customer = Customer::factory()->create();
  813. $cart = Cart::factory()->create([
  814. 'customer_id' => $customer->id,
  815. 'customer_first_name' => $customer->first_name,
  816. 'customer_last_name' => $customer->last_name,
  817. 'customer_email' => $customer->email,
  818. 'is_guest' => 0,
  819. ]);
  820. $additional = [
  821. 'product_id' => $product->id,
  822. 'rating' => '0',
  823. 'is_buy_now' => '0',
  824. 'quantity' => '1',
  825. ];
  826. $cartItem = CartItem::factory()->create([
  827. 'cart_id' => $cart->id,
  828. 'product_id' => $product->id,
  829. 'sku' => $product->sku,
  830. 'quantity' => $additional['quantity'],
  831. 'name' => $product->name,
  832. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  833. 'base_price' => $price,
  834. 'total' => $convertedPrice * $additional['quantity'],
  835. 'base_total' => $price * $additional['quantity'],
  836. 'weight' => $product->weight ?? 0,
  837. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  838. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  839. 'type' => $product->type,
  840. 'additional' => $additional,
  841. ]);
  842. $customerAddress = CustomerAddress::factory()->create([
  843. 'cart_id' => $cart->id,
  844. 'customer_id' => $customer->id,
  845. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  846. ]);
  847. $cartBillingAddress = CartAddress::factory()->create([
  848. 'cart_id' => $cart->id,
  849. 'customer_id' => $customer->id,
  850. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  851. ]);
  852. $cartShippingAddress = CartAddress::factory()->create([
  853. 'cart_id' => $cart->id,
  854. 'customer_id' => $customer->id,
  855. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  856. ]);
  857. $cartPayment = CartPayment::factory()->create([
  858. 'cart_id' => $cart->id,
  859. 'method' => $paymentMethod = 'cashondelivery',
  860. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  861. ]);
  862. $cartShippingRate = CartShippingRate::factory()->create([
  863. 'carrier' => 'free',
  864. 'carrier_title' => 'Free shipping',
  865. 'method' => 'free_free',
  866. 'method_title' => 'Free Shipping',
  867. 'method_description' => 'Free Shipping',
  868. 'cart_address_id' => $cartShippingAddress->id,
  869. ]);
  870. $order = Order::factory()->create([
  871. 'cart_id' => $cart->id,
  872. 'customer_id' => $customer->id,
  873. 'customer_email' => $customer->email,
  874. 'customer_first_name' => $customer->first_name,
  875. 'customer_last_name' => $customer->last_name,
  876. ]);
  877. $orderItem = OrderItem::factory()->create([
  878. 'product_id' => $product->id,
  879. 'order_id' => $order->id,
  880. 'sku' => $product->sku,
  881. 'type' => $product->type,
  882. 'name' => $product->name,
  883. ]);
  884. $orderBillingAddress = OrderAddress::factory()->create([
  885. 'cart_id' => $cart->id,
  886. 'customer_id' => $customer->id,
  887. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  888. ]);
  889. $orderShippingAddress = OrderAddress::factory()->create([
  890. 'cart_id' => $cart->id,
  891. 'customer_id' => $customer->id,
  892. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  893. ]);
  894. $orderPayment = OrderPayment::factory()->create([
  895. 'order_id' => $order->id,
  896. ]);
  897. // Act and Assert.
  898. $this->loginAsAdmin();
  899. postJson(route('admin.sales.orders.comment', $order->id), [
  900. 'comment' => $comment = fake()->word(),
  901. 'customer_notified' => 1,
  902. ])
  903. ->assertRedirect(route('admin.sales.orders.view', $order->id))
  904. ->isRedirection();
  905. $cart->refresh();
  906. $cartItem->refresh();
  907. $cartBillingAddress->refresh();
  908. $cartShippingAddress->refresh();
  909. $orderBillingAddress->refresh();
  910. $orderShippingAddress->refresh();
  911. $order->refresh();
  912. $orderItem->refresh();
  913. $this->assertModelWise([
  914. Cart::class => [
  915. $this->prepareCart($cart),
  916. ],
  917. CartItem::class => [
  918. $this->prepareCartItem($cartItem),
  919. ],
  920. CartPayment::class => [
  921. $this->prepareCartPayment($cartPayment),
  922. ],
  923. CartAddress::class => [
  924. $this->prepareAddress($cartBillingAddress),
  925. ],
  926. CartAddress::class => [
  927. $this->prepareAddress($cartShippingAddress),
  928. ],
  929. CartShippingRate::class => [
  930. $this->prepareCartShippingRate($cartShippingRate),
  931. ],
  932. CustomerAddress::class => [
  933. $this->prepareAddress($customerAddress),
  934. ],
  935. Order::class => [
  936. $this->prepareOrder($order),
  937. ],
  938. OrderItem::class => [
  939. $this->prepareOrderItem($orderItem),
  940. ],
  941. OrderAddress::class => [
  942. $this->prepareAddress($orderBillingAddress),
  943. $this->prepareAddress($orderShippingAddress),
  944. ],
  945. OrderPayment::class => [
  946. $this->prepareOrderPayment($orderPayment),
  947. ],
  948. OrderComment::class => [
  949. [
  950. 'order_id' => $order->id,
  951. 'comment' => $comment,
  952. 'customer_notified' => 1,
  953. ],
  954. ],
  955. ]);
  956. Mail::assertQueued(CommentedNotification::class);
  957. Mail::assertQueuedCount(1);
  958. });
  959. it('should search the order', function () {
  960. // Arrange.
  961. $product = (new ProductFaker([
  962. 'attributes' => [
  963. 5 => 'new',
  964. ],
  965. 'attribute_value' => [
  966. 'new' => [
  967. 'boolean_value' => true,
  968. ],
  969. ],
  970. ]))
  971. ->getSimpleProductFactory()
  972. ->create();
  973. $customer = Customer::factory()->create();
  974. $cart = Cart::factory()->create([
  975. 'customer_id' => $customer->id,
  976. 'customer_first_name' => $customer->first_name,
  977. 'customer_last_name' => $customer->last_name,
  978. 'customer_email' => $customer->email,
  979. 'is_guest' => 0,
  980. ]);
  981. $additional = [
  982. 'product_id' => $product->id,
  983. 'rating' => '0',
  984. 'is_buy_now' => '0',
  985. 'quantity' => '1',
  986. ];
  987. $cartItem = CartItem::factory()->create([
  988. 'cart_id' => $cart->id,
  989. 'product_id' => $product->id,
  990. 'sku' => $product->sku,
  991. 'quantity' => $additional['quantity'],
  992. 'name' => $product->name,
  993. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  994. 'base_price' => $price,
  995. 'total' => $convertedPrice * $additional['quantity'],
  996. 'base_total' => $price * $additional['quantity'],
  997. 'weight' => $product->weight ?? 0,
  998. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  999. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  1000. 'type' => $product->type,
  1001. 'additional' => $additional,
  1002. ]);
  1003. $customerAddress = CustomerAddress::factory()->create([
  1004. 'cart_id' => $cart->id,
  1005. 'customer_id' => $customer->id,
  1006. 'address_type' => CustomerAddress::ADDRESS_TYPE,
  1007. ]);
  1008. $cartBillingAddress = CartAddress::factory()->create([
  1009. 'cart_id' => $cart->id,
  1010. 'customer_id' => $customer->id,
  1011. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  1012. ]);
  1013. $cartShippingAddress = CartAddress::factory()->create([
  1014. 'cart_id' => $cart->id,
  1015. 'customer_id' => $customer->id,
  1016. 'address_type' => CartAddress::ADDRESS_TYPE_SHIPPING,
  1017. ]);
  1018. $cartPayment = CartPayment::factory()->create([
  1019. 'cart_id' => $cart->id,
  1020. 'method' => $paymentMethod = 'cashondelivery',
  1021. 'method_title' => core()->getConfigData('sales.payment_methods.'.$paymentMethod.'.title'),
  1022. ]);
  1023. $cartShippingRate = CartShippingRate::factory()->create([
  1024. 'carrier' => 'free',
  1025. 'carrier_title' => 'Free shipping',
  1026. 'method' => 'free_free',
  1027. 'method_title' => 'Free Shipping',
  1028. 'method_description' => 'Free Shipping',
  1029. 'cart_address_id' => $cartShippingAddress->id,
  1030. ]);
  1031. $order = Order::factory()->create([
  1032. 'cart_id' => $cart->id,
  1033. 'customer_id' => $customer->id,
  1034. 'customer_email' => $customer->email,
  1035. 'customer_first_name' => $customer->first_name,
  1036. 'customer_last_name' => $customer->last_name,
  1037. ]);
  1038. $orderItem = OrderItem::factory()->create([
  1039. 'product_id' => $product->id,
  1040. 'order_id' => $order->id,
  1041. 'sku' => $product->sku,
  1042. 'type' => $product->type,
  1043. 'name' => $product->name,
  1044. ]);
  1045. $orderBillingAddress = OrderAddress::factory()->create([
  1046. 'cart_id' => $cart->id,
  1047. 'customer_id' => $customer->id,
  1048. 'address_type' => OrderAddress::ADDRESS_TYPE_BILLING,
  1049. ]);
  1050. $orderShippingAddress = OrderAddress::factory()->create([
  1051. 'cart_id' => $cart->id,
  1052. 'customer_id' => $customer->id,
  1053. 'address_type' => OrderAddress::ADDRESS_TYPE_SHIPPING,
  1054. ]);
  1055. $orderPayment = OrderPayment::factory()->create([
  1056. 'order_id' => $order->id,
  1057. ]);
  1058. // Act and Assert.
  1059. $this->loginAsAdmin();
  1060. get(route('admin.sales.orders.search'), [
  1061. 'query' => fake()->randomElement(['pending', 'completed', 'processing']),
  1062. ])
  1063. ->assertOk()
  1064. ->assertJsonPath('path', route('admin.sales.orders.search'))
  1065. ->assertJsonPath('data.0.id', $order->id)
  1066. ->assertJsonPath('data.0.status', $order->status)
  1067. ->assertJsonPath('data.0.customer_email', $order->customer_email);
  1068. $cart->refresh();
  1069. $cartItem->refresh();
  1070. $cartBillingAddress->refresh();
  1071. $cartShippingAddress->refresh();
  1072. $orderBillingAddress->refresh();
  1073. $orderShippingAddress->refresh();
  1074. $order->refresh();
  1075. $orderItem->refresh();
  1076. $this->assertModelWise([
  1077. Cart::class => [
  1078. $this->prepareCart($cart),
  1079. ],
  1080. CartItem::class => [
  1081. $this->prepareCartItem($cartItem),
  1082. ],
  1083. CartPayment::class => [
  1084. $this->prepareCartPayment($cartPayment),
  1085. ],
  1086. CartAddress::class => [
  1087. $this->prepareAddress($cartBillingAddress),
  1088. ],
  1089. CartAddress::class => [
  1090. $this->prepareAddress($cartShippingAddress),
  1091. ],
  1092. CartShippingRate::class => [
  1093. $this->prepareCartShippingRate($cartShippingRate),
  1094. ],
  1095. CustomerAddress::class => [
  1096. $this->prepareAddress($customerAddress),
  1097. ],
  1098. Order::class => [
  1099. $this->prepareOrder($order),
  1100. ],
  1101. OrderItem::class => [
  1102. $this->prepareOrderItem($orderItem),
  1103. ],
  1104. OrderAddress::class => [
  1105. $this->prepareAddress($orderBillingAddress),
  1106. $this->prepareAddress($orderShippingAddress),
  1107. ],
  1108. OrderPayment::class => [
  1109. $this->prepareOrderPayment($orderPayment),
  1110. ],
  1111. ]);
  1112. });