new test not quite working, almost there

This commit is contained in:
spencerrlongg 2024-05-01 16:12:56 -05:00
parent 39cc99c89b
commit 6a7f3ecc2e
4 changed files with 71 additions and 5 deletions

View file

@ -363,6 +363,15 @@ class AssetFactory extends Factory
}); });
} }
public function hasMultipleCustomFields(array $fields = null): self
{
return $this->state(function () use ($fields) {
return [
'model_id' => AssetModel::factory()->hasMultipleCustomFields($fields),
];
});
}
/** /**
* This allows bypassing model level validation if you want to purposefully * This allows bypassing model level validation if you want to purposefully

View file

@ -439,4 +439,13 @@ class AssetModelFactory extends Factory
]; ];
}); });
} }
public function hasMultipleCustomFields(array $fields = null)
{
return $this->state(function () use ($fields) {
return [
'fieldset_id' => CustomFieldset::factory()->hasMultipleCustomFields($fields),
];
});
}
} }

View file

@ -53,4 +53,25 @@ class CustomFieldsetFactory extends Factory
$fieldset->fields()->attach($field, ['order' => '1', 'required' => false]); $fieldset->fields()->attach($field, ['order' => '1', 'required' => false]);
}); });
} }
public function hasMultipleCustomFields(array $fields = null): self
{
return $this->afterCreating(function (CustomFieldset $fieldset) {
if (empty($fields)) {
return $this->afterCreating(function (CustomFieldset $fieldset) {
$mac_address = CustomField::factory()->macAddress()->create();
$ram = CustomField::factory()->ram()->create();
$cpu = CustomField::factory()->cpu()->create();
$fieldset->fields()->attach($mac_address, ['order' => '1', 'required' => false]);
$fieldset->fields()->attach($ram, ['order' => '2', 'required' => false]);
$fieldset->fields()->attach($cpu, ['order' => '3', 'required' => false]);
});
} else {
foreach ($fields as $field) {
$fieldset->fields()->attach($field, ['order' => '1', 'required' => false]);
}
}
});
}
} }

View file

@ -5,10 +5,10 @@ namespace Tests\Feature\Assets;
use App\Models\Asset; use App\Models\Asset;
use App\Models\AssetModel; use App\Models\AssetModel;
use App\Models\Company; use App\Models\Company;
use App\Models\CustomField;
use App\Models\Statuslabel; use App\Models\Statuslabel;
use App\Models\Supplier; use App\Models\Supplier;
use App\Models\User; use App\Models\User;
use Carbon\Carbon;
use Tests\TestCase; use Tests\TestCase;
class AssetsBulkEditTest extends TestCase class AssetsBulkEditTest extends TestCase
@ -44,7 +44,7 @@ class AssetsBulkEditTest extends TestCase
$id_array = $assets->pluck('id')->toArray(); $id_array = $assets->pluck('id')->toArray();
// submits the ids and new values for each attribute // submits the ids and new values for each attribute
$response = $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [ $this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [
'ids' => $id_array, 'ids' => $id_array,
'purchase_date' => '2024-01-01', 'purchase_date' => '2024-01-01',
'expected_checkin' => '2024-01-01', 'expected_checkin' => '2024-01-01',
@ -73,12 +73,39 @@ class AssetsBulkEditTest extends TestCase
$this->assertEquals(7890, $asset->order_number); $this->assertEquals(7890, $asset->order_number);
$this->assertEquals(36, $asset->warranty_months); $this->assertEquals(36, $asset->warranty_months);
$this->assertEquals('2025-01-01', $asset->next_audit_date->format('Y-m-d')); $this->assertEquals('2025-01-01', $asset->next_audit_date->format('Y-m-d'));
// shouldn't requestable be cast as a boolean??? // shouldn't requestable be cast as a boolean??? it's not.
$this->assertEquals(1, $asset->requestable); $this->assertEquals(1, $asset->requestable);
}); });
}
$this->assertDatabaseHas('assets', [ public function testBulkEditAssetsAcceptsAndUpdatesUnencryptedCustomFields()
'purchase_date' => '2024-01-01' {
$this->markIncompleteIfMySQL('Custom Fields tests do not work on MySQL');
$mac_address = CustomField::factory()->macAddress()->create();
$ram = CustomField::factory()->ram()->create();
$cpu = CustomField::factory()->cpu()->create();
$assets = Asset::factory()->laptopMbp()->count(10)->hasMultipleCustomFields([$mac_address, $ram, $cpu])->create([
$ram->db_column => 8,
$cpu->db_column => '2.1',
]); ]);
$id_array = $assets->pluck('id')->toArray();
// submits the ids and new values for each attribute
$asset = Asset::find(1);
$this->assertEquals(8, $asset->{$ram->db_column});
$this->actingAs(User::factory()->editAssets()->create())->post(route('hardware/bulksave'), [
'ids' => $id_array,
$ram->db_column => 16,
$cpu->db_column => '4.1',
]);
Asset::findMany($id_array)->each(function (Asset $asset) use ($ram, $cpu, $mac_address) {
$this->assertEquals(16, $asset->{$ram->db_column});
$this->assertEquals('4.1', $asset->{$ram->db_column});
});
} }
} }