Skip to content

A free open source IT asset/license management system

Add validation for categories

Marcus Mooree7da56494e48
diff --git a/app/Console/Commands/RegenerateAcceptances.php b/app/Console/Commands/RegenerateAcceptances.php
index 32d67e3096..0e6a767a89 100644
--- a/app/Console/Commands/RegenerateAcceptances.php
+++ b/app/Console/Commands/RegenerateAcceptances.php
@@ -7,6 +7,7 @@ use App\Actions\Acceptances\RegenerateAcceptancesResult;
use App\Models\Category;
use App\Models\Company;
use Illuminate\Console\Command;
+use Illuminate\Support\Str;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\multiselect;
@@ -44,12 +45,16 @@ class RegenerateAcceptances extends Command
public function handle(): int
{
- $this->categoryIds = $this->option('category');
- $this->companyIds = $this->option('company');
+ $this->categoryIds = $this->splitIds($this->option('category'));
+ $this->companyIds = $this->splitIds($this->option('company'));
$this->excludeDeclined = (bool) $this->option('exclude-declined');
$this->dryRun = (bool) $this->option('dry-run');
$this->notify = (bool) $this->option('notify');
+ if ($this->refuseUnusableCategories()) {
+ return self::FAILURE;
+ }
+
if ($this->input->isInteractive() && ! $this->runWizard()) {
return 0;
}
@@ -57,6 +62,79 @@ class RegenerateAcceptances extends Command
return $this->printReport($this->regenerate(dryRun: $this->dryRun));
}
+ /**
+ * The ids behind a repeatable id option, with comma-separated values split out.
+ *
+ * Symfony hands `--category=1,2` back as the single string `'1,2'` rather than two
+ * ids, and MySQL then coerces that string to its leading integer inside a `whereIn`
+ * — so the un-split form scopes the run to category 1 alone and reports nothing
+ * amiss. Splitting here makes `--category=1,2` and `--category=1 --category=2` mean
+ * the same thing, and leaves a genuinely unknown id to be named on its own.
+ *
+ * @param array<int, string> $values
+ * @return array<int, string>
+ */
+ private function splitIds(array $values): array
+ {
+ return collect($values)
+ ->flatMap(fn (string $value) => explode(',', $value))
+ ->map(fn (string $id) => trim($id))
+ ->filter()
+ ->values()
+ ->all();
+ }
+
+ /**
+ * Reports every `--category` id the run cannot use, and says whether it was refused.
+ *
+ * `--category` is ANDed with the requires-acceptance scope rather than added to it,
+ * so an id that is unknown, or whose category does not require acceptance,
+ * contributes nothing and the run reports an empty scope. That report is
+ * indistinguishable from a correctly scoped run with no work to do, which is how a
+ * mistyped id reads to an operator as a clean bill of health.
+ *
+ * The two mistakes are reported separately because they are fixed differently — one
+ * is a wrong id, the other a category setting — and both are reported by the one
+ * 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
+ */
+ private function refuseUnusableCategories(): bool
+ {
+ if ($this->categoryIds === []) {
+ return false;
+ }
+
+ $categories = Category::whereIn('id', $this->categoryIds)->orderBy('name')->get();
+
+ $unknownIds = array_values(array_diff($this->categoryIds, $categories->modelKeys()));
+ $withoutAcceptance = $categories->where('require_acceptance', false);
+
+ if ($unknownIds === [] && $withoutAcceptance->isEmpty()) {
+ return false;
+ }
+
+ if ($unknownIds !== []) {
+ $this->error(vsprintf('No category exists with %s %s.', [
+ Str::plural('id', count($unknownIds)),
+ implode(', ', $unknownIds),
+ ]));
+ }
+
+ if ($withoutAcceptance->isNotEmpty()) {
+ $this->error('These categories do not require acceptance, so nothing in them can be re-requested:');
+ $this->table(
+ ['ID', 'Category'],
+ $withoutAcceptance->map(fn (Category $category) => [$category->id, $category->name])->all(),
+ );
+ }
+
+ $this->line('Nothing was run. Drop those ids from --category, or turn on Require Acceptance on the category, and run again.');
+
+ return true;
+ }
+
/**
* One pass of the Action with whatever options are settled by now.
*/
diff --git a/tests/Feature/Console/RegenerateAcceptancesTest.php b/tests/Feature/Console/RegenerateAcceptancesTest.php
index 9f19bd8b04..8fca3ddf55 100644
--- a/tests/Feature/Console/RegenerateAcceptancesTest.php
+++ b/tests/Feature/Console/RegenerateAcceptancesTest.php
@@ -309,6 +309,150 @@ class RegenerateAcceptancesTest extends TestCase
->assertExitCode(0);
}
+ /**
+ * `--category` is ANDed with the requires-acceptance scope, so a category that does
+ * not require acceptance narrows the run to nothing rather than widening it into
+ * that category. The danger is not a wrong set — it is that an empty report reads
+ * exactly like "correct scope, nothing to do", so a fat-fingered id looks like a
+ * clean bill of health. Refusing to run, and naming what is wrong with each id, is
+ * what tells the two apart.
+ */
+ public function test_run_is_refused_when_a_category_does_not_require_acceptance(): void
+ {
+ $holder = User::factory()->create();
+ $this->heldAsset($holder, $this->acceptanceCategory('asset'), Company::factory()->create(), 'In scope asset');
+ $ignored = Category::factory()->create([
+ 'name' => 'Plain Laptops',
+ 'category_type' => 'asset',
+ 'require_acceptance' => false,
+ ]);
+
+ $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.')
+ ->assertExitCode(1);
+
+ $this->assertSame([], $this->acceptanceRows());
+ }
+
+ public function test_run_is_refused_when_a_category_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, '--category' => ['999999']])
+ ->expectsOutput('No category exists with id 999999.')
+ ->assertExitCode(1);
+
+ $this->assertSame([], $this->acceptanceRows());
+ }
+
+ /**
+ * Both kinds of bad id are reported by the one refused run.
+ *
+ * Reporting only the first kind found would send the operator round a second failed
+ * run to discover the second, which is the same "fix it, still nothing" loop the
+ * refusal exists to end.
+ */
+ public function test_refusal_names_unknown_and_non_acceptance_category_ids_together(): void
+ {
+ $ignored = Category::factory()->create([
+ 'name' => 'Office Supplies',
+ 'category_type' => 'accessory',
+ 'require_acceptance' => false,
+ ]);
+
+ $this->artisan('snipeit:regenerate-acceptances', [
+ '--no-interaction' => true,
+ '--category' => ['999999', $ignored->id, '888888'],
+ ])
+ ->expectsOutput('No category exists with ids 999999, 888888.')
+ ->expectsOutput('These categories do not require acceptance, so nothing in them can be re-requested:')
+ ->expectsTable(['ID', 'Category'], [[$ignored->id, 'Office Supplies']])
+ ->assertExitCode(1);
+ }
+
+ /**
+ * The refusal lands before the wizard, not after it.
+ *
+ * No prompt is pre-answered, so reaching one throws rather than taking a default —
+ * which is what proves the operator is not walked through four questions before
+ * being told the scope they passed was unusable.
+ */
+ public function test_interactive_run_is_refused_before_any_prompt_is_asked(): void
+ {
+ $valid = $this->acceptanceCategory('asset');
+ $ignored = Category::factory()->create([
+ 'name' => 'Plain Laptops',
+ 'category_type' => 'asset',
+ 'require_acceptance' => false,
+ ]);
+
+ $this->artisan('snipeit:regenerate-acceptances', ['--category' => [$valid->id, $ignored->id]])
+ ->expectsTable(['ID', 'Category'], [[$ignored->id, 'Plain Laptops']])
+ ->assertExitCode(1);
+ }
+
+ /**
+ * 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
+ * form used to scope the run to category 1 alone while reporting nothing amiss.
+ */
+ public function test_comma_separated_category_ids_are_accepted(): void
+ {
+ $holder = User::factory()->create();
+ $first = $this->acceptanceCategory('asset');
+ $second = $this->acceptanceCategory('asset');
+
+ $this->heldAsset($holder, $first, Company::factory()->create(), 'First asset');
+ $this->heldAsset($holder, $second, Company::factory()->create(), 'Second asset');
+ $this->heldAsset($holder, $this->acceptanceCategory('asset'), Company::factory()->create(), 'Out of scope asset');
+
+ $this->artisan('snipeit:regenerate-acceptances', [
+ '--no-interaction' => true,
+ '--category' => [$first->id.','.$second->id],
+ ])
+ ->expectsOutputToContain('First asset')
+ ->expectsOutputToContain('Second asset')
+ ->expectsOutput('To re-request: 2.')
+ ->assertExitCode(0);
+ }
+
+ public function test_comma_separated_company_ids_are_accepted(): void
+ {
+ $holder = User::factory()->create();
+ $category = $this->acceptanceCategory('asset');
+ $first = Company::factory()->create();
+ $second = Company::factory()->create();
+
+ $this->heldAsset($holder, $category, $first, 'First asset');
+ $this->heldAsset($holder, $category, $second, 'Second asset');
+ $this->heldAsset($holder, $category, Company::factory()->create(), 'Out of scope asset');
+
+ $this->artisan('snipeit:regenerate-acceptances', [
+ '--no-interaction' => true,
+ '--company' => [$first->id.','.$second->id],
+ ])
+ ->expectsOutputToContain('First asset')
+ ->expectsOutputToContain('Second asset')
+ ->expectsOutput('To re-request: 2.')
+ ->assertExitCode(0);
+ }
+
+ public function test_refusal_names_only_the_unknown_id_from_a_comma_separated_list(): void
+ {
+ $first = $this->acceptanceCategory('asset');
+ $second = $this->acceptanceCategory('asset');
+
+ $this->artisan('snipeit:regenerate-acceptances', [
+ '--no-interaction' => true,
+ '--category' => [$first->id.','.$second->id.',999999'],
+ ])
+ ->expectsOutput('No category exists with id 999999.')
+ ->assertExitCode(1);
+ }
+
/**
* Each builder names its own category path — `model.category` for assets,
* `license.category` for seats, `category` for the other three — and its own