-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed #15005 - Improvements on user merge #15016
Merged
Merged
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a25263f
Transfer files and acceptances on merge
snipe 2b2853a
Added acceptance model
snipe d27a025
Added factories
snipe ceaff7b
Added tests
snipe e34f3c7
Fixed typo
snipe 5488a4d
One more test
snipe 5748675
Standardize query for merging
snipe 4635e92
Added user update log factory
snipe d66d6e7
Added checkedOutToUser factory for consumables
snipe 1774952
Added additional assertions
snipe dab4ace
Renamed test
snipe 50df750
Add merge event
snipe ec24120
Allow admin to be nullable (for cli)
snipe 1553ba5
Added null coalescence for admin id in case via cli
snipe eff1980
Added console test
snipe 9211c8d
Fixed test
snipe 9f1e59c
Marcus’ nitpicks
snipe 59f6605
More eager loading
snipe 0071596
Added upload count
snipe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -481,8 +481,6 @@ public function assetlog() | |
/** | ||
* Establishes the user -> uploads relationship | ||
* | ||
* @todo I don't think we use this? | ||
* | ||
* @author A. Gianotto <[email protected]> | ||
* @since [v3.0] | ||
* @return \Illuminate\Database\Eloquent\Relations\Relation | ||
|
@@ -496,6 +494,21 @@ public function uploads() | |
->orderBy('created_at', 'desc'); | ||
} | ||
|
||
/** | ||
* Establishes the user -> acceptances relationship | ||
* | ||
* @author A. Gianotto <[email protected]> | ||
* @since [v7.0.7] | ||
* @return \Illuminate\Database\Eloquent\Relations\Relation | ||
*/ | ||
public function acceptances() | ||
{ | ||
return $this->hasMany(\App\Models\Actionlog::class, 'target_id') | ||
->where('target_type', self::class) | ||
->where('action_type', '=', 'accepted') | ||
->orderBy('created_at', 'desc'); | ||
} | ||
|
||
/** | ||
* Establishes the user -> requested assets relationship | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Users\Console; | ||
snipe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
use App\Models\Accessory; | ||
use App\Models\Asset; | ||
use App\Models\Consumable; | ||
use App\Models\LicenseSeat; | ||
use App\Models\User; | ||
use App\Models\Actionlog; | ||
use Tests\TestCase; | ||
|
||
|
||
class MergeUsersTest extends TestCase | ||
{ | ||
public function testAssetsAreTransferredOnUserMerge() | ||
{ | ||
$user1 = User::factory()->create(['username' => 'user1']); | ||
$user_to_merge_into = User::factory()->create(['username' => '[email protected]']); | ||
|
||
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()); | ||
snipe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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' => '[email protected]']); | ||
|
||
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()); | ||
snipe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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' => '[email protected]']); | ||
|
||
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()); | ||
snipe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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' => '[email protected]']); | ||
|
||
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()); | ||
snipe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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' => '[email protected]']); | ||
|
||
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()); | ||
snipe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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' => '[email protected]']); | ||
|
||
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()); | ||
snipe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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' => '[email protected]']); | ||
|
||
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()); | ||
snipe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// This needs to be more than the otherwise expected because the merge action itself is logged for the two merging users | ||
$this->assertEquals(7, $user_to_merge_into->refresh()->userlog->count()); | ||
$this->assertEquals(1, $user1->refresh()->userlog->count()); | ||
|
||
} | ||
|
||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick: this would read better in the past tense like the other methods you added. Maybe
userUpdated()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually disagree on this. Since it's in the ActionLog factory, I sorta feel like it's more explicit this way, but it's not a hill I'll die on.