From 91f3556375b09b3a8f35d8817dee22ddda259961 Mon Sep 17 00:00:00 2001 From: snipe Date: Sun, 17 Aug 2025 13:33:53 +0100 Subject: [PATCH] Added delete test Signed-off-by: snipe --- .../Ui/DeleteCustomFieldsTest.php | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 tests/Feature/CustomFields/Ui/DeleteCustomFieldsTest.php diff --git a/tests/Feature/CustomFields/Ui/DeleteCustomFieldsTest.php b/tests/Feature/CustomFields/Ui/DeleteCustomFieldsTest.php new file mode 100644 index 0000000000..543122104f --- /dev/null +++ b/tests/Feature/CustomFields/Ui/DeleteCustomFieldsTest.php @@ -0,0 +1,69 @@ +actingAs(User::factory()->create()) + ->delete(route('fields.destroy', CustomField::factory()->create())) + ->assertForbidden(); + } + + + public function testCanDeleteCustomField() + { + $field = CustomField::factory()->create(); + $this->assertDatabaseHas('custom_fields', ['id' => $field->id]); + + $this->actingAs(User::factory()->deleteCustomFields()->create()) + ->delete(route('fields.destroy', $field)) + ->assertRedirectToRoute('fields.index') + ->assertStatus(302) + ->assertSessionHas('success'); + + $this->assertDatabaseMissing('custom_fields', ['id' => $field->id]); + } + + public function testCannotDeleteCustomFieldThatDoesNotExist() + { + + $response = $this->actingAs(User::factory()->viewCustomFields()->deleteCustomFields()->create()) + ->delete(route('fields.destroy', '49857589')) + ->assertRedirect(route('fields.index')) + ->assertSessionHas('error'); + + $temp = $this->followRedirects($response); + $temp->assertSee(trans('general.error'))->assertSee(trans('general.generic_model_not_found', ['model' => 'custom field'])); + + } + + public function testCannotDeleteFieldThatIsAssociatedWithFieldsets() + { + $field = CustomField::factory()->create(); + $fieldset = CustomFieldset::factory()->create(); + + $this->actingAs(User::factory()->superuser()->create()) + ->post(route('fieldsets.associate', $fieldset), [ + 'field_id' => $field->id, + ]); + + $response = $this->actingAs(User::factory()->viewCustomFields()->deleteCustomFields()->create()) + ->from(route('fields.index')) + ->delete(route('fields.destroy', $field)) + ->assertStatus(302) + ->assertRedirect(route('fields.index')) + ->assertSessionHas('error'); + + $this->followRedirects($response)->assertSee(trans('general.error'))->assertSee(trans('admin/custom_fields/message.field.delete.in_use')); + + // Ensure the field is still in the database + $this->assertDatabaseHas('custom_fields', ['id' => $field->id]); + } +}