LocaleTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. use Illuminate\Http\UploadedFile;
  3. use Illuminate\Support\Facades\Storage;
  4. use Webkul\Core\Models\Locale;
  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 returns the locale index page', function () {
  10. // Act and Assert.
  11. $this->loginAsAdmin();
  12. get(route('admin.settings.locales.index'))
  13. ->assertOk()
  14. ->assertSeeText(trans('admin::app.settings.locales.index.title'))
  15. ->assertSeeText(trans('admin::app.settings.locales.index.create-btn'));
  16. });
  17. it('should fail the validation with errors when certain field not provided when store the locale', function () {
  18. // Act and Assert.
  19. $this->loginAsAdmin();
  20. postJson(route('admin.settings.locales.store'), [
  21. 'logo_path' => [
  22. 'INVALID_FORMAT_OF_LOGO_PATH',
  23. ],
  24. ])
  25. ->assertJsonValidationErrorFor('code')
  26. ->assertJsonValidationErrorFor('name')
  27. ->assertJsonValidationErrorFor('direction')
  28. ->assertJsonValidationErrorFor('logo_path.0')
  29. ->assertUnprocessable();
  30. });
  31. it('should store the newly created locale', function () {
  32. // Act and Assert.
  33. $this->loginAsAdmin();
  34. postJson(route('admin.settings.locales.store'), $data = [
  35. 'code' => fake()->locale(),
  36. 'name' => fake()->name(),
  37. 'direction' => fake()->randomElement(['ltr', 'rtl']),
  38. 'logo_path' => [
  39. UploadedFile::fake()->image('logo.png'),
  40. ],
  41. ])
  42. ->assertOk()
  43. ->assertSeeText(trans('admin::app.settings.locales.index.create-success'));
  44. $this->assertModelWise([
  45. Locale::class => [
  46. [
  47. 'code' => $data['code'],
  48. 'name' => $data['name'],
  49. 'direction' => $data['direction'],
  50. 'logo_path' => 'locales/'.$data['code'].'.png',
  51. ],
  52. ],
  53. ]);
  54. Storage::assertExists('locales/'.$data['code'].'.png');
  55. });
  56. it('should not store the new locale if the file has been tampered with', function () {
  57. // Act and Assert.
  58. $this->loginAsAdmin();
  59. postJson(route('admin.settings.locales.store'), $data = [
  60. 'code' => fake()->locale(),
  61. 'name' => fake()->name(),
  62. 'direction' => fake()->randomElement(['ltr', 'rtl']),
  63. 'logo_path' => [
  64. UploadedFile::fake()->image('tampered.php'),
  65. ],
  66. ])
  67. ->assertJsonValidationErrorFor('logo_path.0')
  68. ->assertUnprocessable();
  69. Storage::assertMissing('locales/'.$data['code'].'.php');
  70. });
  71. it('should return the locale for edit', function () {
  72. // Arrange.
  73. $locale = Locale::factory()->create();
  74. // Act and Assert.
  75. $this->loginAsAdmin();
  76. get(route('admin.settings.locales.edit', $locale->id))
  77. ->assertOk()
  78. ->assertJsonFragment($locale->toArray());
  79. });
  80. it('should fail the validation with errors when certain field not provided when update the locale', function () {
  81. // Arrange.
  82. $locale = Locale::factory()->create();
  83. // Act and Assert.
  84. $this->loginAsAdmin();
  85. putJson(route('admin.settings.locales.update'), [
  86. 'id' => $locale->id,
  87. 'logo_path' => [
  88. UploadedFile::fake()->image(fake()->word().'.png'),
  89. ],
  90. ])
  91. ->assertJsonValidationErrorFor('name')
  92. ->assertJsonValidationErrorFor('direction')
  93. ->assertUnprocessable();
  94. });
  95. it('should update the specified locale', function () {
  96. // Arrange.
  97. $locale = Locale::factory()->create();
  98. // Act and Assert.
  99. $this->loginAsAdmin();
  100. putJson(route('admin.settings.locales.update'), $data = [
  101. 'id' => $locale->id,
  102. 'code' => $locale->code,
  103. 'name' => fake()->name(),
  104. 'direction' => fake()->randomElement(['ltr', 'rtl']),
  105. 'logo_path' => [
  106. UploadedFile::fake()->image('logo.png'),
  107. ],
  108. ])
  109. ->assertOk()
  110. ->assertSeeText(trans('admin::app.settings.locales.index.update-success'));
  111. $this->assertModelWise([
  112. Locale::class => [
  113. [
  114. 'name' => $data['name'],
  115. 'code' => $data['code'],
  116. 'direction' => $data['direction'],
  117. 'logo_path' => 'locales/'.$locale->code.'.png',
  118. ],
  119. ],
  120. ]);
  121. Storage::assertExists('locales/'.$locale->code.'.png');
  122. });
  123. it('should not update the specified locale if the file has been tampered with', function () {
  124. // Arrange.
  125. $locale = Locale::factory()->create();
  126. // Act and Assert.
  127. $this->loginAsAdmin();
  128. putJson(route('admin.settings.locales.update'), [
  129. 'id' => $locale->id,
  130. 'name' => fake()->name(),
  131. 'logo_path' => [
  132. UploadedFile::fake()->image('tampered.php'),
  133. ],
  134. ])
  135. ->assertJsonValidationErrorFor('direction')
  136. ->assertJsonValidationErrorFor('logo_path.0')
  137. ->assertUnprocessable();
  138. Storage::assertMissing('locales/'.$locale->code.'.php');
  139. });
  140. it('should delete the locale', function () {
  141. // Arrange.
  142. $locale = Locale::factory()->create();
  143. // Act and Assert.
  144. $this->loginAsAdmin();
  145. deleteJson(route('admin.settings.locales.delete', $locale->id))
  146. ->assertOk()
  147. ->assertSeeText(trans('admin::app.settings.locales.index.delete-success'));
  148. $this->assertDatabaseMissing('locales', [
  149. 'id' => $locale->id,
  150. ]);
  151. });