Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/EntitlementTest.php
Show First 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | public function testEntitlements(): void | ||||
'custom-domain.com', | 'custom-domain.com', | ||||
[ | [ | ||||
'status' => Domain::STATUS_NEW, | 'status' => Domain::STATUS_NEW, | ||||
'type' => Domain::TYPE_EXTERNAL, | 'type' => Domain::TYPE_EXTERNAL, | ||||
] | ] | ||||
); | ); | ||||
$domain->assignPackage($packageDomain, $owner); | $domain->assignPackage($packageDomain, $owner); | ||||
$owner->assignPackage($packageKolab); | $owner->assignPackage($packageKolab); | ||||
$owner->assignPackage($packageKolab, $user); | $owner->assignPackage($packageKolab, $user); | ||||
$wallet = $owner->wallets->first(); | $wallet = $owner->wallets->first(); | ||||
$this->assertCount(4, $owner->entitlements()->get()); | $this->assertCount(4, $owner->entitlements()->get()); | ||||
$this->assertCount(1, $skuDomain->entitlements()->where('wallet_id', $wallet->id)->get()); | $this->assertCount(1, $skuDomain->entitlements()->where('wallet_id', $wallet->id)->get()); | ||||
$this->assertCount(2, $skuMailbox->entitlements()->where('wallet_id', $wallet->id)->get()); | $this->assertCount(2, $skuMailbox->entitlements()->where('wallet_id', $wallet->id)->get()); | ||||
Show All 29 Lines | public function testEntitlementFunctions(): void | ||||
->where('sku_id', $sku->id)->first(); | ->where('sku_id', $sku->id)->first(); | ||||
$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::where('title', 'kolab')->first(); | |||||
$storage = \App\Sku::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 2 original storage and the additional 4 | |||||
$this->assertCount(8, $user->fresh()->entitlements); | |||||
$wallet = $user->wallets()->first(); | |||||
$backdate = Carbon::now()->subWeeks(7); | |||||
$this->backdateEntitlements($user->entitlements, $backdate); | |||||
$charge = $wallet->chargeEntitlements(); | |||||
$this->assertSame(-1099, $wallet->balance); | |||||
$balance = $wallet->balance; | |||||
$discount = \App\Discount::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); | |||||
} | |||||
} | } |