Skip to content

Commit

Permalink
Add tests for delete custom fields endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusmoore committed Sep 12, 2024
1 parent e3268d3 commit 2047cfe
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
5 changes: 5 additions & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ public function canImport()
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) {
Expand Down
48 changes: 48 additions & 0 deletions tests/Feature/CustomFields/Api/DeleteCustomFieldsTest.php
Original file line number Diff line number Diff line change
@@ -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]);
}
}

0 comments on commit 2047cfe

Please sign in to comment.