Changeset View
Changeset View
Standalone View
Standalone View
src/tests/TestCaseTrait.php
<?php | <?php | ||||
namespace Tests; | namespace Tests; | ||||
use App\Domain; | use App\Domain; | ||||
use App\Group; | use App\Group; | ||||
use App\Sku; | |||||
use App\Transaction; | use App\Transaction; | ||||
use App\User; | use App\User; | ||||
use Carbon\Carbon; | use Carbon\Carbon; | ||||
use Illuminate\Contracts\Console\Kernel; | use Illuminate\Contracts\Console\Kernel; | ||||
use Illuminate\Support\Facades\Queue; | use Illuminate\Support\Facades\Queue; | ||||
use PHPUnit\Framework\Assert; | use PHPUnit\Framework\Assert; | ||||
trait TestCaseTrait | trait TestCaseTrait | ||||
▲ Show 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | trait TestCaseTrait | ||||
* | * | ||||
* Should be generated with `\App\Utils::generatePassphrase()`. | * Should be generated with `\App\Utils::generatePassphrase()`. | ||||
* | * | ||||
* @var ?string | * @var ?string | ||||
*/ | */ | ||||
protected $userPassword; | protected $userPassword; | ||||
/** | /** | ||||
* Register the beta entitlement for a user | |||||
*/ | |||||
protected function addBetaEntitlement($user, $title): void | |||||
{ | |||||
// Add beta + $title entitlements | |||||
$beta_sku = Sku::withEnvTenantContext()->where('title', 'beta')->first(); | |||||
$sku = Sku::withEnvTenantContext()->where('title', $title)->first(); | |||||
$user->assignSku($beta_sku); | |||||
$user->assignSku($sku); | |||||
} | |||||
/** | |||||
* Assert that the entitlements for the user match the expected list of entitlements. | * Assert that the entitlements for the user match the expected list of entitlements. | ||||
* | * | ||||
* @param \App\User|\App\Domain $object The object for which the entitlements need to be pulled. | * @param \App\User|\App\Domain $object The object for which the entitlements need to be pulled. | ||||
* @param array $expected An array of expected \App\SKU titles. | * @param array $expected An array of expected \App\SKU titles. | ||||
*/ | */ | ||||
protected function assertEntitlements($object, $expected) | protected function assertEntitlements($object, $expected) | ||||
{ | { | ||||
// Assert the user entitlements | // Assert the user entitlements | ||||
$skus = $object->entitlements()->get() | $skus = $object->entitlements()->get() | ||||
->map(function ($ent) { | ->map(function ($ent) { | ||||
return $ent->sku->title; | return $ent->sku->title; | ||||
}) | }) | ||||
->toArray(); | ->toArray(); | ||||
sort($skus); | sort($skus); | ||||
Assert::assertSame($expected, $skus); | Assert::assertSame($expected, $skus); | ||||
} | } | ||||
protected function backdateEntitlements($entitlements, $targetDate) | protected function backdateEntitlements($entitlements, $targetDate, $targetCreatedDate = null) | ||||
{ | { | ||||
$wallets = []; | $wallets = []; | ||||
$ids = []; | $ids = []; | ||||
foreach ($entitlements as $entitlement) { | foreach ($entitlements as $entitlement) { | ||||
$ids[] = $entitlement->id; | $ids[] = $entitlement->id; | ||||
$wallets[] = $entitlement->wallet_id; | $wallets[] = $entitlement->wallet_id; | ||||
} | } | ||||
\App\Entitlement::whereIn('id', $ids)->update([ | \App\Entitlement::whereIn('id', $ids)->update([ | ||||
'created_at' => $targetDate, | 'created_at' => $targetCreatedDate ?: $targetDate, | ||||
'updated_at' => $targetDate, | 'updated_at' => $targetDate, | ||||
]); | ]); | ||||
if (!empty($wallets)) { | if (!empty($wallets)) { | ||||
$wallets = array_unique($wallets); | $wallets = array_unique($wallets); | ||||
$owners = \App\Wallet::whereIn('id', $wallets)->pluck('user_id')->all(); | $owners = \App\Wallet::whereIn('id', $wallets)->pluck('user_id')->all(); | ||||
\App\User::whereIn('id', $owners)->update(['created_at' => $targetDate]); | \App\User::whereIn('id', $owners)->update([ | ||||
'created_at' => $targetCreatedDate ?: $targetDate | |||||
]); | |||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Removes all beta entitlements from the database | * Removes all beta entitlements from the database | ||||
*/ | */ | ||||
protected function clearBetaEntitlements(): void | protected function clearBetaEntitlements(): void | ||||
{ | { | ||||
$beta_handlers = [ | $beta_handlers = [ | ||||
'App\Handlers\Beta', | 'App\Handlers\Beta', | ||||
'App\Handlers\Distlist', | 'App\Handlers\Distlist', | ||||
]; | ]; | ||||
$betas = \App\Sku::whereIn('handler_class', $beta_handlers)->pluck('id')->all(); | $betas = Sku::whereIn('handler_class', $beta_handlers)->pluck('id')->all(); | ||||
\App\Entitlement::whereIn('sku_id', $betas)->delete(); | \App\Entitlement::whereIn('sku_id', $betas)->delete(); | ||||
} | } | ||||
/** | /** | ||||
* Creates the application. | * Creates the application. | ||||
* | * | ||||
* @return \Illuminate\Foundation\Application | * @return \Illuminate\Foundation\Application | ||||
▲ Show 20 Lines • Show All 265 Lines • ▼ Show 20 Lines | protected function setUpTest() | ||||
foreach ($this->domainUsers as $user) { | foreach ($this->domainUsers as $user) { | ||||
$this->domainOwner->assignPackage($packageKolab, $user); | $this->domainOwner->assignPackage($packageKolab, $user); | ||||
} | } | ||||
$this->domainUsers[] = $this->domainOwner; | $this->domainUsers[] = $this->domainOwner; | ||||
// assign second factor to joe | // assign second factor to joe | ||||
$this->joe->assignSku(\App\Sku::where('title', '2fa')->first()); | $this->joe->assignSku(Sku::where('title', '2fa')->first()); | ||||
\App\Auth\SecondFactor::seed($this->joe->email); | \App\Auth\SecondFactor::seed($this->joe->email); | ||||
usort( | usort( | ||||
$this->domainUsers, | $this->domainUsers, | ||||
function ($a, $b) { | function ($a, $b) { | ||||
return $a->email > $b->email; | return $a->email > $b->email; | ||||
} | } | ||||
); | ); | ||||
▲ Show 20 Lines • Show All 44 Lines • Show Last 20 Lines |