diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index 03bc193329..cc884a90b6 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -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) { diff --git a/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php b/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php new file mode 100644 index 0000000000..cc16e77ce6 --- /dev/null +++ b/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php @@ -0,0 +1,48 @@ +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]); + } +}