Skip to content

A free open source IT asset/license management system

Add validation for companies

Marcus Moore0bdc04ba63eb
diff --git a/app/Console/Commands/RegenerateAcceptances.php b/app/Console/Commands/RegenerateAcceptances.php
index 0e6a767a89..e0d7a51486 100644
--- a/app/Console/Commands/RegenerateAcceptances.php
+++ b/app/Console/Commands/RegenerateAcceptances.php
@@ -51,12 +51,12 @@ class RegenerateAcceptances extends Command
$this->dryRun = (bool) $this->option('dry-run');
$this->notify = (bool) $this->option('notify');
- if ($this->refuseUnusableCategories()) {
+ if ($this->refuseUnusableScope()) {
return self::FAILURE;
}
if ($this->input->isInteractive() && ! $this->runWizard()) {
- return 0;
+ return self::SUCCESS;
}
return $this->printReport($this->regenerate(dryRun: $this->dryRun));
@@ -98,9 +98,9 @@ class RegenerateAcceptances extends Command
* refused run, since naming only the kind found first would send the operator round
* a second failed run to discover the other.
*
- * @return bool whether the run was refused
+ * @return bool whether the scope is unusable
*/
- private function refuseUnusableCategories(): bool
+ private function reportUnusableCategories(): bool
{
if ($this->categoryIds === []) {
return false;
@@ -128,9 +128,62 @@ class RegenerateAcceptances extends Command
['ID', 'Category'],
$withoutAcceptance->map(fn (Category $category) => [$category->id, $category->name])->all(),
);
+ $this->line('Turn on Require Acceptance on them, or drop them from --category.');
+ }
+
+ return true;
+ }
+
+ /**
+ * Reports every `--company` id that names no company, and says whether it is usable.
+ *
+ * A company has no acceptance setting to get wrong, so an unknown id is the only way
+ * to mis-scope by company — but it fails the same silent way a bad category id does,
+ * narrowing the run to nothing that reads as nothing to do.
+ *
+ * @return bool whether the scope is unusable
+ */
+ private function reportUnknownCompanies(): bool
+ {
+ if ($this->companyIds === []) {
+ return false;
+ }
+
+ $knownIds = Company::whereIn('id', $this->companyIds)->pluck('id')->all();
+ $unknownIds = array_values(array_diff($this->companyIds, $knownIds));
+
+ if ($unknownIds === []) {
+ return false;
+ }
+
+ $this->error(vsprintf('No company exists with %s %s.', [
+ Str::plural('id', count($unknownIds)),
+ implode(', ', $unknownIds),
+ ]));
+
+ return true;
+ }
+
+ /**
+ * Refuses a run whose `--category` or `--company` ids cannot scope it, after naming
+ * every problem with both.
+ *
+ * Both scopes are reported before the refusal, under one closing line, so an
+ * operator who mistyped an id in each is told about both rather than sent round a
+ * second failed run to discover the second.
+ *
+ * @return bool whether the run was refused
+ */
+ private function refuseUnusableScope(): bool
+ {
+ $categoriesAreUnusable = $this->reportUnusableCategories();
+ $companiesAreUnusable = $this->reportUnknownCompanies();
+
+ if (! $categoriesAreUnusable && ! $companiesAreUnusable) {
+ return false;
}
- $this->line('Nothing was run. Drop those ids from --category, or turn on Require Acceptance on the category, and run again.');
+ $this->line('Nothing was run.');
return true;
}
diff --git a/tests/Feature/Console/RegenerateAcceptancesTest.php b/tests/Feature/Console/RegenerateAcceptancesTest.php
index 8fca3ddf55..f515f8e6d0 100644
--- a/tests/Feature/Console/RegenerateAcceptancesTest.php
+++ b/tests/Feature/Console/RegenerateAcceptancesTest.php
@@ -17,6 +17,7 @@ use App\Models\Location;
use App\Models\User;
use Database\Factories\CheckoutAcceptanceFactory;
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Mail;
use Illuminate\Testing\PendingCommand;
use Tests\TestCase;
@@ -330,7 +331,8 @@ class RegenerateAcceptancesTest extends TestCase
$this->artisan('snipeit:regenerate-acceptances', ['--no-interaction' => true, '--category' => [$ignored->id]])
->expectsOutput('These categories do not require acceptance, so nothing in them can be re-requested:')
->expectsTable(['ID', 'Category'], [[$ignored->id, 'Plain Laptops']])
- ->expectsOutput('Nothing was run. Drop those ids from --category, or turn on Require Acceptance on the category, and run again.')
+ ->expectsOutput('Turn on Require Acceptance on them, or drop them from --category.')
+ ->expectsOutput('Nothing was run.')
->assertExitCode(1);
$this->assertSame([], $this->acceptanceRows());
@@ -394,6 +396,38 @@ class RegenerateAcceptancesTest extends TestCase
->assertExitCode(1);
}
+ public function test_run_is_refused_when_a_company_id_does_not_exist(): void
+ {
+ $holder = User::factory()->create();
+ $this->heldAsset($holder, $this->acceptanceCategory('asset'), Company::factory()->create(), 'In scope asset');
+
+ $this->artisan('snipeit:regenerate-acceptances', ['--no-interaction' => true, '--company' => ['999999']])
+ ->expectsOutput('No company exists with id 999999.')
+ ->expectsOutput('Nothing was run.')
+ ->assertExitCode(1);
+
+ $this->assertSame([], $this->acceptanceRows());
+ }
+
+ /**
+ * A bad category id and a bad company id are both reported by the one refused run,
+ * under a single closing line rather than one per scope that failed.
+ */
+ public function test_refusal_names_unknown_category_and_company_ids_together(): void
+ {
+ Artisan::call('snipeit:regenerate-acceptances', [
+ '--no-interaction' => true,
+ '--category' => ['999999'],
+ '--company' => ['888888'],
+ ]);
+
+ $output = Artisan::output();
+
+ $this->assertStringContainsString('No category exists with id 999999.', $output);
+ $this->assertStringContainsString('No company exists with id 888888.', $output);
+ $this->assertSame(1, substr_count($output, 'Nothing was run.'));
+ }
+
/**
* Symfony hands `--category=1,2` back as the single string `'1,2'`, and MySQL then
* coerces that string to its leading integer inside the `whereIn` — so the un-split