From 3105f53afffdd78311314e970f6fe893e1162f8e Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 12 Sep 2024 16:54:29 -0700 Subject: [PATCH] Add tests for delete custom fieldsets endpoint --- database/factories/UserFactory.php | 5 ++ .../Api/DeleteCustomFieldsTest.php | 4 ++ .../Api/DeleteCustomFieldsetsTest.php | 65 +++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index cc884a90b6..c82cabcc49 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -326,6 +326,11 @@ class UserFactory extends Factory return $this->appendPermission(['customfields.delete' => '1']); } + public function deleteCustomFieldsets() + { + 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 index cc16e77ce6..72d8a06953 100644 --- a/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php +++ b/tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php @@ -12,6 +12,8 @@ class DeleteCustomFieldsTest extends TestCase implements TestsPermissionsRequire { public function testRequiresPermission() { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + $customField = CustomField::factory()->create(); $this->actingAsForApi(User::factory()->create()) @@ -21,6 +23,8 @@ class DeleteCustomFieldsTest extends TestCase implements TestsPermissionsRequire public function testCustomFieldsCanBeDeleted() { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + $customField = CustomField::factory()->create(); $this->actingAsForApi(User::factory()->deleteCustomFields()->create()) diff --git a/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php b/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php new file mode 100644 index 0000000000..2610b4cff5 --- /dev/null +++ b/tests/Feature/CustomFieldsets/Api/DeleteCustomFieldsetsTest.php @@ -0,0 +1,65 @@ +markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + $customFieldset = CustomFieldset::factory()->create(); + + $this->actingAsForApi(User::factory()->create()) + ->deleteJson(route('api.fieldsets.destroy', $customFieldset)) + ->assertForbidden(); + } + + public function testCanDeleteCustomFieldsets() + { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + $customFieldset = CustomFieldset::factory()->create(); + + $this->actingAsForApi(User::factory()->deleteCustomFieldsets()->create()) + ->deleteJson(route('api.fieldsets.destroy', $customFieldset)) + ->assertStatusMessageIs('success'); + + $this->assertDatabaseMissing('custom_fieldsets', ['id' => $customFieldset->id]); + } + + public function testCannotDeleteCustomFieldsetWithAssociatedFields() + { + $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()->deleteCustomFieldsets()->create()) + ->deleteJson(route('api.fieldsets.destroy', $customFieldset)) + ->assertStatusMessageIs('error'); + + $this->assertDatabaseHas('custom_fieldsets', ['id' => $customFieldset->id]); + } + + public function testCannotDeleteCustomFieldsetWithAssociatedModels() + { + $this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL'); + + $customFieldset = CustomFieldset::factory()->hasModels()->create(); + + $this->actingAsForApi(User::factory()->deleteCustomFieldsets()->create()) + ->deleteJson(route('api.fieldsets.destroy', $customFieldset)) + ->assertStatusMessageIs('error'); + + $this->assertDatabaseHas('custom_fieldsets', ['id' => $customFieldset->id]); + } +}