EmailTemplateTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. use Webkul\Marketing\Models\Template;
  3. use function Pest\Laravel\deleteJson;
  4. use function Pest\Laravel\get;
  5. use function Pest\Laravel\postJson;
  6. use function Pest\Laravel\putJson;
  7. it('should return the email template index page', function () {
  8. // Act and Assert.
  9. $this->loginAsAdmin();
  10. get(route('admin.marketing.communications.email_templates.index'))
  11. ->assertOk()
  12. ->assertSeeText(trans('admin::app.marketing.communications.templates.index.title'))
  13. ->assertSeeText(trans('admin::app.marketing.communications.templates.index.create-btn'));
  14. });
  15. it('should return the create page of email template', function () {
  16. // Act and Assert.
  17. $this->loginAsAdmin();
  18. get(route('admin.marketing.communications.email_templates.create'))
  19. ->assertOk()
  20. ->assertSeeText(trans('admin::app.marketing.communications.templates.create.title'))
  21. ->assertSeeText(trans('admin::app.marketing.communications.templates.create.save-btn'));
  22. });
  23. it('should fail the validation with errors when certain inputs are not provided when store in email template', function () {
  24. // Act and Assert.
  25. $this->loginAsAdmin();
  26. postJson(route('admin.marketing.communications.email_templates.store'))
  27. ->assertJsonValidationErrorFor('name')
  28. ->assertJsonValidationErrorFor('status')
  29. ->assertJsonValidationErrorFor('content')
  30. ->assertUnprocessable();
  31. });
  32. it('should fail the validation with errors when certain inputs are not provided also status field not provided correctly when store in email template', function () {
  33. // Act and Assert.
  34. $this->loginAsAdmin();
  35. postJson(route('admin.marketing.communications.email_templates.store'), [
  36. 'status' => fake()->word(),
  37. ])
  38. ->assertJsonValidationErrorFor('name')
  39. ->assertJsonValidationErrorFor('status')
  40. ->assertJsonValidationErrorFor('content')
  41. ->assertUnprocessable();
  42. });
  43. it('should store the newly create email template', function () {
  44. // Act and Assert.
  45. $this->loginAsAdmin();
  46. postJson(route('admin.marketing.communications.email_templates.store', [
  47. 'name' => $name = fake()->name(),
  48. 'status' => $status = fake()->randomElement(['active', 'inactive', 'draft']),
  49. 'content' => $content = substr(fake()->paragraph(), 0, 50),
  50. ]))
  51. ->assertRedirect(route('admin.marketing.communications.email_templates.index'))
  52. ->isRedirect();
  53. $this->assertModelWise([
  54. Template::class => [
  55. [
  56. 'name' => $name,
  57. 'status' => $status,
  58. 'content' => $content,
  59. ],
  60. ],
  61. ]);
  62. });
  63. it('should edit the email template', function () {
  64. // Arrange.
  65. $marketingEmailTemplate = Template::factory()->create();
  66. // Act and Assert.
  67. $this->loginAsAdmin();
  68. get(route('admin.marketing.communications.email_templates.edit', $marketingEmailTemplate->id))
  69. ->assertOk()
  70. ->assertSeeText(trans('admin::app.marketing.communications.templates.edit.title'))
  71. ->assertSeeText(trans('admin::app.marketing.communications.templates.edit.save-btn'))
  72. ->assertSeeText(trans('admin::app.marketing.communications.templates.create.content'));
  73. });
  74. it('should fail the validation with errors when certain inputs are not provided when update in email template', function () {
  75. // Arrange.
  76. $marketingEmailTemplate = Template::factory()->create();
  77. // Act and Assert.
  78. $this->loginAsAdmin();
  79. putJson(route('admin.marketing.communications.email_templates.update', $marketingEmailTemplate->id))
  80. ->assertJsonValidationErrorFor('name')
  81. ->assertJsonValidationErrorFor('status')
  82. ->assertJsonValidationErrorFor('content')
  83. ->assertUnprocessable();
  84. });
  85. it('should fail the validation with errors when certain inputs are not provided also status field not provided correctly when update in email template', function () {
  86. // Arrange.
  87. $marketingEmailTemplate = Template::factory()->create();
  88. // Act and Assert.
  89. $this->loginAsAdmin();
  90. putJson(route('admin.marketing.communications.email_templates.update', $marketingEmailTemplate->id))
  91. ->assertJsonValidationErrorFor('name')
  92. ->assertJsonValidationErrorFor('status')
  93. ->assertJsonValidationErrorFor('content')
  94. ->assertUnprocessable();
  95. });
  96. it('should update the existing the template', function () {
  97. // Arrange.
  98. $marketingEmailTemplate = Template::factory()->create();
  99. // Act and Assert.
  100. $this->loginAsAdmin();
  101. putJson(route('admin.marketing.communications.email_templates.update', $marketingEmailTemplate->id), $data = [
  102. 'name' => $marketingEmailTemplate->name,
  103. 'status' => fake()->randomElement(['active', 'inactive', 'draft']),
  104. 'content' => substr(fake()->paragraph(), 0, 50),
  105. ])
  106. ->assertRedirect(route('admin.marketing.communications.email_templates.index'))
  107. ->isRedirect();
  108. $this->assertModelWise([
  109. Template::class => [
  110. [
  111. 'name' => $marketingEmailTemplate->name,
  112. 'status' => $data['status'],
  113. 'content' => $data['content'],
  114. ],
  115. ],
  116. ]);
  117. });
  118. it('should delete the specified email template', function () {
  119. // Arrange.
  120. $marketingEmailTemplate = Template::factory()->create();
  121. // Act and Assert.
  122. $this->loginAsAdmin();
  123. deleteJson(route('admin.marketing.communications.email_templates.delete', $marketingEmailTemplate->id))
  124. ->assertOk()
  125. ->assertSeeText(trans('admin::app.marketing.communications.templates.delete-success'));
  126. });