From eff1980df5a12389ee839c0705fe7be9ca4c66ad Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 3 Jul 2024 20:52:22 +0100 Subject: [PATCH] Added console test Signed-off-by: snipe --- tests/Feature/Console/MergeUsersTest.php | 143 +++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 tests/Feature/Console/MergeUsersTest.php diff --git a/tests/Feature/Console/MergeUsersTest.php b/tests/Feature/Console/MergeUsersTest.php new file mode 100644 index 0000000000..1171dc22a3 --- /dev/null +++ b/tests/Feature/Console/MergeUsersTest.php @@ -0,0 +1,143 @@ +create(['username' => 'user1']); + $user_to_merge_into = User::factory()->create(['username' => 'user1@example.com']); + + Asset::factory()->count(3)->assignedToUser($user1)->create(); + Asset::factory()->count(3)->assignedToUser($user_to_merge_into)->create(); + + $this->artisan('snipeit:merge-users')->assertExitCode(0); + + $this->assertNotEquals(3, $user_to_merge_into->refresh()->assets->count()); + $this->assertEquals(6, $user_to_merge_into->refresh()->assets->count()); + $this->assertEquals(0, $user1->refresh()->assets->count()); + + } + + public function testLicensesAreTransferredOnUserMerge(): void + { + $user1 = User::factory()->create(['username' => 'user1']); + $user_to_merge_into = User::factory()->create(['username' => 'user1@example.com']); + + LicenseSeat::factory()->count(3)->create(['assigned_to' => $user1->id]); + LicenseSeat::factory()->count(3)->create(['assigned_to' => $user_to_merge_into->id]); + + $this->assertEquals(3, $user_to_merge_into->refresh()->licenses->count()); + + $this->artisan('snipeit:merge-users')->assertExitCode(0); + + $this->assertNotEquals(3, $user_to_merge_into->refresh()->licenses->count()); + $this->assertEquals(6, $user_to_merge_into->refresh()->licenses->count()); + $this->assertEquals(0, $user1->refresh()->licenses->count()); + + } + + public function testAccessoriesTransferredOnUserMerge(): void + { + $user1 = User::factory()->create(['username' => 'user1']); + $user_to_merge_into = User::factory()->create(['username' => 'user1@example.com']); + + Accessory::factory()->count(3)->checkedOutToUser($user1)->create(); + Accessory::factory()->count(3)->checkedOutToUser($user_to_merge_into)->create(); + + $this->assertEquals(3, $user_to_merge_into->refresh()->accessories->count()); + + $this->artisan('snipeit:merge-users')->assertExitCode(0); + + $this->assertNotEquals(3, $user_to_merge_into->refresh()->accessories->count()); + $this->assertEquals(6, $user_to_merge_into->refresh()->accessories->count()); + $this->assertEquals(0, $user1->refresh()->accessories->count()); + + } + + public function testConsumablesTransferredOnUserMerge(): void + { + $user1 = User::factory()->create(['username' => 'user1']); + $user_to_merge_into = User::factory()->create(['username' => 'user1@example.com']); + + Consumable::factory()->count(3)->checkedOutToUser($user1)->create(); + Consumable::factory()->count(3)->checkedOutToUser($user_to_merge_into)->create(); + + $this->assertEquals(3, $user_to_merge_into->refresh()->consumables->count()); + + $this->artisan('snipeit:merge-users')->assertExitCode(0); + + $this->assertNotEquals(3, $user_to_merge_into->refresh()->consumables->count()); + $this->assertEquals(6, $user_to_merge_into->refresh()->consumables->count()); + $this->assertEquals(0, $user1->refresh()->consumables->count()); + + } + + public function testFilesAreTransferredOnUserMerge(): void + { + $user1 = User::factory()->create(['username' => 'user1']); + $user_to_merge_into = User::factory()->create(['username' => 'user1@example.com']); + + Actionlog::factory()->count(3)->filesUploaded()->create(['item_id' => $user1->id]); + Actionlog::factory()->count(3)->filesUploaded()->create(['item_id' => $user_to_merge_into->id]); + + $this->assertEquals(3, $user_to_merge_into->refresh()->uploads->count()); + + $this->artisan('snipeit:merge-users')->assertExitCode(0); + + $this->assertNotEquals(3, $user_to_merge_into->refresh()->uploads->count()); + $this->assertEquals(6, $user_to_merge_into->refresh()->uploads->count()); + $this->assertEquals(0, $user1->refresh()->uploads->count()); + + } + + public function testAcceptancesAreTransferredOnUserMerge(): void + { + $user1 = User::factory()->create(['username' => 'user1']); + $user_to_merge_into = User::factory()->create(['username' => 'user1@example.com']); + + Actionlog::factory()->count(3)->acceptedSignature()->create(['target_id' => $user1->id]); + Actionlog::factory()->count(3)->acceptedSignature()->create(['target_id' => $user_to_merge_into->id]); + + $this->assertEquals(3, $user_to_merge_into->refresh()->acceptances->count()); + + $this->artisan('snipeit:merge-users')->assertExitCode(0); + + $this->assertNotEquals(3, $user_to_merge_into->refresh()->acceptances->count()); + $this->assertEquals(6, $user_to_merge_into->refresh()->acceptances->count()); + $this->assertEquals(0, $user1->refresh()->acceptances->count()); + + } + + public function testUserUpdateHistoryIsTransferredOnUserMerge(): void + { + $user1 = User::factory()->create(['username' => 'user1']); + $user_to_merge_into = User::factory()->create(['username' => 'user1@example.com']); + + Actionlog::factory()->count(3)->logUserUpdate()->create(['target_id' => $user1->id, 'item_id' => $user1->id]); + Actionlog::factory()->count(3)->logUserUpdate()->create(['target_id' => $user_to_merge_into->id, 'item_id' => $user_to_merge_into->id]); + + $this->assertEquals(3, $user_to_merge_into->refresh()->userlog->count()); + + $this->artisan('snipeit:merge-users')->assertExitCode(0); + + $this->assertNotEquals(3, $user_to_merge_into->refresh()->userlog->count()); + + // This needs to be 2 more than the otherwise expected because the merge action itself is logged for the two merging users + $this->assertEquals(11, $user_to_merge_into->refresh()->userlog->count()); + $this->assertEquals(2, $user1->refresh()->userlog->count()); + + } + + +}