AccountTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. <?php
  2. use Illuminate\Http\UploadedFile;
  3. use Illuminate\Support\Facades\Hash;
  4. use Illuminate\Support\Facades\Mail;
  5. use Illuminate\Support\Facades\Notification;
  6. use Webkul\Customer\Models\Customer;
  7. use Webkul\Customer\Models\CustomerAddress;
  8. use Webkul\Faker\Helpers\Product as ProductFaker;
  9. use Webkul\Product\Models\ProductReview;
  10. use Webkul\Shop\Mail\Customer\ResetPasswordNotification;
  11. use Webkul\Shop\Mail\Customer\UpdatePasswordNotification;
  12. use function Pest\Laravel\deleteJson;
  13. use function Pest\Laravel\get;
  14. use function Pest\Laravel\patchJson;
  15. use function Pest\Laravel\postJson;
  16. use function Pest\Laravel\putJson;
  17. it('should returns the profile page', function () {
  18. // Act and Assert.
  19. $customer = $this->loginAsCustomer();
  20. get(route('shop.customers.account.profile.index'))
  21. ->assertOk()
  22. ->assertSeeText(trans('shop::app.customers.account.profile.index.edit'))
  23. ->assertSeeText(trans('shop::app.customers.account.profile.index.delete'))
  24. ->assertSeeText($customer->first_name)
  25. ->assertSeeText($customer->last_name)
  26. ->assertSeeText($customer->email);
  27. });
  28. it('should returns the edit page of the customer', function () {
  29. // Act and Assert.
  30. $customer = $this->loginAsCustomer();
  31. get(route('shop.customers.account.profile.edit'))
  32. ->assertOk()
  33. ->assertSeeText($customer->email)
  34. ->assertSeeText($customer->first_name)
  35. ->assertSeeText(trans('shop::app.customers.account.profile.edit.edit-profile'));
  36. });
  37. it('should fails the validations error when certain inputs are not provided when update the customer', function () {
  38. // Act and Assert.
  39. $this->loginAsCustomer();
  40. postJson(route('shop.customers.account.profile.update'), [
  41. 'gender' => 'UNKNOWN_GENDER',
  42. 'date_of_birth' => now()->tomorrow()->toDateString(),
  43. 'email' => 'WRONG_EMAIL_FORMAT',
  44. 'image' => 'INVALID_FORMAT_IMAGE',
  45. ])
  46. ->assertJsonValidationErrorFor('first_name')
  47. ->assertJsonValidationErrorFor('last_name')
  48. ->assertJsonValidationErrorFor('gender')
  49. ->assertJsonValidationErrorFor('phone')
  50. ->assertJsonValidationErrorFor('date_of_birth')
  51. ->assertJsonValidationErrorFor('email')
  52. ->assertJsonValidationErrorFor('image')
  53. ->assertUnprocessable();
  54. });
  55. it('should update the customer', function () {
  56. // Act and Assert.
  57. $customer = $this->loginAsCustomer();
  58. postJson(route('shop.customers.account.profile.update'), [
  59. 'first_name' => $firstName = 'test',
  60. 'last_name' => $lastName = fake()->lastName(),
  61. 'gender' => $gender = fake()->randomElement(['Other', 'Male', 'Female']),
  62. 'email' => $customer->email,
  63. 'status' => 1,
  64. 'customer_group_id' => 2,
  65. 'phone' => $phone = fake()->e164PhoneNumber(),
  66. 'date_of_birth' => now()->subYear(20)->toDateString(),
  67. 'subscribed_to_news_letter' => true,
  68. 'image' => [
  69. UploadedFile::fake()->image('TEST.png'),
  70. ],
  71. ])
  72. ->assertRedirect(route('shop.customers.account.profile.index'));
  73. $this->assertModelWise([
  74. Customer::class => [
  75. [
  76. 'first_name' => $firstName,
  77. 'last_name' => $lastName,
  78. 'gender' => $gender,
  79. 'email' => $customer->email,
  80. 'status' => 1,
  81. 'customer_group_id' => 2,
  82. 'phone' => $phone,
  83. ],
  84. ],
  85. ]);
  86. });
  87. it('should update the customer password and send email to the customer', function () {
  88. // Act and Assert.
  89. Mail::fake();
  90. $customer = Customer::factory()->create([
  91. 'password' => Hash::make($currentPassword = fake()->password(8, 10)),
  92. ]);
  93. $customer = $this->loginAsCustomer($customer);
  94. postJson(route('shop.customers.account.profile.update'), [
  95. 'first_name' => $firstName = fake()->firstName(),
  96. 'last_name' => $lastName = fake()->lastName(),
  97. 'gender' => $gender = fake()->randomElement(['Other', 'Male', 'Female']),
  98. 'email' => $customer->email,
  99. 'status' => 1,
  100. 'customer_group_id' => 2,
  101. 'phone' => $phone = fake()->e164PhoneNumber(),
  102. 'current_password' => $currentPassword,
  103. 'new_password' => $newPassword = fake()->password(8, 10),
  104. 'new_password_confirmation' => $newPassword,
  105. ])
  106. ->assertRedirect(route('shop.customers.account.profile.index'));
  107. $this->assertModelWise([
  108. Customer::class => [
  109. [
  110. 'first_name' => $firstName,
  111. 'last_name' => $lastName,
  112. 'gender' => $gender,
  113. 'email' => $customer->email,
  114. 'status' => 1,
  115. 'customer_group_id' => 2,
  116. 'phone' => $phone,
  117. ],
  118. ],
  119. ]);
  120. Mail::assertQueued(UpdatePasswordNotification::class);
  121. Mail::assertQueuedCount(1);
  122. });
  123. it('should fails the validation error when password is not provided when delete the customer account', function () {
  124. // Act and Assert.
  125. $this->loginAsCustomer();
  126. postJson(route('shop.customers.account.profile.destroy'))
  127. ->assertJsonValidationErrorFor('password')
  128. ->assertUnprocessable();
  129. });
  130. it('should delete the customer account', function () {
  131. // Arrange.
  132. $customer = Customer::factory()->create([
  133. 'password' => Hash::make('admin123'),
  134. ]);
  135. // Act and Assert.
  136. $this->loginAsCustomer($customer);
  137. postJson(route('shop.customers.account.profile.destroy'), [
  138. 'password' => 'admin123',
  139. ])
  140. ->assertRedirect(route('shop.customer.session.index'));
  141. $this->assertDatabaseMissing('customers', [
  142. 'id' => $customer->id,
  143. ]);
  144. });
  145. it('should shows the reviews of customer', function () {
  146. // Arrange.
  147. $product = (new ProductFaker([
  148. 'attributes' => [
  149. 5 => 'new',
  150. 6 => 'featured',
  151. 11 => 'price',
  152. 26 => 'guest_checkout',
  153. ],
  154. 'attribute_value' => [
  155. 'new' => [
  156. 'boolean_value' => true,
  157. ],
  158. 'featured' => [
  159. 'boolean_value' => true,
  160. ],
  161. 'price' => [
  162. 'float_value' => fake()->randomFloat(2, 1000, 5000),
  163. ],
  164. 'guest_checkout' => [
  165. 'boolean_value' => true,
  166. ],
  167. ],
  168. ]))->getSimpleProductFactory()->create();
  169. $customer = Customer::factory()->create();
  170. $productReview = ProductReview::factory()->create([
  171. 'product_id' => $product->id,
  172. 'customer_id' => $customer->id,
  173. ]);
  174. // Act and Assert.
  175. $customer = $this->loginAsCustomer($customer);
  176. get(route('shop.customers.account.reviews.index'))
  177. ->assertOk()
  178. ->assertSeeText(trans('shop::app.customers.account.reviews.title'))
  179. ->assertSeeText($productReview->title)
  180. ->assertSeeText($productReview->comment);
  181. });
  182. it('should returns the address page of the customer', function () {
  183. // Arrange.
  184. $customer = Customer::factory()->create();
  185. $customerAddress = CustomerAddress::factory()->create([
  186. 'customer_id' => $customer->id,
  187. ]);
  188. // Act and Assert.
  189. $this->loginAsCustomer($customer);
  190. get(route('shop.customers.account.addresses.index'))
  191. ->assertOk()
  192. ->assertSeeText($customerAddress->fast_name)
  193. ->assertSeeText($customerAddress->last_name)
  194. ->assertSeeText($customerAddress->address)
  195. ->assertSeeText($customerAddress->city)
  196. ->assertSeeText($customerAddress->state)
  197. ->assertSeeText($customerAddress->company_name)
  198. ->assertSeeText(trans('shop::app.customers.account.addresses.index.add-address'));
  199. });
  200. it('should returns the create page of address', function () {
  201. // Act and Assert.
  202. $this->loginAsCustomer();
  203. get(route('shop.customers.account.addresses.create'))
  204. ->assertOk()
  205. ->assertSeeText(trans('shop::app.customers.account.addresses.index.add-address'))
  206. ->assertSeeText(trans('shop::app.customers.account.addresses.create.first-name'))
  207. ->assertSeeText(trans('shop::app.customers.account.addresses.create.last-name'))
  208. ->assertSeeText(trans('shop::app.customers.account.addresses.create.vat-id'))
  209. ->assertSeeText(trans('shop::app.customers.account.addresses.create.street-address'))
  210. ->assertSeeText(trans('shop::app.customers.account.addresses.create.company-name'));
  211. });
  212. it('should fails the validation error when certain inputs not provided when store the customer address', function () {
  213. // Act and Assert.
  214. $this->loginAsCustomer();
  215. postJson(route('shop.customers.account.addresses.store'))
  216. ->assertJsonValidationErrorFor('city')
  217. ->assertJsonValidationErrorFor('phone')
  218. ->assertJsonValidationErrorFor('state')
  219. ->assertJsonValidationErrorFor('country')
  220. ->assertJsonValidationErrorFor('address')
  221. ->assertJsonValidationErrorFor('postcode')
  222. ->assertJsonValidationErrorFor('last_name')
  223. ->assertJsonValidationErrorFor('first_name')
  224. ->assertJsonValidationErrorFor('email')
  225. ->assertUnprocessable();
  226. });
  227. it('should store the customer address', function () {
  228. // Act and Assert.
  229. $customer = $this->loginAsCustomer();
  230. postJson(route('shop.customers.account.addresses.store'), [
  231. 'customer_id' => $customer->id,
  232. 'company_name' => $companyName = fake()->word(),
  233. 'first_name' => $firstName = fake()->firstName(),
  234. 'last_name' => $lastName = fake()->lastName(),
  235. 'address' => [fake()->word()],
  236. 'country' => $countryCode = fake()->countryCode(),
  237. 'state' => $state = fake()->state(),
  238. 'city' => $city = fake()->city(),
  239. 'postcode' => $postCode = rand(11111, 99999),
  240. 'phone' => $phoneNumber = fake()->e164PhoneNumber(),
  241. 'default_address' => fake()->randomElement([0, 1]),
  242. 'address_type' => $addressType = CustomerAddress::ADDRESS_TYPE,
  243. 'email' => $email = fake()->email(),
  244. ])
  245. ->assertRedirect(route('shop.customers.account.addresses.index'));
  246. $this->assertModelWise([
  247. CustomerAddress::class => [
  248. [
  249. 'customer_id' => $customer->id,
  250. 'company_name' => $companyName,
  251. 'first_name' => $firstName,
  252. 'last_name' => $lastName,
  253. 'country' => $countryCode,
  254. 'state' => $state,
  255. 'city' => $city,
  256. 'postcode' => $postCode,
  257. 'phone' => $phoneNumber,
  258. 'address_type' => $addressType,
  259. 'email' => $email,
  260. ],
  261. ],
  262. ]);
  263. });
  264. it('should edit the customer address', function () {
  265. // Arrange.
  266. $customer = Customer::factory()->create();
  267. $customerAddress = CustomerAddress::factory()->create([
  268. 'customer_id' => $customer->id,
  269. ]);
  270. // Act and Assert.
  271. $this->loginAsCustomer($customer);
  272. get(route('shop.customers.account.addresses.edit', $customerAddress->id))
  273. ->assertOk()
  274. ->assertSeeText(trans('shop::app.customers.account.addresses.edit.edit'))
  275. ->assertSeeText(trans('shop::app.customers.account.addresses.edit.title'))
  276. ->assertSeeText(trans('shop::app.customers.account.addresses.edit.update-btn'));
  277. });
  278. it('should fails the validation error when certain inputs not provided update the customer address', function () {
  279. $customer = Customer::factory()->create();
  280. $customerAddress = CustomerAddress::factory()->create([
  281. 'customer_id' => $customer->id,
  282. ]);
  283. // Act and Assert.
  284. $this->loginAsCustomer($customer);
  285. putJson(route('shop.customers.account.addresses.update', $customerAddress->id))
  286. ->assertJsonValidationErrorFor('city')
  287. ->assertJsonValidationErrorFor('phone')
  288. ->assertJsonValidationErrorFor('state')
  289. ->assertJsonValidationErrorFor('country')
  290. ->assertJsonValidationErrorFor('address')
  291. ->assertJsonValidationErrorFor('postcode')
  292. ->assertJsonValidationErrorFor('last_name')
  293. ->assertJsonValidationErrorFor('first_name')
  294. ->assertJsonValidationErrorFor('email')
  295. ->assertUnprocessable();
  296. });
  297. it('should update the customer address', function () {
  298. $customer = Customer::factory()->create();
  299. $customerAddress = CustomerAddress::factory()->create([
  300. 'customer_id' => $customer->id,
  301. ]);
  302. // Act and Assert.
  303. $this->loginAsCustomer($customer);
  304. putJson(route('shop.customers.account.addresses.update', $customerAddress->id), [
  305. 'customer_id' => $customer->id,
  306. 'company_name' => $companyName = fake()->word(),
  307. 'first_name' => $firstName = fake()->firstName(),
  308. 'last_name' => $lastName = fake()->lastName(),
  309. 'address' => [fake()->word()],
  310. 'country' => $customerAddress->country,
  311. 'state' => $customerAddress->state,
  312. 'city' => $customerAddress->city,
  313. 'postcode' => $postCode = rand(1111, 99999),
  314. 'phone' => $customerAddress->phone,
  315. 'default_address' => 1,
  316. 'address_type' => $customerAddress->address_type,
  317. 'email' => $email = fake()->email(),
  318. ])
  319. ->assertRedirect(route('shop.customers.account.addresses.index'));
  320. $this->assertModelWise([
  321. CustomerAddress::class => [
  322. [
  323. 'customer_id' => $customer->id,
  324. 'company_name' => $companyName,
  325. 'first_name' => $firstName,
  326. 'last_name' => $lastName,
  327. 'country' => $customerAddress->country,
  328. 'state' => $customerAddress->state,
  329. 'city' => $customerAddress->city,
  330. 'postcode' => $postCode,
  331. 'phone' => $customerAddress->phone,
  332. 'default_address' => $customerAddress->default_address,
  333. 'address_type' => $customerAddress->address_type,
  334. 'email' => $email,
  335. ],
  336. ],
  337. ]);
  338. });
  339. it('should set default address for the customer', function () {
  340. // Arrange.
  341. $customer = Customer::factory()->create();
  342. $customerAddresses = CustomerAddress::factory()->create([
  343. 'customer_id' => $customer->id,
  344. 'default_address' => 0,
  345. ]);
  346. // Act and Assert.
  347. $this->loginAsCustomer($customer);
  348. patchJson(route('shop.customers.account.addresses.update.default', $customerAddresses->id))
  349. ->assertRedirect();
  350. $this->assertModelWise([
  351. CustomerAddress::class => [
  352. [
  353. 'customer_id' => $customer->id,
  354. 'default_address' => 1,
  355. ],
  356. ],
  357. ]);
  358. });
  359. it('should delete the customer address', function () {
  360. // Arrange.
  361. $customer = Customer::factory()->create();
  362. $customerAddress = CustomerAddress::factory()->create([
  363. 'customer_id' => $customer->id,
  364. 'default_address' => 0,
  365. ]);
  366. // Act and Assert.
  367. $this->loginAsCustomer($customer);
  368. deleteJson(route('shop.customers.account.addresses.delete', $customerAddress->id))
  369. ->assertRedirect();
  370. $this->assertDatabaseMissing('addresses', [
  371. 'customer_id' => $customer->id,
  372. 'id' => $customerAddress->id,
  373. ]);
  374. });
  375. it('should send email for password reset', function () {
  376. // Arrange.
  377. Notification::fake();
  378. $customer = Customer::factory()->create();
  379. postJson(route('shop.customers.forgot_password.store'), [
  380. 'email' => $customer->email,
  381. ])
  382. ->assertRedirect(route('shop.customers.forgot_password.create'))
  383. ->isRedirect();
  384. $this->assertDatabaseHas('customer_password_resets', [
  385. 'email' => $customer->email,
  386. ]);
  387. Notification::assertSentTo(
  388. $customer,
  389. ResetPasswordNotification::class,
  390. );
  391. Notification::assertCount(1);
  392. });
  393. it('should not send email for password reset when email is invalid', function () {
  394. // Arrange.
  395. postJson(route('shop.customers.forgot_password.store'), [
  396. 'email' => $email = 'WRONG_EMAIL@gmail.com',
  397. ])
  398. ->assertRedirect(route('shop.customers.forgot_password.create'))
  399. ->isRedirect();
  400. $this->assertDatabaseMissing('customer_password_resets', [
  401. 'email' => $email,
  402. ]);
  403. });
  404. it('should fails the validation errors certain inputs not provided', function () {
  405. // Arrange.
  406. postJson(route('shop.customers.forgot_password.store'))
  407. ->assertJsonValidationErrorFor('email');
  408. });