Skip to content

Create default label when importing assets if none exists #16683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions app/Importer/AssetImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,22 @@ public function __construct($filename)
{
parent::__construct($filename);

$this->defaultStatusLabelId = Statuslabel::first()->id;
$this->defaultStatusLabelId = Statuslabel::first()?->id;

if (!is_null(Statuslabel::deployable()->first())) {
$this->defaultStatusLabelId = Statuslabel::deployable()->first()->id;
$this->defaultStatusLabelId = Statuslabel::deployable()->first()?->id;
}

if (is_null($this->defaultStatusLabelId)) {
$defaultLabel = Statuslabel::create([
'name' => 'Default Status',
'deployable' => 0,
'pending' => 1,
'archived' => 0,
'notes' => 'Default status label created by AssetImporter',
]);

$this->defaultStatusLabelId = $defaultLabel->id;
}
}

Expand Down
56 changes: 56 additions & 0 deletions tests/Unit/Importer/AssetImportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Tests\Unit\Importer;

use App\Importer\AssetImporter;
use App\Models\Statuslabel;
use Tests\TestCase;
use function Livewire\invade;

class AssetImportTest extends TestCase
{
public function test_uses_first_deployable_status_label_as_default_if_one_exists()
{
Statuslabel::truncate();

$pendingStatusLabel = Statuslabel::factory()->pending()->create();
$readyToDeployStatusLabel = Statuslabel::factory()->readyToDeploy()->create();

$importer = new AssetImporter('assets.csv');

$this->assertEquals(
$readyToDeployStatusLabel->id,
invade($importer)->defaultStatusLabelId
);
}

public function test_uses_first_status_label_as_default_if_deployable_status_label_does_not_exist()
{
Statuslabel::truncate();

$statusLabel = Statuslabel::factory()->pending()->create();

$importer = new AssetImporter('assets.csv');

$this->assertEquals(
$statusLabel->id,
invade($importer)->defaultStatusLabelId
);
}

public function test_creates_default_status_label_if_one_does_not_exist()
{
Statuslabel::truncate();

$this->assertEquals(0, Statuslabel::count());

$importer = new AssetImporter('assets.csv');

$this->assertEquals(1, Statuslabel::count());

$this->assertEquals(
Statuslabel::first()->id,
invade($importer)->defaultStatusLabelId
);
}
}
Loading