Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/UserTest.php
Show All 14 Lines | public function setUp(): void | ||||
parent::setUp(); | parent::setUp(); | ||||
$this->deleteTestUser('user-test@' . \config('app.domain')); | $this->deleteTestUser('user-test@' . \config('app.domain')); | ||||
$this->deleteTestUser('UserAccountA@UserAccount.com'); | $this->deleteTestUser('UserAccountA@UserAccount.com'); | ||||
$this->deleteTestUser('UserAccountB@UserAccount.com'); | $this->deleteTestUser('UserAccountB@UserAccount.com'); | ||||
$this->deleteTestUser('UserAccountC@UserAccount.com'); | $this->deleteTestUser('UserAccountC@UserAccount.com'); | ||||
$this->deleteTestGroup('test-group@UserAccount.com'); | $this->deleteTestGroup('test-group@UserAccount.com'); | ||||
$this->deleteTestDomain('UserAccount.com'); | $this->deleteTestDomain('UserAccount.com'); | ||||
$this->deleteTestDomain('UserAccountAdd.com'); | |||||
} | } | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestUser('user-test@' . \config('app.domain')); | $this->deleteTestUser('user-test@' . \config('app.domain')); | ||||
$this->deleteTestUser('UserAccountA@UserAccount.com'); | $this->deleteTestUser('UserAccountA@UserAccount.com'); | ||||
$this->deleteTestUser('UserAccountB@UserAccount.com'); | $this->deleteTestUser('UserAccountB@UserAccount.com'); | ||||
$this->deleteTestUser('UserAccountC@UserAccount.com'); | $this->deleteTestUser('UserAccountC@UserAccount.com'); | ||||
$this->deleteTestGroup('test-group@UserAccount.com'); | $this->deleteTestGroup('test-group@UserAccount.com'); | ||||
$this->deleteTestDomain('UserAccount.com'); | $this->deleteTestDomain('UserAccount.com'); | ||||
$this->deleteTestDomain('UserAccountAdd.com'); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | /** | ||||
* Tests for User::assignPackage() | * Tests for User::assignPackage() | ||||
*/ | */ | ||||
public function testAssignPackage(): void | public function testAssignPackage(): void | ||||
▲ Show 20 Lines • Show All 259 Lines • ▼ Show 20 Lines | public function testDelete(): void | ||||
$this->assertCount(0, User::withTrashed()->where('id', $userC->id)->get()); | $this->assertCount(0, User::withTrashed()->where('id', $userC->id)->get()); | ||||
$this->assertCount(0, Domain::withTrashed()->where('id', $domain->id)->get()); | $this->assertCount(0, Domain::withTrashed()->where('id', $domain->id)->get()); | ||||
$this->assertCount(0, Group::withTrashed()->where('id', $group->id)->get()); | $this->assertCount(0, Group::withTrashed()->where('id', $group->id)->get()); | ||||
} | } | ||||
/** | /** | ||||
* Test user deletion vs. group membership | * Test user deletion vs. group membership | ||||
*/ | */ | ||||
public function testDeleteAandGroups(): void | public function testDeleteAndGroups(): void | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
$package_kolab = \App\Package::where('title', 'kolab')->first(); | $package_kolab = \App\Package::where('title', 'kolab')->first(); | ||||
$userA = $this->getTestUser('UserAccountA@UserAccount.com'); | $userA = $this->getTestUser('UserAccountA@UserAccount.com'); | ||||
$userB = $this->getTestUser('UserAccountB@UserAccount.com'); | $userB = $this->getTestUser('UserAccountB@UserAccount.com'); | ||||
$userA->assignPackage($package_kolab, $userB); | $userA->assignPackage($package_kolab, $userB); | ||||
$group = $this->getTestGroup('test-group@UserAccount.com'); | $group = $this->getTestGroup('test-group@UserAccount.com'); | ||||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | public function testName(): void | ||||
$user->setSetting('last_name', 'Last'); | $user->setSetting('last_name', 'Last'); | ||||
$this->assertSame('First Last', $user->name()); | $this->assertSame('First Last', $user->name()); | ||||
$this->assertSame('First Last', $user->name(true)); | $this->assertSame('First Last', $user->name(true)); | ||||
} | } | ||||
/** | /** | ||||
* Test user restoring | |||||
*/ | |||||
public function testRestore(): void | |||||
{ | |||||
Queue::fake(); | |||||
// Test an account with users and domain | |||||
$userA = $this->getTestUser('UserAccountA@UserAccount.com', [ | |||||
'status' => User::STATUS_LDAP_READY | User::STATUS_IMAP_READY | User::STATUS_SUSPENDED, | |||||
]); | |||||
$userB = $this->getTestUser('UserAccountB@UserAccount.com'); | |||||
$package_kolab = \App\Package::where('title', 'kolab')->first(); | |||||
$package_domain = \App\Package::where('title', 'domain-hosting')->first(); | |||||
$domainA = $this->getTestDomain('UserAccount.com', [ | |||||
'status' => Domain::STATUS_NEW, | |||||
'type' => Domain::TYPE_HOSTED, | |||||
]); | |||||
$domainB = $this->getTestDomain('UserAccountAdd.com', [ | |||||
'status' => Domain::STATUS_NEW, | |||||
'type' => Domain::TYPE_HOSTED, | |||||
]); | |||||
$userA->assignPackage($package_kolab); | |||||
$domainA->assignPackage($package_domain, $userA); | |||||
$domainB->assignPackage($package_domain, $userA); | |||||
$userA->assignPackage($package_kolab, $userB); | |||||
$storage_sku = \App\Sku::where('title', 'storage')->first(); | |||||
$now = \Carbon\Carbon::now(); | |||||
$wallet_id = $userA->wallets->first()->id; | |||||
// add an extra storage entitlement | |||||
$ent1 = \App\Entitlement::create([ | |||||
'wallet_id' => $wallet_id, | |||||
'sku_id' => $storage_sku->id, | |||||
'cost' => 0, | |||||
'entitleable_id' => $userA->id, | |||||
'entitleable_type' => User::class, | |||||
]); | |||||
$entitlementsA = \App\Entitlement::where('entitleable_id', $userA->id); | |||||
$entitlementsB = \App\Entitlement::where('entitleable_id', $userB->id); | |||||
$entitlementsDomain = \App\Entitlement::where('entitleable_id', $domainA->id); | |||||
// First delete the user | |||||
$userA->delete(); | |||||
$this->assertSame(0, $entitlementsA->count()); | |||||
$this->assertSame(0, $entitlementsB->count()); | |||||
$this->assertSame(0, $entitlementsDomain->count()); | |||||
$this->assertTrue($userA->fresh()->trashed()); | |||||
$this->assertTrue($userB->fresh()->trashed()); | |||||
$this->assertTrue($domainA->fresh()->trashed()); | |||||
$this->assertTrue($domainB->fresh()->trashed()); | |||||
$this->assertFalse($userA->isDeleted()); | |||||
$this->assertFalse($userB->isDeleted()); | |||||
$this->assertFalse($domainA->isDeleted()); | |||||
// Backdate one storage entitlement (it's not expected to be restored) | |||||
\App\Entitlement::withTrashed()->where('id', $ent1->id) | |||||
->update(['deleted_at' => $now->copy()->subMinutes(2)]); | |||||
// Backdate entitlements to assert that they were restored with proper updated_at timestamp | |||||
\App\Entitlement::withTrashed()->where('wallet_id', $wallet_id) | |||||
->update(['updated_at' => $now->subMinutes(10)]); | |||||
Queue::fake(); | |||||
// Then restore it | |||||
$userA->restore(); | |||||
$userA->refresh(); | |||||
$this->assertFalse($userA->trashed()); | |||||
$this->assertFalse($userA->isDeleted()); | |||||
$this->assertFalse($userA->isSuspended()); | |||||
$this->assertFalse($userA->isLdapReady()); | |||||
$this->assertFalse($userA->isImapReady()); | |||||
$this->assertTrue($userA->isActive()); | |||||
$this->assertTrue($userB->fresh()->trashed()); | |||||
$this->assertTrue($domainB->fresh()->trashed()); | |||||
$this->assertFalse($domainA->fresh()->trashed()); | |||||
// Assert entitlements | |||||
$this->assertSame(4, $entitlementsA->count()); // mailbox + groupware + 2 x storage | |||||
$this->assertTrue($ent1->fresh()->trashed()); | |||||
$entitlementsA->get()->each(function ($ent) { | |||||
$this->assertTrue($ent->updated_at->greaterThan(\Carbon\Carbon::now()->subSeconds(5))); | |||||
}); | |||||
// We expect only CreateJob + UpdateJob pair for both user and domain. | |||||
// Because how Illuminate/Database/Eloquent/SoftDeletes::restore() method | |||||
// is implemented we cannot skip the UpdateJob in any way. | |||||
// I don't want to overwrite this method, the extra job shouldn't do any harm. | |||||
$this->assertCount(4, Queue::pushedJobs()); // @phpstan-ignore-line | |||||
Queue::assertPushed(\App\Jobs\Domain\UpdateJob::class, 1); | |||||
Queue::assertPushed(\App\Jobs\Domain\CreateJob::class, 1); | |||||
Queue::assertPushed(\App\Jobs\User\UpdateJob::class, 1); | |||||
Queue::assertPushed(\App\Jobs\User\CreateJob::class, 1); | |||||
Queue::assertPushed( | |||||
\App\Jobs\User\CreateJob::class, | |||||
function ($job) use ($userA) { | |||||
return $userA->id === TestCase::getObjectProperty($job, 'userId'); | |||||
} | |||||
); | |||||
Queue::assertPushedWithChain( | |||||
\App\Jobs\User\CreateJob::class, | |||||
[ | |||||
\App\Jobs\User\VerifyJob::class, | |||||
] | |||||
); | |||||
} | |||||
/** | |||||
* Tests for UserAliasesTrait::setAliases() | * Tests for UserAliasesTrait::setAliases() | ||||
*/ | */ | ||||
public function testSetAliases(): void | public function testSetAliases(): void | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
Queue::assertNothingPushed(); | Queue::assertNothingPushed(); | ||||
$user = $this->getTestUser('UserAccountA@UserAccount.com'); | $user = $this->getTestUser('UserAccountA@UserAccount.com'); | ||||
▲ Show 20 Lines • Show All 165 Lines • Show Last 20 Lines |