CartTest.php 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235
  1. <?php
  2. use Webkul\Checkout\Models\Cart;
  3. use Webkul\Checkout\Models\CartAddress;
  4. use Webkul\Checkout\Models\CartItem;
  5. use Webkul\Core\Models\CoreConfig;
  6. use Webkul\Customer\Models\Customer;
  7. use Webkul\Faker\Helpers\Product as ProductFaker;
  8. use Webkul\Tax\Models\TaxCategory;
  9. use Webkul\Tax\Models\TaxMap;
  10. use Webkul\Tax\Models\TaxRate;
  11. use function Pest\Laravel\deleteJson;
  12. use function Pest\Laravel\get;
  13. use function Pest\Laravel\getJson;
  14. use function Pest\Laravel\postJson;
  15. use function Pest\Laravel\putJson;
  16. it('should display the cart items for a guest user', function () {
  17. // Arrange.
  18. $product = (new ProductFaker([
  19. 'attributes' => [
  20. 5 => 'new',
  21. 26 => 'guest_checkout',
  22. ],
  23. 'attribute_value' => [
  24. 'new' => [
  25. 'boolean_value' => true,
  26. ],
  27. 'guest_checkout' => [
  28. 'boolean_value' => true,
  29. ],
  30. ],
  31. ]))
  32. ->getSimpleProductFactory()
  33. ->create();
  34. $cart = Cart::factory()->create();
  35. $additional = [
  36. 'product_id' => $product->id,
  37. 'rating' => '0',
  38. 'is_buy_now' => '0',
  39. 'quantity' => '1',
  40. ];
  41. CartItem::factory()->create([
  42. 'cart_id' => $cart->id,
  43. 'product_id' => $product->id,
  44. 'sku' => $product->sku,
  45. 'quantity' => $additional['quantity'],
  46. 'name' => $product->name,
  47. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  48. 'base_price' => $price,
  49. 'total' => $convertedPrice * $additional['quantity'],
  50. 'base_total' => $price * $additional['quantity'],
  51. 'weight' => $product->weight ?? 0,
  52. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  53. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  54. 'type' => $product->type,
  55. 'additional' => $additional,
  56. ]);
  57. cart()->setCart($cart);
  58. cart()->collectTotals();
  59. // Act and Assert.
  60. $response = get(route('shop.api.checkout.cart.index'))
  61. ->assertOk()
  62. ->assertJsonPath('data.id', $cart->id)
  63. ->assertJsonPath('data.is_guest', $cart->is_guest)
  64. ->assertJsonPath('data.customer_id', $cart->customer_id)
  65. ->assertJsonPath('data.items_count', 1)
  66. ->assertJsonPath('data.items_qty', 1);
  67. $cart->refresh();
  68. $response->assertJsonPath('data.formatted_discount_amount', core()->currency($cart->discount_amount));
  69. $this->assertPrice(! empty($cart->tax_total) ? $cart->tax_total : 0, $response['data']['tax_total']);
  70. $this->assertPrice(! empty($cart->discount_amount) ? $cart->discount_amount : 0, $response['data']['discount_amount']);
  71. $this->assertPrice($cart->grand_total, $response['data']['grand_total']);
  72. $this->assertPrice($cart->sub_total, $response['data']['sub_total']);
  73. foreach ($cart->items as $key => $cartItem) {
  74. $response->assertJsonPath('data.items.'.$key.'.id', $cartItem->id);
  75. $response->assertJsonPath('data.items.'.$key.'.quantity', $cartItem->quantity);
  76. $response->assertJsonPath('data.items.'.$key.'.type', $cartItem->type);
  77. $response->assertJsonPath('data.items.'.$key.'.name', $cartItem->name);
  78. $response->assertJsonPath('data.items.'.$key.'.price', $cartItem->price);
  79. $response->assertJsonPath('data.items.'.$key.'.formatted_price', core()->formatPrice($cartItem->price));
  80. $response->assertJsonPath('data.items.'.$key.'.total', $cartItem->total);
  81. $response->assertJsonPath('data.items.'.$key.'.formatted_total', core()->formatPrice($cartItem->total));
  82. $response->assertJsonPath('data.items.'.$key.'.options', $cartItem->options ?? []);
  83. $response->assertJsonPath('data.items.'.$key.'.product_url_key', $cartItem->product->url_key);
  84. }
  85. });
  86. it('should display the cart items for a customer', function () {
  87. // Arrange.
  88. $product = (new ProductFaker([
  89. 'attributes' => [
  90. 5 => 'new',
  91. ],
  92. 'attribute_value' => [
  93. 'new' => [
  94. 'boolean_value' => true,
  95. ],
  96. ],
  97. ]))
  98. ->getSimpleProductFactory()
  99. ->create();
  100. $customer = Customer::factory()->create();
  101. $cart = Cart::factory()->create([
  102. 'customer_id' => $customer->id,
  103. 'customer_first_name' => $customer->first_name,
  104. 'customer_last_name' => $customer->last_name,
  105. 'customer_email' => $customer->email,
  106. 'is_guest' => 0,
  107. ]);
  108. $additional = [
  109. 'product_id' => $product->id,
  110. 'rating' => '0',
  111. 'is_buy_now' => '0',
  112. 'quantity' => '1',
  113. ];
  114. CartItem::factory()->create([
  115. 'cart_id' => $cart->id,
  116. 'product_id' => $product->id,
  117. 'sku' => $product->sku,
  118. 'quantity' => $additional['quantity'],
  119. 'name' => $product->name,
  120. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  121. 'base_price' => $price,
  122. 'total' => $convertedPrice * $additional['quantity'],
  123. 'base_total' => $price * $additional['quantity'],
  124. 'weight' => $product->weight ?? 0,
  125. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  126. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  127. 'type' => $product->type,
  128. 'additional' => $additional,
  129. ]);
  130. cart()->setCart($cart);
  131. cart()->collectTotals();
  132. // Act and Assert.
  133. $this->loginAsCustomer($customer);
  134. $response = get(route('shop.api.checkout.cart.index'))
  135. ->assertOk()
  136. ->assertJsonPath('data.id', $cart->id)
  137. ->assertJsonPath('data.is_guest', $cart->is_guest)
  138. ->assertJsonPath('data.customer_id', $cart->customer_id)
  139. ->assertJsonPath('data.items_count', 1)
  140. ->assertJsonPath('data.items_qty', 1);
  141. $cart->refresh();
  142. $response->assertJsonPath('data.formatted_discount_amount', core()->currency($cart->discount_amount));
  143. $this->assertPrice(! empty($cart->tax_total) ? $cart->tax_total : 0, $response['data']['tax_total']);
  144. $this->assertPrice(! empty($cart->discount_amount) ? $cart->discount_amount : 0, $response['data']['discount_amount']);
  145. $this->assertPrice($cart->grand_total, $response['data']['grand_total']);
  146. $this->assertPrice($cart->sub_total, $response['data']['sub_total']);
  147. foreach ($cart->items as $key => $cartItem) {
  148. $response->assertJsonPath('data.items.'.$key.'.id', $cartItem->id);
  149. $response->assertJsonPath('data.items.'.$key.'.quantity', $cartItem->quantity);
  150. $response->assertJsonPath('data.items.'.$key.'.type', $cartItem->type);
  151. $response->assertJsonPath('data.items.'.$key.'.name', $cartItem->name);
  152. $response->assertJsonPath('data.items.'.$key.'.price', $cartItem->price);
  153. $response->assertJsonPath('data.items.'.$key.'.formatted_price', core()->formatPrice($cartItem->price));
  154. $response->assertJsonPath('data.items.'.$key.'.total', $cartItem->total);
  155. $response->assertJsonPath('data.items.'.$key.'.formatted_total', core()->formatPrice($cartItem->total));
  156. $response->assertJsonPath('data.items.'.$key.'.options', $cartItem->options ?? []);
  157. $response->assertJsonPath('data.items.'.$key.'.product_url_key', $cartItem->product->url_key);
  158. }
  159. });
  160. it('should fails the validation error when the cart item id not provided when remove product items into the cart for a guest user', function () {
  161. // Arrange.
  162. $product = (new ProductFaker([
  163. 'attributes' => [
  164. 5 => 'new',
  165. 26 => 'guest_checkout',
  166. ],
  167. 'attribute_value' => [
  168. 'new' => [
  169. 'boolean_value' => true,
  170. ],
  171. 'guest_checkout' => [
  172. 'boolean_value' => true,
  173. ],
  174. ],
  175. ]))
  176. ->getSimpleProductFactory()
  177. ->create();
  178. $cart = Cart::factory()->create();
  179. $additional = [
  180. 'product_id' => $product->id,
  181. 'rating' => '0',
  182. 'is_buy_now' => '0',
  183. 'quantity' => '1',
  184. ];
  185. CartItem::factory()->create([
  186. 'cart_id' => $cart->id,
  187. 'product_id' => $product->id,
  188. 'sku' => $product->sku,
  189. 'quantity' => $additional['quantity'],
  190. 'name' => $product->name,
  191. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  192. 'base_price' => $price,
  193. 'total' => $convertedPrice * $additional['quantity'],
  194. 'base_total' => $price * $additional['quantity'],
  195. 'weight' => $product->weight ?? 0,
  196. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  197. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  198. 'type' => $product->type,
  199. 'additional' => $additional,
  200. ]);
  201. // Act and Assert.
  202. deleteJson(route('shop.api.checkout.cart.destroy'))
  203. ->assertJsonValidationErrorFor('cart_item_id')
  204. ->assertUnprocessable();
  205. });
  206. it('should fails the validation error when the cart item id not provided when remove product items into the cart for a customer', function () {
  207. // Arrange.
  208. $product = (new ProductFaker([
  209. 'attributes' => [
  210. 5 => 'new',
  211. ],
  212. 'attribute_value' => [
  213. 'new' => [
  214. 'boolean_value' => true,
  215. ],
  216. ],
  217. ]))
  218. ->getSimpleProductFactory()
  219. ->create();
  220. $customer = Customer::factory()->create();
  221. $cart = Cart::factory()->create([
  222. 'customer_id' => $customer->id,
  223. 'customer_first_name' => $customer->first_name,
  224. 'customer_last_name' => $customer->last_name,
  225. 'customer_email' => $customer->email,
  226. 'is_guest' => 0,
  227. ]);
  228. $additional = [
  229. 'product_id' => $product->id,
  230. 'rating' => '0',
  231. 'is_buy_now' => '0',
  232. 'quantity' => '1',
  233. ];
  234. CartItem::factory()->create([
  235. 'cart_id' => $cart->id,
  236. 'product_id' => $product->id,
  237. 'sku' => $product->sku,
  238. 'quantity' => $additional['quantity'],
  239. 'name' => $product->name,
  240. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  241. 'base_price' => $price,
  242. 'total' => $convertedPrice * $additional['quantity'],
  243. 'base_total' => $price * $additional['quantity'],
  244. 'weight' => $product->weight ?? 0,
  245. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  246. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  247. 'type' => $product->type,
  248. 'additional' => $additional,
  249. ]);
  250. cart()->setCart($cart);
  251. // Act and Assert.
  252. $this->loginAsCustomer($customer);
  253. deleteJson(route('shop.api.checkout.cart.destroy'))
  254. ->assertJsonValidationErrorFor('cart_item_id')
  255. ->assertUnprocessable();
  256. });
  257. it('should fails the validation error when the wrong cart item id provided when remove product items to the cart for a guest user', function () {
  258. // Arrange.
  259. $product = (new ProductFaker([
  260. 'attributes' => [
  261. 5 => 'new',
  262. 26 => 'guest_checkout',
  263. ],
  264. 'attribute_value' => [
  265. 'new' => [
  266. 'boolean_value' => true,
  267. ],
  268. 'guest_checkout' => [
  269. 'boolean_value' => true,
  270. ],
  271. ],
  272. ]))
  273. ->getSimpleProductFactory()
  274. ->create();
  275. $cart = Cart::factory()->create();
  276. $additional = [
  277. 'product_id' => $product->id,
  278. 'rating' => '0',
  279. 'is_buy_now' => '0',
  280. 'quantity' => '1',
  281. ];
  282. CartItem::factory()->create([
  283. 'cart_id' => $cart->id,
  284. 'product_id' => $product->id,
  285. 'sku' => $product->sku,
  286. 'quantity' => $additional['quantity'],
  287. 'name' => $product->name,
  288. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  289. 'base_price' => $price,
  290. 'total' => $convertedPrice * $additional['quantity'],
  291. 'base_total' => $price * $additional['quantity'],
  292. 'weight' => $product->weight ?? 0,
  293. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  294. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  295. 'type' => $product->type,
  296. 'additional' => $additional,
  297. ]);
  298. // Act and Assert.
  299. deleteJson(route('shop.api.checkout.cart.destroy'), [
  300. 'cart_item_id' => 'WRONG_ID',
  301. ])
  302. ->assertJsonValidationErrorFor('cart_item_id')
  303. ->assertUnprocessable();
  304. });
  305. it('should fails the validation error when the wrong cart item id provided when remove product items to the cart for a customer', function () {
  306. // Arrange.
  307. $product = (new ProductFaker([
  308. 'attributes' => [
  309. 5 => 'new',
  310. ],
  311. 'attribute_value' => [
  312. 'new' => [
  313. 'boolean_value' => true,
  314. ],
  315. ],
  316. ]))
  317. ->getSimpleProductFactory()
  318. ->create();
  319. $customer = Customer::factory()->create();
  320. $cart = Cart::factory()->create([
  321. 'customer_id' => $customer->id,
  322. 'customer_first_name' => $customer->first_name,
  323. 'customer_last_name' => $customer->last_name,
  324. 'customer_email' => $customer->email,
  325. 'is_guest' => 0,
  326. ]);
  327. $additional = [
  328. 'product_id' => $product->id,
  329. 'rating' => '0',
  330. 'is_buy_now' => '0',
  331. 'quantity' => '1',
  332. ];
  333. CartItem::factory()->create([
  334. 'cart_id' => $cart->id,
  335. 'product_id' => $product->id,
  336. 'sku' => $product->sku,
  337. 'quantity' => $additional['quantity'],
  338. 'name' => $product->name,
  339. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  340. 'base_price' => $price,
  341. 'total' => $convertedPrice * $additional['quantity'],
  342. 'base_total' => $price * $additional['quantity'],
  343. 'weight' => $product->weight ?? 0,
  344. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  345. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  346. 'type' => $product->type,
  347. 'additional' => $additional,
  348. ]);
  349. $this->loginAsCustomer($customer);
  350. // Act and Assert.
  351. deleteJson(route('shop.api.checkout.cart.destroy'), [
  352. 'cart_item_id' => 'WRONG_ID',
  353. ])
  354. ->assertJsonValidationErrorFor('cart_item_id')
  355. ->assertUnprocessable();
  356. });
  357. it('should remove only one product item from the cart for the guest user', function () {
  358. // Arrange.
  359. $product = (new ProductFaker([
  360. 'attributes' => [
  361. 5 => 'new',
  362. 26 => 'guest_checkout',
  363. ],
  364. 'attribute_value' => [
  365. 'new' => [
  366. 'boolean_value' => true,
  367. ],
  368. 'guest_checkout' => [
  369. 'boolean_value' => true,
  370. ],
  371. ],
  372. ]))
  373. ->getSimpleProductFactory()
  374. ->create();
  375. $cart = Cart::factory()->create();
  376. $additional = [
  377. 'product_id' => $product->id,
  378. 'rating' => '0',
  379. 'is_buy_now' => '0',
  380. 'quantity' => '1',
  381. ];
  382. $cartItem = CartItem::factory()->create([
  383. 'cart_id' => $cart->id,
  384. 'product_id' => $product->id,
  385. 'sku' => $product->sku,
  386. 'quantity' => $additional['quantity'],
  387. 'name' => $product->name,
  388. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  389. 'base_price' => $price,
  390. 'total' => $convertedPrice * $additional['quantity'],
  391. 'base_total' => $price * $additional['quantity'],
  392. 'weight' => $product->weight ?? 0,
  393. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  394. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  395. 'type' => $product->type,
  396. 'additional' => $additional,
  397. ]);
  398. cart()->collectTotals();
  399. cart()->setCart($cart);
  400. // Act and Assert.
  401. $response = deleteJson(route('shop.api.checkout.cart.destroy', [
  402. 'cart_item_id' => $cartItem->id,
  403. ]))
  404. ->assertOk()
  405. ->assertJsonPath('data.id', $cart->id)
  406. ->assertJsonPath('data.items_count', 0)
  407. ->assertJsonPath('data.items_qty', 0)
  408. ->assertJsonPath('message', trans('shop::app.checkout.cart.success-remove'));
  409. $this->assertDatabaseMissing('cart_items', [
  410. 'id' => $cartItem->id,
  411. ]);
  412. $this->assertDatabaseHas('cart', [
  413. 'id' => $cart->id,
  414. ]);
  415. $cart->refresh();
  416. $this->assertEquals(0, $cart->items_count);
  417. $this->assertEquals(0, $cart->items_qty);
  418. $this->assertEquals(0, $cart->grand_total);
  419. });
  420. it('should remove only one product item from the cart for the customer', function () {
  421. // Arrange.
  422. $product = (new ProductFaker([
  423. 'attributes' => [
  424. 5 => 'new',
  425. ],
  426. 'attribute_value' => [
  427. 'new' => [
  428. 'boolean_value' => true,
  429. ],
  430. ],
  431. ]))
  432. ->getSimpleProductFactory()
  433. ->create();
  434. $customer = Customer::factory()->create();
  435. $cart = Cart::factory()->create([
  436. 'customer_id' => $customer->id,
  437. 'customer_first_name' => $customer->first_name,
  438. 'customer_last_name' => $customer->last_name,
  439. 'customer_email' => $customer->email,
  440. 'is_guest' => 0,
  441. ]);
  442. $additional = [
  443. 'product_id' => $product->id,
  444. 'rating' => '0',
  445. 'is_buy_now' => '0',
  446. 'quantity' => '1',
  447. ];
  448. $cartItem = CartItem::factory()->create([
  449. 'cart_id' => $cart->id,
  450. 'product_id' => $product->id,
  451. 'sku' => $product->sku,
  452. 'quantity' => $additional['quantity'],
  453. 'name' => $product->name,
  454. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  455. 'base_price' => $price,
  456. 'total' => $convertedPrice * $additional['quantity'],
  457. 'base_total' => $price * $additional['quantity'],
  458. 'weight' => $product->weight ?? 0,
  459. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  460. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  461. 'type' => $product->type,
  462. 'additional' => $additional,
  463. ]);
  464. cart()->collectTotals();
  465. cart()->setCart($cart);
  466. // Act and Assert.
  467. $this->loginAsCustomer($customer);
  468. $response = deleteJson(route('shop.api.checkout.cart.destroy', [
  469. 'cart_item_id' => $cartItem->id,
  470. ]))
  471. ->assertOk()
  472. ->assertJsonPath('data.id', $cart->id)
  473. ->assertJsonPath('data.items_count', 0)
  474. ->assertJsonPath('data.items_qty', 0)
  475. ->assertJsonPath('message', trans('shop::app.checkout.cart.success-remove'));
  476. $this->assertDatabaseMissing('cart_items', [
  477. 'id' => $cartItem->id,
  478. ]);
  479. $this->assertDatabaseHas('cart', [
  480. 'id' => $cart->id,
  481. ]);
  482. $cart->refresh();
  483. $this->assertEquals(0, $cart->items_count);
  484. $this->assertEquals(0, $cart->items_qty);
  485. $this->assertEquals(0, $cart->grand_total);
  486. });
  487. it('should only remove one product from the cart for now the cart will contains two products for a guest user', function () {
  488. // Arrange.
  489. $products = (new ProductFaker([
  490. 'attributes' => [
  491. 5 => 'new',
  492. 26 => 'guest_checkout',
  493. ],
  494. 'attribute_value' => [
  495. 'new' => [
  496. 'boolean_value' => true,
  497. ],
  498. 'guest_checkout' => [
  499. 'boolean_value' => true,
  500. ],
  501. ],
  502. ]))
  503. ->getSimpleProductFactory()
  504. ->count(2)
  505. ->create();
  506. [$product1, $product2] = $products;
  507. $cart = Cart::factory()->create();
  508. $additional1 = [
  509. 'product_id' => $product1->id,
  510. 'rating' => '0',
  511. 'is_buy_now' => '0',
  512. 'quantity' => '1',
  513. ];
  514. $additional2 = [
  515. 'product_id' => $product2->id,
  516. 'rating' => '0',
  517. 'is_buy_now' => '0',
  518. 'quantity' => '1',
  519. ];
  520. $cartItem1 = CartItem::factory()->create([
  521. 'cart_id' => $cart->id,
  522. 'product_id' => $product1->id,
  523. 'sku' => $product1->sku,
  524. 'quantity' => $additional1['quantity'],
  525. 'name' => $product1->name,
  526. 'price' => $convertedPrice = core()->convertPrice($price = $product1->price),
  527. 'base_price' => $price,
  528. 'total' => $convertedPrice * $additional1['quantity'],
  529. 'base_total' => $price * $additional1['quantity'],
  530. 'weight' => $product1->weight ?? 0,
  531. 'total_weight' => ($product1->weight ?? 0) * $additional1['quantity'],
  532. 'base_total_weight' => ($product1->weight ?? 0) * $additional1['quantity'],
  533. 'type' => $product1->type,
  534. 'additional' => $additional1,
  535. ]);
  536. $cartItem2 = CartItem::factory()->create([
  537. 'cart_id' => $cart->id,
  538. 'product_id' => $product2->id,
  539. 'sku' => $product2->sku,
  540. 'quantity' => $additional2['quantity'],
  541. 'name' => $product2->name,
  542. 'price' => $convertedPrice = core()->convertPrice($price = $product2->price),
  543. 'base_price' => $price,
  544. 'total' => $convertedPrice * $additional2['quantity'],
  545. 'base_total' => $price * $additional2['quantity'],
  546. 'weight' => $product2->weight ?? 0,
  547. 'total_weight' => ($product2->weight ?? 0) * $additional2['quantity'],
  548. 'base_total_weight' => ($product2->weight ?? 0) * $additional2['quantity'],
  549. 'type' => $product2->type,
  550. 'additional' => $additional2,
  551. ]);
  552. cart()->collectTotals();
  553. cart()->setCart($cart);
  554. // Act and Assert.
  555. $response = deleteJson(route('shop.api.checkout.cart.destroy'), [
  556. 'cart_item_id' => $cartItem1->id,
  557. ])
  558. ->assertOk()
  559. ->assertJsonPath('data.id', $cart->id)
  560. ->assertJsonPath('data.is_guest', $cart->is_guest)
  561. ->assertJsonPath('data.customer_id', $cart->customer_id)
  562. ->assertJsonPath('data.items_count', 1)
  563. ->assertJsonPath('data.items_qty', 1)
  564. ->assertJsonPath('message', trans('shop::app.checkout.cart.success-remove'));
  565. $cart->refresh();
  566. $cartItem2->refresh();
  567. $response->assertJsonPath('data.formatted_discount_amount', core()->currency($cart->discount_amount));
  568. $this->assertPrice(! empty($cart->tax_total) ? $cart->tax_total : 0, $response['data']['tax_total']);
  569. $this->assertPrice(! empty($cart->discount_amount) ? $cart->discount_amount : 0, $response['data']['discount_amount']);
  570. $this->assertPrice($cart->grand_total, $response['data']['grand_total']);
  571. $this->assertPrice($cart->sub_total, $response['data']['sub_total']);
  572. foreach ($cart->items as $key => $cartItem) {
  573. $response->assertJsonPath('data.items.'.$key.'.id', $cartItem->id);
  574. $response->assertJsonPath('data.items.'.$key.'.quantity', $cartItem->quantity);
  575. $response->assertJsonPath('data.items.'.$key.'.type', $cartItem->type);
  576. $response->assertJsonPath('data.items.'.$key.'.name', $cartItem->name);
  577. $response->assertJsonPath('data.items.'.$key.'.price', $cartItem->price);
  578. $response->assertJsonPath('data.items.'.$key.'.formatted_price', core()->formatPrice($cartItem->price));
  579. $response->assertJsonPath('data.items.'.$key.'.total', $cartItem->total);
  580. $response->assertJsonPath('data.items.'.$key.'.formatted_total', core()->formatPrice($cartItem->total));
  581. $response->assertJsonPath('data.items.'.$key.'.options', $cartItem->options ?? []);
  582. $response->assertJsonPath('data.items.'.$key.'.product_url_key', $cartItem->product->url_key);
  583. }
  584. $this->assertDatabaseMissing('cart_items', [
  585. 'id' => $cartItem1->id,
  586. ]);
  587. $cart->refresh();
  588. $cartItem->refresh();
  589. $this->assertModelWise([
  590. Cart::class => [
  591. $this->prepareCart($cart),
  592. ],
  593. CartItem::class => [
  594. $this->prepareCartItem($cartItem),
  595. ],
  596. ]);
  597. });
  598. it('should only remove one product from the cart for now the cart will contains two products for a customer', function () {
  599. // Arrange.
  600. $products = (new ProductFaker([
  601. 'attributes' => [
  602. 5 => 'new',
  603. ],
  604. 'attribute_value' => [
  605. 'new' => [
  606. 'boolean_value' => true,
  607. ],
  608. ],
  609. ]))
  610. ->getSimpleProductFactory()
  611. ->count(2)
  612. ->create();
  613. [$product1, $product2] = $products;
  614. $customer = Customer::factory()->create();
  615. $cart = Cart::factory()->create([
  616. 'customer_id' => $customer->id,
  617. 'customer_first_name' => $customer->first_name,
  618. 'customer_last_name' => $customer->last_name,
  619. 'customer_email' => $customer->email,
  620. 'is_guest' => 0,
  621. ]);
  622. $additional1 = [
  623. 'product_id' => $product1->id,
  624. 'rating' => '0',
  625. 'is_buy_now' => '0',
  626. 'quantity' => '1',
  627. ];
  628. $additional2 = [
  629. 'product_id' => $product2->id,
  630. 'rating' => '0',
  631. 'is_buy_now' => '0',
  632. 'quantity' => '1',
  633. ];
  634. $cartItem1 = CartItem::factory()->create([
  635. 'cart_id' => $cart->id,
  636. 'product_id' => $product1->id,
  637. 'sku' => $product1->sku,
  638. 'quantity' => $additional1['quantity'],
  639. 'name' => $product1->name,
  640. 'price' => $convertedPrice = core()->convertPrice($price = $product1->price),
  641. 'base_price' => $price,
  642. 'total' => $convertedPrice * $additional1['quantity'],
  643. 'base_total' => $price * $additional1['quantity'],
  644. 'weight' => $product1->weight ?? 0,
  645. 'total_weight' => ($product1->weight ?? 0) * $additional1['quantity'],
  646. 'base_total_weight' => ($product1->weight ?? 0) * $additional1['quantity'],
  647. 'type' => $product1->type,
  648. 'additional' => $additional1,
  649. ]);
  650. $cartItem2 = CartItem::factory()->create([
  651. 'cart_id' => $cart->id,
  652. 'product_id' => $product2->id,
  653. 'sku' => $product2->sku,
  654. 'quantity' => $additional2['quantity'],
  655. 'name' => $product2->name,
  656. 'price' => $convertedPrice = core()->convertPrice($price = $product2->price),
  657. 'base_price' => $price,
  658. 'total' => $convertedPrice * $additional2['quantity'],
  659. 'base_total' => $price * $additional2['quantity'],
  660. 'weight' => $product2->weight ?? 0,
  661. 'total_weight' => ($product2->weight ?? 0) * $additional2['quantity'],
  662. 'base_total_weight' => ($product2->weight ?? 0) * $additional2['quantity'],
  663. 'type' => $product2->type,
  664. 'additional' => $additional2,
  665. ]);
  666. cart()->collectTotals();
  667. cart()->setCart($cart);
  668. // Act and Assert.
  669. $this->loginAsCustomer();
  670. $response = deleteJson(route('shop.api.checkout.cart.destroy'), [
  671. 'cart_item_id' => $cartItem1->id,
  672. ])
  673. ->assertOk()
  674. ->assertJsonPath('data.id', $cart->id)
  675. ->assertJsonPath('data.is_guest', $cart->is_guest)
  676. ->assertJsonPath('data.customer_id', $cart->customer_id)
  677. ->assertJsonPath('data.items_count', $cart->items_count)
  678. ->assertJsonPath('data.items_qty', 1)
  679. ->assertJsonPath('message', trans('shop::app.checkout.cart.success-remove'));
  680. $cart->refresh();
  681. $cartItem2->refresh();
  682. $response->assertJsonPath('data.formatted_discount_amount', core()->currency($cart->discount_amount));
  683. $this->assertPrice(! empty($cart->tax_total) ? $cart->tax_total : 0, $response['data']['tax_total']);
  684. $this->assertPrice(! empty($cart->discount_amount) ? $cart->discount_amount : 0, $response['data']['discount_amount']);
  685. $this->assertPrice($cart->grand_total, $response['data']['grand_total']);
  686. $this->assertPrice($cart->sub_total, $response['data']['sub_total']);
  687. foreach ($cart->items as $key => $cartItem) {
  688. $response->assertJsonPath('data.items.'.$key.'.id', $cartItem->id);
  689. $response->assertJsonPath('data.items.'.$key.'.quantity', $cartItem->quantity);
  690. $response->assertJsonPath('data.items.'.$key.'.type', $cartItem->type);
  691. $response->assertJsonPath('data.items.'.$key.'.name', $cartItem->name);
  692. $response->assertJsonPath('data.items.'.$key.'.price', $cartItem->price);
  693. $response->assertJsonPath('data.items.'.$key.'.formatted_price', core()->formatPrice($cartItem->price));
  694. $response->assertJsonPath('data.items.'.$key.'.total', $cartItem->total);
  695. $response->assertJsonPath('data.items.'.$key.'.formatted_total', core()->formatPrice($cartItem->total));
  696. $response->assertJsonPath('data.items.'.$key.'.options', $cartItem->options ?? []);
  697. $response->assertJsonPath('data.items.'.$key.'.product_url_key', $cartItem->product->url_key);
  698. }
  699. $this->assertDatabaseMissing('cart_items', [
  700. 'id' => $cartItem1->id,
  701. ]);
  702. $cart->refresh();
  703. $cartItem2->refresh();
  704. $this->assertModelWise([
  705. Cart::class => [
  706. $this->prepareCart($cart),
  707. ],
  708. CartItem::class => [
  709. $this->prepareCartItem($cartItem2),
  710. ],
  711. ]);
  712. });
  713. it('should not allow a user to remove another user\'s cart item', function () {
  714. // Arrange.
  715. $product = (new ProductFaker([
  716. 'attributes' => [
  717. 5 => 'new',
  718. 26 => 'guest_checkout',
  719. ],
  720. 'attribute_value' => [
  721. 'new' => [
  722. 'boolean_value' => true,
  723. ],
  724. 'guest_checkout' => [
  725. 'boolean_value' => true,
  726. ],
  727. ],
  728. ]))
  729. ->getSimpleProductFactory()
  730. ->create();
  731. // Create customer A with a cart A
  732. $customerA = Customer::factory()->create();
  733. $cartA = Cart::factory()->create([
  734. 'customer_id' => $customerA->id,
  735. 'is_guest' => 0,
  736. 'customer_first_name' => $customerA->first_name,
  737. 'customer_last_name' => $customerA->last_name,
  738. 'customer_email' => $customerA->email,
  739. ]);
  740. $additional = [
  741. 'product_id' => $product->id,
  742. 'rating' => '0',
  743. 'is_buy_now' => '0',
  744. 'quantity' => '1',
  745. ];
  746. $cartItemA = CartItem::factory()->create([
  747. 'cart_id' => $cartA->id,
  748. 'product_id' => $product->id,
  749. 'sku' => $product->sku,
  750. 'quantity' => $additional['quantity'],
  751. 'name' => $product->name,
  752. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  753. 'base_price' => $price,
  754. 'total' => $convertedPrice * $additional['quantity'],
  755. 'base_total' => $price * $additional['quantity'],
  756. 'weight' => $product->weight ?? 0,
  757. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  758. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  759. 'type' => $product->type,
  760. 'additional' => $additional,
  761. ]);
  762. // Create customer B and login as customer B
  763. $customerB = Customer::factory()->create();
  764. $cartB = Cart::factory()->create([
  765. 'customer_id' => $customerB->id,
  766. 'is_guest' => 0,
  767. 'customer_first_name' => $customerB->first_name,
  768. 'customer_last_name' => $customerB->last_name,
  769. 'customer_email' => $customerB->email,
  770. ]);
  771. cart()->setCart($cartB);
  772. // Act and Assert.
  773. $this->loginAsCustomer($customerB);
  774. // Customer B tries to delete Customer A's cart item
  775. deleteJson(route('shop.api.checkout.cart.destroy'), [
  776. 'cart_item_id' => $cartItemA->id,
  777. ])
  778. ->assertOk();
  779. // Assert that Customer A's cart item still exists (was not deleted)
  780. $this->assertDatabaseHas('cart_items', [
  781. 'id' => $cartItemA->id,
  782. 'cart_id' => $cartA->id,
  783. ]);
  784. // Assert that Customer A's cart still exists
  785. $this->assertDatabaseHas('cart', [
  786. 'id' => $cartA->id,
  787. 'customer_id' => $customerA->id,
  788. ]);
  789. });
  790. it('should not allow a guest user to remove another guest user\'s cart item', function () {
  791. // Arrange.
  792. $product = (new ProductFaker([
  793. 'attributes' => [
  794. 5 => 'new',
  795. 26 => 'guest_checkout',
  796. ],
  797. 'attribute_value' => [
  798. 'new' => [
  799. 'boolean_value' => true,
  800. ],
  801. 'guest_checkout' => [
  802. 'boolean_value' => true,
  803. ],
  804. ],
  805. ]))
  806. ->getSimpleProductFactory()
  807. ->create();
  808. // Create Guest A's cart
  809. $cartA = Cart::factory()->create([
  810. 'is_guest' => 1,
  811. ]);
  812. $additional = [
  813. 'product_id' => $product->id,
  814. 'rating' => '0',
  815. 'is_buy_now' => '0',
  816. 'quantity' => '1',
  817. ];
  818. $cartItemA = CartItem::factory()->create([
  819. 'cart_id' => $cartA->id,
  820. 'product_id' => $product->id,
  821. 'sku' => $product->sku,
  822. 'quantity' => $additional['quantity'],
  823. 'name' => $product->name,
  824. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  825. 'base_price' => $price,
  826. 'total' => $convertedPrice * $additional['quantity'],
  827. 'base_total' => $price * $additional['quantity'],
  828. 'weight' => $product->weight ?? 0,
  829. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  830. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  831. 'type' => $product->type,
  832. 'additional' => $additional,
  833. ]);
  834. // Create Guest B's cart and set it as active session
  835. $cartB = Cart::factory()->create([
  836. 'is_guest' => 1,
  837. ]);
  838. cart()->setCart($cartB);
  839. // Act and Assert.
  840. // Guest B tries to delete Guest A's cart item
  841. deleteJson(route('shop.api.checkout.cart.destroy'), [
  842. 'cart_item_id' => $cartItemA->id,
  843. ])
  844. ->assertOk();
  845. // Assert that Guest A's cart item still exists (was not deleted)
  846. $this->assertDatabaseHas('cart_items', [
  847. 'id' => $cartItemA->id,
  848. 'cart_id' => $cartA->id,
  849. ]);
  850. // Assert that Guest A's cart still exists
  851. $this->assertDatabaseHas('cart', [
  852. 'id' => $cartA->id,
  853. 'is_guest' => 1,
  854. ]);
  855. });
  856. it('should not allow a guest user to remove a customer\'s cart item', function () {
  857. // Arrange.
  858. $product = (new ProductFaker([
  859. 'attributes' => [
  860. 5 => 'new',
  861. 26 => 'guest_checkout',
  862. ],
  863. 'attribute_value' => [
  864. 'new' => [
  865. 'boolean_value' => true,
  866. ],
  867. 'guest_checkout' => [
  868. 'boolean_value' => true,
  869. ],
  870. ],
  871. ]))
  872. ->getSimpleProductFactory()
  873. ->create();
  874. // Create customer's cart
  875. $customer = Customer::factory()->create();
  876. $customerCart = Cart::factory()->create([
  877. 'customer_id' => $customer->id,
  878. 'is_guest' => 0,
  879. 'customer_first_name' => $customer->first_name,
  880. 'customer_last_name' => $customer->last_name,
  881. 'customer_email' => $customer->email,
  882. ]);
  883. $additional = [
  884. 'product_id' => $product->id,
  885. 'rating' => '0',
  886. 'is_buy_now' => '0',
  887. 'quantity' => '1',
  888. ];
  889. $customerCartItem = CartItem::factory()->create([
  890. 'cart_id' => $customerCart->id,
  891. 'product_id' => $product->id,
  892. 'sku' => $product->sku,
  893. 'quantity' => $additional['quantity'],
  894. 'name' => $product->name,
  895. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  896. 'base_price' => $price,
  897. 'total' => $convertedPrice * $additional['quantity'],
  898. 'base_total' => $price * $additional['quantity'],
  899. 'weight' => $product->weight ?? 0,
  900. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  901. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  902. 'type' => $product->type,
  903. 'additional' => $additional,
  904. ]);
  905. // Create guest cart and set it as active session
  906. $guestCart = Cart::factory()->create([
  907. 'is_guest' => 1,
  908. ]);
  909. cart()->setCart($guestCart);
  910. // Act and Assert.
  911. // Guest tries to delete customer's cart item
  912. deleteJson(route('shop.api.checkout.cart.destroy'), [
  913. 'cart_item_id' => $customerCartItem->id,
  914. ])
  915. ->assertOk();
  916. // Assert that customer's cart item still exists (was not deleted)
  917. $this->assertDatabaseHas('cart_items', [
  918. 'id' => $customerCartItem->id,
  919. 'cart_id' => $customerCart->id,
  920. ]);
  921. // Assert that customer's cart still exists
  922. $this->assertDatabaseHas('cart', [
  923. 'id' => $customerCart->id,
  924. 'customer_id' => $customer->id,
  925. 'is_guest' => 0,
  926. ]);
  927. });
  928. it('should not allow a customer to remove a guest user\'s cart item', function () {
  929. // Arrange.
  930. $product = (new ProductFaker([
  931. 'attributes' => [
  932. 5 => 'new',
  933. 26 => 'guest_checkout',
  934. ],
  935. 'attribute_value' => [
  936. 'new' => [
  937. 'boolean_value' => true,
  938. ],
  939. 'guest_checkout' => [
  940. 'boolean_value' => true,
  941. ],
  942. ],
  943. ]))
  944. ->getSimpleProductFactory()
  945. ->create();
  946. // Create guest cart
  947. $guestCart = Cart::factory()->create([
  948. 'is_guest' => 1,
  949. ]);
  950. $additional = [
  951. 'product_id' => $product->id,
  952. 'rating' => '0',
  953. 'is_buy_now' => '0',
  954. 'quantity' => '1',
  955. ];
  956. $guestCartItem = CartItem::factory()->create([
  957. 'cart_id' => $guestCart->id,
  958. 'product_id' => $product->id,
  959. 'sku' => $product->sku,
  960. 'quantity' => $additional['quantity'],
  961. 'name' => $product->name,
  962. 'price' => $convertedPrice = core()->convertPrice($price = $product->price),
  963. 'base_price' => $price,
  964. 'total' => $convertedPrice * $additional['quantity'],
  965. 'base_total' => $price * $additional['quantity'],
  966. 'weight' => $product->weight ?? 0,
  967. 'total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  968. 'base_total_weight' => ($product->weight ?? 0) * $additional['quantity'],
  969. 'type' => $product->type,
  970. 'additional' => $additional,
  971. ]);
  972. // Create customer and login
  973. $customer = Customer::factory()->create();
  974. $customerCart = Cart::factory()->create([
  975. 'customer_id' => $customer->id,
  976. 'is_guest' => 0,
  977. 'customer_first_name' => $customer->first_name,
  978. 'customer_last_name' => $customer->last_name,
  979. 'customer_email' => $customer->email,
  980. ]);
  981. cart()->setCart($customerCart);
  982. // Act and Assert.
  983. $this->loginAsCustomer($customer);
  984. // Customer tries to delete guest's cart item
  985. deleteJson(route('shop.api.checkout.cart.destroy'), [
  986. 'cart_item_id' => $guestCartItem->id,
  987. ])
  988. ->assertOk();
  989. // Assert that guest's cart item still exists (was not deleted)
  990. $this->assertDatabaseHas('cart_items', [
  991. 'id' => $guestCartItem->id,
  992. 'cart_id' => $guestCart->id,
  993. ]);
  994. // Assert that guest's cart still exists
  995. $this->assertDatabaseHas('cart', [
  996. 'id' => $guestCart->id,
  997. 'is_guest' => 1,
  998. ]);
  999. });
  1000. it('should remove all products from the cart for a guest user', function () {
  1001. // Arrange.
  1002. $products = (new ProductFaker([
  1003. 'attributes' => [
  1004. 5 => 'new',
  1005. 26 => 'guest_checkout',
  1006. ],
  1007. 'attribute_value' => [
  1008. 'new' => [
  1009. 'boolean_value' => true,
  1010. ],
  1011. 'guest_checkout' => [
  1012. 'boolean_value' => true,
  1013. ],
  1014. ],
  1015. ]))
  1016. ->getSimpleProductFactory()
  1017. ->count(2)
  1018. ->create();
  1019. [$product1, $product2] = $products;
  1020. $cart = Cart::factory()->create();
  1021. $additional1 = [
  1022. 'product_id' => $product1->id,
  1023. 'rating' => '0',
  1024. 'is_buy_now' => '0',
  1025. 'quantity' => '1',
  1026. ];
  1027. $additional2 = [
  1028. 'product_id' => $product2->id,
  1029. 'rating' => '0',
  1030. 'is_buy_now' => '0',
  1031. 'quantity' => '1',
  1032. ];
  1033. $cartItem1 = CartItem::factory()->create([
  1034. 'cart_id' => $cart->id,
  1035. 'product_id' => $product1->id,
  1036. 'sku' => $product1->sku,
  1037. 'quantity' => $additional1['quantity'],
  1038. 'name' => $product1->name,
  1039. 'price' => $convertedPrice = core()->convertPrice($price = $product1->price),
  1040. 'base_price' => $price,
  1041. 'total' => $convertedPrice * $additional1['quantity'],
  1042. 'base_total' => $price * $additional1['quantity'],
  1043. 'weight' => $product1->weight ?? 0,
  1044. 'total_weight' => ($product1->weight ?? 0) * $additional1['quantity'],
  1045. 'base_total_weight' => ($product1->weight ?? 0) * $additional1['quantity'],
  1046. 'type' => $product1->type,
  1047. 'additional' => $additional1,
  1048. ]);
  1049. $cartItem2 = CartItem::factory()->create([
  1050. 'cart_id' => $cart->id,
  1051. 'product_id' => $product2->id,
  1052. 'sku' => $product2->sku,
  1053. 'quantity' => $additional2['quantity'],
  1054. 'name' => $product2->name,
  1055. 'price' => $convertedPrice = core()->convertPrice($price = $product2->price),
  1056. 'base_price' => $price,
  1057. 'total' => $convertedPrice * $additional2['quantity'],
  1058. 'base_total' => $price * $additional2['quantity'],
  1059. 'weight' => $product2->weight ?? 0,
  1060. 'total_weight' => ($product2->weight ?? 0) * $additional2['quantity'],
  1061. 'base_total_weight' => ($product2->weight ?? 0) * $additional2['quantity'],
  1062. 'type' => $product2->type,
  1063. 'additional' => $additional2,
  1064. ]);
  1065. cart()->collectTotals();
  1066. cart()->setCart($cart);
  1067. // Act and Assert.
  1068. deleteJson(route('shop.api.checkout.cart.destroy_selected'), [
  1069. 'ids' => [$cartItem1->id, $cartItem2->id],
  1070. ]);
  1071. $this->assertDatabaseMissing('cart_items', [
  1072. 'id' => $cartItem1->id,
  1073. ]);
  1074. $this->assertDatabaseMissing('cart_items', [
  1075. 'id' => $cartItem2->id,
  1076. ]);
  1077. });
  1078. it('should remove all products from the cart for a customer', function () {
  1079. // Arrange.
  1080. $products = (new ProductFaker([
  1081. 'attributes' => [
  1082. 5 => 'new',
  1083. ],
  1084. 'attribute_value' => [
  1085. 'new' => [
  1086. 'boolean_value' => true,
  1087. ],
  1088. ],
  1089. ]))
  1090. ->getSimpleProductFactory()
  1091. ->count(2)
  1092. ->create();
  1093. [$product1, $product2] = $products;
  1094. $customer = Customer::factory()->create();
  1095. $cart = Cart::factory()->create([
  1096. 'customer_id' => $customer->id,
  1097. 'customer_first_name' => $customer->first_name,
  1098. 'customer_last_name' => $customer->last_name,
  1099. 'customer_email' => $customer->email,
  1100. 'is_guest' => 0,
  1101. ]);
  1102. $additional1 = [
  1103. 'product_id' => $product1->id,
  1104. 'rating' => '0',
  1105. 'is_buy_now' => '0',
  1106. 'quantity' => '1',
  1107. ];
  1108. $additional2 = [
  1109. 'product_id' => $product2->id,
  1110. 'rating' => '0',
  1111. 'is_buy_now' => '0',
  1112. 'quantity' => '1',
  1113. ];
  1114. $cartItem1 = CartItem::factory()->create([
  1115. 'cart_id' => $cart->id,
  1116. 'product_id' => $product1->id,
  1117. 'sku' => $product1->sku,
  1118. 'quantity' => $additional1['quantity'],
  1119. 'name' => $product1->name,
  1120. 'price' => $convertedPrice = core()->convertPrice($price = $product1->price),
  1121. 'base_price' => $price,
  1122. 'total' => $convertedPrice * $additional1['quantity'],
  1123. 'base_total' => $price * $additional1['quantity'],
  1124. 'weight' => $product1->weight ?? 0,
  1125. 'total_weight' => ($product1->weight ?? 0) * $additional1['quantity'],
  1126. 'base_total_weight' => ($product1->weight ?? 0) * $additional1['quantity'],
  1127. 'type' => $product1->type,
  1128. 'additional' => $additional1,
  1129. ]);
  1130. $cartItem2 = CartItem::factory()->create([
  1131. 'cart_id' => $cart->id,
  1132. 'product_id' => $product2->id,
  1133. 'sku' => $product2->sku,
  1134. 'quantity' => $additional2['quantity'],
  1135. 'name' => $product2->name,
  1136. 'price' => $convertedPrice = core()->convertPrice($price = $product2->price),
  1137. 'base_price' => $price,
  1138. 'total' => $convertedPrice * $additional2['quantity'],
  1139. 'base_total' => $price * $additional2['quantity'],
  1140. 'weight' => $product2->weight ?? 0,
  1141. 'total_weight' => ($product2->weight ?? 0) * $additional2['quantity'],
  1142. 'base_total_weight' => ($product2->weight ?? 0) * $additional2['quantity'],
  1143. 'type' => $product2->type,
  1144. 'additional' => $additional2,
  1145. ]);
  1146. cart()->collectTotals();
  1147. cart()->setCart($cart);
  1148. // Act and Assert.
  1149. $this->loginAsCustomer();
  1150. deleteJson(route('shop.api.checkout.cart.destroy_selected'), [
  1151. 'ids' => [$cartItem1->id, $cartItem2->id],
  1152. ]);
  1153. $this->assertDatabaseMissing('cart_items', [
  1154. 'id' => $cartItem1->id,
  1155. ]);
  1156. $this->assertDatabaseMissing('cart_items', [
  1157. 'id' => $cartItem2->id,
  1158. ]);
  1159. });
  1160. it('should update cart quantities for guest user', function () {
  1161. // Arrange.
  1162. $products = (new ProductFaker([
  1163. 'attributes' => [
  1164. 5 => 'new',
  1165. 26 => 'guest_checkout',
  1166. ],
  1167. 'attribute_value' => [
  1168. 'new' => [
  1169. 'boolean_value' => true,
  1170. ],
  1171. 'guest_checkout' => [
  1172. 'boolean_value' => true,
  1173. ],
  1174. ],
  1175. ]))
  1176. ->getSimpleProductFactory()
  1177. ->count(2)
  1178. ->create();
  1179. [$product1, $product2] = $products;
  1180. $cart = Cart::factory()->create();
  1181. $additional1 = [
  1182. 'product_id' => $product1->id,
  1183. 'rating' => '0',
  1184. 'is_buy_now' => '0',
  1185. 'quantity' => '1',
  1186. ];
  1187. $additional2 = [
  1188. 'product_id' => $product2->id,
  1189. 'rating' => '0',
  1190. 'is_buy_now' => '0',
  1191. 'quantity' => '1',
  1192. ];
  1193. $cartItem1 = CartItem::factory()->create([
  1194. 'cart_id' => $cart->id,
  1195. 'product_id' => $product1->id,
  1196. 'sku' => $product1->sku,
  1197. 'quantity' => $additional1['quantity'],
  1198. 'name' => $product1->name,
  1199. 'price' => $convertedPrice = core()->convertPrice($price = $product1->price),
  1200. 'base_price' => $price,
  1201. 'total' => $convertedPrice * $additional1['quantity'],
  1202. 'base_total' => $price * $additional1['quantity'],
  1203. 'weight' => $product1->weight ?? 0,
  1204. 'total_weight' => ($product1->weight ?? 0) * $additional1['quantity'],
  1205. 'base_total_weight' => ($product1->weight ?? 0) * $additional1['quantity'],
  1206. 'type' => $product1->type,
  1207. 'additional' => $additional1,
  1208. ]);
  1209. $cartItem2 = CartItem::factory()->create([
  1210. 'cart_id' => $cart->id,
  1211. 'product_id' => $product2->id,
  1212. 'sku' => $product2->sku,
  1213. 'quantity' => $additional2['quantity'],
  1214. 'name' => $product2->name,
  1215. 'price' => $convertedPrice = core()->convertPrice($price = $product2->price),
  1216. 'base_price' => $price,
  1217. 'total' => $convertedPrice * $additional2['quantity'],
  1218. 'base_total' => $price * $additional2['quantity'],
  1219. 'weight' => $product2->weight ?? 0,
  1220. 'total_weight' => ($product2->weight ?? 0) * $additional2['quantity'],
  1221. 'base_total_weight' => ($product2->weight ?? 0) * $additional2['quantity'],
  1222. 'type' => $product2->type,
  1223. 'additional' => $additional2,
  1224. ]);
  1225. cart()->collectTotals();
  1226. cart()->setCart($cart);
  1227. // Act and Assert.
  1228. $response = putJson(route('shop.api.checkout.cart.update'), [
  1229. 'qty' => $data = [
  1230. $cartItem1->id => rand(2, 10),
  1231. $cartItem2->id => rand(2, 10),
  1232. ],
  1233. ])
  1234. ->assertOk()
  1235. ->assertJsonPath('data.id', $cart->id)
  1236. ->assertJsonPath('data.is_guest', $cart->is_guest)
  1237. ->assertJsonPath('data.customer_id', $cart->customer_id)
  1238. ->assertJsonPath('data.items_count', 2)
  1239. ->assertJsonPath('data.items_qty', array_sum($data))
  1240. ->assertJsonPath('message', trans('shop::app.checkout.cart.index.quantity-update'));
  1241. $cart->refresh();
  1242. $cartItem1->refresh();
  1243. $cartItem2->refresh();
  1244. $response->assertJsonPath('data.formatted_discount_amount', core()->currency($cart->discount_amount));
  1245. $this->assertPrice(! empty($cart->tax_total) ? $cart->tax_total : 0, $response['data']['tax_total']);
  1246. $this->assertPrice(! empty($cart->discount_amount) ? $cart->discount_amount : 0, $response['data']['discount_amount']);
  1247. $this->assertPrice($cart->grand_total, $response['data']['grand_total']);
  1248. $this->assertPrice($cart->sub_total, $response['data']['sub_total']);
  1249. $cart->refresh();
  1250. $cartItem1->refresh();
  1251. $cartItem2->refresh();
  1252. $this->assertModelWise([
  1253. CartItem::class => [
  1254. $this->prepareCartItem($cartItem1),
  1255. $this->prepareCartItem($cartItem2),
  1256. ],
  1257. ]);
  1258. foreach ($cart->items as $cartItem) {
  1259. $this->assertModelWise([
  1260. CartItem::class => [
  1261. $this->prepareCartItem($cartItem),
  1262. ],
  1263. ]);
  1264. }
  1265. });
  1266. it('should update cart quantities for customer', function () {
  1267. // Arrange.
  1268. $products = (new ProductFaker([
  1269. 'attributes' => [
  1270. 5 => 'new',
  1271. ],
  1272. 'attribute_value' => [
  1273. 'new' => [
  1274. 'boolean_value' => true,
  1275. ],
  1276. ],
  1277. ]))
  1278. ->getSimpleProductFactory()
  1279. ->count(2)
  1280. ->create();
  1281. [$product1, $product2] = $products;
  1282. $customer = Customer::factory()->create();
  1283. $cart = Cart::factory()->create([
  1284. 'customer_id' => $customer->id,
  1285. 'customer_first_name' => $customer->first_name,
  1286. 'customer_last_name' => $customer->last_name,
  1287. 'customer_email' => $customer->email,
  1288. 'is_guest' => 0,
  1289. ]);
  1290. $additional1 = [
  1291. 'product_id' => $product1->id,
  1292. 'rating' => '0',
  1293. 'is_buy_now' => '0',
  1294. 'quantity' => '1',
  1295. ];
  1296. $additional2 = [
  1297. 'product_id' => $product2->id,
  1298. 'rating' => '0',
  1299. 'is_buy_now' => '0',
  1300. 'quantity' => '1',
  1301. ];
  1302. $cartItem1 = CartItem::factory()->create([
  1303. 'cart_id' => $cart->id,
  1304. 'product_id' => $product1->id,
  1305. 'sku' => $product1->sku,
  1306. 'quantity' => $additional1['quantity'],
  1307. 'name' => $product1->name,
  1308. 'price' => $convertedPrice = core()->convertPrice($price = $product1->price),
  1309. 'base_price' => $price,
  1310. 'total' => $convertedPrice * $additional1['quantity'],
  1311. 'base_total' => $price * $additional1['quantity'],
  1312. 'weight' => $product1->weight ?? 0,
  1313. 'total_weight' => ($product1->weight ?? 0) * $additional1['quantity'],
  1314. 'base_total_weight' => ($product1->weight ?? 0) * $additional1['quantity'],
  1315. 'type' => $product1->type,
  1316. 'additional' => $additional1,
  1317. ]);
  1318. $cartItem2 = CartItem::factory()->create([
  1319. 'cart_id' => $cart->id,
  1320. 'product_id' => $product2->id,
  1321. 'sku' => $product2->sku,
  1322. 'quantity' => $additional2['quantity'],
  1323. 'name' => $product2->name,
  1324. 'price' => $convertedPrice = core()->convertPrice($price = $product2->price),
  1325. 'base_price' => $price,
  1326. 'total' => $convertedPrice * $additional2['quantity'],
  1327. 'base_total' => $price * $additional2['quantity'],
  1328. 'weight' => $product2->weight ?? 0,
  1329. 'total_weight' => ($product2->weight ?? 0) * $additional2['quantity'],
  1330. 'base_total_weight' => ($product2->weight ?? 0) * $additional2['quantity'],
  1331. 'type' => $product2->type,
  1332. 'additional' => $additional2,
  1333. ]);
  1334. cart()->setCart($cart);
  1335. cart()->collectTotals();
  1336. // Act and Assert.
  1337. $this->loginAsCustomer();
  1338. $response = putJson(route('shop.api.checkout.cart.update'), [
  1339. 'qty' => $data = [
  1340. $cartItem1->id => rand(2, 10),
  1341. $cartItem2->id => rand(2, 10),
  1342. ],
  1343. ])
  1344. ->assertOk()
  1345. ->assertJsonPath('data.id', $cart->id)
  1346. ->assertJsonPath('data.is_guest', $cart->is_guest)
  1347. ->assertJsonPath('data.customer_id', $cart->customer_id)
  1348. ->assertJsonPath('data.items_count', 2)
  1349. ->assertJsonPath('data.items_qty', array_sum($data))
  1350. ->assertJsonPath('message', trans('shop::app.checkout.cart.index.quantity-update'));
  1351. $cart->refresh();
  1352. $cartItem1->refresh();
  1353. $cartItem2->refresh();
  1354. $response->assertJsonPath('data.formatted_discount_amount', core()->currency($cart->discount_amount));
  1355. $this->assertPrice(! empty($cart->tax_total) ? $cart->tax_total : 0, $response['data']['tax_total']);
  1356. $this->assertPrice(! empty($cart->discount_amount) ? $cart->discount_amount : 0, $response['data']['discount_amount']);
  1357. $this->assertPrice($cart->grand_total, $response['data']['grand_total']);
  1358. $this->assertPrice($cart->sub_total, $response['data']['sub_total']);
  1359. $this->assertModelWise([
  1360. CartItem::class => [
  1361. $this->prepareCartItem($cartItem1),
  1362. $this->prepareCartItem($cartItem2),
  1363. ],
  1364. ]);
  1365. foreach ($cart->items as $cartItem) {
  1366. $this->assertModelWise([
  1367. CartItem::class => [
  1368. $this->prepareCartItem($cartItem),
  1369. ],
  1370. ]);
  1371. }
  1372. });
  1373. it('should fails the validation error when the product id not provided when add a simple product to the cart', function () {
  1374. // Arrange.
  1375. (new ProductFaker([
  1376. 'attributes' => [
  1377. 5 => 'new',
  1378. 6 => 'featured',
  1379. 11 => 'price',
  1380. 26 => 'guest_checkout',
  1381. ],
  1382. 'attribute_value' => [
  1383. 'new' => [
  1384. 'boolean_value' => true,
  1385. ],
  1386. 'featured' => [
  1387. 'boolean_value' => true,
  1388. ],
  1389. 'price' => [
  1390. 'float_value' => rand(1000, 5000),
  1391. ],
  1392. 'guest_checkout' => [
  1393. 'boolean_value' => true,
  1394. ],
  1395. ],
  1396. ]))->getSimpleProductFactory()->create();
  1397. // Act and Assert.
  1398. postJson(route('shop.api.checkout.cart.store', [
  1399. 'quantity' => rand(1, 10),
  1400. ]))
  1401. ->assertJsonValidationErrorFor('product_id')
  1402. ->assertUnprocessable();
  1403. });
  1404. it('should add a simple product to the cart for guest user', function () {
  1405. // Arrange.
  1406. $product = (new ProductFaker([
  1407. 'attributes' => [
  1408. 5 => 'new',
  1409. 6 => 'featured',
  1410. 11 => 'price',
  1411. 26 => 'guest_checkout',
  1412. ],
  1413. 'attribute_value' => [
  1414. 'new' => [
  1415. 'boolean_value' => true,
  1416. ],
  1417. 'featured' => [
  1418. 'boolean_value' => true,
  1419. ],
  1420. 'price' => [
  1421. 'float_value' => rand(1000, 5000),
  1422. ],
  1423. 'guest_checkout' => [
  1424. 'boolean_value' => true,
  1425. ],
  1426. ],
  1427. ]))->getSimpleProductFactory()->create();
  1428. // Act and Assert.
  1429. $response = postJson(route('shop.api.checkout.cart.store', [
  1430. 'product_id' => $product->id,
  1431. 'quantity' => $quantity = rand(1, 10),
  1432. ]))
  1433. ->assertOk()
  1434. ->assertJsonPath('data.items_count', 1)
  1435. ->assertJsonPath('data.is_guest', 1)
  1436. ->assertJsonPath('data.customer_id', null)
  1437. ->assertJsonPath('data.items_qty', $quantity)
  1438. ->assertJsonPath('data.tax_total', 0)
  1439. ->assertJsonPath('data.discount_amount', 0)
  1440. ->assertJsonPath('data.coupon_code', null)
  1441. ->assertJsonPath('data.items.0.type', $product->type)
  1442. ->assertJsonPath('data.items.0.name', $product->name)
  1443. ->assertJsonPath('data.items.0.quantity', $quantity)
  1444. ->assertJsonPath('data.billing_address', null)
  1445. ->assertJsonPath('data.shipping_address', null)
  1446. ->assertJsonPath('data.have_stockable_items', true)
  1447. ->assertJsonPath('data.payment_method', null)
  1448. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'));
  1449. $this->assertPrice($product->price, $response['data']['items'][0]['price']);
  1450. $this->assertPrice($product->price * $quantity, $response['data']['grand_total']);
  1451. $this->assertPrice($product->price * $quantity, $response['data']['sub_total']);
  1452. });
  1453. it('should add a simple product to the cart for customer', function () {
  1454. // Arrange.
  1455. $product = (new ProductFaker([
  1456. 'attributes' => [
  1457. 5 => 'new',
  1458. 6 => 'featured',
  1459. 11 => 'price',
  1460. ],
  1461. 'attribute_value' => [
  1462. 'new' => [
  1463. 'boolean_value' => true,
  1464. ],
  1465. 'featured' => [
  1466. 'boolean_value' => true,
  1467. ],
  1468. 'price' => [
  1469. 'float_value' => rand(1000, 5000),
  1470. ],
  1471. ],
  1472. ]))->getSimpleProductFactory()->create();
  1473. // Act and Assert.
  1474. $customer = $this->loginAsCustomer();
  1475. $response = postJson(route('shop.api.checkout.cart.store', [
  1476. 'product_id' => $product->id,
  1477. 'quantity' => $quantity = rand(1, 10),
  1478. ]))
  1479. ->assertOk()
  1480. ->assertJsonPath('data.items_count', 1)
  1481. ->assertJsonPath('data.is_guest', 0)
  1482. ->assertJsonPath('data.customer_id', $customer->id)
  1483. ->assertJsonPath('data.items_qty', $quantity)
  1484. ->assertJsonPath('data.tax_total', 0)
  1485. ->assertJsonPath('data.discount_amount', 0)
  1486. ->assertJsonPath('data.coupon_code', null)
  1487. ->assertJsonPath('data.items.0.type', $product->type)
  1488. ->assertJsonPath('data.items.0.name', $product->name)
  1489. ->assertJsonPath('data.items.0.quantity', $quantity)
  1490. ->assertJsonPath('data.billing_address', null)
  1491. ->assertJsonPath('data.shipping_address', null)
  1492. ->assertJsonPath('data.have_stockable_items', true)
  1493. ->assertJsonPath('data.payment_method', null)
  1494. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'));
  1495. $this->assertPrice($product->price, $response['data']['items'][0]['price']);
  1496. $this->assertPrice($product->price * $quantity, $response['data']['grand_total']);
  1497. $this->assertPrice($product->price * $quantity, $response['data']['sub_total']);
  1498. });
  1499. it('should fails the validation error when the product id not provided add a bundle product to the cart', function () {
  1500. // Arrange.
  1501. $product = (new ProductFaker([
  1502. 'attributes' => [
  1503. 5 => 'new',
  1504. 6 => 'featured',
  1505. 11 => 'price',
  1506. 26 => 'guest_checkout',
  1507. ],
  1508. 'attribute_value' => [
  1509. 'new' => [
  1510. 'boolean_value' => true,
  1511. ],
  1512. 'featured' => [
  1513. 'boolean_value' => true,
  1514. ],
  1515. 'price' => [
  1516. 'float_value' => rand(1000, 5000),
  1517. ],
  1518. 'guest_checkout' => [
  1519. 'boolean_value' => true,
  1520. ],
  1521. ],
  1522. ]))->getBundleProductFactory()->create();
  1523. $bundleOptions = [
  1524. 'bundle_option_quantities' => [],
  1525. 'bundle_options' => [],
  1526. ];
  1527. $grandTotal = 0;
  1528. $product->load('bundle_options.product');
  1529. foreach ($product->bundle_options as $bundleOption) {
  1530. $grandTotal += $bundleOption->product->price;
  1531. $bundleOptions['bundle_option_quantities'][$bundleOption->id] = 1;
  1532. $bundleOptions['bundle_options'][$bundleOption->id] = [$bundleOption->id];
  1533. }
  1534. // Act and Assert.
  1535. postJson(route('shop.api.checkout.cart.store', [
  1536. 'quantity' => 1,
  1537. 'is_buy_now' => '0',
  1538. 'rating' => '0',
  1539. 'bundle_option_qty' => $bundleOptions['bundle_option_quantities'],
  1540. 'bundle_options' => $bundleOptions['bundle_options'],
  1541. ]))
  1542. ->assertJsonValidationErrorFor('product_id')
  1543. ->assertUnprocessable();
  1544. });
  1545. it('should add a bundle product to the cart for guest user', function () {
  1546. // Arrange.
  1547. $product = (new ProductFaker([
  1548. 'attributes' => [
  1549. 5 => 'new',
  1550. 6 => 'featured',
  1551. 11 => 'price',
  1552. 26 => 'guest_checkout',
  1553. ],
  1554. 'attribute_value' => [
  1555. 'new' => [
  1556. 'boolean_value' => true,
  1557. ],
  1558. 'featured' => [
  1559. 'boolean_value' => true,
  1560. ],
  1561. 'price' => [
  1562. 'float_value' => rand(1000, 5000),
  1563. ],
  1564. 'guest_checkout' => [
  1565. 'boolean_value' => true,
  1566. ],
  1567. ],
  1568. ]))->getBundleProductFactory()->create();
  1569. $bundleOptions = [
  1570. 'bundle_option_quantities' => [],
  1571. 'bundle_options' => [],
  1572. ];
  1573. $grandTotal = 0;
  1574. $product->load('bundle_options.product');
  1575. foreach ($product->bundle_options as $bundleOption) {
  1576. $grandTotal += $bundleOption->product->price;
  1577. $bundleOptions['bundle_option_quantities'][$bundleOption->id] = 1;
  1578. $bundleOptions['bundle_options'][$bundleOption->id] = [$bundleOption->id];
  1579. }
  1580. // Act and Assert.
  1581. $response = postJson(route('shop.api.checkout.cart.store', [
  1582. 'product_id' => $product->id,
  1583. 'quantity' => 1,
  1584. 'is_buy_now' => '0',
  1585. 'rating' => '0',
  1586. 'bundle_option_qty' => $bundleOptions['bundle_option_quantities'],
  1587. 'bundle_options' => $bundleOptions['bundle_options'],
  1588. ]))
  1589. ->assertOk()
  1590. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'))
  1591. ->assertJsonPath('data.items_qty', 1)
  1592. ->assertJsonPath('data.items_count', 1)
  1593. ->assertJsonPath('data.items.0.quantity', 1)
  1594. ->assertJsonPath('data.items.0.type', $product->type)
  1595. ->assertJsonPath('data.items.0.name', $product->name)
  1596. ->assertJsonPath('data.is_guest', 1)
  1597. ->assertJsonPath('data.customer_id', null)
  1598. ->assertJsonPath('data.tax_total', 0)
  1599. ->assertJsonPath('data.discount_amount', 0)
  1600. ->assertJsonPath('data.coupon_code', null)
  1601. ->assertJsonPath('data.billing_address', null)
  1602. ->assertJsonPath('data.shipping_address', null)
  1603. ->assertJsonPath('data.have_stockable_items', true)
  1604. ->assertJsonPath('data.payment_method', null)
  1605. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'));
  1606. $this->assertPrice($grandTotal, $response['data']['grand_total']);
  1607. $this->assertPrice($grandTotal, $response['data']['sub_total']);
  1608. });
  1609. it('should add a bundle product to the cart for customer', function () {
  1610. // Arrange.
  1611. $product = (new ProductFaker([
  1612. 'attributes' => [
  1613. 5 => 'new',
  1614. 6 => 'featured',
  1615. 11 => 'price',
  1616. ],
  1617. 'attribute_value' => [
  1618. 'new' => [
  1619. 'boolean_value' => true,
  1620. ],
  1621. 'featured' => [
  1622. 'boolean_value' => true,
  1623. ],
  1624. 'price' => [
  1625. 'float_value' => rand(1000, 5000),
  1626. ],
  1627. ],
  1628. ]))->getBundleProductFactory()->create();
  1629. $bundleOptions = [
  1630. 'bundle_option_quantities' => [],
  1631. 'bundle_options' => [],
  1632. ];
  1633. $grandTotal = 0;
  1634. $product->load('bundle_options.product');
  1635. foreach ($product->bundle_options as $bundleOption) {
  1636. $grandTotal += $bundleOption->product->price;
  1637. $bundleOptions['bundle_option_quantities'][$bundleOption->id] = 1;
  1638. $bundleOptions['bundle_options'][$bundleOption->id] = [$bundleOption->id];
  1639. }
  1640. // Act and Assert.
  1641. $customer = $this->loginAsCustomer();
  1642. $response = postJson(route('shop.api.checkout.cart.store', [
  1643. 'product_id' => $product->id,
  1644. 'quantity' => 1,
  1645. 'is_buy_now' => '0',
  1646. 'rating' => '0',
  1647. 'bundle_option_qty' => $bundleOptions['bundle_option_quantities'],
  1648. 'bundle_options' => $bundleOptions['bundle_options'],
  1649. ]))
  1650. ->assertOk()
  1651. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'))
  1652. ->assertJsonPath('data.items_qty', 1)
  1653. ->assertJsonPath('data.items_count', 1)
  1654. ->assertJsonPath('data.items.0.quantity', 1)
  1655. ->assertJsonPath('data.items.0.type', $product->type)
  1656. ->assertJsonPath('data.items.0.name', $product->name)
  1657. ->assertJsonPath('data.is_guest', 0)
  1658. ->assertJsonPath('data.customer_id', $customer->id)
  1659. ->assertJsonPath('data.tax_total', 0)
  1660. ->assertJsonPath('data.discount_amount', 0)
  1661. ->assertJsonPath('data.coupon_code', null)
  1662. ->assertJsonPath('data.billing_address', null)
  1663. ->assertJsonPath('data.shipping_address', null)
  1664. ->assertJsonPath('data.have_stockable_items', true)
  1665. ->assertJsonPath('data.payment_method', null)
  1666. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'));
  1667. $this->assertPrice($grandTotal, $response['data']['grand_total']);
  1668. $this->assertPrice($grandTotal, $response['data']['sub_total']);
  1669. });
  1670. it('should fails the validation when the product id not provided when add a configurable product to the cart', function () {
  1671. // Arrange.
  1672. $product = (new ProductFaker([
  1673. 'attributes' => [
  1674. 5 => 'new',
  1675. 6 => 'featured',
  1676. 11 => 'price',
  1677. 26 => 'guest_checkout',
  1678. ],
  1679. 'attribute_value' => [
  1680. 'new' => [
  1681. 'boolean_value' => true,
  1682. ],
  1683. 'featured' => [
  1684. 'boolean_value' => true,
  1685. ],
  1686. 'price' => [
  1687. 'float_value' => rand(1000, 2000),
  1688. ],
  1689. 'guest_checkout' => [
  1690. 'boolean_value' => true,
  1691. ],
  1692. ],
  1693. ]))->getConfigurableProductFactory()->create();
  1694. $childProduct = $product->variants()->first();
  1695. // Act and Assert.
  1696. postJson(route('shop.api.checkout.cart.store'), [
  1697. 'selected_configurable_option' => $childProduct->id,
  1698. 'is_buy_now' => '0',
  1699. 'rating' => '0',
  1700. 'quantity' => '1',
  1701. 'super_attribute' => [
  1702. 23 => '1',
  1703. 24 => '7',
  1704. ],
  1705. ])
  1706. ->assertJsonValidationErrorFor('product_id')
  1707. ->assertUnprocessable();
  1708. });
  1709. it('should add a configurable product to the cart for guest user', function () {
  1710. // Arrange.
  1711. $product = (new ProductFaker([
  1712. 'attributes' => [
  1713. 5 => 'new',
  1714. 6 => 'featured',
  1715. 11 => 'price',
  1716. 26 => 'guest_checkout',
  1717. ],
  1718. 'attribute_value' => [
  1719. 'new' => [
  1720. 'boolean_value' => true,
  1721. ],
  1722. 'featured' => [
  1723. 'boolean_value' => true,
  1724. ],
  1725. 'price' => [
  1726. 'float_value' => rand(1000, 2000),
  1727. ],
  1728. 'guest_checkout' => [
  1729. 'boolean_value' => true,
  1730. ],
  1731. ],
  1732. ]))->getConfigurableProductFactory()->create();
  1733. $childProduct = $product->variants()->first();
  1734. // Act and Assert.
  1735. $response = postJson(route('shop.api.checkout.cart.store'), [
  1736. 'selected_configurable_option' => $childProduct->id,
  1737. 'product_id' => $product->id,
  1738. 'is_buy_now' => '0',
  1739. 'rating' => '0',
  1740. 'quantity' => '1',
  1741. 'super_attribute' => [
  1742. 23 => '1',
  1743. 24 => '7',
  1744. ],
  1745. ])
  1746. ->assertOk()
  1747. ->assertJsonPath('data.items_qty', 1)
  1748. ->assertJsonPath('data.items_count', 1)
  1749. ->assertJsonPath('data.items.0.quantity', 1)
  1750. ->assertJsonPath('data.items.0.type', $product->type)
  1751. ->assertJsonPath('data.items.0.name', $product->name)
  1752. ->assertJsonPath('data.is_guest', 1)
  1753. ->assertJsonPath('data.discount_amount', 0)
  1754. ->assertJsonPath('data.tax_total', 0)
  1755. ->assertJsonPath('data.have_stockable_items', true)
  1756. ->assertJsonPath('data.customer_id', null)
  1757. ->assertJsonPath('data.coupon_code', null)
  1758. ->assertJsonPath('data.billing_address', null)
  1759. ->assertJsonPath('data.shipping_address', null)
  1760. ->assertJsonPath('data.payment_method', null)
  1761. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'));
  1762. $this->assertPrice($childProduct->price, $response['data']['grand_total']);
  1763. $this->assertPrice($childProduct->price, $response['data']['sub_total']);
  1764. });
  1765. it('should add a configurable product to the cart for customer', function () {
  1766. // Arrange.
  1767. $product = (new ProductFaker([
  1768. 'attributes' => [
  1769. 5 => 'new',
  1770. 6 => 'featured',
  1771. 11 => 'price',
  1772. ],
  1773. 'attribute_value' => [
  1774. 'new' => [
  1775. 'boolean_value' => true,
  1776. ],
  1777. 'featured' => [
  1778. 'boolean_value' => true,
  1779. ],
  1780. 'price' => [
  1781. 'float_value' => rand(1000, 2000),
  1782. ],
  1783. ],
  1784. ]))->getConfigurableProductFactory()->create();
  1785. $childProduct = $product->variants()->first();
  1786. // Act and Assert.
  1787. $customer = $this->loginAsCustomer();
  1788. $response = postJson(route('shop.api.checkout.cart.store'), [
  1789. 'selected_configurable_option' => $childProduct->id,
  1790. 'product_id' => $product->id,
  1791. 'is_buy_now' => '0',
  1792. 'rating' => '0',
  1793. 'quantity' => '1',
  1794. 'super_attribute' => [
  1795. 23 => '1',
  1796. 24 => '7',
  1797. ],
  1798. ])
  1799. ->assertOk()
  1800. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'))
  1801. ->assertJsonPath('data.shipping_address', null)
  1802. ->assertJsonPath('data.payment_method', null)
  1803. ->assertJsonPath('data.items_qty', 1)
  1804. ->assertJsonPath('data.items_count', 1)
  1805. ->assertJsonPath('data.items.0.type', $product->type)
  1806. ->assertJsonPath('data.items.0.quantity', 1)
  1807. ->assertJsonPath('data.items.0.name', $product->name)
  1808. ->assertJsonPath('data.is_guest', 0)
  1809. ->assertJsonPath('data.have_stockable_items', true)
  1810. ->assertJsonPath('data.customer_id', $customer->id)
  1811. ->assertJsonPath('data.coupon_code', null)
  1812. ->assertJsonPath('data.billing_address', null)
  1813. ->assertJsonPath('data.tax_total', 0)
  1814. ->assertJsonPath('data.discount_amount', 0);
  1815. $this->assertPrice($childProduct->price, $response['data']['grand_total']);
  1816. $this->assertPrice($childProduct->price, $response['data']['sub_total']);
  1817. });
  1818. it('should fails the validation error when the product id not provided when add a downloadable product to the cart', function () {
  1819. // Arrange.
  1820. $product = (new ProductFaker([
  1821. 'attributes' => [
  1822. 5 => 'new',
  1823. 6 => 'featured',
  1824. 11 => 'price',
  1825. 26 => 'guest_checkout',
  1826. ],
  1827. 'attribute_value' => [
  1828. 'new' => [
  1829. 'boolean_value' => true,
  1830. ],
  1831. 'featured' => [
  1832. 'boolean_value' => true,
  1833. ],
  1834. 'price' => [
  1835. 'float_value' => rand(1000, 5000),
  1836. ],
  1837. 'guest_checkout' => [
  1838. 'boolean_value' => true,
  1839. ],
  1840. ],
  1841. ]))->getDownloadableProductFactory()->create();
  1842. // Act and Assert.
  1843. postJson(route('shop.api.checkout.cart.store', [
  1844. 'quantity' => 1,
  1845. 'is_buy_now' => '0',
  1846. 'rating' => '0',
  1847. 'links' => $product->downloadable_links()->pluck('id')->toArray(),
  1848. ]))
  1849. ->assertJsonValidationErrorFor('product_id')
  1850. ->assertUnprocessable();
  1851. });
  1852. it('should add a downloadable product to the cart for guest user', function () {
  1853. // Arrange.
  1854. $product = (new ProductFaker([
  1855. 'attributes' => [
  1856. 5 => 'new',
  1857. 6 => 'featured',
  1858. 11 => 'price',
  1859. 26 => 'guest_checkout',
  1860. ],
  1861. 'attribute_value' => [
  1862. 'new' => [
  1863. 'boolean_value' => true,
  1864. ],
  1865. 'featured' => [
  1866. 'boolean_value' => true,
  1867. ],
  1868. 'price' => [
  1869. 'float_value' => rand(1000, 5000),
  1870. ],
  1871. 'guest_checkout' => [
  1872. 'boolean_value' => true,
  1873. ],
  1874. ],
  1875. ]))->getDownloadableProductFactory()->create();
  1876. // Act and Assert.
  1877. $response = postJson(route('shop.api.checkout.cart.store', [
  1878. 'product_id' => $product->id,
  1879. 'quantity' => 1,
  1880. 'is_buy_now' => '0',
  1881. 'rating' => '0',
  1882. 'links' => $product->downloadable_links()->pluck('id')->toArray(),
  1883. ]))
  1884. ->assertOk()
  1885. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'))
  1886. ->assertJsonPath('data.shipping_address', null)
  1887. ->assertJsonPath('data.payment_method', null)
  1888. ->assertJsonPath('data.items_qty', 1)
  1889. ->assertJsonPath('data.items_count', 1)
  1890. ->assertJsonPath('data.items.0.type', $product->type)
  1891. ->assertJsonPath('data.items.0.quantity', 1)
  1892. ->assertJsonPath('data.items.0.name', $product->name)
  1893. ->assertJsonPath('data.is_guest', 1)
  1894. ->assertJsonPath('data.have_stockable_items', false)
  1895. ->assertJsonPath('data.customer_id', null)
  1896. ->assertJsonPath('data.coupon_code', null)
  1897. ->assertJsonPath('data.billing_address', null)
  1898. ->assertJsonPath('data.tax_total', 0)
  1899. ->assertJsonPath('data.discount_amount', 0);
  1900. $this->assertPrice($product->price, $response['data']['items'][0]['price']);
  1901. $this->assertPrice($product->price, $response['data']['grand_total']);
  1902. });
  1903. it('should add a downloadable product to the cart for customer', function () {
  1904. // Arrange.
  1905. $product = (new ProductFaker([
  1906. 'attributes' => [
  1907. 5 => 'new',
  1908. 6 => 'featured',
  1909. 11 => 'price',
  1910. ],
  1911. 'attribute_value' => [
  1912. 'new' => [
  1913. 'boolean_value' => true,
  1914. ],
  1915. 'featured' => [
  1916. 'boolean_value' => true,
  1917. ],
  1918. 'price' => [
  1919. 'float_value' => rand(1000, 5000),
  1920. ],
  1921. ],
  1922. ]))->getDownloadableProductFactory()->create();
  1923. // Act and Assert.
  1924. $customer = $this->loginAsCustomer();
  1925. $response = postJson(route('shop.api.checkout.cart.store', [
  1926. 'product_id' => $product->id,
  1927. 'quantity' => 1,
  1928. 'is_buy_now' => '0',
  1929. 'rating' => '0',
  1930. 'links' => $product->downloadable_links()->pluck('id')->toArray(),
  1931. ]))
  1932. ->assertOk()
  1933. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'))
  1934. ->assertJsonPath('data.shipping_address', null)
  1935. ->assertJsonPath('data.payment_method', null)
  1936. ->assertJsonPath('data.items_qty', 1)
  1937. ->assertJsonPath('data.items_count', 1)
  1938. ->assertJsonPath('data.items.0.type', $product->type)
  1939. ->assertJsonPath('data.items.0.quantity', 1)
  1940. ->assertJsonPath('data.items.0.name', $product->name)
  1941. ->assertJsonPath('data.is_guest', 0)
  1942. ->assertJsonPath('data.have_stockable_items', false)
  1943. ->assertJsonPath('data.customer_id', $customer->id)
  1944. ->assertJsonPath('data.coupon_code', null)
  1945. ->assertJsonPath('data.billing_address', null)
  1946. ->assertJsonPath('data.tax_total', 0)
  1947. ->assertJsonPath('data.discount_amount', 0);
  1948. $this->assertPrice($product->price, $response['data']['items'][0]['price']);
  1949. $this->assertPrice($product->price, $response['data']['grand_total']);
  1950. });
  1951. it('should fails the validation error when the product id not provided when add a grouped product to the cart', function () {
  1952. // Arrange.
  1953. $product = (new ProductFaker([
  1954. 'attributes' => [
  1955. 5 => 'new',
  1956. 6 => 'featured',
  1957. 11 => 'price',
  1958. 26 => 'guest_checkout',
  1959. ],
  1960. 'attribute_value' => [
  1961. 'new' => [
  1962. 'boolean_value' => true,
  1963. ],
  1964. 'featured' => [
  1965. 'boolean_value' => true,
  1966. ],
  1967. 'price' => [
  1968. 'float_value' => rand(1000, 5000),
  1969. ],
  1970. 'guest_checkout' => [
  1971. 'boolean_value' => true,
  1972. ],
  1973. ],
  1974. ]))->getGroupedProductFactory()->create();
  1975. $groupedProducts = $product->grouped_products()->with('associated_product')->get();
  1976. $data = [
  1977. 'quantities' => [],
  1978. 'prices' => [],
  1979. ];
  1980. foreach ($groupedProducts as $groupedProduct) {
  1981. $data['quantities'][$groupedProduct->associated_product_id] = $groupedProduct->qty;
  1982. $data['prices'][] = $groupedProduct->associated_product->price * $groupedProduct->qty;
  1983. }
  1984. // Act and Assert.
  1985. postJson(route('shop.api.checkout.cart.store'), [
  1986. 'quantity' => 1,
  1987. 'is_buy_now' => '0',
  1988. 'rating' => '0',
  1989. 'qty' => $data['quantities'],
  1990. ])
  1991. ->assertJsonValidationErrorFor('product_id')
  1992. ->assertUnprocessable();
  1993. });
  1994. it('should add a grouped product to the cart for guest user', function () {
  1995. // Arrange.
  1996. $product = (new ProductFaker([
  1997. 'attributes' => [
  1998. 5 => 'new',
  1999. 6 => 'featured',
  2000. 11 => 'price',
  2001. 26 => 'guest_checkout',
  2002. ],
  2003. 'attribute_value' => [
  2004. 'new' => [
  2005. 'boolean_value' => true,
  2006. ],
  2007. 'featured' => [
  2008. 'boolean_value' => true,
  2009. ],
  2010. 'price' => [
  2011. 'float_value' => rand(1000, 5000),
  2012. ],
  2013. 'guest_checkout' => [
  2014. 'boolean_value' => true,
  2015. ],
  2016. ],
  2017. ]))->getGroupedProductFactory()->create();
  2018. $groupedProducts = $product->grouped_products()->with('associated_product')->get();
  2019. $data = [
  2020. 'quantities' => [],
  2021. 'prices' => [],
  2022. ];
  2023. foreach ($groupedProducts as $groupedProduct) {
  2024. $data['quantities'][$groupedProduct->associated_product_id] = $groupedProduct->qty;
  2025. $data['prices'][] = $groupedProduct->associated_product->price * $groupedProduct->qty;
  2026. }
  2027. // Act and Assert.
  2028. $response = postJson(route('shop.api.checkout.cart.store'), [
  2029. 'product_id' => $product->id,
  2030. 'quantity' => 1,
  2031. 'is_buy_now' => '0',
  2032. 'rating' => '0',
  2033. 'qty' => $data['quantities'],
  2034. ])
  2035. ->assertOk()
  2036. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'))
  2037. ->assertJsonPath('data.items_qty', array_sum($data['quantities']))
  2038. ->assertJsonPath('data.items_count', 4)
  2039. ->assertJsonPath('data.shipping_address', null)
  2040. ->assertJsonPath('data.payment_method', null)
  2041. ->assertJsonPath('data.is_guest', 1)
  2042. ->assertJsonPath('data.have_stockable_items', true)
  2043. ->assertJsonPath('data.customer_id', null)
  2044. ->assertJsonPath('data.coupon_code', null)
  2045. ->assertJsonPath('data.billing_address', null)
  2046. ->assertJsonPath('data.tax_total', 0)
  2047. ->assertJsonPath('data.discount_amount', 0);
  2048. foreach ($groupedProducts as $key => $groupedProduct) {
  2049. $response->assertJsonPath('data.items.'.$key.'.quantity', $groupedProduct->qty)
  2050. ->assertJsonPath('data.items.'.$key.'.type', $groupedProduct->associated_product->type)
  2051. ->assertJsonPath('data.items.'.$key.'.name', $groupedProduct->associated_product->name);
  2052. }
  2053. $this->assertEquals(round(array_sum($data['prices']), 2), round($response['data']['grand_total'], 2), '', 0.00000001);
  2054. $this->assertEquals(round(array_sum($data['prices']), 2), round($response['data']['sub_total'], 2), '', 0.00000001);
  2055. });
  2056. it('should add a grouped product to the cart for customer', function () {
  2057. // Arrange.
  2058. $product = (new ProductFaker([
  2059. 'attributes' => [
  2060. 5 => 'new',
  2061. 6 => 'featured',
  2062. 11 => 'price',
  2063. 26 => 'guest_checkout',
  2064. ],
  2065. 'attribute_value' => [
  2066. 'new' => [
  2067. 'boolean_value' => true,
  2068. ],
  2069. 'featured' => [
  2070. 'boolean_value' => true,
  2071. ],
  2072. 'price' => [
  2073. 'float_value' => rand(1000, 5000),
  2074. ],
  2075. 'guest_checkout' => [
  2076. 'boolean_value' => true,
  2077. ],
  2078. ],
  2079. ]))->getGroupedProductFactory()->create();
  2080. $groupedProducts = $product->grouped_products()->with('associated_product')->get();
  2081. $data = [
  2082. 'quantities' => [],
  2083. 'prices' => [],
  2084. ];
  2085. foreach ($groupedProducts as $groupedProduct) {
  2086. $data['quantities'][$groupedProduct->associated_product_id] = $groupedProduct->qty;
  2087. $data['prices'][] = $groupedProduct->associated_product->price * $groupedProduct->qty;
  2088. }
  2089. // Act and Assert.
  2090. $customer = $this->loginAsCustomer();
  2091. $response = postJson(route('shop.api.checkout.cart.store'), [
  2092. 'product_id' => $product->id,
  2093. 'quantity' => 1,
  2094. 'is_buy_now' => '0',
  2095. 'rating' => '0',
  2096. 'qty' => $data['quantities'],
  2097. ])
  2098. ->assertOk()
  2099. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'))
  2100. ->assertJsonPath('data.items_qty', array_sum($data['quantities']))
  2101. ->assertJsonPath('data.items_count', 4)
  2102. ->assertJsonPath('data.shipping_address', null)
  2103. ->assertJsonPath('data.payment_method', null)
  2104. ->assertJsonPath('data.is_guest', 0)
  2105. ->assertJsonPath('data.have_stockable_items', true)
  2106. ->assertJsonPath('data.customer_id', $customer->id)
  2107. ->assertJsonPath('data.coupon_code', null)
  2108. ->assertJsonPath('data.billing_address', null)
  2109. ->assertJsonPath('data.tax_total', 0)
  2110. ->assertJsonPath('data.discount_amount', 0);
  2111. foreach ($groupedProducts as $key => $groupedProduct) {
  2112. $response->assertJsonPath('data.items.'.$key.'.quantity', $groupedProduct->qty)
  2113. ->assertJsonPath('data.items.'.$key.'.type', $groupedProduct->associated_product->type)
  2114. ->assertJsonPath('data.items.'.$key.'.name', $groupedProduct->associated_product->name);
  2115. }
  2116. $this->assertEquals(round(array_sum($data['prices']), 2), round($response['data']['grand_total'], 2), '', 0.00000001);
  2117. $this->assertEquals(round(array_sum($data['prices']), 2), round($response['data']['sub_total'], 2), '', 0.00000001);
  2118. });
  2119. it('should fails the validation error when the product id not provided when add a virtual product to the cart', function () {
  2120. // Arrange.
  2121. (new ProductFaker([
  2122. 'attributes' => [
  2123. 5 => 'new',
  2124. 6 => 'featured',
  2125. 11 => 'price',
  2126. 26 => 'guest_checkout',
  2127. ],
  2128. 'attribute_value' => [
  2129. 'new' => [
  2130. 'boolean_value' => true,
  2131. ],
  2132. 'featured' => [
  2133. 'boolean_value' => true,
  2134. ],
  2135. 'price' => [
  2136. 'float_value' => rand(1000, 5000),
  2137. ],
  2138. 'guest_checkout' => [
  2139. 'boolean_value' => true,
  2140. ],
  2141. ],
  2142. ]))->getVirtualProductFactory()->create();
  2143. // Act and Assert.
  2144. postJson(route('shop.api.checkout.cart.store', [
  2145. 'quantity' => rand(1, 10),
  2146. ]))
  2147. ->assertJsonValidationErrorFor('product_id')
  2148. ->assertUnprocessable();
  2149. });
  2150. it('should add a virtual product to the cart for guest user', function () {
  2151. // Arrange.
  2152. $product = (new ProductFaker([
  2153. 'attributes' => [
  2154. 5 => 'new',
  2155. 6 => 'featured',
  2156. 11 => 'price',
  2157. 26 => 'guest_checkout',
  2158. ],
  2159. 'attribute_value' => [
  2160. 'new' => [
  2161. 'boolean_value' => true,
  2162. ],
  2163. 'featured' => [
  2164. 'boolean_value' => true,
  2165. ],
  2166. 'price' => [
  2167. 'float_value' => rand(1000, 5000),
  2168. ],
  2169. 'guest_checkout' => [
  2170. 'boolean_value' => true,
  2171. ],
  2172. ],
  2173. ]))->getVirtualProductFactory()->create();
  2174. // Act and Assert.
  2175. $response = postJson(route('shop.api.checkout.cart.store', [
  2176. 'product_id' => $product->id,
  2177. 'quantity' => $quantity = rand(1, 10),
  2178. ]))
  2179. ->assertOk()
  2180. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'))
  2181. ->assertJsonPath('data.items_qty', $quantity)
  2182. ->assertJsonPath('data.items_count', 1)
  2183. ->assertJsonPath('data.items.0.quantity', $quantity)
  2184. ->assertJsonPath('data.items.0.type', $product->type)
  2185. ->assertJsonPath('data.items.0.name', $product->name)
  2186. ->assertJsonPath('data.shipping_address', null)
  2187. ->assertJsonPath('data.payment_method', null)
  2188. ->assertJsonPath('data.is_guest', 1)
  2189. ->assertJsonPath('data.have_stockable_items', false)
  2190. ->assertJsonPath('data.customer_id', null)
  2191. ->assertJsonPath('data.coupon_code', null)
  2192. ->assertJsonPath('data.billing_address', null)
  2193. ->assertJsonPath('data.tax_total', 0)
  2194. ->assertJsonPath('data.discount_amount', 0);
  2195. $this->assertPrice($product->price, $response['data']['items'][0]['price']);
  2196. $this->assertPrice($product->price * $quantity, $response['data']['grand_total']);
  2197. });
  2198. it('should add a virtual product to the cart for customer', function () {
  2199. // Arrange.
  2200. $product = (new ProductFaker([
  2201. 'attributes' => [
  2202. 5 => 'new',
  2203. 6 => 'featured',
  2204. 11 => 'price',
  2205. ],
  2206. 'attribute_value' => [
  2207. 'new' => [
  2208. 'boolean_value' => true,
  2209. ],
  2210. 'featured' => [
  2211. 'boolean_value' => true,
  2212. ],
  2213. 'price' => [
  2214. 'float_value' => rand(1000, 5000),
  2215. ],
  2216. ],
  2217. ]))->getVirtualProductFactory()->create();
  2218. // Act and Assert.
  2219. $customer = $this->loginAsCustomer();
  2220. $response = postJson(route('shop.api.checkout.cart.store', [
  2221. 'product_id' => $product->id,
  2222. 'quantity' => $quantity = rand(1, 10),
  2223. ]))
  2224. ->assertOk()
  2225. ->assertJsonPath('message', trans('shop::app.checkout.cart.item-add-to-cart'))
  2226. ->assertJsonPath('data.items_qty', $quantity)
  2227. ->assertJsonPath('data.items_count', 1)
  2228. ->assertJsonPath('data.items.0.quantity', $quantity)
  2229. ->assertJsonPath('data.items.0.type', $product->type)
  2230. ->assertJsonPath('data.items.0.name', $product->name)
  2231. ->assertJsonPath('data.shipping_address', null)
  2232. ->assertJsonPath('data.payment_method', null)
  2233. ->assertJsonPath('data.is_guest', 0)
  2234. ->assertJsonPath('data.have_stockable_items', false)
  2235. ->assertJsonPath('data.customer_id', $customer->id)
  2236. ->assertJsonPath('data.coupon_code', null)
  2237. ->assertJsonPath('data.billing_address', null)
  2238. ->assertJsonPath('data.tax_total', 0)
  2239. ->assertJsonPath('data.discount_amount', 0);
  2240. $this->assertPrice($product->price, $response['data']['items'][0]['price']);
  2241. $this->assertPrice($product->price * $quantity, $response['data']['grand_total']);
  2242. });
  2243. it('should check including tax rate when add a product to the cart based on shipping address', function () {
  2244. // Arrange.
  2245. $product = (new ProductFaker([
  2246. 'attributes' => [
  2247. 5 => 'new',
  2248. 6 => 'featured',
  2249. 11 => 'price',
  2250. ],
  2251. 'attribute_value' => [
  2252. 'new' => [
  2253. 'boolean_value' => true,
  2254. ],
  2255. 'featured' => [
  2256. 'boolean_value' => true,
  2257. ],
  2258. 'price' => [
  2259. 'float_value' => 100,
  2260. ],
  2261. ],
  2262. ]))->getSimpleProductFactory()->create();
  2263. $taxRate = TaxRate::factory()->create([
  2264. 'country' => 'IN',
  2265. 'state' => fake()->randomElement(['UP', 'DL', 'HR', 'PB', 'RJ']),
  2266. ]);
  2267. $taxCategory = TaxCategory::factory()->create();
  2268. TaxMap::factory()->create([
  2269. 'tax_category_id' => $taxCategory->id,
  2270. 'tax_rate_id' => $taxRate->id,
  2271. ]);
  2272. CoreConfig::factory()->create([
  2273. 'code' => 'sales.taxes.categories.shipping',
  2274. 'value' => $taxCategory->id,
  2275. ])->create([
  2276. 'code' => 'sales.taxes.categories.product',
  2277. 'value' => $taxCategory->id,
  2278. ])->create([
  2279. 'code' => 'sales.taxes.calculation.based_on',
  2280. 'value' => 'shipping_address',
  2281. ])->create([
  2282. 'code' => 'sales.taxes.calculation.product_prices',
  2283. 'value' => 'including_tax',
  2284. ])->create([
  2285. 'code' => 'sales.taxes.calculation.shipping_prices',
  2286. 'value' => 'including_tax',
  2287. ]);
  2288. $cart = cart()->addProduct($product, [
  2289. 'product_id' => $product->id,
  2290. 'quantity' => 1,
  2291. ]);
  2292. $inclTax = $product->price - ($product->price / (1 + ($taxRate->tax_rate / 100)));
  2293. // Act and Assert.
  2294. $response = postJson(route('shop.api.checkout.cart.estimate_shipping'), [
  2295. 'country' => $taxRate->country,
  2296. 'state' => $taxRate->state,
  2297. 'postcode' => fake()->postcode(),
  2298. ])
  2299. ->assertOk()
  2300. ->assertJsonPath('data.cart.id', $cart->id)
  2301. ->assertJsonPath('data.cart.formatted_tax_total', core()->formatPrice($inclTax))
  2302. ->assertJsonPath('data.cart.formatted_sub_total_incl_tax', core()->formatPrice($product->price))
  2303. ->assertJsonPath('data.cart.formatted_grand_total', core()->formatPrice($product->price))
  2304. ->assertJsonPath('data.cart.formatted_sub_total', core()->formatPrice($product->price - $inclTax))
  2305. ->assertJsonPath('data.cart.items.0.id', $cart->items->first()->id)
  2306. ->assertJsonPath('data.cart.items.0.quantity', 1)
  2307. ->assertJsonPath('data.cart.items.0.type', $product->type);
  2308. $this->assertPrice($inclTax, $response->json('data.cart.tax_total'));
  2309. $this->assertPrice($product->price, $response->json('data.cart.sub_total_incl_tax'));
  2310. $this->assertPrice($product->price - $inclTax, $response->json('data.cart.sub_total'));
  2311. $this->assertPrice($product->price, $response->json('data.cart.grand_total'));
  2312. $this->assertPrice($product->price, $response->json('data.cart.items.0.price_incl_tax'));
  2313. $this->assertPrice($product->price - $inclTax, $response->json('data.cart.items.0.price'));
  2314. });
  2315. it('should check including tax rate when add a product to the cart based on billing address', function () {
  2316. // Arrange.
  2317. $product = (new ProductFaker([
  2318. 'attributes' => [
  2319. 5 => 'new',
  2320. 6 => 'featured',
  2321. 11 => 'price',
  2322. ],
  2323. 'attribute_value' => [
  2324. 'new' => [
  2325. 'boolean_value' => true,
  2326. ],
  2327. 'featured' => [
  2328. 'boolean_value' => true,
  2329. ],
  2330. 'price' => [
  2331. 'float_value' => 100,
  2332. ],
  2333. ],
  2334. ]))->getSimpleProductFactory()->create();
  2335. $taxRate = TaxRate::factory()->create([
  2336. 'country' => 'IN',
  2337. 'state' => fake()->randomElement(['UP', 'DL', 'HR', 'PB', 'RJ']),
  2338. ]);
  2339. $taxCategory = TaxCategory::factory()->create();
  2340. TaxMap::factory()->create([
  2341. 'tax_category_id' => $taxCategory->id,
  2342. 'tax_rate_id' => $taxRate->id,
  2343. ]);
  2344. CoreConfig::factory()->create([
  2345. 'code' => 'sales.taxes.categories.shipping',
  2346. 'value' => $taxCategory->id,
  2347. ])->create([
  2348. 'code' => 'sales.taxes.categories.product',
  2349. 'value' => $taxCategory->id,
  2350. ])->create([
  2351. 'code' => 'sales.taxes.calculation.based_on',
  2352. 'value' => 'billing_address',
  2353. ])->create([
  2354. 'code' => 'sales.taxes.calculation.product_prices',
  2355. 'value' => 'including_tax',
  2356. ])->create([
  2357. 'code' => 'sales.taxes.calculation.shipping_prices',
  2358. 'value' => 'including_tax',
  2359. ]);
  2360. $cart = cart()->addProduct($product, [
  2361. 'product_id' => $product->id,
  2362. 'quantity' => 1,
  2363. ]);
  2364. CartAddress::factory()->create([
  2365. 'cart_id' => $cart->id,
  2366. 'country' => $taxRate->country,
  2367. 'state' => $taxRate->state,
  2368. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  2369. 'use_for_shipping' => true,
  2370. ]);
  2371. CartAddress::factory()->create([
  2372. 'cart_id' => $cart->id,
  2373. 'country' => $taxRate->country,
  2374. 'state' => $taxRate->state,
  2375. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  2376. 'use_for_shipping' => true,
  2377. ]);
  2378. cart()->setCart($cart);
  2379. cart()->collectTotals();
  2380. $inclTax = $product->price - ($product->price / (1 + ($taxRate->tax_rate / 100)));
  2381. // Act and Assert.
  2382. $response = getJson(route('shop.checkout.onepage.summary'))
  2383. ->assertOk()
  2384. ->assertJsonPath('data.id', $cart->id)
  2385. ->assertJsonPath('data.formatted_tax_total', core()->formatPrice($inclTax))
  2386. ->assertJsonPath('data.formatted_sub_total_incl_tax', core()->formatPrice($product->price))
  2387. ->assertJsonPath('data.formatted_grand_total', core()->formatPrice($product->price))
  2388. ->assertJsonPath('data.formatted_sub_total', core()->formatPrice($product->price - $inclTax))
  2389. ->assertJsonPath('data.items.0.id', $cart->items->first()->id)
  2390. ->assertJsonPath('data.items.0.quantity', 1)
  2391. ->assertJsonPath('data.items.0.type', $product->type);
  2392. $this->assertPrice($inclTax, $response->json('data.tax_total'));
  2393. $this->assertPrice($product->price, $response->json('data.sub_total_incl_tax'));
  2394. $this->assertPrice($product->price - $inclTax, $response->json('data.sub_total'));
  2395. $this->assertPrice($product->price, $response->json('data.grand_total'));
  2396. $this->assertPrice($product->price, $response->json('data.items.0.price_incl_tax'));
  2397. $this->assertPrice($product->price - $inclTax, $response->json('data.items.0.price'));
  2398. });
  2399. it('should check including tax rate when add a product to the cart based on shipping origin', function () {
  2400. // Arrange.
  2401. $product = (new ProductFaker([
  2402. 'attributes' => [
  2403. 5 => 'new',
  2404. 6 => 'featured',
  2405. 11 => 'price',
  2406. ],
  2407. 'attribute_value' => [
  2408. 'new' => [
  2409. 'boolean_value' => true,
  2410. ],
  2411. 'featured' => [
  2412. 'boolean_value' => true,
  2413. ],
  2414. 'price' => [
  2415. 'float_value' => 100,
  2416. ],
  2417. ],
  2418. ]))->getSimpleProductFactory()->create();
  2419. $taxRate = TaxRate::factory()->create([
  2420. 'country' => 'IN',
  2421. 'state' => fake()->randomElement(['UP', 'DL', 'HR', 'PB', 'RJ']),
  2422. ]);
  2423. $taxCategory = TaxCategory::factory()->create();
  2424. TaxMap::factory()->create([
  2425. 'tax_category_id' => $taxCategory->id,
  2426. 'tax_rate_id' => $taxRate->id,
  2427. ]);
  2428. CoreConfig::factory()->create([
  2429. 'code' => 'sales.shipping.origin.country',
  2430. 'value' => $taxRate->country,
  2431. 'channel_code' => 'default',
  2432. 'locale_code' => 'en',
  2433. ])->create([
  2434. 'code' => 'sales.shipping.origin.state',
  2435. 'value' => $taxRate->state,
  2436. 'channel_code' => 'default',
  2437. 'locale_code' => 'en',
  2438. ])->create([
  2439. 'code' => 'sales.shipping.origin.city',
  2440. 'value' => fake()->city(),
  2441. 'channel_code' => 'default',
  2442. ])->create([
  2443. 'code' => 'sales.shipping.origin.address',
  2444. 'value' => fake()->address(),
  2445. 'channel_code' => 'default',
  2446. ])->create([
  2447. 'code' => 'sales.shipping.origin.store_name',
  2448. 'value' => 'DEMO STORE',
  2449. 'channel_code' => 'default',
  2450. ])->create([
  2451. 'code' => 'sales.shipping.origin.contact',
  2452. 'value' => '1234567890',
  2453. 'channel_code' => 'default',
  2454. ])->create([
  2455. 'code' => 'sales.shipping.origin.bank_details',
  2456. 'value' => 'TEST BANK',
  2457. 'channel_code' => 'default',
  2458. ])->create([
  2459. 'code' => 'sales.shipping.origin.zipcode',
  2460. 'value' => '123456',
  2461. 'channel_code' => 'default',
  2462. ])->create([
  2463. 'code' => 'sales.taxes.categories.shipping',
  2464. 'value' => $taxCategory->id,
  2465. ])->create([
  2466. 'code' => 'sales.taxes.categories.product',
  2467. 'value' => $taxCategory->id,
  2468. ])->create([
  2469. 'code' => 'sales.taxes.calculation.based_on',
  2470. 'value' => 'shipping_origin',
  2471. ])->create([
  2472. 'code' => 'sales.taxes.calculation.product_prices',
  2473. 'value' => 'including_tax',
  2474. ])->create([
  2475. 'code' => 'sales.taxes.calculation.shipping_prices',
  2476. 'value' => 'including_tax',
  2477. ]);
  2478. $cart = cart()->addProduct($product, [
  2479. 'product_id' => $product->id,
  2480. 'quantity' => 1,
  2481. ]);
  2482. $inclTax = $product->price - ($product->price / (1 + ($taxRate->tax_rate / 100)));
  2483. // Act and Assert.
  2484. $response = postJson(route('shop.api.checkout.cart.estimate_shipping'), [
  2485. 'country' => $taxRate->country,
  2486. 'state' => $taxRate->state,
  2487. 'postcode' => '123456',
  2488. ]);
  2489. $response->assertOk()
  2490. ->assertJsonPath('data.cart.id', $cart->id)
  2491. ->assertJsonPath('data.cart.formatted_tax_total', core()->formatPrice($inclTax))
  2492. ->assertJsonPath('data.cart.formatted_sub_total_incl_tax', core()->formatPrice($product->price))
  2493. ->assertJsonPath('data.cart.formatted_grand_total', core()->formatPrice($product->price))
  2494. ->assertJsonPath('data.cart.formatted_sub_total', core()->formatPrice($product->price - $inclTax))
  2495. ->assertJsonPath('data.cart.items.0.id', $cart->items->first()->id)
  2496. ->assertJsonPath('data.cart.items.0.quantity', 1)
  2497. ->assertJsonPath('data.cart.items.0.type', $product->type);
  2498. $this->assertPrice($inclTax, $response->json('data.cart.tax_total'));
  2499. $this->assertPrice($product->price, $response->json('data.cart.sub_total_incl_tax'));
  2500. $this->assertPrice($product->price - $inclTax, $response->json('data.cart.sub_total'));
  2501. $this->assertPrice($product->price, $response->json('data.cart.grand_total'));
  2502. $this->assertPrice($product->price, $response->json('data.cart.items.0.price_incl_tax'));
  2503. $this->assertPrice($product->price - $inclTax, $response->json('data.cart.items.0.price'));
  2504. });
  2505. it('should check excluding tax rate when add a product to the cart based on billing address', function () {
  2506. // Arrange.
  2507. $product = (new ProductFaker([
  2508. 'attributes' => [
  2509. 5 => 'new',
  2510. 6 => 'featured',
  2511. 11 => 'price',
  2512. ],
  2513. 'attribute_value' => [
  2514. 'new' => [
  2515. 'boolean_value' => true,
  2516. ],
  2517. 'featured' => [
  2518. 'boolean_value' => true,
  2519. ],
  2520. 'price' => [
  2521. 'float_value' => 100,
  2522. ],
  2523. ],
  2524. ]))->getSimpleProductFactory()->create();
  2525. $taxRate = TaxRate::factory()->create([
  2526. 'country' => 'IN',
  2527. 'state' => fake()->randomElement(['UP', 'DL', 'HR', 'PB', 'RJ']),
  2528. ]);
  2529. $taxCategory = TaxCategory::factory()->create();
  2530. TaxMap::factory()->create([
  2531. 'tax_category_id' => $taxCategory->id,
  2532. 'tax_rate_id' => $taxRate->id,
  2533. ]);
  2534. CoreConfig::factory()->create([
  2535. 'code' => 'sales.taxes.categories.shipping',
  2536. 'value' => $taxCategory->id,
  2537. ])->create([
  2538. 'code' => 'sales.taxes.categories.product',
  2539. 'value' => $taxCategory->id,
  2540. ])->create([
  2541. 'code' => 'sales.taxes.calculation.based_on',
  2542. 'value' => 'billing_address',
  2543. ])->create([
  2544. 'code' => 'sales.taxes.calculation.product_prices',
  2545. 'value' => 'excluding_tax',
  2546. ])->create([
  2547. 'code' => 'sales.taxes.calculation.shipping_prices',
  2548. 'value' => 'excluding_tax',
  2549. ]);
  2550. $cart = cart()->addProduct($product, [
  2551. 'product_id' => $product->id,
  2552. 'quantity' => 1,
  2553. ]);
  2554. CartAddress::factory()->create([
  2555. 'cart_id' => $cart->id,
  2556. 'country' => $taxRate->country,
  2557. 'state' => $taxRate->state,
  2558. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  2559. 'use_for_shipping' => true,
  2560. ]);
  2561. CartAddress::factory()->create([
  2562. 'cart_id' => $cart->id,
  2563. 'country' => $taxRate->country,
  2564. 'state' => $taxRate->state,
  2565. 'address_type' => CartAddress::ADDRESS_TYPE_BILLING,
  2566. 'use_for_shipping' => true,
  2567. ]);
  2568. cart()->setCart($cart);
  2569. cart()->collectTotals();
  2570. $exclTax = ($taxRate->tax_rate / 100) * $product->price;
  2571. // Act and Assert.
  2572. $response = getJson(route('shop.checkout.onepage.summary'))
  2573. ->assertOk()
  2574. ->assertJsonPath('data.id', $cart->id)
  2575. ->assertJsonPath('data.formatted_tax_total', core()->formatPrice($exclTax))
  2576. ->assertJsonPath('data.formatted_sub_total_incl_tax', core()->formatPrice($product->price + $exclTax))
  2577. ->assertJsonPath('data.formatted_grand_total', core()->formatPrice($product->price + $exclTax))
  2578. ->assertJsonPath('data.formatted_sub_total', core()->formatPrice($product->price))
  2579. ->assertJsonPath('data.items.0.id', $cart->items->first()->id)
  2580. ->assertJsonPath('data.items.0.quantity', 1)
  2581. ->assertJsonPath('data.items.0.type', $product->type);
  2582. $this->assertPrice($exclTax, $response->json('data.tax_total'));
  2583. $this->assertPrice($product->price + $exclTax, $response->json('data.sub_total_incl_tax'));
  2584. $this->assertPrice($product->price, $response->json('data.sub_total'));
  2585. $this->assertPrice($product->price + $exclTax, $response->json('data.grand_total'));
  2586. $this->assertPrice($product->price + $exclTax, $response->json('data.items.0.price_incl_tax'));
  2587. $this->assertPrice($product->price, $response->json('data.items.0.price'));
  2588. });
  2589. it('should check excluding tax rate when add a product to the cart based on shipping address', function () {
  2590. // Arrange.
  2591. $product = (new ProductFaker([
  2592. 'attributes' => [
  2593. 5 => 'new',
  2594. 6 => 'featured',
  2595. 11 => 'price',
  2596. ],
  2597. 'attribute_value' => [
  2598. 'new' => [
  2599. 'boolean_value' => true,
  2600. ],
  2601. 'featured' => [
  2602. 'boolean_value' => true,
  2603. ],
  2604. 'price' => [
  2605. 'float_value' => 100,
  2606. ],
  2607. ],
  2608. ]))->getSimpleProductFactory()->create();
  2609. $taxRate = TaxRate::factory()->create([
  2610. 'country' => 'IN',
  2611. 'state' => fake()->randomElement(['UP', 'DL', 'HR', 'PB', 'RJ']),
  2612. ]);
  2613. $taxCategory = TaxCategory::factory()->create();
  2614. TaxMap::factory()->create([
  2615. 'tax_category_id' => $taxCategory->id,
  2616. 'tax_rate_id' => $taxRate->id,
  2617. ]);
  2618. CoreConfig::factory()->create([
  2619. 'code' => 'sales.taxes.categories.shipping',
  2620. 'value' => $taxCategory->id,
  2621. ])->create([
  2622. 'code' => 'sales.taxes.categories.product',
  2623. 'value' => $taxCategory->id,
  2624. ])->create([
  2625. 'code' => 'sales.taxes.calculation.based_on',
  2626. 'value' => 'shipping_address',
  2627. ])->create([
  2628. 'code' => 'sales.taxes.calculation.product_prices',
  2629. 'value' => 'excluding_tax',
  2630. ])->create([
  2631. 'code' => 'sales.taxes.calculation.shipping_prices',
  2632. 'value' => 'excluding_tax',
  2633. ]);
  2634. $cart = cart()->addProduct($product, [
  2635. 'product_id' => $product->id,
  2636. 'quantity' => 1,
  2637. ]);
  2638. $exclTax = ($taxRate->tax_rate / 100) * $product->price;
  2639. // Act and Assert.
  2640. $response = postJson(route('shop.api.checkout.cart.estimate_shipping'), [
  2641. 'country' => $taxRate->country,
  2642. 'state' => $taxRate->state,
  2643. 'postcode' => fake()->postcode(),
  2644. ])
  2645. ->assertOk()
  2646. ->assertJsonPath('data.cart.id', $cart->id)
  2647. ->assertJsonPath('data.cart.formatted_tax_total', core()->formatPrice($exclTax))
  2648. ->assertJsonPath('data.cart.formatted_sub_total_incl_tax', core()->formatPrice($product->price + $exclTax))
  2649. ->assertJsonPath('data.cart.formatted_grand_total', core()->formatPrice($product->price + $exclTax))
  2650. ->assertJsonPath('data.cart.formatted_sub_total', core()->formatPrice($product->price))
  2651. ->assertJsonPath('data.cart.items.0.id', $cart->items->first()->id)
  2652. ->assertJsonPath('data.cart.items.0.quantity', 1)
  2653. ->assertJsonPath('data.cart.items.0.type', $product->type);
  2654. $this->assertPrice($exclTax, $response->json('data.cart.tax_total'));
  2655. $this->assertPrice($product->price + $exclTax, $response->json('data.cart.sub_total_incl_tax'));
  2656. $this->assertPrice($product->price, $response->json('data.cart.sub_total'));
  2657. $this->assertPrice($product->price + $exclTax, $response->json('data.cart.grand_total'));
  2658. $this->assertPrice($product->price + $exclTax, $response->json('data.cart.items.0.price_incl_tax'));
  2659. $this->assertPrice($product->price, $response->json('data.cart.items.0.price'));
  2660. });
  2661. it('should check excluding tax rate when add a product to the cart based on shipping origin', function () {
  2662. // Arrange.
  2663. $product = (new ProductFaker([
  2664. 'attributes' => [
  2665. 5 => 'new',
  2666. 6 => 'featured',
  2667. 11 => 'price',
  2668. ],
  2669. 'attribute_value' => [
  2670. 'new' => [
  2671. 'boolean_value' => true,
  2672. ],
  2673. 'featured' => [
  2674. 'boolean_value' => true,
  2675. ],
  2676. 'price' => [
  2677. 'float_value' => 100,
  2678. ],
  2679. ],
  2680. ]))->getSimpleProductFactory()->create();
  2681. $taxRate = TaxRate::factory()->create([
  2682. 'country' => 'IN',
  2683. 'state' => fake()->randomElement(['UP', 'DL', 'HR', 'PB', 'RJ']),
  2684. ]);
  2685. $taxCategory = TaxCategory::factory()->create();
  2686. TaxMap::factory()->create([
  2687. 'tax_category_id' => $taxCategory->id,
  2688. 'tax_rate_id' => $taxRate->id,
  2689. ]);
  2690. CoreConfig::factory()->create([
  2691. 'code' => 'sales.shipping.origin.country',
  2692. 'value' => $taxRate->country,
  2693. 'channel_code' => 'default',
  2694. 'locale_code' => 'en',
  2695. ])->create([
  2696. 'code' => 'sales.shipping.origin.state',
  2697. 'value' => $taxRate->state,
  2698. 'channel_code' => 'default',
  2699. 'locale_code' => 'en',
  2700. ])->create([
  2701. 'code' => 'sales.shipping.origin.city',
  2702. 'value' => fake()->city(),
  2703. 'channel_code' => 'default',
  2704. ])->create([
  2705. 'code' => 'sales.shipping.origin.address',
  2706. 'value' => fake()->address(),
  2707. 'channel_code' => 'default',
  2708. ])->create([
  2709. 'code' => 'sales.shipping.origin.store_name',
  2710. 'value' => 'DEMO STORE',
  2711. 'channel_code' => 'default',
  2712. ])->create([
  2713. 'code' => 'sales.shipping.origin.contact',
  2714. 'value' => '1234567890',
  2715. 'channel_code' => 'default',
  2716. ])->create([
  2717. 'code' => 'sales.shipping.origin.bank_details',
  2718. 'value' => 'TEST BANK',
  2719. 'channel_code' => 'default',
  2720. ])->create([
  2721. 'code' => 'sales.shipping.origin.zipcode',
  2722. 'value' => '123456',
  2723. 'channel_code' => 'default',
  2724. ])->create([
  2725. 'code' => 'sales.taxes.categories.shipping',
  2726. 'value' => $taxCategory->id,
  2727. ])->create([
  2728. 'code' => 'sales.taxes.categories.product',
  2729. 'value' => $taxCategory->id,
  2730. ])->create([
  2731. 'code' => 'sales.taxes.calculation.based_on',
  2732. 'value' => 'shipping_origin',
  2733. ])->create([
  2734. 'code' => 'sales.taxes.calculation.product_prices',
  2735. 'value' => 'excluding_tax',
  2736. ])->create([
  2737. 'code' => 'sales.taxes.calculation.shipping_prices',
  2738. 'value' => 'excluding_tax',
  2739. ]);
  2740. $cart = cart()->addProduct($product, [
  2741. 'product_id' => $product->id,
  2742. 'quantity' => 1,
  2743. ]);
  2744. $exclTax = ($taxRate->tax_rate / 100) * $product->price;
  2745. // Act and Assert.
  2746. $response = postJson(route('shop.api.checkout.cart.estimate_shipping'), [
  2747. 'country' => $taxRate->country,
  2748. 'state' => $taxRate->state,
  2749. 'postcode' => '123456',
  2750. ])
  2751. ->assertOk()
  2752. ->assertJsonPath('data.cart.id', $cart->id)
  2753. ->assertJsonPath('data.cart.formatted_tax_total', core()->formatPrice($exclTax))
  2754. ->assertJsonPath('data.cart.formatted_sub_total_incl_tax', core()->formatPrice($product->price + $exclTax))
  2755. ->assertJsonPath('data.cart.formatted_grand_total', core()->formatPrice($product->price + $exclTax))
  2756. ->assertJsonPath('data.cart.formatted_sub_total', core()->formatPrice($product->price))
  2757. ->assertJsonPath('data.cart.items.0.id', $cart->items->first()->id)
  2758. ->assertJsonPath('data.cart.items.0.quantity', 1)
  2759. ->assertJsonPath('data.cart.items.0.type', $product->type);
  2760. $this->assertPrice($exclTax, $response->json('data.cart.tax_total'));
  2761. $this->assertPrice($product->price + $exclTax, $response->json('data.cart.sub_total_incl_tax'));
  2762. $this->assertPrice($product->price, $response->json('data.cart.sub_total'));
  2763. $this->assertPrice($product->price + $exclTax, $response->json('data.cart.grand_total'));
  2764. $this->assertPrice($product->price + $exclTax, $response->json('data.cart.items.0.price_incl_tax'));
  2765. $this->assertPrice($product->price, $response->json('data.cart.items.0.price'));
  2766. });