| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- <?php
- use Illuminate\Http\UploadedFile;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Storage;
- use Webkul\Customer\Models\Customer;
- use Webkul\Faker\Helpers\Product as ProductFaker;
- use Webkul\Product\Models\ProductReview;
- use Webkul\Product\Models\ProductReviewAttachment;
- use function Pest\Laravel\deleteJson;
- use function Pest\Laravel\get;
- use function Pest\Laravel\postJson;
- use function Pest\Laravel\putJson;
- it('should returns the review page', function () {
- // Act and Assert.
- $this->loginAsAdmin();
- get(route('admin.customers.customers.review.index'))
- ->assertOk()
- ->assertSeeText(trans('admin::app.customers.reviews.index.title'));
- });
- it('should return the edit page of the review', function () {
- // Arrange.
- $product = (new ProductFaker([
- 'attributes' => [
- 5 => 'new',
- ],
- 'attribute_value' => [
- 'new' => [
- 'boolean_value' => true,
- ],
- ],
- ]))
- ->getSimpleProductFactory()
- ->create();
- $customer = Customer::factory()->create();
- $productReview = ProductReview::factory()->create([
- 'product_id' => $product->id,
- 'customer_id' => $customer->id,
- 'name' => $customer->name,
- ]);
- $attachment = UploadedFile::fake()->image('test.png');
- $fileType = explode('/', $attachment->getMimeType());
- $productReviewAttachment = ProductReviewAttachment::factory()->create([
- 'path' => $attachment->store('review/'.$productReview->id),
- 'review_id' => $productReview->id,
- 'type' => $fileType[0],
- 'mime_type' => $fileType[1],
- ]);
- // Act and Assert.
- $this->loginAsAdmin();
- get(route('admin.customers.customers.review.edit', $productReview->id))
- ->assertOk()
- ->assertJsonPath('data.id', $productReview->id)
- ->assertJsonPath('data.title', $productReview->title)
- ->assertJsonPath('data.comment', $productReview->comment);
- $this->assertModelWise([
- ProductReviewAttachment::class => [
- [
- 'review_id' => $productReview->id,
- 'path' => $productReviewAttachment->path,
- 'type' => $productReviewAttachment->type,
- 'mime_type' => $productReviewAttachment->mime_type,
- ],
- ],
- ProductReview::class => [
- [
- 'name' => $productReview->name,
- 'title' => $productReview->title,
- 'rating' => $productReview->rating,
- 'comment' => $productReview->comment,
- 'status' => $productReview->status,
- 'product_id' => $productReview->product_id,
- 'customer_id' => $productReview->customer_id,
- ],
- ],
- ]);
- Storage::assertExists($productReviewAttachment->path);
- });
- it('should fail the validation with errors for status for review update', function () {
- // Arrange.
- $product = (new ProductFaker([
- 'attributes' => [
- 5 => 'new',
- ],
- 'attribute_value' => [
- 'new' => [
- 'boolean_value' => true,
- ],
- ],
- ]))
- ->getSimpleProductFactory()
- ->create();
- $customer = Customer::factory()->create();
- $productReview = ProductReview::factory()->create([
- 'product_id' => $product->id,
- 'customer_id' => $customer->id,
- 'name' => $customer->name,
- ]);
- $attachment = UploadedFile::fake()->image('test.png');
- $fileType = explode('/', $attachment->getMimeType());
- $productReviewAttachment = ProductReviewAttachment::factory()->create([
- 'path' => $attachment->store('review/'.$productReview->id),
- 'review_id' => $productReview->id,
- 'type' => $fileType[0],
- 'mime_type' => $fileType[1],
- ]);
- // Act and Assert.
- $this->loginAsAdmin();
- putJson(route('admin.customers.customers.review.update', $productReview->id))
- ->assertJsonValidationErrorFor('status')
- ->assertUnprocessable();
- $this->assertModelWise([
- ProductReviewAttachment::class => [
- [
- 'review_id' => $productReview->id,
- 'path' => $productReviewAttachment->path,
- 'type' => $productReviewAttachment->type,
- 'mime_type' => $productReviewAttachment->mime_type,
- ],
- ],
- ProductReview::class => [
- [
- 'name' => $productReview->name,
- 'title' => $productReview->title,
- 'rating' => $productReview->rating,
- 'comment' => $productReview->comment,
- 'status' => $productReview->status,
- 'product_id' => $productReview->product_id,
- 'customer_id' => $productReview->customer_id,
- ],
- ],
- ]);
- Storage::assertExists($productReviewAttachment->path);
- });
- it('should update the status of the review', function () {
- // Arrange.
- $product = (new ProductFaker([
- 'attributes' => [
- 5 => 'new',
- ],
- 'attribute_value' => [
- 'new' => [
- 'boolean_value' => true,
- ],
- ],
- ]))
- ->getSimpleProductFactory()
- ->create();
- $customer = Customer::factory()->create();
- $productReview = ProductReview::factory()->create([
- 'product_id' => $product->id,
- 'customer_id' => $customer->id,
- 'name' => $customer->name,
- ]);
- $attachment = UploadedFile::fake()->image('test.png');
- $fileType = explode('/', $attachment->getMimeType());
- $productReviewAttachment = ProductReviewAttachment::factory()->create([
- 'path' => $attachment->store('review/'.$productReview->id),
- 'review_id' => $productReview->id,
- 'type' => $fileType[0],
- 'mime_type' => $fileType[1],
- ]);
- // Act and Assert.
- $this->loginAsAdmin();
- putJson(route('admin.customers.customers.review.update', $productReview->id), [
- 'status' => $status = Arr::random(['approved', 'disapproved', 'pending']),
- ])
- ->assertOk()
- ->assertJsonPath('message', trans('admin::app.customers.reviews.index.edit.update-success'));
- $this->assertModelWise([
- ProductReviewAttachment::class => [
- [
- 'review_id' => $productReview->id,
- 'path' => $productReviewAttachment->path,
- 'type' => $productReviewAttachment->type,
- 'mime_type' => $productReviewAttachment->mime_type,
- ],
- ],
- ProductReview::class => [
- [
- 'name' => $productReview->name,
- 'title' => $productReview->title,
- 'rating' => $productReview->rating,
- 'comment' => $productReview->comment,
- 'status' => $status,
- 'product_id' => $productReview->product_id,
- 'customer_id' => $productReview->customer_id,
- ],
- ],
- ]);
- Storage::assertExists($productReviewAttachment->path);
- });
- it('should delete the review', function () {
- // Arrange.
- $product = (new ProductFaker([
- 'attributes' => [
- 5 => 'new',
- ],
- 'attribute_value' => [
- 'new' => [
- 'boolean_value' => true,
- ],
- ],
- ]))
- ->getSimpleProductFactory()
- ->create();
- $customer = Customer::factory()->create();
- $productReview = ProductReview::factory()->create([
- 'product_id' => $product->id,
- 'customer_id' => $customer->id,
- 'name' => $customer->name,
- ]);
- $attachment = UploadedFile::fake()->image('test.png');
- $fileType = explode('/', $attachment->getMimeType());
- $productReviewAttachment = ProductReviewAttachment::factory()->create([
- 'path' => $attachment->store('review/'.$productReview->id),
- 'review_id' => $productReview->id,
- 'type' => $fileType[0],
- 'mime_type' => $fileType[1],
- ]);
- // Act and Assert.
- $this->loginAsAdmin();
- deleteJson(route('admin.customers.customers.review.delete', $productReview->id))
- ->assertOk()
- ->assertSeeText(trans('admin::app.customers.reviews.index.datagrid.delete-success'));
- $this->assertDatabaseMissing('product_reviews', [
- 'id' => $productReview->id,
- ]);
- $this->assertDatabaseMissing('product_review_attachments', [
- 'id' => $productReviewAttachment->id,
- ]);
- Storage::assertDirectoryEmpty($productReviewAttachment->path);
- });
- it('should mass delete the product review', function () {
- // Arrange.
- $product = (new ProductFaker([
- 'attributes' => [
- 5 => 'new',
- ],
- 'attribute_value' => [
- 'new' => [
- 'boolean_value' => true,
- ],
- ],
- ]))
- ->getSimpleProductFactory()
- ->create();
- $customer = Customer::factory()->create();
- $productReviews = ProductReview::factory()->count(5)->create([
- 'product_id' => $product->id,
- 'customer_id' => $customer->id,
- 'name' => $customer->name,
- ]);
- $productReviewAttachments = [];
- foreach ($productReviews as $key => $productReview) {
- $attachment = UploadedFile::fake()->image('test_'.$key.'.png');
- $fileType = explode('/', $attachment->getMimeType());
- $productReviewAttachments[] = ProductReviewAttachment::factory()->create([
- 'path' => $attachment->store('review/'.$productReview->id),
- 'review_id' => $productReview->id,
- 'type' => $fileType[0],
- 'mime_type' => $fileType[1],
- ]);
- }
- // Act and Assert.
- $this->loginAsAdmin();
- postJson(route('admin.customers.customers.review.mass_delete', [
- 'indices' => $productReviews->pluck('id')->toArray(),
- ]))
- ->assertOk()
- ->assertSeeText(trans('admin::app.customers.reviews.index.datagrid.mass-delete-success'));
- foreach ($productReviews as $productReview) {
- $this->assertDatabaseMissing('product_reviews', [
- 'id' => $productReview->id,
- ]);
- }
- foreach ($productReviewAttachments as $productReviewAttachment) {
- $this->assertDatabaseMissing('product_review_attachments', [
- 'id' => $productReviewAttachment->id,
- ]);
- Storage::assertDirectoryEmpty($productReviewAttachment->path);
- }
- });
- it('should mass update the product review', function () {
- // Arrange.
- $status = Arr::random(['approved', 'disapproved', 'pending']);
- $product = (new ProductFaker([
- 'attributes' => [
- 5 => 'new',
- ],
- 'attribute_value' => [
- 'new' => [
- 'boolean_value' => true,
- ],
- ],
- ]))
- ->getSimpleProductFactory()
- ->create();
- $productReviews = ProductReview::factory()->count(2)->create([
- 'status' => $status,
- 'product_id' => $product->id,
- ]);
- // Act and Assert.
- $this->loginAsAdmin();
- postJson(route('admin.customers.customers.review.mass_update', [
- 'indices' => $productReviews->pluck('id')->toArray(),
- 'value' => $status,
- ]))
- ->assertOk()
- ->assertSeeText(trans('admin::app.customers.reviews.index.datagrid.mass-update-success'));
- foreach ($productReviews as $productReview) {
- $this->assertModelWise([
- ProductReview::class => [
- [
- 'id' => $productReview->id,
- 'status' => $productReview->status,
- ],
- ],
- ]);
- }
- });
|