CustomersTest.php 10 KB

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