Skip to content

Commit 1c38779

Browse files
authored
Merge pull request #16683 from marcusmoore/bug/sc-28755
Create default label when importing assets if none exists
2 parents 102f26c + b82d835 commit 1c38779

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

app/Importer/AssetImporter.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,22 @@ public function __construct($filename)
1616
{
1717
parent::__construct($filename);
1818

19-
$this->defaultStatusLabelId = Statuslabel::first()->id;
20-
19+
$this->defaultStatusLabelId = Statuslabel::first()?->id;
20+
2121
if (!is_null(Statuslabel::deployable()->first())) {
22-
$this->defaultStatusLabelId = Statuslabel::deployable()->first()->id;
22+
$this->defaultStatusLabelId = Statuslabel::deployable()->first()?->id;
23+
}
24+
25+
if (is_null($this->defaultStatusLabelId)) {
26+
$defaultLabel = Statuslabel::create([
27+
'name' => 'Default Status',
28+
'deployable' => 0,
29+
'pending' => 1,
30+
'archived' => 0,
31+
'notes' => 'Default status label created by AssetImporter',
32+
]);
33+
34+
$this->defaultStatusLabelId = $defaultLabel->id;
2335
}
2436
}
2537

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Tests\Unit\Importer;
4+
5+
use App\Importer\AssetImporter;
6+
use App\Models\Statuslabel;
7+
use Tests\TestCase;
8+
use function Livewire\invade;
9+
10+
class AssetImportTest extends TestCase
11+
{
12+
public function test_uses_first_deployable_status_label_as_default_if_one_exists()
13+
{
14+
Statuslabel::truncate();
15+
16+
$pendingStatusLabel = Statuslabel::factory()->pending()->create();
17+
$readyToDeployStatusLabel = Statuslabel::factory()->readyToDeploy()->create();
18+
19+
$importer = new AssetImporter('assets.csv');
20+
21+
$this->assertEquals(
22+
$readyToDeployStatusLabel->id,
23+
invade($importer)->defaultStatusLabelId
24+
);
25+
}
26+
27+
public function test_uses_first_status_label_as_default_if_deployable_status_label_does_not_exist()
28+
{
29+
Statuslabel::truncate();
30+
31+
$statusLabel = Statuslabel::factory()->pending()->create();
32+
33+
$importer = new AssetImporter('assets.csv');
34+
35+
$this->assertEquals(
36+
$statusLabel->id,
37+
invade($importer)->defaultStatusLabelId
38+
);
39+
}
40+
41+
public function test_creates_default_status_label_if_one_does_not_exist()
42+
{
43+
Statuslabel::truncate();
44+
45+
$this->assertEquals(0, Statuslabel::count());
46+
47+
$importer = new AssetImporter('assets.csv');
48+
49+
$this->assertEquals(1, Statuslabel::count());
50+
51+
$this->assertEquals(
52+
Statuslabel::first()->id,
53+
invade($importer)->defaultStatusLabelId
54+
);
55+
}
56+
}

0 commit comments

Comments
 (0)