ReviewTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. use Illuminate\Http\UploadedFile;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Facades\Storage;
  5. use Webkul\Customer\Models\Customer;
  6. use Webkul\Faker\Helpers\Product as ProductFaker;
  7. use Webkul\Product\Models\ProductReview;
  8. use Webkul\Product\Models\ProductReviewAttachment;
  9. use function Pest\Laravel\deleteJson;
  10. use function Pest\Laravel\get;
  11. use function Pest\Laravel\postJson;
  12. use function Pest\Laravel\putJson;
  13. it('should returns the review page', function () {
  14. // Act and Assert.
  15. $this->loginAsAdmin();
  16. get(route('admin.customers.customers.review.index'))
  17. ->assertOk()
  18. ->assertSeeText(trans('admin::app.customers.reviews.index.title'));
  19. });
  20. it('should return the edit page of the review', function () {
  21. // Arrange.
  22. $product = (new ProductFaker([
  23. 'attributes' => [
  24. 5 => 'new',
  25. ],
  26. 'attribute_value' => [
  27. 'new' => [
  28. 'boolean_value' => true,
  29. ],
  30. ],
  31. ]))
  32. ->getSimpleProductFactory()
  33. ->create();
  34. $customer = Customer::factory()->create();
  35. $productReview = ProductReview::factory()->create([
  36. 'product_id' => $product->id,
  37. 'customer_id' => $customer->id,
  38. 'name' => $customer->name,
  39. ]);
  40. $attachment = UploadedFile::fake()->image('test.png');
  41. $fileType = explode('/', $attachment->getMimeType());
  42. $productReviewAttachment = ProductReviewAttachment::factory()->create([
  43. 'path' => $attachment->store('review/'.$productReview->id),
  44. 'review_id' => $productReview->id,
  45. 'type' => $fileType[0],
  46. 'mime_type' => $fileType[1],
  47. ]);
  48. // Act and Assert.
  49. $this->loginAsAdmin();
  50. get(route('admin.customers.customers.review.edit', $productReview->id))
  51. ->assertOk()
  52. ->assertJsonPath('data.id', $productReview->id)
  53. ->assertJsonPath('data.title', $productReview->title)
  54. ->assertJsonPath('data.comment', $productReview->comment);
  55. $this->assertModelWise([
  56. ProductReviewAttachment::class => [
  57. [
  58. 'review_id' => $productReview->id,
  59. 'path' => $productReviewAttachment->path,
  60. 'type' => $productReviewAttachment->type,
  61. 'mime_type' => $productReviewAttachment->mime_type,
  62. ],
  63. ],
  64. ProductReview::class => [
  65. [
  66. 'name' => $productReview->name,
  67. 'title' => $productReview->title,
  68. 'rating' => $productReview->rating,
  69. 'comment' => $productReview->comment,
  70. 'status' => $productReview->status,
  71. 'product_id' => $productReview->product_id,
  72. 'customer_id' => $productReview->customer_id,
  73. ],
  74. ],
  75. ]);
  76. Storage::assertExists($productReviewAttachment->path);
  77. });
  78. it('should fail the validation with errors for status for review update', function () {
  79. // Arrange.
  80. $product = (new ProductFaker([
  81. 'attributes' => [
  82. 5 => 'new',
  83. ],
  84. 'attribute_value' => [
  85. 'new' => [
  86. 'boolean_value' => true,
  87. ],
  88. ],
  89. ]))
  90. ->getSimpleProductFactory()
  91. ->create();
  92. $customer = Customer::factory()->create();
  93. $productReview = ProductReview::factory()->create([
  94. 'product_id' => $product->id,
  95. 'customer_id' => $customer->id,
  96. 'name' => $customer->name,
  97. ]);
  98. $attachment = UploadedFile::fake()->image('test.png');
  99. $fileType = explode('/', $attachment->getMimeType());
  100. $productReviewAttachment = ProductReviewAttachment::factory()->create([
  101. 'path' => $attachment->store('review/'.$productReview->id),
  102. 'review_id' => $productReview->id,
  103. 'type' => $fileType[0],
  104. 'mime_type' => $fileType[1],
  105. ]);
  106. // Act and Assert.
  107. $this->loginAsAdmin();
  108. putJson(route('admin.customers.customers.review.update', $productReview->id))
  109. ->assertJsonValidationErrorFor('status')
  110. ->assertUnprocessable();
  111. $this->assertModelWise([
  112. ProductReviewAttachment::class => [
  113. [
  114. 'review_id' => $productReview->id,
  115. 'path' => $productReviewAttachment->path,
  116. 'type' => $productReviewAttachment->type,
  117. 'mime_type' => $productReviewAttachment->mime_type,
  118. ],
  119. ],
  120. ProductReview::class => [
  121. [
  122. 'name' => $productReview->name,
  123. 'title' => $productReview->title,
  124. 'rating' => $productReview->rating,
  125. 'comment' => $productReview->comment,
  126. 'status' => $productReview->status,
  127. 'product_id' => $productReview->product_id,
  128. 'customer_id' => $productReview->customer_id,
  129. ],
  130. ],
  131. ]);
  132. Storage::assertExists($productReviewAttachment->path);
  133. });
  134. it('should update the status of the review', function () {
  135. // Arrange.
  136. $product = (new ProductFaker([
  137. 'attributes' => [
  138. 5 => 'new',
  139. ],
  140. 'attribute_value' => [
  141. 'new' => [
  142. 'boolean_value' => true,
  143. ],
  144. ],
  145. ]))
  146. ->getSimpleProductFactory()
  147. ->create();
  148. $customer = Customer::factory()->create();
  149. $productReview = ProductReview::factory()->create([
  150. 'product_id' => $product->id,
  151. 'customer_id' => $customer->id,
  152. 'name' => $customer->name,
  153. ]);
  154. $attachment = UploadedFile::fake()->image('test.png');
  155. $fileType = explode('/', $attachment->getMimeType());
  156. $productReviewAttachment = ProductReviewAttachment::factory()->create([
  157. 'path' => $attachment->store('review/'.$productReview->id),
  158. 'review_id' => $productReview->id,
  159. 'type' => $fileType[0],
  160. 'mime_type' => $fileType[1],
  161. ]);
  162. // Act and Assert.
  163. $this->loginAsAdmin();
  164. putJson(route('admin.customers.customers.review.update', $productReview->id), [
  165. 'status' => $status = Arr::random(['approved', 'disapproved', 'pending']),
  166. ])
  167. ->assertOk()
  168. ->assertJsonPath('message', trans('admin::app.customers.reviews.index.edit.update-success'));
  169. $this->assertModelWise([
  170. ProductReviewAttachment::class => [
  171. [
  172. 'review_id' => $productReview->id,
  173. 'path' => $productReviewAttachment->path,
  174. 'type' => $productReviewAttachment->type,
  175. 'mime_type' => $productReviewAttachment->mime_type,
  176. ],
  177. ],
  178. ProductReview::class => [
  179. [
  180. 'name' => $productReview->name,
  181. 'title' => $productReview->title,
  182. 'rating' => $productReview->rating,
  183. 'comment' => $productReview->comment,
  184. 'status' => $status,
  185. 'product_id' => $productReview->product_id,
  186. 'customer_id' => $productReview->customer_id,
  187. ],
  188. ],
  189. ]);
  190. Storage::assertExists($productReviewAttachment->path);
  191. });
  192. it('should delete the review', function () {
  193. // Arrange.
  194. $product = (new ProductFaker([
  195. 'attributes' => [
  196. 5 => 'new',
  197. ],
  198. 'attribute_value' => [
  199. 'new' => [
  200. 'boolean_value' => true,
  201. ],
  202. ],
  203. ]))
  204. ->getSimpleProductFactory()
  205. ->create();
  206. $customer = Customer::factory()->create();
  207. $productReview = ProductReview::factory()->create([
  208. 'product_id' => $product->id,
  209. 'customer_id' => $customer->id,
  210. 'name' => $customer->name,
  211. ]);
  212. $attachment = UploadedFile::fake()->image('test.png');
  213. $fileType = explode('/', $attachment->getMimeType());
  214. $productReviewAttachment = ProductReviewAttachment::factory()->create([
  215. 'path' => $attachment->store('review/'.$productReview->id),
  216. 'review_id' => $productReview->id,
  217. 'type' => $fileType[0],
  218. 'mime_type' => $fileType[1],
  219. ]);
  220. // Act and Assert.
  221. $this->loginAsAdmin();
  222. deleteJson(route('admin.customers.customers.review.delete', $productReview->id))
  223. ->assertOk()
  224. ->assertSeeText(trans('admin::app.customers.reviews.index.datagrid.delete-success'));
  225. $this->assertDatabaseMissing('product_reviews', [
  226. 'id' => $productReview->id,
  227. ]);
  228. $this->assertDatabaseMissing('product_review_attachments', [
  229. 'id' => $productReviewAttachment->id,
  230. ]);
  231. Storage::assertDirectoryEmpty($productReviewAttachment->path);
  232. });
  233. it('should mass delete the product review', function () {
  234. // Arrange.
  235. $product = (new ProductFaker([
  236. 'attributes' => [
  237. 5 => 'new',
  238. ],
  239. 'attribute_value' => [
  240. 'new' => [
  241. 'boolean_value' => true,
  242. ],
  243. ],
  244. ]))
  245. ->getSimpleProductFactory()
  246. ->create();
  247. $customer = Customer::factory()->create();
  248. $productReviews = ProductReview::factory()->count(5)->create([
  249. 'product_id' => $product->id,
  250. 'customer_id' => $customer->id,
  251. 'name' => $customer->name,
  252. ]);
  253. $productReviewAttachments = [];
  254. foreach ($productReviews as $key => $productReview) {
  255. $attachment = UploadedFile::fake()->image('test_'.$key.'.png');
  256. $fileType = explode('/', $attachment->getMimeType());
  257. $productReviewAttachments[] = ProductReviewAttachment::factory()->create([
  258. 'path' => $attachment->store('review/'.$productReview->id),
  259. 'review_id' => $productReview->id,
  260. 'type' => $fileType[0],
  261. 'mime_type' => $fileType[1],
  262. ]);
  263. }
  264. // Act and Assert.
  265. $this->loginAsAdmin();
  266. postJson(route('admin.customers.customers.review.mass_delete', [
  267. 'indices' => $productReviews->pluck('id')->toArray(),
  268. ]))
  269. ->assertOk()
  270. ->assertSeeText(trans('admin::app.customers.reviews.index.datagrid.mass-delete-success'));
  271. foreach ($productReviews as $productReview) {
  272. $this->assertDatabaseMissing('product_reviews', [
  273. 'id' => $productReview->id,
  274. ]);
  275. }
  276. foreach ($productReviewAttachments as $productReviewAttachment) {
  277. $this->assertDatabaseMissing('product_review_attachments', [
  278. 'id' => $productReviewAttachment->id,
  279. ]);
  280. Storage::assertDirectoryEmpty($productReviewAttachment->path);
  281. }
  282. });
  283. it('should mass update the product review', function () {
  284. // Arrange.
  285. $status = Arr::random(['approved', 'disapproved', 'pending']);
  286. $product = (new ProductFaker([
  287. 'attributes' => [
  288. 5 => 'new',
  289. ],
  290. 'attribute_value' => [
  291. 'new' => [
  292. 'boolean_value' => true,
  293. ],
  294. ],
  295. ]))
  296. ->getSimpleProductFactory()
  297. ->create();
  298. $productReviews = ProductReview::factory()->count(2)->create([
  299. 'status' => $status,
  300. 'product_id' => $product->id,
  301. ]);
  302. // Act and Assert.
  303. $this->loginAsAdmin();
  304. postJson(route('admin.customers.customers.review.mass_update', [
  305. 'indices' => $productReviews->pluck('id')->toArray(),
  306. 'value' => $status,
  307. ]))
  308. ->assertOk()
  309. ->assertSeeText(trans('admin::app.customers.reviews.index.datagrid.mass-update-success'));
  310. foreach ($productReviews as $productReview) {
  311. $this->assertModelWise([
  312. ProductReview::class => [
  313. [
  314. 'id' => $productReview->id,
  315. 'status' => $productReview->status,
  316. ],
  317. ],
  318. ]);
  319. }
  320. });