Skip to content

Commit 246f137

Browse files
authored
Merge pull request #16948 from marcusmoore/bug/sc-29181
Require assigned_x to be integer on asset model
2 parents b993f42 + 02fa7da commit 246f137

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

app/Models/Asset.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ public function declinedCheckout(User $declinedBy, $signature)
122122
'assigned_to' => ['nullable', 'integer', 'required_with:assigned_type'],
123123
'assigned_type' => ['nullable', 'required_with:assigned_to', 'in:'.User::class.",".Location::class.",".Asset::class],
124124
'requestable' => ['nullable', 'boolean'],
125-
'assigned_user' => ['nullable', 'exists:users,id,deleted_at,NULL'],
126-
'assigned_location' => ['nullable', 'exists:locations,id,deleted_at,NULL', 'fmcs_location'],
127-
'assigned_asset' => ['nullable', 'exists:assets,id,deleted_at,NULL']
125+
'assigned_user' => ['integer', 'nullable', 'exists:users,id,deleted_at,NULL'],
126+
'assigned_location' => ['integer', 'nullable', 'exists:locations,id,deleted_at,NULL', 'fmcs_location'],
127+
'assigned_asset' => ['integer', 'nullable', 'exists:assets,id,deleted_at,NULL']
128128
];
129129

130130

tests/Feature/Assets/Api/StoreAssetTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use App\Models\User;
1313
use Illuminate\Support\Facades\Crypt;
1414
use Illuminate\Testing\Fluent\AssertableJson;
15+
use PHPUnit\Framework\Attributes\DataProvider;
1516
use Tests\TestCase;
1617

1718
class StoreAssetTest extends TestCase
@@ -572,6 +573,64 @@ public function testAnAssetCanBeCheckedOutToUserOnStore()
572573
$this->assertTrue($asset->assignedTo->is($userAssigned));
573574
}
574575

576+
public static function checkoutTargets()
577+
{
578+
yield 'Users' => [
579+
function () {
580+
return [
581+
'key' => 'assigned_user',
582+
'value' => [
583+
User::factory()->create()->id,
584+
User::factory()->create()->id,
585+
],
586+
];
587+
},
588+
];
589+
590+
yield 'Locations' => [
591+
function () {
592+
return [
593+
'key' => 'assigned_location',
594+
'value' => [
595+
Location::factory()->create()->id,
596+
Location::factory()->create()->id,
597+
],
598+
];
599+
},
600+
];
601+
602+
yield 'Assets' => [
603+
function () {
604+
return [
605+
'key' => 'assigned_asset',
606+
'value' => [
607+
Asset::factory()->create()->id,
608+
Asset::factory()->create()->id,
609+
],
610+
];
611+
},
612+
];
613+
}
614+
615+
/** @link https://app.shortcut.com/grokability/story/29181 */
616+
#[DataProvider('checkoutTargets')]
617+
public function testAssignedFieldValidationCannotBeArray($data)
618+
{
619+
['key' => $key, 'value' => $value] = $data();
620+
621+
$this->actingAsForApi(User::factory()->createAssets()->create())
622+
->postJson(route('api.assets.store'), [
623+
'asset_tag' => '123456',
624+
'model_id' => AssetModel::factory()->create()->id,
625+
'status_id' => Statuslabel::factory()->readyToDeploy()->create()->id,
626+
$key => $value,
627+
])
628+
->assertStatusMessageIs('error')
629+
->assertJson(function (AssertableJson $json) use ($key) {
630+
$json->has("messages.{$key}")->etc();
631+
});
632+
}
633+
575634
public function testAnAssetCanBeCheckedOutToLocationOnStore()
576635
{
577636
$model = AssetModel::factory()->create();

0 commit comments

Comments
 (0)