CampaignsTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. use Webkul\Marketing\Models\Campaign;
  3. use Webkul\Marketing\Models\Event;
  4. use Webkul\Marketing\Models\Template;
  5. use function Pest\Laravel\deleteJson;
  6. use function Pest\Laravel\get;
  7. use function Pest\Laravel\postJson;
  8. use function Pest\Laravel\putJson;
  9. it('should return the campaign index page', function () {
  10. // Act and Assert.
  11. $this->loginAsAdmin();
  12. get(route('admin.marketing.communications.campaigns.index'))
  13. ->assertOk()
  14. ->assertSeeText(trans('admin::app.marketing.communications.campaigns.index.title'))
  15. ->assertSeeText(trans('admin::app.marketing.communications.campaigns.index.create-btn'));
  16. });
  17. it('should returns the create page of campaigns', function () {
  18. // Act and Assert.
  19. $this->loginAsAdmin();
  20. get(route('admin.marketing.communications.campaigns.create'))
  21. ->assertOk()
  22. ->assertSeeText(trans('admin::app.marketing.communications.campaigns.create.title'))
  23. ->assertSeeText(trans('admin::app.marketing.communications.campaigns.create.back-btn'));
  24. });
  25. it('should fail the validation with errors when certain inputs are not provided when store in campaigns', function () {
  26. // Act and Assert.
  27. $this->loginAsAdmin();
  28. postJson(route('admin.marketing.communications.campaigns.store'))
  29. ->assertJsonValidationErrorFor('name')
  30. ->assertJsonValidationErrorFor('subject')
  31. ->assertJsonValidationErrorFor('marketing_template_id')
  32. ->assertJsonValidationErrorFor('marketing_event_id')
  33. ->assertJsonValidationErrorFor('channel_id')
  34. ->assertJsonValidationErrorFor('customer_group_id')
  35. ->assertUnprocessable();
  36. });
  37. it('should fail the validation with errors when certain inputs are not provided and if provided bad status type when store in campaigns', function () {
  38. // Act and Assert.
  39. $this->loginAsAdmin();
  40. postJson(route('admin.marketing.communications.campaigns.store'), [
  41. 'status' => fake()->word(),
  42. ])
  43. ->assertJsonValidationErrorFor('name')
  44. ->assertJsonValidationErrorFor('subject')
  45. ->assertJsonValidationErrorFor('marketing_template_id')
  46. ->assertJsonValidationErrorFor('marketing_event_id')
  47. ->assertJsonValidationErrorFor('channel_id')
  48. ->assertJsonValidationErrorFor('customer_group_id')
  49. ->assertJsonValidationErrorFor('status')
  50. ->assertUnprocessable();
  51. });
  52. it('should store the newly created campaigns', function () {
  53. // Arrange.
  54. $marketingTemplate = Template::factory()->create();
  55. $event = Event::factory()->create();
  56. // Act and Assert.
  57. $this->loginAsAdmin();
  58. postJson(route('admin.marketing.communications.campaigns.store'), $data = [
  59. 'name' => fake()->name(),
  60. 'subject' => fake()->title(),
  61. 'marketing_template_id' => $marketingTemplate->id,
  62. 'marketing_event_id' => $event->id,
  63. 'channel_id' => 1,
  64. 'customer_group_id' => rand(1, 3),
  65. ])
  66. ->assertRedirect(route('admin.marketing.communications.campaigns.index'))
  67. ->isRedirect();
  68. $this->assertModelWise([
  69. Campaign::class => [
  70. [
  71. 'name' => $data['name'],
  72. 'subject' => $data['subject'],
  73. 'marketing_template_id' => $marketingTemplate->id,
  74. 'marketing_event_id' => $event->id,
  75. 'channel_id' => 1,
  76. 'customer_group_id' => $data['customer_group_id'],
  77. ],
  78. ],
  79. ]);
  80. });
  81. it('should show the edit page of campaigns', function () {
  82. // Arrange.
  83. $campaign = Campaign::factory()->create();
  84. // Act and Assert.
  85. $this->loginAsAdmin();
  86. get(route('admin.marketing.communications.campaigns.edit', $campaign->id))
  87. ->assertOk()
  88. ->assertSeeText(trans('admin::app.marketing.communications.campaigns.edit.title'))
  89. ->assertSeeText(trans('admin::app.marketing.communications.campaigns.edit.back-btn'));
  90. });
  91. it('should fail the validation with errors when certain inputs are not provided when update in campaigns', function () {
  92. // Arrange.
  93. $campaign = Campaign::factory()->create();
  94. // Act and Assert.
  95. $this->loginAsAdmin();
  96. putJson(route('admin.marketing.communications.campaigns.update', $campaign->id))
  97. ->assertJsonValidationErrorFor('name')
  98. ->assertJsonValidationErrorFor('subject')
  99. ->assertJsonValidationErrorFor('marketing_template_id')
  100. ->assertJsonValidationErrorFor('marketing_event_id')
  101. ->assertJsonValidationErrorFor('channel_id')
  102. ->assertJsonValidationErrorFor('customer_group_id')
  103. ->assertUnprocessable();
  104. });
  105. it('should fail the validation with errors when certain inputs are not provided and if provided bad status type when update in campaigns', function () {
  106. // Arrange.
  107. $campaign = Campaign::factory()->create();
  108. // Act and Assert.
  109. $this->loginAsAdmin();
  110. putJson(route('admin.marketing.communications.campaigns.update', $campaign->id), [
  111. 'status' => fake()->word(),
  112. ])
  113. ->assertJsonValidationErrorFor('name')
  114. ->assertJsonValidationErrorFor('subject')
  115. ->assertJsonValidationErrorFor('marketing_template_id')
  116. ->assertJsonValidationErrorFor('marketing_event_id')
  117. ->assertJsonValidationErrorFor('channel_id')
  118. ->assertJsonValidationErrorFor('customer_group_id')
  119. ->assertUnprocessable();
  120. });
  121. it('should update specified the campaigns', function () {
  122. // Arrange.
  123. $campaign = Campaign::factory()->create(['marketing_template_id' => Template::factory()->create()->id]);
  124. $event = Event::factory()->create();
  125. // Act and Assert.
  126. $this->loginAsAdmin();
  127. putJson(route('admin.marketing.communications.campaigns.edit', $campaign->id), $data = [
  128. 'name' => $campaign->name,
  129. 'subject' => fake()->title(),
  130. 'marketing_template_id' => $campaign->marketing_template_id,
  131. 'marketing_event_id' => $event->id,
  132. 'channel_id' => 1,
  133. 'customer_group_id' => $customerGroupId = rand(1, 3),
  134. ])
  135. ->assertRedirect(route('admin.marketing.communications.campaigns.index'))
  136. ->isRedirect();
  137. $this->assertModelWise([
  138. Campaign::class => [
  139. [
  140. 'id' => $campaign->id,
  141. 'name' => $campaign->name,
  142. 'subject' => $data['subject'],
  143. 'marketing_template_id' => $campaign->marketing_template_id,
  144. 'marketing_event_id' => $event->id,
  145. 'channel_id' => 1,
  146. 'customer_group_id' => $customerGroupId,
  147. ],
  148. ],
  149. ]);
  150. });
  151. it('should delete the campaign', function () {
  152. // Arrange.
  153. $campaign = Campaign::factory()->create();
  154. // Act and Assert.
  155. $this->loginAsAdmin();
  156. deleteJson(route('admin.marketing.communications.campaigns.delete', $campaign->id))
  157. ->assertOk()
  158. ->assertSeeText(trans('admin::app.marketing.communications.campaigns.delete-success'));
  159. $this->assertDatabaseMissing('marketing_campaigns', [
  160. 'id' => $campaign->id,
  161. ]);
  162. });