CustomersTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. use Illuminate\Support\Facades\Hash;
  3. use Illuminate\Support\Facades\Mail;
  4. use Webkul\Admin\Mail\Customer\NewCustomerNotification;
  5. use Webkul\Core\Models\CoreConfig;
  6. use Webkul\Customer\Models\Customer;
  7. use Webkul\Customer\Models\CustomerNote;
  8. use Webkul\Faker\Helpers\Customer as CustomerFaker;
  9. use Webkul\Shop\Mail\Customer\NoteNotification;
  10. use function Pest\Laravel\get;
  11. use function Pest\Laravel\getJson;
  12. use function Pest\Laravel\postJson;
  13. use function Pest\Laravel\putJson;
  14. it('should returns the customers page', function () {
  15. // Act and Assert.
  16. $this->loginAsAdmin();
  17. get(route('admin.customers.customers.index'))
  18. ->assertOk()
  19. ->assertSeeText(trans('admin::app.customers.customers.index.title'))
  20. ->assertSeeText(trans('admin::app.customers.customers.index.create.create-btn'));
  21. });
  22. it('should return listing items of customers', function () {
  23. // Arrange.
  24. $customer = (new CustomerFaker)->factory()->create([
  25. 'password' => Hash::make('admin123'),
  26. 'customer_source' => 'popup',
  27. ]);
  28. // Act and Assert.
  29. $this->loginAsAdmin();
  30. getJson(route('admin.customers.customers.index'), [
  31. 'X-Requested-With' => 'XMLHttpRequest',
  32. ])
  33. ->assertOk()
  34. ->assertJsonPath('records.0.customer_id', $customer->id)
  35. ->assertJsonPath('records.0.email', $customer->email)
  36. ->assertJsonPath('records.0.full_name', $customer->name)
  37. ->assertJsonPath('records.0.customer_source', 'popup');
  38. });
  39. it('should return the view page of customer', function () {
  40. // Arrange.
  41. $customer = (new CustomerFaker)->factory()->create([
  42. 'password' => Hash::make('admin123'),
  43. 'customer_source' => 'popup',
  44. ]);
  45. // Act and Assert.
  46. $this->loginAsAdmin();
  47. get(route('admin.customers.customers.view', $customer->id))
  48. ->assertOk()
  49. ->assertSeeText($customer->first_name)
  50. ->assertSeeText($customer->last_name)
  51. ->assertSeeText($customer->gender)
  52. ->assertSeeText($customer->email)
  53. ->assertSeeText($customer->phone)
  54. ->assertSeeText('popup')
  55. ->assertSeeText(trans('admin::app.customers.customers.view.title'));
  56. });
  57. it('should fail the validation with errors when certain inputs are not provided when store in customer', function () {
  58. // Act and Assert.
  59. $this->loginAsAdmin();
  60. postJson(route('admin.customers.customers.store'))
  61. ->assertJsonValidationErrorFor('first_name')
  62. ->assertJsonValidationErrorFor('last_name')
  63. ->assertJsonValidationErrorFor('gender')
  64. ->assertJsonValidationErrorFor('email')
  65. ->assertUnprocessable();
  66. });
  67. it('should create a new customer', function () {
  68. // Act and Assert.
  69. $this->loginAsAdmin();
  70. postJson(route('admin.customers.customers.store'), $data = [
  71. 'first_name' => fake()->firstName(),
  72. 'last_name' => fake()->lastName(),
  73. 'gender' => fake()->randomElement(['male', 'female', 'other']),
  74. 'email' => fake()->email(),
  75. ])
  76. ->assertOk()
  77. ->assertSeeText(trans('admin::app.customers.customers.index.create.create-success'));
  78. $this->assertModelWise([
  79. Customer::class => [
  80. [
  81. 'first_name' => $data['first_name'],
  82. 'last_name' => $data['last_name'],
  83. 'gender' => $data['gender'],
  84. 'email' => $data['email'],
  85. ],
  86. ],
  87. ]);
  88. });
  89. it('should create a new customer and send notification to the customer', function () {
  90. // Arrange.
  91. Mail::fake();
  92. CoreConfig::factory()->create([
  93. 'code' => 'emails.general.notifications.emails.general.notifications.customer_account_credentials',
  94. 'value' => 1,
  95. ]);
  96. // Act and Assert.
  97. $this->loginAsAdmin();
  98. postJson(route('admin.customers.customers.store'), $data = [
  99. 'first_name' => fake()->firstName(),
  100. 'last_name' => fake()->lastName(),
  101. 'gender' => fake()->randomElement(['male', 'female', 'other']),
  102. 'email' => fake()->email(),
  103. ])
  104. ->assertOk()
  105. ->assertSeeText(trans('admin::app.customers.customers.index.create.create-success'));
  106. $this->assertModelWise([
  107. Customer::class => [
  108. [
  109. 'first_name' => $data['first_name'],
  110. 'last_name' => $data['last_name'],
  111. 'gender' => $data['gender'],
  112. 'email' => $data['email'],
  113. ],
  114. ],
  115. ]);
  116. Mail::assertQueued(NewCustomerNotification::class);
  117. });
  118. it('should search the customers for mega search', function () {
  119. // Arrange.
  120. $customer = (new CustomerFaker)->factory()->create([
  121. 'password' => Hash::make('admin123'),
  122. ]);
  123. // Act and Assert.
  124. $this->loginAsAdmin();
  125. getJson(route('admin.customers.customers.search'), [
  126. 'query' => $customer->name,
  127. ])
  128. ->assertOk()
  129. ->assertJsonPath('data.0.id', $customer->id)
  130. ->assertJsonPath('data.0.first_name', $customer->first_name)
  131. ->assertJsonPath('data.0.email', $customer->email);
  132. });
  133. it('should login the customer from the admin panel', function () {
  134. // Arrange.
  135. $customer = (new CustomerFaker)->factory()->create([
  136. 'password' => Hash::make('admin123'),
  137. ]);
  138. // Act and Assert.
  139. $this->loginAsAdmin();
  140. get(route('admin.customers.customers.login_as_customer', $customer->id))
  141. ->assertRedirect(route('shop.customers.account.profile.index'))
  142. ->isRedirection();
  143. });
  144. it('should fail the validation with errors for notes', function () {
  145. // Arrange.
  146. $customer = (new CustomerFaker)->factory()->create([
  147. 'password' => Hash::make('admin123'),
  148. ]);
  149. // Act and Assert.
  150. $this->loginAsAdmin();
  151. postJson(route('admin.customer.note.store', $customer->id))
  152. ->assertJsonValidationErrorFor('note')
  153. ->assertUnprocessable();
  154. });
  155. it('should store the notes for the customer', function () {
  156. // Arrange.
  157. $customer = (new CustomerFaker)->factory()->create([
  158. 'password' => Hash::make('admin123'),
  159. ]);
  160. // Act and Assert.
  161. $this->loginAsAdmin();
  162. postJson(route('admin.customer.note.store', $customer->id), [
  163. 'note' => $note = substr(fake()->paragraph(), 0, 50),
  164. ])
  165. ->assertRedirect(route('admin.customers.customers.view', $customer->id))
  166. ->isRedirection();
  167. $this->assertModelWise([
  168. CustomerNote::class => [
  169. [
  170. 'note' => $note,
  171. ],
  172. ],
  173. ]);
  174. });
  175. it('should store the notes for the customer and send email to the customer', function () {
  176. // Arrange.
  177. Mail::fake();
  178. $customer = (new CustomerFaker)->factory()->create([
  179. 'password' => Hash::make('admin123'),
  180. ]);
  181. // Act and Assert.
  182. $this->loginAsAdmin();
  183. postJson(route('admin.customer.note.store', $customer->id), [
  184. 'note' => $note = substr(fake()->paragraph(), 0, 50),
  185. 'customer_notified' => 1,
  186. ])
  187. ->assertRedirect(route('admin.customers.customers.view', $customer->id))
  188. ->isRedirection();
  189. $this->assertModelWise([
  190. CustomerNote::class => [
  191. [
  192. 'note' => $note,
  193. ],
  194. ],
  195. ]);
  196. Mail::assertQueued(NoteNotification::class);
  197. Mail::assertQueuedCount(1);
  198. });
  199. it('should fail the validation with errors when certain inputs are not provided when update in customer', function () {
  200. // Arrange.
  201. $customer = (new CustomerFaker)->factory()->create([
  202. 'password' => Hash::make('admin123'),
  203. ]);
  204. // Act and Assert.
  205. $this->loginAsAdmin();
  206. putJson(route('admin.customers.customers.update', $customer->id))
  207. ->assertJsonValidationErrorFor('first_name')
  208. ->assertJsonValidationErrorFor('last_name')
  209. ->assertJsonValidationErrorFor('gender')
  210. ->assertJsonValidationErrorFor('email')
  211. ->assertUnprocessable();
  212. });
  213. it('should update the the existing customer', function () {
  214. // Arrange.
  215. $customer = (new CustomerFaker)->factory()->create([
  216. 'password' => Hash::make('admin123'),
  217. ]);
  218. // Act and Assert.
  219. $this->loginAsAdmin();
  220. putJson(route('admin.customers.customers.update', $customer->id), $data = [
  221. 'first_name' => fake()->firstName(),
  222. 'last_name' => $customer->last_name,
  223. 'gender' => $customer->gender,
  224. 'email' => fake()->email(),
  225. ])
  226. ->assertOk()
  227. ->assertJsonPath('message', trans('admin::app.customers.customers.update-success'));
  228. $this->assertModelWise([
  229. Customer::class => [
  230. [
  231. 'first_name' => $data['first_name'],
  232. 'last_name' => $customer->last_name,
  233. 'gender' => $customer->gender,
  234. 'email' => $data['email'],
  235. ],
  236. ],
  237. ]);
  238. });
  239. it('should mass delete the customers', function () {
  240. // Arrange.
  241. $customers = (new CustomerFaker)->factory()->count(2)->create([
  242. 'password' => Hash::make('admin123'),
  243. ]);
  244. // Act and Assert.
  245. $this->loginAsAdmin();
  246. postJson(route('admin.customers.customers.mass_delete'), [
  247. 'indices' => $customers->pluck('id')->toArray(),
  248. ])
  249. ->assertOk()
  250. ->assertSeeText(trans('admin::app.customers.customers.index.datagrid.delete-success'));
  251. foreach ($customers as $customer) {
  252. $this->assertDatabaseMissing('customers', [
  253. 'id' => $customer->id,
  254. ]);
  255. }
  256. });
  257. it('should mass update the customers', function () {
  258. // Arrange.
  259. $customers = (new CustomerFaker)->factory()->count(2)->create([
  260. 'password' => Hash::make('admin123'),
  261. ]);
  262. // Act and Assert.
  263. $this->loginAsAdmin();
  264. postJson(route('admin.customers.customers.mass_update'), [
  265. 'indices' => $customers->pluck('id')->toArray(),
  266. 'value' => 1,
  267. ])
  268. ->assertOk()
  269. ->assertSeeText(trans('admin::app.customers.customers.index.datagrid.update-success'));
  270. foreach ($customers as $customer) {
  271. $this->assertModelWise([
  272. Customer::class => [
  273. [
  274. 'id' => $customer->id,
  275. 'status' => 1,
  276. ],
  277. ],
  278. ]);
  279. }
  280. });
  281. it('should delete a specific customer', function () {
  282. // Arrange.
  283. $customer = (new CustomerFaker)->factory()->create([
  284. 'password' => Hash::make('admin123'),
  285. ]);
  286. // Act and Assert.
  287. $this->loginAsAdmin();
  288. postJson(route('admin.customers.customers.delete', $customer->id))
  289. ->assertRedirect(route('admin.customers.customers.index'))
  290. ->isRedirection();
  291. $this->assertDatabaseMissing('customers', [
  292. 'id' => $customer->id,
  293. ]);
  294. });