snipe-it/tests/Unit/HasCustomFieldsTraitTest.php
Brady Wetherington d2b7828569 This is a squashed branch of all of the various commits that make up the new HasCustomFields trait.
This should allow us to add custom fields to just about anything we want to within Snipe-IT.

Below are the commits that have been squashed together:

Initial decoupling of custom field behavior from Assets for re-use

Add new DB columns to Custom Fields and fieldsets for 'type'

WIP: trying to figure out UI for custom fields for things other than Assets, find problematic places

Real progress towards getting to where this stuff might actually work...

Fix the table-name determining code for Custom Fields

Getting it closer to where Assets at least work

Rename the trait to it's new, even better name

Solid progress on the new Trait!

WIP: HasCustomFields, still working some stuff out

Got some basics working; creating custom fields and stuff

HasCustomFields now validates and saves

Starting to yank the other boilerplate code as things start to work (!)

Got the start of defaultValuesForCustomField() working

More progress (squash me!)

Add migrations for default_values_for_custom_fields table

WIP: more towards hasCustomFields trait

Progress cleaning up the PR, fixing FIXME's

New, passing HasCustomFieldsTrait test!

Fix date formatter helper for custom fields

Fixed more FIXME's
2024-06-06 13:35:38 +01:00

80 lines
2.9 KiB
PHP

<?php
namespace Tests\Unit;
use App\Models\Asset;
use App\Models\CustomField;
use App\Models\CustomFieldset;
use App\Models\Traits\HasCustomFields;
use Illuminate\Support\Collection;
use Tests\Support\InteractsWithSettings;
use Tests\TestCase;
use Illuminate\Support\Facades\Schema;
class HasCustomFieldsTraitTest extends TestCase
{
use InteractsWithSettings; //seems bonkers, but Assets needs it? (for currency calculation?)
public function testAssetSchema()
{
$asset = Asset::factory()->withComplicatedCustomFields()->create();
$this->assertEquals($asset->model->fieldset->fields->count(), 3,'Custom Fieldset should have exactly 3 custom fields');
$this->assertTrue(Schema::hasColumn('assets','_snipeit_mac_address_explicit_2'),'Assets table should have MAC address column');
$this->assertTrue(Schema::hasColumn('assets','_snipeit_plain_text_3'),'Assets table should have MAC address column');
$this->assertTrue(Schema::hasColumn('assets','_snipeit_date_4'),'Assets table should have MAC address column');
}
public function testRequired()
{
$asset = Asset::factory()->withComplicatedCustomFields()->create();
$this->assertFalse($asset->save(),'save() should fail due to required text field');
}
public function testFormat()
{
$asset = Asset::factory()->withComplicatedCustomFields()->make();
$asset->_snipeit_plain_text_3 = 'something';
$asset->_snipeit_mac_address_explicit_2 = 'fartsssssss';
$this->assertFalse($asset->save(), 'should fail due to bad MAC address');
}
public function testDate()
{
// \Log::error("uh, what the heck is going on here?!");
$asset = Asset::factory()->withComplicatedCustomFields()->make();
$asset->_snipeit_plain_text_3 = 'some text';
$asset->_snipeit_date_4 = '1/2/2023';
// $asset->save();
// dd($asset);
$this->assertFalse($asset->save(),'Should fail due to incorrectly formatted date.');
}
public function testSaveMinimal()
{
$asset = Asset::factory()->withComplicatedCustomFields()->make();
$asset->_snipeit_plain_text_3 = "some text";
$this->assertTrue($asset->save(),"Asset should've saved okay, the one required field was filled out");
}
public function testSaveMaximal()
{
$asset = Asset::factory()->withComplicatedCustomFields()->make();
$asset->_snipeit_plain_text_3 = "some text";
$asset->_snipeit_date_4 = "2023-01-02";
$asset->_snipeit_mac_address_explicit_2 = "ff:ff:ff:ff:ff:ff";
$this->assertTrue($asset->save(),"Asset should've saved okay, the one required field was filled out, and so were the others");
}
public function testJsonPost()
{
$asset = Asset::factory()->withComplicatedCustomFields()->make();
$response = $this->postJson('/api/v1/hardware', [
]);
$response->assertStatus(200);
}
}