AttributeTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. use Webkul\Attribute\Models\Attribute;
  3. use function Pest\Laravel\deleteJson;
  4. use function Pest\Laravel\get;
  5. use function Pest\Laravel\getJson;
  6. use function Pest\Laravel\postJson;
  7. use function Pest\Laravel\putJson;
  8. it('should show attribute page', function () {
  9. // Act and Assert.
  10. $this->loginAsAdmin();
  11. get(route('admin.catalog.attributes.index'))
  12. ->assertOk()
  13. ->assertSeeText(trans('admin::app.catalog.attributes.index.title'))
  14. ->assertSeeText(trans('admin::app.catalog.attributes.index.create-btn'));
  15. });
  16. it('should return listing items of attributes', function () {
  17. // Arrange.
  18. $attribute = Attribute::factory()->create();
  19. // Act and Assert.
  20. $this->loginAsAdmin();
  21. getJson(route('admin.catalog.attributes.index'), [
  22. 'X-Requested-With' => 'XMLHttpRequest',
  23. ])
  24. ->assertOk()
  25. ->assertJsonPath('records.0.id', $attribute->id)
  26. ->assertJsonPath('records.0.code', $attribute->code)
  27. ->assertJsonPath('meta.total', 29);
  28. });
  29. it('should returns attributes options', function () {
  30. // Arrange.
  31. $attribute = Attribute::factory()->create();
  32. // Act and Assert.
  33. $this->loginAsAdmin();
  34. get(route('admin.catalog.attributes.options', $attribute->id))
  35. ->assertOk()
  36. ->assertJsonIsArray();
  37. });
  38. it('should show create page of attribute', function () {
  39. // Act and Assert.
  40. $this->loginAsAdmin();
  41. get(route('admin.catalog.attributes.create'))
  42. ->assertOk()
  43. ->assertSeeText(trans('admin::app.catalog.attributes.create.title'))
  44. ->assertSeeText(trans('admin::app.catalog.attributes.create.save-btn'));
  45. });
  46. it('should fail the validation with errors when certain inputs are not provided when store in attribute', function () {
  47. // Act and Assert.
  48. $this->loginAsAdmin();
  49. postJson(route('admin.catalog.attributes.store'))
  50. ->assertJsonValidationErrorFor('code')
  51. ->assertJsonValidationErrorFor('admin_name')
  52. ->assertJsonValidationErrorFor('type')
  53. ->assertUnprocessable();
  54. });
  55. it('should store newly created attribute', function () {
  56. // Act and Assert.
  57. $this->loginAsAdmin();
  58. postJson(route('admin.catalog.attributes.store'), $data = [
  59. 'admin_name' => fake()->name(),
  60. 'code' => fake()->numerify('code########'),
  61. 'type' => 'text',
  62. 'default_value' => 1,
  63. ])
  64. ->assertRedirectToRoute('admin.catalog.attributes.index')
  65. ->isRedirection();
  66. $this->assertModelWise([
  67. Attribute::class => [
  68. $data,
  69. ],
  70. ]);
  71. });
  72. it('should show edit page of attribute', function () {
  73. // Arrange.
  74. $attribute = Attribute::factory()->create();
  75. // Act and Assert.
  76. $this->loginAsAdmin();
  77. get(route('admin.catalog.attributes.edit', $attribute->id))
  78. ->assertOk()
  79. ->assertSeeText(trans('admin::app.catalog.attributes.edit.title'))
  80. ->assertSeeText(trans('admin::app.catalog.attributes.edit.back-btn'));
  81. });
  82. it('should fail the validation with errors when certain inputs are not provided when update in attribute', function () {
  83. // Act and Assert.
  84. $this->loginAsAdmin();
  85. postJson(route('admin.catalog.attributes.store'))
  86. ->assertJsonValidationErrorFor('code')
  87. ->assertJsonValidationErrorFor('admin_name')
  88. ->assertJsonValidationErrorFor('type')
  89. ->assertUnprocessable();
  90. });
  91. it('should update an attribute', function () {
  92. // Arrange.
  93. $attribute = Attribute::factory()->create();
  94. // Act and Assert.
  95. $this->loginAsAdmin();
  96. putJson(route('admin.catalog.attributes.update', $attribute->id), $data = [
  97. 'admin_name' => fake()->name(),
  98. 'code' => $attribute->code,
  99. 'type' => $attribute->type,
  100. 'default_value' => 1,
  101. ])
  102. ->assertRedirectToRoute('admin.catalog.attributes.index')
  103. ->isRedirection();
  104. $this->assertModelWise([
  105. Attribute::class => [
  106. $data,
  107. ],
  108. ]);
  109. });
  110. it('should destroy an attribute', function () {
  111. // Arrange.
  112. $attribute = Attribute::factory()->create();
  113. // Act and Assert.
  114. $this->loginAsAdmin();
  115. deleteJson(route('admin.catalog.attributes.delete', $attribute->id))
  116. ->assertOk()
  117. ->assertSeeText(trans('admin::app.catalog.attributes.delete-success'));
  118. $this->assertDatabaseMissing('attributes', [
  119. 'id' => $attribute->id,
  120. ]);
  121. });
  122. it('should not destroy an attribute if it is not user-defined', function () {
  123. // Arrange.
  124. $attribute = Attribute::factory()->create([
  125. 'is_user_defined' => false,
  126. ]);
  127. // Act and Assert.
  128. $this->loginAsAdmin();
  129. deleteJson(route('admin.catalog.attributes.delete', $attribute->id))
  130. ->assertBadRequest()
  131. ->assertSeeText(trans('admin::app.catalog.attributes.user-define-error'));
  132. $this->assertModelWise([
  133. Attribute::class => [
  134. [
  135. 'id' => $attribute->id,
  136. ],
  137. ],
  138. ]);
  139. });
  140. it('should mass delete attributes', function () {
  141. // Arrange.
  142. $attributes = Attribute::factory()->count(2)->create();
  143. // Act and Assert.
  144. $this->loginAsAdmin();
  145. postJson(route('admin.catalog.attributes.mass_delete', [
  146. 'indices' => $attributes->pluck('id')->toArray(),
  147. ]))
  148. ->assertOk()
  149. ->assertSeeText(trans('admin::app.catalog.attributes.index.datagrid.mass-delete-success'));
  150. foreach ($attributes as $attribute) {
  151. $this->assertDatabaseMissing('attributes', [
  152. 'id' => $attribute->id,
  153. ]);
  154. }
  155. });