A free open source IT asset/license management system
Added tests
snipeaa0b4910801b
diff --git a/tests/Feature/Importing/Api/ImportUsersMultiCompanyTest.php b/tests/Feature/Importing/Api/ImportUsersMultiCompanyTest.phpnew file mode 100644index 0000000000..7b8157ca5b--- /dev/null+++ b/tests/Feature/Importing/Api/ImportUsersMultiCompanyTest.php@@ -0,0 +1,111 @@+<?php++namespace Tests\Feature\Importing\Api;++use App\Models\Company;+use App\Models\Import;+use App\Models\User;+use Illuminate\Testing\TestResponse;+use Tests\Support\Importing\CleansUpImportFiles;+use Tests\Support\Importing\UsersImportFileBuilder as ImportFileBuilder;++class ImportUsersMultiCompanyTest extends ImportDataTestCase+{+ use CleansUpImportFiles;++ protected function importFileResponse(array $parameters = []): TestResponse+ {+ if (! array_key_exists('import-type', $parameters)) {+ $parameters['import-type'] = 'user';+ }++ return parent::importFileResponse($parameters);+ }++ public function test_pipe_separated_company_names_create_multiple_pivot_entries()+ {+ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $importFileBuilder = ImportFileBuilder::new([+ 'companyName' => $companyA->name.'|'.$companyB->name,+ ]);++ $row = $importFileBuilder->firstRow();+ $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);++ $this->actingAsForApi(User::factory()->superuser()->create());++ $this->importFileResponse(['import' => $import->id])+ ->assertOk();++ $user = User::where('username', $row['username'])->firstOrFail();++ $this->assertCount(2, $user->companies, 'User should belong to both pipe-separated companies');+ $this->assertTrue($user->companies->contains($companyA));+ $this->assertTrue($user->companies->contains($companyB));+ }++ public function test_pipe_separated_companies_create_new_companies_when_not_found()+ {+ $importFileBuilder = ImportFileBuilder::new([+ 'companyName' => 'Acme Corp|Widget Inc',+ ]);++ $row = $importFileBuilder->firstRow();+ $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);++ $this->actingAsForApi(User::factory()->superuser()->create());++ $this->importFileResponse(['import' => $import->id])+ ->assertOk();++ $user = User::where('username', $row['username'])->firstOrFail();++ $this->assertCount(2, $user->companies, 'User should belong to two newly-created companies');++ $names = $user->companies->pluck('name')->all();+ $this->assertContains('Acme Corp', $names);+ $this->assertContains('Widget Inc', $names);+ }++ public function test_single_company_name_without_pipe_works_as_before()+ {+ $company = Company::factory()->create();++ $importFileBuilder = ImportFileBuilder::new([+ 'companyName' => $company->name,+ ]);++ $row = $importFileBuilder->firstRow();+ $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);++ $this->actingAsForApi(User::factory()->superuser()->create());++ $this->importFileResponse(['import' => $import->id])+ ->assertOk();++ $user = User::where('username', $row['username'])->firstOrFail();++ $this->assertCount(1, $user->companies);+ $this->assertTrue($user->companies->contains($company));+ }++ public function test_blank_company_column_leaves_user_without_companies()+ {+ $importFileBuilder = ImportFileBuilder::new([+ 'companyName' => '',+ ]);++ $row = $importFileBuilder->firstRow();+ $import = Import::factory()->users()->create(['file_path' => $importFileBuilder->saveToImportsDirectory()]);++ $this->actingAsForApi(User::factory()->superuser()->create());++ $this->importFileResponse(['import' => $import->id])+ ->assertOk();++ $user = User::where('username', $row['username'])->firstOrFail();++ $this->assertCount(0, $user->companies, 'Blank company column should leave user with no companies');+ }+}diff --git a/tests/Feature/Users/Api/UserCompanyMembershipTest.php b/tests/Feature/Users/Api/UserCompanyMembershipTest.phpnew file mode 100644index 0000000000..fa45889af7--- /dev/null+++ b/tests/Feature/Users/Api/UserCompanyMembershipTest.php@@ -0,0 +1,156 @@+<?php++namespace Tests\Feature\Users\Api;++use App\Models\Company;+use App\Models\User;+use Tests\TestCase;++class UserCompanyMembershipTest extends TestCase+{+ public function test_store_with_company_ids_syncs_pivot()+ {+ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $actor = User::factory()->superuser()->create();++ $response = $this->actingAsForApi($actor)+ ->postJson(route('api.users.store'), [+ 'first_name' => 'Jane',+ 'last_name' => 'Doe',+ 'username' => 'janedoe_pivot_test',+ 'password' => 'secret123456',+ 'password_confirmation' => 'secret123456',+ 'company_ids' => [$companyA->id, $companyB->id],+ ])+ ->assertOk()+ ->assertStatusMessageIs('success');++ $user = User::where('username', 'janedoe_pivot_test')->firstOrFail();++ $this->assertCount(2, $user->companies, 'User should belong to two companies via pivot');+ $this->assertTrue($user->companies->contains($companyA));+ $this->assertTrue($user->companies->contains($companyB));+ }++ public function test_update_with_company_ids_syncs_pivot()+ {+ [$companyA, $companyB, $companyC] = Company::factory()->count(3)->create();++ $user = User::factory()->create(['company_id' => $companyA->id]);+ $user->companies()->sync([$companyA->id]);++ $actor = User::factory()->superuser()->create();++ $this->actingAsForApi($actor)+ ->patchJson(route('api.users.update', $user), [+ 'first_name' => $user->first_name,+ 'last_name' => $user->last_name,+ 'username' => $user->username,+ 'company_ids' => [$companyB->id, $companyC->id],+ ])+ ->assertOk()+ ->assertStatusMessageIs('success');++ $user->refresh();+ $this->assertCount(2, $user->companies, 'Pivot should be updated to two companies');+ $this->assertFalse($user->companies->contains($companyA), 'Old company should be removed');+ $this->assertTrue($user->companies->contains($companyB));+ $this->assertTrue($user->companies->contains($companyC));+ }++ public function test_api_response_includes_companies_array()+ {+ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $user = User::factory()->create(['company_id' => $companyA->id]);+ $user->companies()->sync([$companyA->id, $companyB->id]);++ $actor = User::factory()->superuser()->create();++ $response = $this->actingAsForApi($actor)+ ->getJson(route('api.users.show', $user))+ ->assertOk();++ $companies = $response->json('companies');++ $this->assertIsArray($companies);+ $this->assertCount(2, $companies, 'Response should include both companies');++ $returnedIds = collect($companies)->pluck('id')->all();+ $this->assertContains($companyA->id, $returnedIds);+ $this->assertContains($companyB->id, $returnedIds);+ }++ public function test_api_response_company_entries_include_tag_color()+ {+ $company = Company::factory()->create(['tag_color' => '#ff0000']);+ $user = User::factory()->create(['company_id' => $company->id]);+ $user->companies()->sync([$company->id]);++ $actor = User::factory()->superuser()->create();++ $response = $this->actingAsForApi($actor)+ ->getJson(route('api.users.show', $user))+ ->assertOk();++ $companies = $response->json('companies');++ $this->assertEquals('#ff0000', $companies[0]['tag_color']);+ }++ public function test_multi_company_user_can_see_users_from_all_their_companies_when_fmcs_enabled()+ {+ $this->settings->enableMultipleFullCompanySupport();++ [$companyA, $companyB, $companyC] = Company::factory()->count(3)->create();++ $userInA = User::factory()->create(['first_name' => 'Alice', 'last_name' => 'Alpha', 'company_id' => $companyA->id]);+ $companyA->users()->syncWithoutDetaching([$userInA->id]);++ $userInB = User::factory()->create(['first_name' => 'Bob', 'last_name' => 'Beta', 'company_id' => $companyB->id]);+ $companyB->users()->syncWithoutDetaching([$userInB->id]);++ $userInC = User::factory()->create(['first_name' => 'Carol', 'last_name' => 'Gamma', 'company_id' => $companyC->id]);+ $companyC->users()->syncWithoutDetaching([$userInC->id]);++ // Acting user belongs to both A and B.+ $actor = User::factory()->viewUsers()->create(['company_id' => null]);+ $actor->companies()->sync([$companyA->id, $companyB->id]);++ $response = $this->actingAsForApi($actor)+ ->getJson(route('api.users.index'))+ ->assertOk();++ $names = collect($response->json('rows'))->pluck('name');++ $this->assertTrue($names->contains(fn ($n) => str_contains($n, 'Alice')), 'Should see company A user');+ $this->assertTrue($names->contains(fn ($n) => str_contains($n, 'Bob')), 'Should see company B user');+ $this->assertFalse($names->contains(fn ($n) => str_contains($n, 'Carol')), 'Should NOT see company C user');+ }++ public function test_user_with_no_companies_sees_only_unassigned_users_when_fmcs_enabled()+ {+ $this->settings->enableMultipleFullCompanySupport();++ $company = Company::factory()->create();++ $assignedUser = User::factory()->create(['company_id' => $company->id]);+ $company->users()->syncWithoutDetaching([$assignedUser->id]);++ $unassignedUser = User::factory()->create(['company_id' => null]);++ // Actor belongs to no companies.+ $actor = User::factory()->viewUsers()->create(['company_id' => null]);++ $response = $this->actingAsForApi($actor)+ ->getJson(route('api.users.index'))+ ->assertOk();++ $ids = collect($response->json('rows'))->pluck('id');++ $this->assertFalse($ids->contains($assignedUser->id), 'Should not see user assigned to a company');+ $this->assertTrue($ids->contains($unassignedUser->id), 'Should see user with no company');+ $this->assertTrue($ids->contains($actor->id), 'Should see self');+ }+}diff --git a/tests/Feature/Users/Ui/CloneUserTest.php b/tests/Feature/Users/Ui/CloneUserTest.phpindex 2c7761a101..8534e8116f 100644--- a/tests/Feature/Users/Ui/CloneUserTest.php+++ b/tests/Feature/Users/Ui/CloneUserTest.php@@ -2,6 +2,7 @@namespace Tests\Feature\Users\Ui;+use App\Models\Company;use App\Models\User;use Tests\TestCase;@@ -13,4 +14,20 @@ class CloneUserTest extends TestCase->get(route('users.clone.show', User::factory()->create()))->assertOk();}++ public function test_clone_prepopulates_all_companies_for_multi_company_user()+ {+ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $user = User::factory()->create(['company_id' => $companyA->id]);+ $user->companies()->sync([$companyA->id, $companyB->id]);++ $response = $this->actingAs(User::factory()->superuser()->create())+ ->get(route('users.clone.show', $user))+ ->assertOk();++ // Both company IDs should be pre-selected in the form.+ $response->assertSee('value="'.$companyA->id.'"', false);+ $response->assertSee('value="'.$companyB->id.'"', false);+ }}diff --git a/tests/Feature/Users/Ui/ExportUsersTest.php b/tests/Feature/Users/Ui/ExportUsersTest.phpindex 1ebc54db15..02dabcc4fa 100644--- a/tests/Feature/Users/Ui/ExportUsersTest.php+++ b/tests/Feature/Users/Ui/ExportUsersTest.php@@ -4,6 +4,7 @@ namespace Tests\Feature\Users\Ui;use App\Models\Accessory;use App\Models\Asset;+use App\Models\Company;use App\Models\Consumable;use App\Models\Group;use App\Models\LicenseSeat;@@ -130,4 +131,24 @@ class ExportUsersTest extends TestCasetrans('general.end_date') => '2030-12-31',]);}++ public function test_multi_company_user_exports_pipe_separated_company_names()+ {+ [$companyA, $companyB] = Company::factory()->sequence(+ ['name' => 'Rebel Alliance'],+ ['name' => 'Galactic Senate'],+ )->count(2)->create();++ $user = User::factory()->create(['company_id' => $companyA->id]);+ $user->companies()->sync([$companyA->id, $companyB->id]);++ $this->actingAs(User::factory()->viewUsers()->create())+ ->get(route('users.export'))+ ->assertOk()+ ->assertCsvHeader()+ ->assertSeePairsInStreamedResponse([+ trans('admin/users/table.first_name') => $user->first_name,+ trans('admin/companies/table.title') => 'Rebel Alliance|Galactic Senate',+ ]);+ }}diff --git a/tests/Feature/Users/Ui/UserCompanyMembershipTest.php b/tests/Feature/Users/Ui/UserCompanyMembershipTest.phpnew file mode 100644index 0000000000..bb39dac157--- /dev/null+++ b/tests/Feature/Users/Ui/UserCompanyMembershipTest.php@@ -0,0 +1,88 @@+<?php++namespace Tests\Feature\Users\Ui;++use App\Models\Company;+use App\Models\User;+use Tests\TestCase;++class UserCompanyMembershipTest extends TestCase+{+ public function test_updating_user_via_ui_syncs_company_pivot()+ {+ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $user = User::factory()->create(['company_id' => $companyA->id]);+ $user->companies()->sync([$companyA->id]);++ $actor = User::factory()->superuser()->create();++ $this->actingAs($actor)+ ->put(route('users.update', $user), [+ 'first_name' => $user->first_name,+ 'last_name' => $user->last_name,+ 'username' => $user->username,+ 'activated' => $user->activated,+ 'company_ids' => [$companyA->id, $companyB->id],+ ])+ ->assertRedirect();++ $user->refresh();++ $this->assertCount(2, $user->companies, 'Pivot should hold both companies after UI update');+ $this->assertTrue($user->companies->contains($companyA));+ $this->assertTrue($user->companies->contains($companyB));+ }++ public function test_bulk_edit_assigns_companies_via_pivot()+ {+ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $users = User::factory()->count(3)->create(['company_id' => null]);++ $actor = User::factory()->superuser()->create();++ $ids = $users->pluck('id')->mapWithKeys(fn ($id) => [$id => $id])->all();++ $this->actingAs($actor)+ ->post(route('users/bulkeditsave'), array_merge(+ ['ids' => $ids],+ ['company_ids' => [$companyA->id, $companyB->id]],+ ))+ ->assertRedirect();++ foreach ($users as $user) {+ $user->refresh();+ $this->assertCount(2, $user->companies, "User {$user->id} should belong to two companies after bulk edit");+ $this->assertTrue($user->companies->contains($companyA));+ $this->assertTrue($user->companies->contains($companyB));+ }+ }++ public function test_bulk_edit_clears_company_pivot_when_null_flag_set()+ {+ $company = Company::factory()->create();++ $users = User::factory()->count(2)->create(['company_id' => $company->id]);+ foreach ($users as $user) {+ $user->companies()->sync([$company->id]);+ }++ $actor = User::factory()->superuser()->create();++ $ids = $users->pluck('id')->mapWithKeys(fn ($id) => [$id => $id])->all();++ $this->actingAs($actor)+ ->post(route('users/bulkeditsave'), [+ 'ids' => $ids,+ 'null_company_ids' => '1',+ ])+ ->assertRedirect();++ foreach ($users as $user) {+ $user->refresh();+ $this->assertCount(0, $user->companies, "User {$user->id} should have no companies after null flag");+ $this->assertNull($user->company_id);+ }+ }+}diff --git a/tests/Feature/Users/UserCompanyLoggingTest.php b/tests/Feature/Users/UserCompanyLoggingTest.phpnew file mode 100644index 0000000000..1ab8680a3f--- /dev/null+++ b/tests/Feature/Users/UserCompanyLoggingTest.php@@ -0,0 +1,151 @@+<?php++namespace Tests\Feature\Users;++use App\Models\Actionlog;+use App\Models\Company;+use App\Models\User;+use Tests\TestCase;++class UserCompanyLoggingTest extends TestCase+{+ public function test_field_and_company_changes_produce_single_log_entry()+ {+ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $user = User::factory()->create(['company_id' => $companyA->id, 'jobtitle' => 'Engineer']);+ $user->companies()->sync([$companyA->id]);++ $actor = User::factory()->superuser()->create();++ $existingLogIds = Actionlog::where('item_type', User::class)+ ->where('item_id', $user->id)+ ->pluck('id');++ $this->actingAsForApi($actor)+ ->patchJson(route('api.users.update', $user), [+ 'first_name' => $user->first_name,+ 'last_name' => $user->last_name,+ 'username' => $user->username,+ 'jobtitle' => 'Senior Engineer',+ 'company_ids' => [$companyB->id],+ ])+ ->assertOk();++ $newLogs = Actionlog::where('item_type', User::class)+ ->where('item_id', $user->id)+ ->where('action_type', 'update')+ ->whereNotIn('id', $existingLogIds)+ ->get();++ $this->assertCount(1, $newLogs, 'Field and company changes should produce exactly one log entry');++ $meta = json_decode($newLogs->first()->log_meta, true);+ $this->assertArrayHasKey('jobtitle', $meta, 'Log should include field change');+ $this->assertArrayHasKey('companies', $meta, 'Log should include company change in same entry');+ }++ public function test_company_only_change_produces_standalone_log_entry()+ {+ [$companyA, $companyB] = Company::factory()->count(2)->create();++ $user = User::factory()->create(['company_id' => $companyA->id]);+ $user->companies()->sync([$companyA->id]);++ $actor = User::factory()->superuser()->create();++ $existingLogIds = Actionlog::where('item_type', User::class)+ ->where('item_id', $user->id)+ ->pluck('id');++ // Patch with no field changes — only company_ids differs.+ $this->actingAsForApi($actor)+ ->patchJson(route('api.users.update', $user), [+ 'first_name' => $user->first_name,+ 'last_name' => $user->last_name,+ 'username' => $user->username,+ 'company_ids' => [$companyB->id],+ ])+ ->assertOk();++ $newLogs = Actionlog::where('item_type', User::class)+ ->where('item_id', $user->id)+ ->where('action_type', 'update')+ ->whereNotIn('id', $existingLogIds)+ ->get();++ $this->assertCount(1, $newLogs, 'Company-only change should produce one log entry');++ $meta = json_decode($newLogs->first()->log_meta, true);+ $this->assertArrayHasKey('companies', $meta, 'Log should record company change');+ }++ public function test_log_entry_records_old_and_new_company_ids()+ {+ [$companyA, $companyB, $companyC] = Company::factory()->count(3)->create();++ $user = User::factory()->create(['company_id' => $companyA->id]);+ $user->companies()->sync([$companyA->id, $companyB->id]);++ $actor = User::factory()->superuser()->create();++ $this->actingAsForApi($actor)+ ->patchJson(route('api.users.update', $user), [+ 'first_name' => $user->first_name,+ 'last_name' => $user->last_name,+ 'username' => $user->username,+ 'company_ids' => [$companyC->id],+ ])+ ->assertOk();++ $log = Actionlog::where('item_type', User::class)+ ->where('item_id', $user->id)+ ->where('action_type', 'update')+ ->latest('id')+ ->first();++ $meta = json_decode($log->log_meta, true);++ $this->assertEqualsCanonicalizing(+ [$companyA->id, $companyB->id],+ $meta['companies']['old'],+ 'Log old company IDs should match previous pivot'+ );+ $this->assertEqualsCanonicalizing(+ [$companyC->id],+ $meta['companies']['new'],+ 'Log new company IDs should match updated pivot'+ );+ }++ public function test_no_change_to_companies_does_not_create_extra_log_entry()+ {+ $company = Company::factory()->create();++ $user = User::factory()->create(['company_id' => $company->id]);+ $user->companies()->sync([$company->id]);++ $actor = User::factory()->superuser()->create();++ $existingLogIds = Actionlog::where('item_type', User::class)+ ->where('item_id', $user->id)+ ->pluck('id');++ // Send the same company_ids — no field changes either.+ $this->actingAsForApi($actor)+ ->patchJson(route('api.users.update', $user), [+ 'first_name' => $user->first_name,+ 'last_name' => $user->last_name,+ 'username' => $user->username,+ 'company_ids' => [$company->id],+ ])+ ->assertOk();++ $newLogs = Actionlog::where('item_type', User::class)+ ->where('item_id', $user->id)+ ->whereNotIn('id', $existingLogIds)+ ->count();++ $this->assertEquals(0, $newLogs, 'No changes should produce no new log entries');+ }+}