UrlRewritesTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. use Webkul\Marketing\Models\URLRewrite;
  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 show the url rewrite index page', function () {
  8. // Act and Assert.
  9. $this->loginAsAdmin();
  10. get(route('admin.marketing.search_seo.url_rewrites.index'))
  11. ->assertOk()
  12. ->assertSeeText(trans('admin::app.marketing.search-seo.url-rewrites.index.title'))
  13. ->assertSeeText(trans('admin::app.marketing.search-seo.url-rewrites.index.create-btn'));
  14. });
  15. it('should fail the validation with errors when certain field not provided when store the url rewrites', function () {
  16. // Act and Assert.
  17. $this->loginAsAdmin();
  18. postJson(route('admin.marketing.search_seo.url_rewrites.store'))
  19. ->assertJsonValidationErrorFor('entity_type')
  20. ->assertJsonValidationErrorFor('request_path')
  21. ->assertJsonValidationErrorFor('target_path')
  22. ->assertJsonValidationErrorFor('redirect_type')
  23. ->assertJsonValidationErrorFor('locale')
  24. ->assertUnprocessable();
  25. });
  26. it('should store the newly created url', function () {
  27. // Act and Assert.
  28. $this->loginAsAdmin();
  29. postJson(route('admin.marketing.search_seo.url_rewrites.store'), [
  30. 'entity_type' => $entityType = fake()->randomElement(['product', 'category', 'cms_page']),
  31. 'request_path' => $requestPath = fake()->url(),
  32. 'target_path' => $targetPath = fake()->url(),
  33. 'redirect_type' => $redirectType = fake()->randomElement([302, 301]),
  34. 'locale' => $localeCode = core()->getCurrentLocale()->code,
  35. ])
  36. ->assertOk()
  37. ->assertSeeText(trans('admin::app.marketing.search-seo.url-rewrites.index.create.success'));
  38. $this->assertModelWise([
  39. URLRewrite::class => [
  40. [
  41. 'entity_type' => $entityType,
  42. 'request_path' => $requestPath,
  43. 'target_path' => $targetPath,
  44. 'redirect_type' => $redirectType,
  45. 'locale' => $localeCode,
  46. ],
  47. ],
  48. ]);
  49. });
  50. it('should fail the validation with errors when certain field not provided when update the url rewrites', function () {
  51. // Arrange.
  52. $urlRewrite = URLRewrite::factory()->create();
  53. // Act and Assert.
  54. $this->loginAsAdmin();
  55. putJson(route('admin.marketing.search_seo.url_rewrites.update', $urlRewrite->id))
  56. ->assertJsonValidationErrorFor('entity_type')
  57. ->assertJsonValidationErrorFor('request_path')
  58. ->assertJsonValidationErrorFor('target_path')
  59. ->assertJsonValidationErrorFor('redirect_type')
  60. ->assertJsonValidationErrorFor('locale')
  61. ->assertUnprocessable();
  62. });
  63. it('should update the existing url rewrite', function () {
  64. // Arrange.
  65. $urlRewrite = URLRewrite::factory()->create();
  66. // Act and Assert.
  67. $this->loginAsAdmin();
  68. putJson(route('admin.marketing.search_seo.url_rewrites.update'), [
  69. 'id' => $urlRewrite->id,
  70. 'entity_type' => $entityType = fake()->randomElement(['product', 'category', 'cms_page']),
  71. 'request_path' => $requestPath = fake()->url(),
  72. 'target_path' => $targetPath = fake()->url(),
  73. 'redirect_type' => $redirectType = fake()->randomElement([302, 301]),
  74. 'locale' => $localeCode = core()->getCurrentLocale()->code,
  75. ])
  76. ->assertOk()
  77. ->assertSeeText(trans('admin::app.marketing.search-seo.url-rewrites.index.edit.success'));
  78. $this->assertModelWise([
  79. URLRewrite::class => [
  80. [
  81. 'id' => $urlRewrite->id,
  82. 'entity_type' => $entityType,
  83. 'request_path' => $requestPath,
  84. 'target_path' => $targetPath,
  85. 'redirect_type' => $redirectType,
  86. 'locale' => $localeCode,
  87. ],
  88. ],
  89. ]);
  90. });
  91. it('should delete the existing url rewrite', function () {
  92. // Arrange.
  93. $urlRewrite = URLRewrite::factory()->create();
  94. // Act and Assert.
  95. $this->loginAsAdmin();
  96. deleteJson(route('admin.marketing.search_seo.url_rewrites.delete', $urlRewrite->id))
  97. ->assertOk()
  98. ->assertSeeText(trans('admin::app.marketing.search-seo.url-rewrites.index.edit.delete-success'));
  99. $this->assertDatabaseMissing('url_rewrites', [
  100. 'id' => $urlRewrite->id,
  101. ]);
  102. });
  103. it('should mass delete the existing url rewrites', function () {
  104. // Arrange.
  105. $urlRewrites = URLRewrite::factory()->count(2)->create();
  106. // Act and Assert.
  107. $this->loginAsAdmin();
  108. postJson(route('admin.marketing.search_seo.url_rewrites.mass_delete'), [
  109. 'indices' => $urlRewrites->pluck('id')->toArray(),
  110. ])
  111. ->assertOk()
  112. ->assertSeeText(trans('admin::app.marketing.search-seo.url-rewrites.index.datagrid.mass-delete-success'));
  113. foreach ($urlRewrites as $urlRewrite) {
  114. $this->assertDatabaseMissing('url_rewrites', [
  115. 'id' => $urlRewrite->id,
  116. ]);
  117. }
  118. });