Skip to content

A free open source IT asset/license management system

Handle asset not having location

Marcus Moorebb68ed3ad9f0
diff --git a/app/View/Label.php b/app/View/Label.php
index 11370cfcdf..d168be883b 100644
--- a/app/View/Label.php
+++ b/app/View/Label.php
@@ -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:
diff --git a/tests/Unit/Labels/LabelTest.php b/tests/Unit/Labels/LabelTest.php
index b175dfcad9..a0e1c05348 100644
--- a/tests/Unit/Labels/LabelTest.php
+++ b/tests/Unit/Labels/LabelTest.php
@@ -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.');
}
}