CmsTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. use Illuminate\Support\Arr;
  3. use Webkul\CMS\Models\Page;
  4. use Webkul\CMS\Models\PageTranslation;
  5. use function Pest\Laravel\deleteJson;
  6. use function Pest\Laravel\get;
  7. use function Pest\Laravel\getJson;
  8. use function Pest\Laravel\postJson;
  9. use function Pest\Laravel\putJson;
  10. it('should returns the cms page', function () {
  11. // Act and Assert.
  12. $this->loginAsAdmin();
  13. get(route('admin.cms.index'))
  14. ->assertOk()
  15. ->assertSeeText(trans('admin::app.cms.index.title'))
  16. ->assertSeeText(trans('admin::app.cms.index.create-btn'));
  17. });
  18. it('should returns the listing cms', function () {
  19. // Act and Assert.
  20. $this->loginAsAdmin();
  21. getJson(route('admin.cms.index'), [
  22. 'X-Requested-With' => 'XMLHttpRequest',
  23. ])
  24. ->assertOk()
  25. ->assertJsonPath('records.0.id', 10)
  26. ->assertJsonPath('records.0.page_title', 'Privacy Policy')
  27. ->assertJsonPath('records.0.url_key', 'privacy-policy')
  28. ->assertJsonPath('meta.total', 10);
  29. });
  30. it('should return the cms create page', function () {
  31. // Act and Assert.
  32. $this->loginAsAdmin();
  33. get(route('admin.cms.create'))
  34. ->assertOk()
  35. ->assertSeeText(trans('admin::app.cms.create.title'))
  36. ->assertSeeText(trans('admin::app.account.edit.back-btn'));
  37. });
  38. it('should fail the validation with errors when certain inputs are not provided when store in cms page', function () {
  39. // Act and Assert.
  40. $this->loginAsAdmin();
  41. postJson(route('admin.cms.store'))
  42. ->assertJsonValidationErrorFor('url_key')
  43. ->assertJsonValidationErrorFor('page_title')
  44. ->assertJsonValidationErrorFor('html_content')
  45. ->assertJsonValidationErrorFor('channels')
  46. ->assertUnprocessable();
  47. });
  48. it('should fail with the error URL key not provided in the correct format', function () {
  49. // Act and Assert.
  50. $this->loginAsAdmin();
  51. postJson(route('admin.cms.store'), [
  52. 'url_key' => 'invalid url key',
  53. ])
  54. ->assertJsonValidationErrorFor('url_key')
  55. ->assertUnprocessable();
  56. });
  57. it('should store newly created cms pages', function () {
  58. // Act and Assert.
  59. $this->loginAsAdmin();
  60. postJson(route('admin.cms.store'), $data = [
  61. 'url_key' => fake()->slug(),
  62. 'page_title' => fake()->title(),
  63. 'html_content' => substr(fake()->paragraph(), 0, 50),
  64. 'meta_title' => fake()->title(),
  65. 'meta_keywords' => fake()->word(),
  66. 'meta_description' => fake()->paragraph(3),
  67. 'channels' => [
  68. 'value' => 1,
  69. ],
  70. ])
  71. ->assertRedirect(route('admin.cms.index'))
  72. ->isRedirection();
  73. $this->assertModelWise([
  74. PageTranslation::class => [
  75. Arr::except($data, ['channels']),
  76. ],
  77. ]);
  78. });
  79. it('should show the edit cms page', function () {
  80. // Arrange.
  81. $cms = Page::factory()->hasTranslations()->create();
  82. // Act and Assert.
  83. $this->loginAsAdmin();
  84. get(route('admin.cms.edit', $cms->id))
  85. ->assertOk()
  86. ->assertSeeText(trans('admin::app.cms.edit.title'))
  87. ->assertSeeText(trans('admin::app.cms.edit.back-btn'));
  88. });
  89. it('should fail the validation with errors when certain inputs are not provided when update in cms page', function () {
  90. // Arrange.
  91. $cms = Page::factory()->hasTranslations()->create();
  92. $localeCode = core()->getRequestedLocaleCode();
  93. // Act and Assert.
  94. $this->loginAsAdmin();
  95. putJson(route('admin.cms.update', $cms->id))
  96. ->assertJsonValidationErrorFor($localeCode.'.url_key')
  97. ->assertJsonValidationErrorFor($localeCode.'.page_title')
  98. ->assertJsonValidationErrorFor($localeCode.'.html_content')
  99. ->assertJsonValidationErrorFor('channels')
  100. ->assertUnprocessable();
  101. });
  102. it('should update the cms page', function () {
  103. // Arrange.
  104. $cms = Page::factory()->hasTranslations()->create();
  105. $localeCode = core()->getCurrentLocale()->code;
  106. // Act and Assert.
  107. $this->loginAsAdmin();
  108. putJson(route('admin.cms.update', $cms->id), [
  109. $localeCode => $data = [
  110. 'url_key' => $cms->url_key,
  111. 'page_title' => fake()->word(),
  112. 'html_content' => substr(fake()->paragraph(), 0, 50),
  113. ],
  114. 'locale' => $localeCode,
  115. 'channels' => [
  116. 1,
  117. ],
  118. ])
  119. ->assertRedirect(route('admin.cms.index'))
  120. ->isRedirection();
  121. $this->assertModelWise([
  122. PageTranslation::class => [
  123. [
  124. 'url_key' => $data['url_key'],
  125. 'page_title' => $data['page_title'],
  126. 'html_content' => $data['html_content'],
  127. ],
  128. ],
  129. ]);
  130. });
  131. it('should delete the cms page', function () {
  132. // Arrange.
  133. $cms = Page::factory()->hasTranslations()->create();
  134. // Act and Assert.
  135. $this->loginAsAdmin();
  136. deleteJson(route('admin.cms.delete', $cms->id))
  137. ->assertOk()
  138. ->assertSeeText(trans('admin::app.cms.delete-success'));
  139. $this->assertDatabaseMissing('cms_pages', [
  140. 'id' => $cms->id,
  141. ]);
  142. $this->assertDatabaseMissing('cms_page_translations', [
  143. 'cms_page_id' => $cms->id,
  144. ]);
  145. });
  146. it('should mass delete cms pages', function () {
  147. // Arrange.
  148. $cmsPages = Page::factory()->count(2)->hasTranslations()->create();
  149. // Act and Assert.
  150. $this->loginAsAdmin();
  151. postJson(route('admin.cms.mass_delete'), [
  152. 'indices' => $cmsPages->pluck('id')->toArray(),
  153. ])
  154. ->assertOk()
  155. ->assertSeeText(trans('admin::app.cms.index.datagrid.mass-delete-success'));
  156. foreach ($cmsPages as $page) {
  157. $this->assertDatabaseMissing('cms_pages', [
  158. 'id' => $page->id,
  159. ]);
  160. $this->assertDatabaseMissing('cms_page_translations', [
  161. 'cms_page_id' => $page->id,
  162. ]);
  163. }
  164. });