snipe-it/tests/Feature/Settings/BrandingSettingsTest.php
snipe beac4c8b8a Updated tests
Signed-off-by: snipe <snipe@snipe.net>
2024-07-20 03:39:54 +01:00

310 lines
11 KiB
PHP

<?php
namespace Tests\Feature\Settings;
use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use App\Models\User;
use App\Models\Setting;
class BrandingSettingsTest extends TestCase
{
public function testSiteNameIsRequired()
{
$response = $this->actingAs(User::factory()->superuser()->create())
->from(route('settings.branding.index'))
->post(route('settings.branding.save', ['site_name' => '']))
->assertSessionHasErrors(['site_name'])
->assertInvalid(['site_name'])
->assertStatus(302)
->assertRedirect(route('settings.branding.index'));
$this->followRedirects($response)->assertSee(trans('general.error'));
}
public function testSiteNameCanBeSaved()
{
$response = $this->actingAs(User::factory()->superuser()->create())
->post(route('settings.branding.save', ['site_name' => 'My Awesome Site']))
->assertStatus(302)
->assertValid('site_name')
->assertRedirect(route('settings.index'))
->assertSessionHasNoErrors();
$this->followRedirects($response)->assertSee('alert-success');
}
public function testLogoCanBeUploaded()
{
Storage::fake('public');
$setting = Setting::factory()->create(['logo' => null]);
$response = $this->actingAs(User::factory()->superuser()->create())
->post(route('settings.branding.save',
['logo' => UploadedFile::fake()->image('test_logo.png')->storeAs('', 'test_logo.png', 'public')]
))
->assertValid('logo')
->assertStatus(302)
->assertRedirect(route('settings.index'))
->assertSessionHasNoErrors();
$this->followRedirects($response)->assertSee('alert-success');
$setting->refresh();
Storage::disk('public')->assertExists($setting->logo);
}
public function testLogoCanBeDeleted()
{
Storage::fake('public');
$setting = Setting::factory()->create(['logo' => 'new_test_logo.png']);
$original_file = UploadedFile::fake()->image('new_test_logo.png')->storeAs('', 'new_test_logo.png', 'public');
Storage::disk('public')->assertExists($original_file);
$this->assertNotNull($setting->email_logo);
$response = $this->actingAs(User::factory()->superuser()->create())
->from(route('settings.branding.index'))
->post(route('settings.branding.save',
['clear_logo' => '1']
))
->assertValid('logo')
->assertStatus(302)
->assertRedirect(route('settings.index'));
$setting->refresh();
$this->followRedirects($response)->assertSee(trans('alert-success'));
// $this->assertNull($setting->refresh()->logo);
// Storage::disk('public')->assertMissing($original_file);
}
public function testEmailLogoCanBeUploaded()
{
Storage::fake('public');
$original_file = UploadedFile::fake()->image('before_test_email_logo.png')->storeAs('', 'before_test_email_logo.png', 'public');
Storage::disk('public')->assertExists($original_file);
Setting::factory()->create(['email_logo' => $original_file]);
$response = $this->actingAs(User::factory()->superuser()->create())
->from(route('settings.branding.index'))
->post(route('settings.branding.save',
[
'email_logo' => UploadedFile::fake()->image('new_test_email_logo.png')->storeAs('', 'new_test_email_logo.png', 'public')
]
))
->assertValid('email_logo')
->assertStatus(302)
->assertRedirect(route('settings.index'));
$this->followRedirects($response)->assertSee(trans('alert-success'));
Storage::disk('public')->assertExists('new_test_email_logo.png');
// Storage::disk('public')->assertMissing($original_file);
}
public function testEmailLogoCanBeDeleted()
{
Storage::fake('public');
$setting = Setting::factory()->create(['email_logo' => 'new_test_email_logo.png']);
$original_file = UploadedFile::fake()->image('new_test_email_logo.png')->storeAs('', 'new_test_email_logo.png', 'public');
Storage::disk('public')->assertExists($original_file);
$this->assertNotNull($setting->email_logo);
$response = $this->actingAs(User::factory()->superuser()->create())
->from(route('settings.branding.index'))
->post(route('settings.branding.save',
['clear_email_logo' => '1']
))
->assertValid('email_logo')
->assertStatus(302)
->assertRedirect(route('settings.index'));
$setting->refresh();
$this->followRedirects($response)->assertSee(trans('alert-success'));
//$this->assertNull($setting->refresh()->email_logo);
Storage::disk('public')->assertExists('new_test_email_logo.png');
// Storage::disk('public')->assertMissing($original_file);
}
public function testLabelLogoCanBeUploaded()
{
Storage::fake('public');
$original_file = UploadedFile::fake()->image('before_test_label_logo.png')->storeAs('', 'before_test_label_logo.png', 'public');
Storage::disk('public')->assertExists($original_file);
Setting::factory()->create(['label_logo' => $original_file]);
$response = $this->actingAs(User::factory()->superuser()->create())
->from(route('settings.branding.index'))
->post(route('settings.branding.save',
[
'label_logo' => UploadedFile::fake()->image('new_test_label_logo.png')->storeAs('', 'new_test_label_logo.png', 'public')
]
))
->assertValid('label_logo')
->assertStatus(302)
->assertRedirect(route('settings.index'));
$this->followRedirects($response)->assertSee(trans('alert-success'));
Storage::disk('public')->assertExists('new_test_label_logo.png');
// Storage::disk('public')->assertMissing($original_file);
}
public function testLabelLogoCanBeDeleted()
{
Storage::fake('public');
$setting = Setting::factory()->create(['label_logo' => 'new_test_label_logo.png']);
$original_file = UploadedFile::fake()->image('new_test_label_logo.png')->storeAs('', 'new_test_label_logo.png', 'public');
Storage::disk('public')->assertExists($original_file);
$this->assertNotNull($setting->label_logo);
$response = $this->actingAs(User::factory()->superuser()->create())
->from(route('settings.branding.index'))
->post(route('settings.branding.save',
['label_logo' => '1']
))
->assertValid('label_logo')
->assertStatus(302)
->assertRedirect(route('settings.index'));
$setting->refresh();
$this->followRedirects($response)->assertSee(trans('alert-success'));
// $this->assertNull($setting->refresh()->logo);
// Storage::disk('public')->assertMissing($original_file);
}
public function testDefaultAvatarCanBeUploaded()
{
Storage::fake('public');
$setting = Setting::factory()->create();
$response = $this->actingAs(User::factory()->superuser()->create())
->from(route('settings.branding.index'))
->post(route('settings.branding.save',
[
'default_avatar' => UploadedFile::fake()->image('default_avatar.png')->storeAs('', 'default_avatar.png', 'public')
]
))
->assertValid('default_avatar')
->assertStatus(302)
->assertRedirect(route('settings.index'))
->assertSessionHasNoErrors();
$this->followRedirects($response)->assertSee(trans('alert-success'));
Storage::disk('public')->assertExists('default_avatar.png');
// Storage::disk('public')->assertMissing($original_file);
}
public function testDefaultAvatarCanBeDeleted()
{
Storage::fake('public');
$setting = Setting::factory()->create(['default_avatar' => 'new_test_label_logo.png']);
$original_file = UploadedFile::fake()->image('default_avatar.png')->storeAs('', 'default_avatar.png', 'public');
Storage::disk('public')->assertExists($original_file);
$this->assertNotNull($setting->default_avatar);
$response = $this->actingAs(User::factory()->superuser()->create())
->from(route('settings.branding.index'))
->post(route('settings.branding.save',
['clear_default_avatar' => '1']
))
->assertValid('default_avatar')
->assertStatus(302)
->assertRedirect(route('settings.index'));
$setting->refresh();
$this->followRedirects($response)->assertSee(trans('alert-success'));
// $this->assertNull($setting->refresh()->default_avatar);
// Storage::disk('public')->assertMissing($original_file);
}
public function testSnipeDefaultAvatarCanBeDeleted()
{
$setting = Setting::getSettings()->first();
Storage::fake('public');
$this->actingAs(User::factory()->superuser()->create())
->post(route('settings.branding.save',
['default_avatar' => UploadedFile::fake()->image('default.png')->storeAs('avatars', 'default.png', 'public')]
));
Storage::disk('public')->assertExists('avatars/default.png');
$setting->refresh();
$this->actingAs(User::factory()->superuser()->create())
->post(route('settings.branding.save',
['clear_default_avatar' => '1']
));
$this->assertEquals('default.png', $setting->refresh()->default_avatar);
Storage::disk('public')->assertExists('avatars/default.png');
}
public function testFaviconCanBeUploaded()
{
$this->markTestIncomplete('This fails mimetype validation on the mock');
Storage::fake('testdisk');
$this->actingAs(User::factory()->superuser()->create())
->post(route('settings.branding.save',
['favicon' => UploadedFile::fake()->image('favicon.svg')]
))
->assertValid('favicon')
->assertStatus(302)
->assertRedirect(route('settings.index'))
->assertSessionHasNoErrors();
$setting = Setting::getSettings()->first();
Storage::disk('testdisk')->assertExists($setting->favicon);
}
public function testFaviconCanBeDeleted()
{
$this->markTestIncomplete('This fails mimetype validation on the mock');
Storage::fake('testdisk');
$this->actingAs(User::factory()->superuser()->create())
->post(route('settings.branding.save',
['favicon' => UploadedFile::fake()->image('favicon.ico')->mimeType('image/x-icon')]
));
$setting = Setting::getSettings()->first();
$this->actingAs(User::factory()->superuser()->create())
->post(route('settings.branding.save',['clear_favicon' => '1']));
Storage::disk('testdisk')->assertMissing('favicon.ico');
$setting->refresh();
$this->assertNull($setting->favicon);
}
}