Handle asset not having location

This commit is contained in:
Marcus Moore
2025-05-29 14:49:53 -07:00
parent 402ca07aa2
commit bb68ed3ad9
2 changed files with 10 additions and 4 deletions

View File

@@ -140,7 +140,9 @@ class Label implements View
$barcode2DTarget = $asset->serial;
break;
case 'location':
$barcode2DTarget = route('locations.show', $asset->location_id);
$barcode2DTarget = $asset->location_id
? route('locations.show', $asset->location_id)
: null;
break;
case 'hardware_id':
default:

View File

@@ -3,6 +3,7 @@
namespace Tests\Unit\Labels;
use App\Models\Asset;
use App\Models\Location;
use App\Models\Setting;
use App\View\Label;
use Tests\TestCase;
@@ -20,16 +21,19 @@ class LabelTest extends TestCase
'label2_2d_target' => 'location',
]);
$asset = Asset::factory()->create(['location_id' => null]);
$location = Location::factory()->create();
$assets = Asset::factory()->count(2)->create(['location_id' => $location->id]);
$assets->first()->update(['location_id' => null]);
// pulled from BulkAssetsController@edit method
$label = (new Label)
// receives eloquent collection in practice
->with('assets', Asset::findMany($asset->id))
->with('assets', $assets)
->with('settings', Setting::getSettings())
->with('bulkedit', true)
->with('count', 0);
$label->render();
$this->assertTrue(true, 'Label rendering should not throw an error when location is not set on an asset.');
}
}