A free open source IT asset/license management system
Added tests
snipe89bf60e091d7
diff --git a/tests/Feature/Licenses/Api/BulkSelectableTest.php b/tests/Feature/Licenses/Api/BulkSelectableTest.phpnew file mode 100644index 0000000000..b6160eecd6--- /dev/null+++ b/tests/Feature/Licenses/Api/BulkSelectableTest.php@@ -0,0 +1,76 @@+<?php++namespace Tests\Feature\Licenses\Api;++use App\Models\Asset;+use App\Models\License;+use App\Models\User;+use Tests\TestCase;++/**+ * Verifies the per-row eligibility flags that drive the licenses index bulk-actions+ * dropdown. `available_actions.bulk_selectable.delete` gates the plain delete action+ * (only clean licenses); `available_actions.bulk_selectable.delete_with_checkin`+ * gates the check-in-and-delete action (any license the user can delete, regardless+ * of assigned seats). The dropdown intersects these keys across all selected rows.+ */+class BulkSelectableTest extends TestCase+{+ public function test_clean_license_supports_both_delete_actions()+ {+ $license = License::factory()->create(['seats' => 5]);++ $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.licenses.show', $license))+ ->assertOk()+ ->assertJsonPath('available_actions.bulk_selectable.delete', true)+ ->assertJsonPath('available_actions.bulk_selectable.delete_with_checkin', true);+ }++ public function test_license_with_assigned_user_seat_only_supports_delete_with_checkin()+ {+ $license = License::factory()->create(['seats' => 5]);+ $license->licenseseats->first()->update(['assigned_to' => User::factory()->create()->id]);++ $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.licenses.show', $license))+ ->assertOk()+ ->assertJsonPath('available_actions.bulk_selectable.delete', false)+ ->assertJsonPath('available_actions.bulk_selectable.delete_with_checkin', true);+ }++ public function test_license_with_assigned_asset_seat_only_supports_delete_with_checkin()+ {+ $license = License::factory()->create(['seats' => 5]);+ $asset = Asset::factory()->create();+ $license->licenseseats->first()->update(['asset_id' => $asset->id]);++ $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.licenses.show', $license))+ ->assertOk()+ ->assertJsonPath('available_actions.bulk_selectable.delete', false)+ ->assertJsonPath('available_actions.bulk_selectable.delete_with_checkin', true);+ }++ public function test_user_with_delete_but_not_checkin_permission_cannot_delete_with_checkin()+ {+ $license = License::factory()->create(['seats' => 5]);++ $this->actingAsForApi(User::factory()->viewLicenses()->deleteLicenses()->create())+ ->getJson(route('api.licenses.show', $license))+ ->assertOk()+ ->assertJsonPath('available_actions.bulk_selectable.delete', true)+ ->assertJsonPath('available_actions.bulk_selectable.delete_with_checkin', false);+ }++ public function test_user_without_delete_permission_gets_false_for_both()+ {+ $license = License::factory()->create(['seats' => 5]);++ $this->actingAsForApi(User::factory()->viewLicenses()->create())+ ->getJson(route('api.licenses.show', $license))+ ->assertOk()+ ->assertJsonPath('available_actions.bulk_selectable.delete', false)+ ->assertJsonPath('available_actions.bulk_selectable.delete_with_checkin', false);+ }+}diff --git a/tests/Feature/Licenses/Ui/BulkDeleteWithCheckinLicensesTest.php b/tests/Feature/Licenses/Ui/BulkDeleteWithCheckinLicensesTest.phpnew file mode 100644index 0000000000..002085ca58--- /dev/null+++ b/tests/Feature/Licenses/Ui/BulkDeleteWithCheckinLicensesTest.php@@ -0,0 +1,175 @@+<?php++namespace Tests\Feature\Licenses\Ui;++use App\Models\Actionlog;+use App\Models\Asset;+use App\Models\Company;+use App\Models\License;+use App\Models\LicenseSeat;+use App\Models\User;+use Tests\Concerns\TestsPermissionsRequirement;+use Tests\TestCase;++class BulkDeleteWithCheckinLicensesTest extends TestCase implements TestsPermissionsRequirement+{+ public function test_requires_permission()+ {+ $this->actingAs(User::factory()->create())+ ->post(route('licenses.bulk.delete'), [+ 'bulk_actions' => 'delete_with_checkin',+ 'ids' => [1, 2, 3],+ ])+ ->assertForbidden();+ }++ public function test_delete_with_checkin_deletes_license_with_user_assigned_seat()+ {+ $license = License::factory()->create(['seats' => 3]);+ $seat = LicenseSeat::factory()->assignedToUser()->create(['license_id' => $license->id]);+ $assignedUser = $seat->user;++ $this->actingAs(User::factory()->deleteLicenses()->checkinLicenses()->create())+ ->post(route('licenses.bulk.delete'), [+ 'bulk_actions' => 'delete_with_checkin',+ 'ids' => [$license->id],+ ])+ ->assertRedirect(route('licenses.index'))+ ->assertSessionHas('success');++ $this->assertSoftDeleted($license);++ $this->assertDatabaseHas('action_logs', [+ 'action_type' => 'checkin from',+ 'target_id' => $assignedUser->id,+ 'target_type' => User::class,+ 'item_type' => License::class,+ 'item_id' => $license->id,+ ]);+ }++ public function test_delete_with_checkin_deletes_license_with_asset_assigned_seat()+ {+ $license = License::factory()->create(['seats' => 3]);+ $asset = Asset::factory()->create();+ $seat = LicenseSeat::factory()->assignedToAsset($asset)->create(['license_id' => $license->id]);++ $this->actingAs(User::factory()->deleteLicenses()->checkinLicenses()->create())+ ->post(route('licenses.bulk.delete'), [+ 'bulk_actions' => 'delete_with_checkin',+ 'ids' => [$license->id],+ ])+ ->assertRedirect(route('licenses.index'))+ ->assertSessionHas('success');++ $this->assertSoftDeleted($license);++ $this->assertDatabaseHas('action_logs', [+ 'action_type' => 'checkin from',+ 'target_id' => $asset->id,+ 'target_type' => Asset::class,+ 'item_type' => License::class,+ 'item_id' => $license->id,+ ]);+ }++ public function test_delete_with_checkin_works_across_mixed_selection()+ {+ $cleanLicense = License::factory()->create(['seats' => 3]);+ $checkedOutLicense = License::factory()->create(['seats' => 3]);+ LicenseSeat::factory()->assignedToUser()->create(['license_id' => $checkedOutLicense->id]);++ $this->actingAs(User::factory()->deleteLicenses()->checkinLicenses()->create())+ ->post(route('licenses.bulk.delete'), [+ 'bulk_actions' => 'delete_with_checkin',+ 'ids' => [$cleanLicense->id, $checkedOutLicense->id],+ ])+ ->assertRedirect(route('licenses.index'))+ ->assertSessionHas('success');++ $this->assertSoftDeleted($cleanLicense);+ $this->assertSoftDeleted($checkedOutLicense);+ }++ public function test_delete_with_checkin_processes_multiple_seats_per_license()+ {+ $license = License::factory()->create(['seats' => 4]);+ LicenseSeat::factory()->count(2)->assignedToUser()->create(['license_id' => $license->id]);+ $asset = Asset::factory()->create();+ LicenseSeat::factory()->assignedToAsset($asset)->create(['license_id' => $license->id]);++ $this->actingAs(User::factory()->deleteLicenses()->checkinLicenses()->create())+ ->post(route('licenses.bulk.delete'), [+ 'bulk_actions' => 'delete_with_checkin',+ 'ids' => [$license->id],+ ])+ ->assertRedirect(route('licenses.index'))+ ->assertSessionHas('success');++ $this->assertSoftDeleted($license);++ $this->assertEquals(+ 3,+ Actionlog::where('action_type', 'checkin from')+ ->where('item_type', License::class)+ ->where('item_id', $license->id)+ ->count()+ );+ }++ public function test_delete_defaults_to_plain_delete_when_bulk_actions_missing()+ {+ // Backwards compat: existing form POSTs with no bulk_actions key should still+ // execute the original delete-only behavior and refuse licenses with assigned seats.+ $license = License::factory()->create(['seats' => 3]);+ LicenseSeat::factory()->assignedToUser()->create(['license_id' => $license->id]);++ $this->actingAs(User::factory()->deleteLicenses()->create())+ ->post(route('licenses.bulk.delete'), [+ 'ids' => [$license->id],+ ])+ ->assertRedirect(route('licenses.index'))+ ->assertSessionMissing('success');++ $this->assertNotSoftDeleted($license);+ }++ public function test_delete_with_checkin_requires_both_delete_and_checkin_permission()+ {+ $license = License::factory()->create(['seats' => 3]);+ LicenseSeat::factory()->assignedToUser()->create(['license_id' => $license->id]);++ // User has delete but not checkin. The seat-checkin step must refuse.+ $this->actingAs(User::factory()->deleteLicenses()->create())+ ->post(route('licenses.bulk.delete'), [+ 'bulk_actions' => 'delete_with_checkin',+ 'ids' => [$license->id],+ ])+ ->assertRedirect(route('licenses.index'))+ ->assertSessionMissing('success');++ $this->assertNotSoftDeleted($license);+ }++ public function test_fmcs_prevents_delete_with_checkin_from_other_company()+ {+ [$myCompany, $otherCompany] = Company::factory()->count(2)->create();++ $actor = User::factory()->deleteLicenses()->checkinLicenses()->forCompany($myCompany->id)->create();+ $otherLicense = License::factory()->create(['company_id' => $otherCompany->id, 'seats' => 2]);+ LicenseSeat::factory()->assignedToUser()->create(['license_id' => $otherLicense->id]);++ $this->settings->enableMultipleFullCompanySupport();++ $this->actingAs($actor)+ ->post(route('licenses.bulk.delete'), [+ 'bulk_actions' => 'delete_with_checkin',+ 'ids' => [$otherLicense->id],+ ])+ ->assertRedirect(route('licenses.index'))+ ->assertSessionMissing('success');++ $this->assertModelExists($otherLicense);+ $this->assertNotSoftDeleted($otherLicense);+ }+}