A free open source IT asset/license management system
Use wizard
Marcus Moore88a335ae70b7
diff --git a/app/Console/Commands/RegenerateAcceptances.php b/app/Console/Commands/RegenerateAcceptances.phpindex d620904444..32d67e3096 100644--- a/app/Console/Commands/RegenerateAcceptances.php+++ b/app/Console/Commands/RegenerateAcceptances.php@@ -4,8 +4,13 @@ namespace App\Console\Commands;use App\Actions\Acceptances\RegenerateAcceptancesAction;use App\Actions\Acceptances\RegenerateAcceptancesResult;+use App\Models\Category;+use App\Models\Company;use Illuminate\Console\Command;+use function Laravel\Prompts\confirm;+use function Laravel\Prompts\multiselect;+class RegenerateAcceptances extends Command{protected $signature = 'snipeit:regenerate-acceptances@@ -17,21 +22,166 @@ class RegenerateAcceptances extends Commandprotected $description = 'Re-request EULA acceptance from users who currently hold items';+ /**+ * Category ids, empty when the operator did not narrow the run.+ *+ * @var array<int, int|string>+ */+ private array $categoryIds = [];++ /**+ * Company ids, empty when the operator did not narrow the run.+ *+ * @var array<int, int|string>+ */+ private array $companyIds = [];++ private bool $excludeDeclined = false;++ private bool $dryRun = false;++ private bool $notify = false;+public function handle(): int{- $result = RegenerateAcceptancesAction::run(- categoryIds: $this->option('category'),- companyIds: $this->option('company'),- excludeDeclined: (bool) $this->option('exclude-declined'),- dryRun: (bool) $this->option('dry-run'),- notify: (bool) $this->option('notify'),+ $this->categoryIds = $this->option('category');+ $this->companyIds = $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->input->isInteractive() && ! $this->runWizard()) {+ return 0;+ }++ return $this->printReport($this->regenerate(dryRun: $this->dryRun));+ }++ /**+ * One pass of the Action with whatever options are settled by now.+ */+ private function regenerate(bool $dryRun): RegenerateAcceptancesResult+ {+ return RegenerateAcceptancesAction::run(+ categoryIds: $this->categoryIds,+ companyIds: $this->companyIds,+ excludeDeclined: $this->excludeDeclined,+ dryRun: $dryRun,+ notify: $this->notify,);+ }++ /**+ * Walks the operator through the options no flag already answered, previews what+ * the run would do, and asks whether to go ahead.+ *+ * Every step is walked in order, and one whose flag was passed announces itself and+ * moves on, so the operator sees the same sequence whatever they typed. The preview+ * is a real dry run, so confirming costs a second pass over the same items — the+ * alternative, holding every send pair in memory until the operator answers, would+ * undo the chunking that bounds the Action on a large install.+ *+ * The gate on this is the caller's `isInteractive()` check rather than anything+ * Prompts does on its own: `ConfiguresPrompts` forces Prompts interactive whenever+ * the app is running unit tests, so a prompt reached under test hits a mocked+ * question helper and throws rather than taking its default. Nothing here may run+ * before that check.+ *+ * @return bool whether to go on and create the rows+ */+ private function runWizard(): bool+ {+ $this->askForCategories();+ $this->askForCompanies();+ $this->askWhetherToExcludeDeclined();+ $this->askWhetherToNotify();++ $this->printReport($this->regenerate(dryRun: true));++ if ($this->dryRun) {+ return false;+ }++ if (! confirm(label: 'Create these acceptance requests?', default: false)) {+ $this->info('Nothing was created.');++ return false;+ }++ return true;+ }++ private function askForCategories(): void+ {+ if ($this->categoryIds !== []) {+ $this->line('--category passed — skipping');++ return;+ }++ $categories = Category::requiresAcceptance()->orderBy('name')->pluck('name', 'id');++ if ($categories->isEmpty()) {+ return;+ }++ $this->categoryIds = multiselect(+ label: 'Which categories should this run cover?',+ options: $categories->all(),+ hint: 'Select none to cover every category requiring acceptance.',+ );+ }++ private function askForCompanies(): void+ {+ if ($this->companyIds !== []) {+ $this->line('--company passed — skipping');++ return;+ }++ $companies = Company::orderBy('name')->pluck('name', 'id');++ if ($companies->isEmpty()) {+ return;+ }++ $this->companyIds = multiselect(+ label: 'Which companies should this run cover?',+ options: $companies->all(),+ hint: 'Select none to cover every company.',+ );+ }++ private function askWhetherToExcludeDeclined(): void+ {+ if ($this->option('exclude-declined')) {+ $this->line('--exclude-declined passed — skipping');++ return;+ }++ $this->excludeDeclined = confirm(+ label: 'Skip holders whose latest response was a decline?',+ default: false,+ yes: 'Yes — leave decliners alone',+ no: 'No — re-ask them too',+ );+ }++ private function askWhetherToNotify(): void+ {+ if ($this->option('notify')) {+ $this->line('--notify passed — skipping');++ return;+ }- return $this->printReport($result);+ $this->notify = confirm(label: 'Email each affected holder?', default: false);}/**- * Prints what the run re-requested, or under --dry-run what it would have.+ * Prints what the run re-requested, or under a dry run what it would have.*/private function printReport(RegenerateAcceptancesResult $result): int{diff --git a/tests/Feature/Console/RegenerateAcceptancesTest.php b/tests/Feature/Console/RegenerateAcceptancesTest.phpindex cf32c3ecc8..9f19bd8b04 100644--- a/tests/Feature/Console/RegenerateAcceptancesTest.php+++ b/tests/Feature/Console/RegenerateAcceptancesTest.php@@ -18,6 +18,7 @@ use App\Models\User;use Database\Factories\CheckoutAcceptanceFactory;use Illuminate\Database\Eloquent\Model;use Illuminate\Support\Facades\Mail;+use Illuminate\Testing\PendingCommand;use Tests\TestCase;class RegenerateAcceptancesTest extends TestCase@@ -855,6 +856,198 @@ class RegenerateAcceptancesTest extends TestCase->assertExitCode(0);}+ /**+ * The wizard's prompts, pre-answered in the order `runWizard()` walks them.+ *+ * `multiselect()` falls back to a single `choice()` call under test, so each picker+ * is one `expectsQuestion` taking an array of ids. The two pickers are only asked+ * when there is something to pick, so a fixture with no company never sees the+ * company step.+ *+ * @param array<int, int> $categoryIds+ * @param array<int, int> $companyIds+ */+ private function runWizard(+ array $categoryIds = [],+ array $companyIds = [],+ bool $excludeDeclined = false,+ bool $notify = false,+ bool $confirm = true,+ ): PendingCommand {+ return $this->artisan('snipeit:regenerate-acceptances')+ ->expectsQuestion('Which categories should this run cover?', $categoryIds)+ ->expectsQuestion('Which companies should this run cover?', $companyIds)+ ->expectsConfirmation('Skip holders whose latest response was a decline?', $excludeDeclined ? 'yes' : 'no')+ ->expectsConfirmation('Email each affected holder?', $notify ? 'yes' : 'no')+ ->expectsConfirmation('Create these acceptance requests?', $confirm ? 'yes' : 'no');+ }++ public function test_wizard_creates_the_rows_it_previewed_when_confirmed(): void+ {+ $company = Company::factory()->create();+ $holder = User::factory()->create();+ $asset = $this->heldAsset($holder, $this->acceptanceCategory('asset'), $company, 'Wizard laptop');++ $this->runWizard()+ ->expectsOutput('Created: 1.')+ ->assertExitCode(0);++ $this->assertSame(+ ['Asset #'.$asset->id.' -> user #'.$holder->id.' qty null'],+ $this->acceptanceRows(),+ );+ }++ public function test_wizard_creates_nothing_when_the_operator_aborts(): void+ {+ $company = Company::factory()->create();+ $holder = User::factory()->create();+ $this->heldAsset($holder, $this->acceptanceCategory('asset'), $company, 'Wizard laptop');++ $this->runWizard(confirm: false)+ ->expectsOutput('Nothing was created.')+ ->doesntExpectOutput('Created: 1.')+ ->assertExitCode(0);++ $this->assertSame([], $this->acceptanceRows());+ }++ public function test_wizard_sends_nothing_when_the_operator_aborts(): void+ {+ Mail::fake();+ $company = Company::factory()->create();+ $holder = User::factory()->create(['email' => '[email protected]']);+ $this->heldAsset($holder, $this->acceptanceCategory('asset'), $company, 'Wizard laptop');++ $this->runWizard(notify: true, confirm: false)->assertExitCode(0);++ Mail::assertNothingSent();+ }++ /**+ * The preview is a dry run, so a confirmed wizard run scans twice.+ *+ * "Nothing was created." is the dry pass's footer and "Created: 1." the real one;+ * seeing both in a run the operator confirmed is what proves the preview happened+ * before the rows did.+ */+ public function test_wizard_previews_as_a_dry_run_then_creates_for_real(): void+ {+ $company = Company::factory()->create();+ $holder = User::factory()->create();+ $this->heldAsset($holder, $this->acceptanceCategory('asset'), $company, 'Wizard laptop');++ $this->runWizard()+ ->expectsOutput('Nothing was created.')+ ->expectsOutput('Created: 1.')+ ->assertExitCode(0);++ $this->assertCount(1, $this->acceptanceRows());+ }++ public function test_wizard_answers_carry_into_the_run(): void+ {+ Mail::fake();+ $company = Company::factory()->create();+ $holder = User::factory()->create(['email' => '[email protected]']);+ $this->heldAsset($holder, $this->acceptanceCategory('asset'), $company, 'Wizard laptop');++ $this->runWizard(notify: true)+ ->expectsOutput('Notified: 1.')+ ->assertExitCode(0);++ Mail::assertSent(AcceptanceReRequestMail::class, 1);+ }++ public function test_wizard_excludes_decliners_when_the_operator_says_so(): void+ {+ $company = Company::factory()->create();+ $holder = User::factory()->create();+ $asset = $this->heldAsset($holder, $this->acceptanceCategory('asset'), $company, 'Wizard laptop');+ $this->acceptanceFor($asset, $holder)->declined()->create();++ $this->runWizard(excludeDeclined: true)+ ->expectsOutput('Previously declined and excluded: 1.')+ ->assertExitCode(0);++ $this->assertSame(+ ['Asset #'.$asset->id.' -> user #'.$holder->id.' qty null declined'],+ $this->acceptanceRows(),+ );+ }++ /**+ * Decision 16's announce-and-skip: a flag answers its step, and the step is not asked.+ *+ * Every prompt the wizard could reach is left unanswered here except the final+ * confirm. If a skipped step were actually asked, it would reach the mocked question+ * helper and throw rather than silently taking a default.+ */+ public function test_wizard_announces_and_skips_every_step_a_flag_answered(): void+ {+ Mail::fake();+ $company = Company::factory()->create();+ $category = $this->acceptanceCategory('asset');+ $holder = User::factory()->create(['email' => '[email protected]']);+ $this->heldAsset($holder, $category, $company, 'Wizard laptop');++ $this->artisan('snipeit:regenerate-acceptances', [+ '--category' => [$category->id],+ '--company' => [$company->id],+ '--exclude-declined' => true,+ '--notify' => true,+ ])+ ->expectsConfirmation('Create these acceptance requests?', 'yes')+ ->expectsOutput('--category passed — skipping')+ ->expectsOutput('--company passed — skipping')+ ->expectsOutput('--exclude-declined passed — skipping')+ ->expectsOutput('--notify passed — skipping')+ ->expectsOutput('Notified: 1.')+ ->assertExitCode(0);+ }++ public function test_interactive_dry_run_stops_at_the_preview_without_confirming(): void+ {+ $company = Company::factory()->create();+ $holder = User::factory()->create();+ $this->heldAsset($holder, $this->acceptanceCategory('asset'), $company, 'Wizard laptop');++ $this->artisan('snipeit:regenerate-acceptances', ['--dry-run' => true])+ ->expectsQuestion('Which categories should this run cover?', [])+ ->expectsQuestion('Which companies should this run cover?', [])+ ->expectsConfirmation('Skip holders whose latest response was a decline?', 'no')+ ->expectsConfirmation('Email each affected holder?', 'no')+ ->expectsOutput('Nothing was created.')+ ->assertExitCode(0);++ $this->assertSame([], $this->acceptanceRows());+ }++ /**+ * Decision C parity: a --no-interaction run is slice 3's run, unchanged.+ *+ * The wizard never starts, so no prompt is pre-answered here; reaching one would+ * throw. The flag defaults are the ones that applied before the wizard existed.+ */+ public function test_no_interaction_run_behaves_exactly_as_it_did_before_the_wizard(): void+ {+ Mail::fake();+ $company = Company::factory()->create();+ $holder = User::factory()->create(['email' => '[email protected]']);+ $asset = $this->heldAsset($holder, $this->acceptanceCategory('asset'), $company, 'Wizard laptop');++ $this->artisan('snipeit:regenerate-acceptances', ['--no-interaction' => true])+ ->expectsOutput('Created: 1.')+ ->doesntExpectOutput('Notified: 1.')+ ->assertExitCode(0);++ $this->assertSame(+ ['Asset #'.$asset->id.' -> user #'.$holder->id.' qty null'],+ $this->acceptanceRows(),+ );+ Mail::assertNothingSent();+ }+/*** One realistic install, run for real.*