Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/SkuTest.php
<?php | <?php | ||||
namespace Tests\Feature; | namespace Tests\Feature; | ||||
use App\Domain; | |||||
use App\Entitlement; | use App\Entitlement; | ||||
use App\Handlers; | use App\Handlers; | ||||
use App\Package; | |||||
use App\Sku; | use App\Sku; | ||||
use App\User; | use Carbon\Carbon; | ||||
use Tests\TestCase; | use Tests\TestCase; | ||||
class SkuTest extends TestCase | class SkuTest extends TestCase | ||||
{ | { | ||||
public function setUp(): void | public function setUp(): void | ||||
{ | { | ||||
parent::setUp(); | parent::setUp(); | ||||
$this->deleteTestUser('sku-test-user@custom-domain.com'); | $this->deleteTestUser('jane@kolabnow.com'); | ||||
$this->deleteTestDomain('custom-domain.com'); | |||||
} | } | ||||
public function tearDown(): void | public function tearDown(): void | ||||
{ | { | ||||
$this->deleteTestUser('sku-test-user@custom-domain.com'); | $this->deleteTestUser('jane@kolabnow.com'); | ||||
$this->deleteTestDomain('custom-domain.com'); | |||||
parent::tearDown(); | parent::tearDown(); | ||||
} | } | ||||
/** | public function testPackageEntitlements(): void | ||||
* Tests for Sku::registerEntitlements() | { | ||||
*/ | $user = $this->getTestUser('jane@kolabnow.com'); | ||||
public function testRegisterEntitlement(): void | |||||
{ | |||||
// TODO: This test depends on seeded SKUs, but probably should not | |||||
$domain = $this->getTestDomain( | |||||
'custom-domain.com', | |||||
[ | |||||
'status' => Domain::STATUS_NEW, | |||||
'type' => Domain::TYPE_EXTERNAL, | |||||
] | |||||
); | |||||
\Log::debug(var_export($domain->toArray(), true)); | |||||
$user = $this->getTestUser('sku-test-user@custom-domain.com'); | |||||
$wallet = $user->wallets()->first(); | $wallet = $user->wallets()->first(); | ||||
// \App\Handlers\Mailbox SKU | $package = Package::where('title', 'lite')->first(); | ||||
// Note, we're testing mailbox SKU before domain SKU as it may potentially fail in that | |||||
// order | |||||
$sku = Sku::where('title', 'mailbox')->first(); | |||||
Entitlement::create( | |||||
[ | |||||
'owner_id' => $user->id, | |||||
'wallet_id' => $wallet->id, | |||||
'sku_id' => $sku->id, | |||||
'entitleable_id' => $user->id, | |||||
'entitleable_type' => User::class | |||||
] | |||||
); | |||||
$entitlements = $sku->entitlements()->where('owner_id', $user->id)->get(); | |||||
$wallet->refresh(); | |||||
if ($sku->active) { | $sku_mailbox = Sku::where('title', 'mailbox')->first(); | ||||
$balance = -$sku->cost; | $sku_storage = Sku::where('title', 'storage')->first(); | ||||
$this->assertCount(1, $entitlements); | |||||
$this->assertEquals($user->id, $entitlements[0]->entitleable_id); | |||||
$this->assertSame( | |||||
Handlers\Mailbox::entitleableClass(), | |||||
$entitlements[0]->entitleable_type | |||||
); | |||||
} else { | |||||
$this->assertCount(0, $entitlements); | |||||
} | |||||
$this->assertEquals($balance, $wallet->balance); | $user = $user->assignPackage($package); | ||||
// \App\Handlers\Domain SKU | $this->backdateEntitlements($user->fresh()->entitlements, Carbon::now()->subMonths(1)); | ||||
$sku = Sku::where('title', 'domain')->first(); | |||||
Entitlement::create( | |||||
[ | |||||
'owner_id' => $user->id, | |||||
'wallet_id' => $wallet->id, | |||||
'sku_id' => $sku->id, | |||||
'entitleable_id' => $domain->id, | |||||
'entitleable_type' => Domain::class | |||||
] | |||||
); | |||||
$entitlements = $sku->entitlements->where('owner_id', $user->id); | $wallet->chargeEntitlements(); | ||||
foreach ($entitlements as $entitlement) { | $this->assertTrue($wallet->balance < 0); | ||||
\Log::debug(var_export($entitlement->toArray(), true)); | |||||
} | } | ||||
$wallet->refresh(); | public function testSkuEntitlements(): void | ||||
{ | |||||
if ($sku->active) { | $this->assertCount(2, Sku::where('title', 'mailbox')->first()->entitlements); | ||||
$balance -= $sku->cost; | |||||
$this->assertCount(1, $entitlements); | |||||
$_domain = Domain::find($entitlements->first()->entitleable_id); | |||||
$this->assertEquals( | |||||
$domain->id, | |||||
$entitlements->first()->entitleable_id, | |||||
var_export($_domain->toArray(), true) | |||||
); | |||||
$this->assertSame( | |||||
Handlers\Domain::entitleableClass(), | |||||
$entitlements->first()->entitleable_type | |||||
); | |||||
} else { | |||||
$this->assertCount(0, $entitlements); | |||||
} | } | ||||
$this->assertEquals($balance, $wallet->balance); | public function testSkuPackages(): void | ||||
{ | |||||
// \App\Handlers\DomainRegistration SKU | $this->assertCount(2, Sku::where('title', 'mailbox')->first()->packages); | ||||
$sku = Sku::where('title', 'domain-registration')->first(); | |||||
Entitlement::create( | |||||
[ | |||||
'owner_id' => $user->id, | |||||
'wallet_id' => $user->wallets()->get()[0]->id, | |||||
'sku_id' => $sku->id, | |||||
'entitleable_id' => $domain->id, | |||||
'entitleable_type' => Domain::class | |||||
] | |||||
); | |||||
$entitlements = $sku->entitlements->where('owner_id', $user->id); | |||||
$wallet->refresh(); | |||||
if ($sku->active) { | |||||
$balance -= $sku->cost; | |||||
$this->assertCount(1, $entitlements); | |||||
$this->assertEquals($domain->id, $entitlements->first()->entitleable_id); | |||||
$this->assertSame( | |||||
Handlers\DomainRegistration::entitleableClass(), | |||||
$entitlements->first()->entitleable_type | |||||
); | |||||
} else { | |||||
$this->assertCount(0, $entitlements); | |||||
} | } | ||||
$this->assertEquals($balance, $wallet->balance); | public function testSkuHandlerDomainHosting(): void | ||||
{ | |||||
// \App\Handlers\DomainHosting SKU | |||||
$sku = Sku::where('title', 'domain-hosting')->first(); | $sku = Sku::where('title', 'domain-hosting')->first(); | ||||
Entitlement::create( | |||||
[ | |||||
'owner_id' => $user->id, | |||||
'wallet_id' => $wallet->id, | |||||
'sku_id' => $sku->id, | |||||
'entitleable_id' => $domain->id, | |||||
'entitleable_type' => Domain::class | |||||
] | |||||
); | |||||
$entitlements = $sku->entitlements->where('owner_id', $user->id); | $entitlement = $sku->entitlements->first(); | ||||
$wallet->refresh(); | |||||
if ($sku->active) { | |||||
$balance -= $sku->cost; | |||||
$this->assertCount(1, $entitlements); | |||||
$this->assertEquals($domain->id, $entitlements->first()->entitleable_id); | |||||
$this->assertSame( | $this->assertSame( | ||||
Handlers\DomainHosting::entitleableClass(), | Handlers\DomainHosting::entitleableClass(), | ||||
$entitlements->first()->entitleable_type | $entitlement->entitleable_type | ||||
); | ); | ||||
} else { | |||||
$this->assertCount(0, $entitlements); | |||||
} | } | ||||
$this->assertEquals($balance, $wallet->balance); | public function testSkuHandlerMailbox(): void | ||||
{ | |||||
// \App\Handlers\Groupware SKU | $sku = Sku::where('title', 'mailbox')->first(); | ||||
$sku = Sku::where('title', 'groupware')->first(); | |||||
Entitlement::create( | |||||
[ | |||||
'owner_id' => $user->id, | |||||
'wallet_id' => $user->wallets()->get()[0]->id, | |||||
'sku_id' => $sku->id, | |||||
'entitleable_id' => $user->id, | |||||
'entitleable_type' => User::class | |||||
] | |||||
); | |||||
$entitlements = $sku->entitlements->where('owner_id', $user->id); | $entitlement = $sku->entitlements->first(); | ||||
$wallet->refresh(); | |||||
if ($sku->active) { | |||||
$balance -= $sku->cost; | |||||
$this->assertCount(1, $entitlements); | |||||
$this->assertEquals($user->id, $entitlements->first()->entitleable_id); | |||||
$this->assertSame( | $this->assertSame( | ||||
Handlers\Mailbox::entitleableClass(), | Handlers\Mailbox::entitleableClass(), | ||||
$entitlements->first()->entitleable_type | $entitlement->entitleable_type | ||||
); | |||||
} else { | |||||
$this->assertCount(0, $entitlements); | |||||
} | |||||
$this->assertEquals($balance, $wallet->balance); | |||||
// \App\Handlers\Storage SKU | |||||
$sku = Sku::where('title', 'storage')->first(); | |||||
Entitlement::create( | |||||
[ | |||||
'owner_id' => $user->id, | |||||
'wallet_id' => $wallet->id, | |||||
'sku_id' => $sku->id, | |||||
'entitleable_id' => $user->id, | |||||
'entitleable_type' => User::class | |||||
] | |||||
); | ); | ||||
$entitlements = $sku->entitlements->where('owner_id', $user->id); | |||||
$wallet->refresh(); | |||||
if ($sku->active) { | |||||
$balance -= $sku->cost; | |||||
$this->assertCount(1, $entitlements); | |||||
} else { | |||||
$this->assertCount(0, $entitlements); | |||||
} | |||||
$this->assertEquals($balance, $wallet->balance); | |||||
} | } | ||||
public function testSkuPackages(): void | public function testSkuHandlerStorage(): void | ||||
{ | { | ||||
$sku = Sku::where('title', 'mailbox')->first(); | $sku = Sku::where('title', 'storage')->first(); | ||||
$packages = $sku->packages; | $entitlement = $sku->entitlements->first(); | ||||
$this->assertCount(2, $packages); | $this->assertSame( | ||||
Handlers\Storage::entitleableClass(), | |||||
$entitlement->entitleable_type | |||||
); | |||||
} | } | ||||
} | } |