Skip to content

A free open source IT asset/license management system

Added “all companies” to bulk delete console command

snipe518c1bf002f5
diff --git a/app/Console/Commands/BulkDelete.php b/app/Console/Commands/BulkDelete.php
index a1cd8d660d..b097d27bb7 100644
--- a/app/Console/Commands/BulkDelete.php
+++ b/app/Console/Commands/BulkDelete.php
@@ -98,6 +98,10 @@ class BulkDelete extends Command
options: function (string $value): array {
$results = [];
+ if ($value === '' || str_contains('(all companies)', strtolower($value))) {
+ $results['__all__'] = '(All Companies + Unassigned)';
+ }
+
if ($value === '' || str_contains('(no company / unassigned)', strtolower($value))) {
$results['__null__'] = '(No Company / Unassigned)';
}
@@ -118,6 +122,17 @@ class BulkDelete extends Command
hint: 'If you\'re searching on several differently named companies, use the up-arrow to go back to the search box to search again. ',
);
+ // "All Companies + Unassigned" expands to every company plus the
+ // no-company scope, so callers don't have to pick each one by
+ // hand. Any other selections alongside it are redundant, so we
+ // let it win by resetting the key list.
+ if (in_array('__all__', $selectedCompanyKeys, true)) {
+ $selectedCompanyKeys = array_merge(
+ Company::orderBy('name')->pluck('id')->map(fn ($id) => (int) $id)->all(),
+ ['__null__'],
+ );
+ }
+
$includeNullCompany = in_array('__null__', $selectedCompanyKeys);
$selectedCompanyIds = array_values(array_filter(
$selectedCompanyKeys,
diff --git a/tests/Feature/Console/BulkDeleteAllCompaniesTest.php b/tests/Feature/Console/BulkDeleteAllCompaniesTest.php
new file mode 100644
index 0000000000..b56c44a8c0
--- /dev/null
+++ b/tests/Feature/Console/BulkDeleteAllCompaniesTest.php
@@ -0,0 +1,132 @@
+<?php
+
+namespace Tests\Feature\Console;
+
+use App\Models\Asset;
+use App\Models\Company;
+use App\Models\User;
+use Illuminate\Testing\PendingCommand;
+use Tests\TestCase;
+
+/**
+ * Covers the '__all__' sentinel in the multisearch company picker:
+ * selecting "All Companies + Unassigned" must expand to every company
+ * id plus the no-company scope, so items across every tenant are
+ * covered without picking each one by hand.
+ */
+class BulkDeleteAllCompaniesTest extends TestCase
+{
+ private function runCommand(User $admin, array $companySelectionKeys, array $types, string $deleteType): PendingCommand
+ {
+ $searchLabel = 'Who are you? Search by username, first or last name.';
+ $companiesLabel = 'Which companies would you like to check in and delete items for?';
+ $typesLabel = 'What item types would you like to check in and delete?';
+ $deleteLabel = 'How should items be deleted?';
+
+ $hasNotifiable = ! empty(array_intersect($types, ['assets', 'licenses', 'accessories', 'components']));
+
+ $cmd = $this->artisan('snipeit:checkin-delete-items')
+ ->expectsConfirmation('Is this a dry run?', 'no')
+ ->expectsQuestion($searchLabel, $admin->username)
+ ->expectsQuestion($searchLabel, (string) $admin->id)
+ ->expectsQuestion($companiesLabel, '')
+ ->expectsQuestion($companiesLabel, $companySelectionKeys)
+ ->expectsQuestion($typesLabel, $types)
+ ->expectsQuestion($deleteLabel, $deleteType);
+
+ if ($hasNotifiable) {
+ $cmd->expectsConfirmation('Should we send checkin notifications?', 'no');
+ }
+
+ $cmd->expectsConfirmation('Should we clear related action logs?', 'no')
+ ->expectsConfirmation('Should we also delete associated image and upload files?', 'no');
+
+ // The company-deletion prompt only appears when real company ids
+ // (not just __null__) end up in scope. __all__ expands to real
+ // company ids, so the prompt does render for that case.
+ $expandsToRealCompanies = in_array('__all__', $companySelectionKeys, true)
+ || count(array_filter($companySelectionKeys, fn ($k) => is_int($k))) > 0;
+ if ($expandsToRealCompanies) {
+ $cmd->expectsQuestion('Should the selected companies also be deleted?', 'keep');
+ }
+
+ $cmd->expectsConfirmation('Should we run a backup before proceeding?', 'no');
+
+ if ($admin->email) {
+ $cmd->expectsConfirmation("Send an email report to {$admin->email}?", 'no');
+ }
+
+ return $cmd->expectsConfirmation('Are you sure you want to proceed? This cannot be undone.', 'yes');
+ }
+
+ public function test_all_companies_sentinel_covers_every_company_plus_unassigned(): void
+ {
+ $admin = User::factory()->superuser()->create();
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assetA = Asset::factory()->for($companyA)->create();
+ $assetB = Asset::factory()->for($companyB)->create();
+ $orphanAsset = Asset::factory()->create(['company_id' => null]);
+
+ $this->runCommand($admin, ['__all__'], ['assets'], 'soft')->assertExitCode(0);
+
+ $this->assertSoftDeleted($assetA);
+ $this->assertSoftDeleted($assetB);
+ $this->assertSoftDeleted($orphanAsset);
+ }
+
+ public function test_all_companies_sentinel_wins_when_combined_with_specific_picks(): void
+ {
+ // Selecting __all__ alongside a specific company id shouldn't
+ // narrow the scope. The __all__ branch resets the key list to
+ // every company plus __null__, so the specific pick becomes a
+ // no-op rather than a filter.
+ $admin = User::factory()->superuser()->create();
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assetA = Asset::factory()->for($companyA)->create();
+ $assetB = Asset::factory()->for($companyB)->create();
+ $orphanAsset = Asset::factory()->create(['company_id' => null]);
+
+ $this->runCommand($admin, ['__all__', $companyA->id], ['assets'], 'soft')->assertExitCode(0);
+
+ $this->assertSoftDeleted($assetA);
+ $this->assertSoftDeleted($assetB);
+ $this->assertSoftDeleted($orphanAsset);
+ }
+
+ public function test_specific_company_only_still_scopes_correctly(): void
+ {
+ // Regression guard: __all__ handling must not accidentally
+ // widen a specific-company selection.
+ $admin = User::factory()->superuser()->create();
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assetA = Asset::factory()->for($companyA)->create();
+ $assetB = Asset::factory()->for($companyB)->create();
+ $orphanAsset = Asset::factory()->create(['company_id' => null]);
+
+ $this->runCommand($admin, [$companyA->id], ['assets'], 'soft')->assertExitCode(0);
+
+ $this->assertSoftDeleted($assetA);
+ $this->assertNotSoftDeleted($assetB);
+ $this->assertNotSoftDeleted($orphanAsset);
+ }
+
+ public function test_null_only_still_scopes_to_unassigned_only(): void
+ {
+ // Regression guard: __null__ alone still means "no-company only".
+ $admin = User::factory()->superuser()->create();
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $assetA = Asset::factory()->for($companyA)->create();
+ $assetB = Asset::factory()->for($companyB)->create();
+ $orphanAsset = Asset::factory()->create(['company_id' => null]);
+
+ $this->runCommand($admin, ['__null__'], ['assets'], 'soft')->assertExitCode(0);
+
+ $this->assertSoftDeleted($orphanAsset);
+ $this->assertNotSoftDeleted($assetA);
+ $this->assertNotSoftDeleted($assetB);
+ }
+}