Add tests for delete custom fields endpoint

This commit is contained in:
Marcus Moore 2024-09-12 16:20:32 -07:00
parent e3268d32df
commit 2047cfed09
No known key found for this signature in database
2 changed files with 53 additions and 0 deletions

View file

@ -321,6 +321,11 @@ class UserFactory extends Factory
return $this->appendPermission(['import' => '1']);
}
public function deleteCustomFields()
{
return $this->appendPermission(['customfields.delete' => '1']);
}
private function appendPermission(array $permission)
{
return $this->state(function ($currentState) use ($permission) {

View file

@ -0,0 +1,48 @@
<?php
namespace Tests\Feature\CustomFields\Api;
use App\Models\CustomField;
use App\Models\CustomFieldset;
use App\Models\User;
use Tests\Concerns\TestsPermissionsRequirement;
use Tests\TestCase;
class DeleteCustomFieldsTest extends TestCase implements TestsPermissionsRequirement
{
public function testRequiresPermission()
{
$customField = CustomField::factory()->create();
$this->actingAsForApi(User::factory()->create())
->deleteJson(route('api.customfields.destroy', $customField))
->assertForbidden();
}
public function testCustomFieldsCanBeDeleted()
{
$customField = CustomField::factory()->create();
$this->actingAsForApi(User::factory()->deleteCustomFields()->create())
->deleteJson(route('api.customfields.destroy', $customField))
->assertStatusMessageIs('success');
$this->assertDatabaseMissing('custom_fields', ['id' => $customField->id]);
}
public function testCustomFieldsCannotBeDeletedIfTheyHaveAssociatedFieldsets()
{
$this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL');
$customField = CustomField::factory()->create();
$customFieldset = CustomFieldset::factory()->create();
$customField->fieldset()->attach($customFieldset, ['order' => 1, 'required' => 'false']);
$this->actingAsForApi(User::factory()->deleteCustomFields()->create())
->deleteJson(route('api.customfields.destroy', $customField))
->assertStatusMessageIs('error');
$this->assertDatabaseHas('custom_fields', ['id' => $customField->id]);
}
}