Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/UserTest.php
<?php | <?php | ||||
namespace Tests\Feature; | namespace Tests\Feature; | ||||
use App\Domain; | |||||
use App\User; | use App\User; | ||||
use Illuminate\Support\Facades\Queue; | use Illuminate\Support\Facades\Queue; | ||||
use Tests\TestCase; | use Tests\TestCase; | ||||
class UserTest extends TestCase | class UserTest extends TestCase | ||||
{ | { | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
$this->deleteTestUser('user-create-test@' . \config('app.domain')); | $this->deleteTestUser('user-create-test@' . \config('app.domain')); | ||||
$this->deleteTestUser('userdeletejob@kolabnow.com'); | |||||
$this->deleteTestUser('UserAccountA@UserAccount.com'); | $this->deleteTestUser('UserAccountA@UserAccount.com'); | ||||
$this->deleteTestUser('UserAccountB@UserAccount.com'); | $this->deleteTestUser('UserAccountB@UserAccount.com'); | ||||
$this->deleteTestUser('userdeletejob@kolabnow.com'); | $this->deleteTestUser('UserAccountC@UserAccount.com'); | ||||
$this->deleteTestDomain('UserAccount.com'); | |||||
} | } | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestUser('user-create-test@' . \config('app.domain')); | $this->deleteTestUser('user-create-test@' . \config('app.domain')); | ||||
$this->deleteTestUser('userdeletejob@kolabnow.com'); | |||||
$this->deleteTestUser('UserAccountA@UserAccount.com'); | $this->deleteTestUser('UserAccountA@UserAccount.com'); | ||||
$this->deleteTestUser('UserAccountB@UserAccount.com'); | $this->deleteTestUser('UserAccountB@UserAccount.com'); | ||||
$this->deleteTestUser('userdeletejob@kolabnow.com'); | $this->deleteTestUser('UserAccountC@UserAccount.com'); | ||||
$this->deleteTestDomain('UserAccount.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 75 Lines • ▼ Show 20 Lines | */ | ||||
{ | { | ||||
$john = $this->getTestUser('john@kolab.org'); | $john = $this->getTestUser('john@kolab.org'); | ||||
$this->assertSame($john->id, $john->controller()->id); | $this->assertSame($john->id, $john->controller()->id); | ||||
$jack = $this->getTestUser('jack@kolab.org'); | $jack = $this->getTestUser('jack@kolab.org'); | ||||
$this->assertSame($john->id, $jack->controller()->id); | $this->assertSame($john->id, $jack->controller()->id); | ||||
// TODO: More sophisticated cases regarding wallets | |||||
} | } | ||||
/** | /** | ||||
* Tests for User::domains() | * Tests for User::domains() | ||||
*/ | */ | ||||
public function testDomains(): void | public function testDomains(): void | ||||
{ | { | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
Show All 16 Lines | public function testDomains(): void | ||||
} | } | ||||
$this->assertContains(\config('app.domain'), $domains); | $this->assertContains(\config('app.domain'), $domains); | ||||
$this->assertNotContains('kolab.org', $domains); | $this->assertNotContains('kolab.org', $domains); | ||||
} | } | ||||
public function testUserQuota(): void | public function testUserQuota(): void | ||||
{ | { | ||||
// TODO: This test does not test much, probably could be removed | |||||
// or moved to somewhere else, or extended with | |||||
// other entitlements() related cases. | |||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
$storage_sku = \App\Sku::where('title', 'storage')->first(); | $storage_sku = \App\Sku::where('title', 'storage')->first(); | ||||
$count = 0; | $count = 0; | ||||
foreach ($user->entitlements()->get() as $entitlement) { | foreach ($user->entitlements()->get() as $entitlement) { | ||||
if ($entitlement->sku_id == $storage_sku->id) { | if ($entitlement->sku_id == $storage_sku->id) { | ||||
$count += 1; | $count += 1; | ||||
} | } | ||||
} | } | ||||
$this->assertTrue($count == 2); | $this->assertTrue($count == 2); | ||||
} | } | ||||
/** | /** | ||||
* Test user deletion | * Test user deletion | ||||
*/ | */ | ||||
public function testUserDelete(): void | public function testDelete(): void | ||||
{ | { | ||||
$user = $this->getTestUser('userdeletejob@kolabnow.com'); | Queue::fake(); | ||||
$user = $this->getTestUser('userdeletejob@kolabnow.com'); | |||||
$package = \App\Package::where('title', 'kolab')->first(); | $package = \App\Package::where('title', 'kolab')->first(); | ||||
$user->assignPackage($package); | $user->assignPackage($package); | ||||
$id = $user->id; | $id = $user->id; | ||||
$entitlements = \App\Entitlement::where('owner_id', $id)->get(); | |||||
$this->assertCount(4, $entitlements); | |||||
$user->delete(); | $user->delete(); | ||||
$entitlements = \App\Entitlement::where('owner_id', $id)->get(); | |||||
$this->assertCount(0, $entitlements); | |||||
$this->assertTrue($user->fresh()->trashed()); | |||||
$this->assertFalse($user->fresh()->isDeleted()); | |||||
// Delete the user for real | |||||
$job = new \App\Jobs\UserDelete($id); | $job = new \App\Jobs\UserDelete($id); | ||||
$job->handle(); | $job->handle(); | ||||
$this->assertTrue(User::withTrashed()->where('id', $id)->first()->isDeleted()); | |||||
$user->forceDelete(); | $user->forceDelete(); | ||||
$entitlements = \App\Entitlement::where('owner_id', 'id')->get(); | $this->assertCount(0, User::withTrashed()->where('id', $id)->get()); | ||||
$this->assertCount(0, $entitlements); | // Test an account with users | ||||
$userA = $this->getTestUser('UserAccountA@UserAccount.com'); | |||||
$userB = $this->getTestUser('UserAccountB@UserAccount.com'); | |||||
$userC = $this->getTestUser('UserAccountC@UserAccount.com'); | |||||
$package_kolab = \App\Package::where('title', 'kolab')->first(); | |||||
$package_domain = \App\Package::where('title', 'domain-hosting')->first(); | |||||
$domain = $this->getTestDomain('UserAccount.com', [ | |||||
'status' => Domain::STATUS_NEW, | |||||
'type' => Domain::TYPE_HOSTED, | |||||
]); | |||||
$userA->assignPackage($package_kolab); | |||||
$domain->assignPackage($package_domain, $userA); | |||||
$userA->assignPackage($package_kolab, $userB); | |||||
$userA->assignPackage($package_kolab, $userC); | |||||
$entitlementsA = \App\Entitlement::where('entitleable_id', $userA->id); | |||||
$entitlementsB = \App\Entitlement::where('entitleable_id', $userB->id); | |||||
$entitlementsC = \App\Entitlement::where('entitleable_id', $userC->id); | |||||
$entitlementsDomain = \App\Entitlement::where('entitleable_id', $domain->id); | |||||
$this->assertSame(4, $entitlementsA->count()); | |||||
$this->assertSame(4, $entitlementsB->count()); | |||||
$this->assertSame(4, $entitlementsC->count()); | |||||
$this->assertSame(1, $entitlementsDomain->count()); | |||||
// Delete non-controller user | |||||
$userC->delete(); | |||||
$this->assertTrue($userC->fresh()->trashed()); | |||||
$this->assertFalse($userC->fresh()->isDeleted()); | |||||
$this->assertSame(0, $entitlementsC->count()); | |||||
// Delete the controller (and expect "sub"-users to be deleted too) | |||||
$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($domain->fresh()->trashed()); | |||||
$this->assertFalse($userA->isDeleted()); | |||||
$this->assertFalse($userB->isDeleted()); | |||||
$this->assertFalse($domain->isDeleted()); | |||||
} | } | ||||
/** | /** | ||||
* Tests for User::findByEmail() | * Tests for User::findByEmail() | ||||
*/ | */ | ||||
public function testFindByEmail(): void | public function testFindByEmail(): void | ||||
{ | { | ||||
$user = $this->getTestUser('john@kolab.org'); | $user = $this->getTestUser('john@kolab.org'); | ||||
▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | */ | ||||
/** | /** | ||||
* Tests for UserSettingsTrait::setSettings() | * Tests for UserSettingsTrait::setSettings() | ||||
*/ | */ | ||||
public function testSetSettings(): void | public function testSetSettings(): void | ||||
{ | { | ||||
$this->markTestIncomplete(); | $this->markTestIncomplete(); | ||||
} | } | ||||
/** | |||||
* Tests for User::users() | |||||
*/ | |||||
public function testUsers(): void | |||||
{ | |||||
$john = $this->getTestUser('john@kolab.org'); | |||||
$jack = $this->getTestUser('jack@kolab.org'); | |||||
$wallet = $john->wallets()->first(); | |||||
$users = $john->users()->orderBy('email')->get(); | |||||
$this->assertCount(2, $users); | |||||
$this->assertEquals($jack->id, $users[0]->id); | |||||
$this->assertEquals($john->id, $users[1]->id); | |||||
$this->assertSame($wallet->id, $users[0]->wallet_id); | |||||
$this->assertSame($wallet->id, $users[1]->wallet_id); | |||||
$users = $jack->users()->orderBy('email')->get(); | |||||
$this->assertCount(0, $users); | |||||
} | |||||
} | } |