Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/EntitlementTest.php
Show First 20 Lines • Show All 125 Lines • ▼ Show 20 Lines | public function testEntitlementFunctions(): void | ||||
$this->assertNotNull($entitlement); | $this->assertNotNull($entitlement); | ||||
$this->assertSame($sku->id, $entitlement->sku->id); | $this->assertSame($sku->id, $entitlement->sku->id); | ||||
$this->assertSame($wallet->id, $entitlement->wallet->id); | $this->assertSame($wallet->id, $entitlement->wallet->id); | ||||
$this->assertEquals($user->id, $entitlement->entitleable->id); | $this->assertEquals($user->id, $entitlement->entitleable->id); | ||||
$this->assertTrue($entitlement->entitleable instanceof \App\User); | $this->assertTrue($entitlement->entitleable instanceof \App\User); | ||||
} | } | ||||
/** | /** | ||||
* @todo This really should be in User or Wallet tests file | |||||
*/ | |||||
public function testBillDeletedEntitlement(): void | |||||
{ | |||||
$user = $this->getTestUser('entitlement-test@kolabnow.com'); | |||||
$package = \App\Package::withEnvTenantContext()->where('title', 'kolab')->first(); | |||||
$storage = \App\Sku::withEnvTenantContext()->where('title', 'storage')->first(); | |||||
$user->assignPackage($package); | |||||
// some additional SKUs so we have something to delete. | |||||
$user->assignSku($storage, 4); | |||||
// the mailbox, the groupware, the 5 original storage and the additional 4 | |||||
$this->assertCount(11, $user->fresh()->entitlements); | |||||
$wallet = $user->wallets()->first(); | |||||
$backdate = Carbon::now()->subWeeks(7); | |||||
$this->backdateEntitlements($user->entitlements, $backdate); | |||||
$charge = $wallet->chargeEntitlements(); | |||||
$this->assertSame(-1090, $wallet->balance); | |||||
$balance = $wallet->balance; | |||||
$discount = \App\Discount::withEnvTenantContext()->where('discount', 30)->first(); | |||||
$wallet->discount()->associate($discount); | |||||
$wallet->save(); | |||||
$user->removeSku($storage, 4); | |||||
// we expect the wallet to have been charged for ~3 weeks of use of | |||||
// 4 deleted storage entitlements, it should also take discount into account | |||||
$backdate->addMonthsWithoutOverflow(1); | |||||
$diffInDays = $backdate->diffInDays(Carbon::now()); | |||||
// entitlements-num * cost * discount * days-in-month | |||||
$max = intval(4 * 25 * 0.7 * $diffInDays / 28); | |||||
$min = intval(4 * 25 * 0.7 * $diffInDays / 31); | |||||
$wallet->refresh(); | |||||
$this->assertTrue($wallet->balance >= $balance - $max); | |||||
$this->assertTrue($wallet->balance <= $balance - $min); | |||||
$transactions = \App\Transaction::where('object_id', $wallet->id) | |||||
->where('object_type', \App\Wallet::class)->get(); | |||||
// one round of the monthly invoicing, four sku deletions getting invoiced | |||||
$this->assertCount(5, $transactions); | |||||
// Test that deleting an entitlement on a degraded account costs nothing | |||||
$balance = $wallet->balance; | |||||
User::where('id', $user->id)->update(['status' => $user->status | User::STATUS_DEGRADED]); | |||||
$backdate = Carbon::now()->subWeeks(7); | |||||
$this->backdateEntitlements($user->entitlements()->get(), $backdate); | |||||
$groupware = \App\Sku::withEnvTenantContext()->where('title', 'groupware')->first(); | |||||
$entitlement = $wallet->entitlements()->where('sku_id', $groupware->id)->first(); | |||||
$entitlement->delete(); | |||||
$this->assertSame($wallet->refresh()->balance, $balance); | |||||
} | |||||
/** | |||||
* Test Entitlement::entitlementTitle() | * Test Entitlement::entitlementTitle() | ||||
*/ | */ | ||||
public function testEntitlementTitle(): void | public function testEntitlementTitle(): void | ||||
{ | { | ||||
$packageDomain = Package::withEnvTenantContext()->where('title', 'domain-hosting')->first(); | $packageDomain = Package::withEnvTenantContext()->where('title', 'domain-hosting')->first(); | ||||
$packageKolab = Package::withEnvTenantContext()->where('title', 'kolab')->first(); | $packageKolab = Package::withEnvTenantContext()->where('title', 'kolab')->first(); | ||||
$user = $this->getTestUser('entitled-user@custom-domain.com'); | $user = $this->getTestUser('entitled-user@custom-domain.com'); | ||||
$group = $this->getTestGroup('test-group@custom-domain.com'); | $group = $this->getTestGroup('test-group@custom-domain.com'); | ||||
Show All 34 Lines |