Changeset View
Changeset View
Standalone View
Standalone View
src/tests/TestCaseTrait.php
Show First 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | trait TestCaseTrait | ||||
* Create a set of transaction log entries for a wallet | * Create a set of transaction log entries for a wallet | ||||
*/ | */ | ||||
protected function createTestTransactions($wallet) | protected function createTestTransactions($wallet) | ||||
{ | { | ||||
$result = []; | $result = []; | ||||
$date = Carbon::now(); | $date = Carbon::now(); | ||||
$debit = 0; | $debit = 0; | ||||
$entitlementTransactions = []; | $entitlementTransactions = []; | ||||
foreach ($wallet->entitlements as $entitlement) { | foreach ($wallet->entitlements as $entitlement) { | ||||
if ($entitlement->cost) { | if ($entitlement->cost) { | ||||
$debit += $entitlement->cost; | $debit += $entitlement->cost; | ||||
$entitlementTransactions[] = $entitlement->createTransaction( | $entitlementTransactions[] = $entitlement->createTransaction( | ||||
Transaction::ENTITLEMENT_BILLED, | Transaction::ENTITLEMENT_BILLED, | ||||
$entitlement->cost | $entitlement->cost | ||||
); | ); | ||||
} | } | ||||
} | } | ||||
$transaction = Transaction::create([ | $transaction = Transaction::create( | ||||
[ | |||||
'user_email' => 'jeroen@jeroen.jeroen', | 'user_email' => 'jeroen@jeroen.jeroen', | ||||
'object_id' => $wallet->id, | 'object_id' => $wallet->id, | ||||
'object_type' => \App\Wallet::class, | 'object_type' => \App\Wallet::class, | ||||
'type' => Transaction::WALLET_DEBIT, | 'type' => Transaction::WALLET_DEBIT, | ||||
'amount' => $debit * -1, | 'amount' => $debit * -1, | ||||
'description' => 'Payment', | 'description' => 'Payment', | ||||
]); | ] | ||||
); | |||||
$result[] = $transaction; | $result[] = $transaction; | ||||
Transaction::whereIn('id', $entitlementTransactions)->update(['transaction_id' => $transaction->id]); | Transaction::whereIn('id', $entitlementTransactions)->update(['transaction_id' => $transaction->id]); | ||||
$transaction = Transaction::create([ | $transaction = Transaction::create( | ||||
[ | |||||
'user_email' => null, | 'user_email' => null, | ||||
'object_id' => $wallet->id, | 'object_id' => $wallet->id, | ||||
'object_type' => \App\Wallet::class, | 'object_type' => \App\Wallet::class, | ||||
'type' => Transaction::WALLET_CREDIT, | 'type' => Transaction::WALLET_CREDIT, | ||||
'amount' => 2000, | 'amount' => 2000, | ||||
'description' => 'Payment', | 'description' => 'Payment', | ||||
]); | ] | ||||
); | |||||
$transaction->created_at = $date->next(Carbon::MONDAY); | $transaction->created_at = $date->next(Carbon::MONDAY); | ||||
$transaction->save(); | $transaction->save(); | ||||
$result[] = $transaction; | $result[] = $transaction; | ||||
$types = [ | $types = [ | ||||
Transaction::WALLET_AWARD, | Transaction::WALLET_AWARD, | ||||
Transaction::WALLET_PENALTY, | Transaction::WALLET_PENALTY, | ||||
]; | ]; | ||||
// The page size is 10, so we generate so many to have at least two pages | // The page size is 10, so we generate so many to have at least two pages | ||||
$loops = 10; | $loops = 10; | ||||
while ($loops-- > 0) { | while ($loops-- > 0) { | ||||
$type = $types[count($result) % count($types)]; | $type = $types[count($result) % count($types)]; | ||||
$transaction = Transaction::create([ | $transaction = Transaction::create([ | ||||
'user_email' => 'jeroen.@jeroen.jeroen', | 'user_email' => 'jeroen.@jeroen.jeroen', | ||||
'object_id' => $wallet->id, | 'object_id' => $wallet->id, | ||||
'object_type' => \App\Wallet::class, | 'object_type' => \App\Wallet::class, | ||||
'type' => $type, | 'type' => $type, | ||||
'amount' => 11 * (count($result) + 1) * ($type == Transaction::WALLET_PENALTY ? -1 : 1), | 'amount' => 11 * (count($result) + 1) * ($type == Transaction::WALLET_PENALTY ? -1 : 1), | ||||
'description' => 'TRANS' . $loops, | 'description' => 'TRANS' . $loops, | ||||
]); | ]); | ||||
$transaction->created_at = $date->next(Carbon::MONDAY); | $transaction->created_at = $date->next(Carbon::MONDAY); | ||||
$transaction->save(); | $transaction->save(); | ||||
$result[] = $transaction; | $result[] = $transaction; | ||||
} | } | ||||
return $result; | return $result; | ||||
} | } | ||||
/** | |||||
* Delete a test domain whatever it takes. | |||||
* | |||||
* @coversNothing | |||||
*/ | |||||
protected function deleteTestDomain($name) | protected function deleteTestDomain($name) | ||||
{ | { | ||||
Queue::fake(); | Queue::fake(); | ||||
$domain = Domain::withTrashed()->where('namespace', $name)->first(); | $domain = Domain::withTrashed()->where('namespace', $name)->first(); | ||||
if (!$domain) { | if (!$domain) { | ||||
return; | return; | ||||
Show All 33 Lines | protected function deleteTestUser($email) | ||||
$job = new \App\Jobs\User\DeleteJob($user->id); | $job = new \App\Jobs\User\DeleteJob($user->id); | ||||
$job->handle(); | $job->handle(); | ||||
$user->forceDelete(); | $user->forceDelete(); | ||||
} | } | ||||
/** | /** | ||||
* Helper to access protected property of an object | |||||
*/ | |||||
protected static function getObjectProperty($object, $property_name) | |||||
{ | |||||
$reflection = new \ReflectionClass($object); | |||||
$property = $reflection->getProperty($property_name); | |||||
$property->setAccessible(true); | |||||
return $property->getValue($object); | |||||
} | |||||
/** | |||||
* Get Domain object by namespace, create it if needed. | * Get Domain object by namespace, create it if needed. | ||||
* Skip LDAP jobs. | * Skip LDAP jobs. | ||||
* | |||||
* @coversNothing | |||||
*/ | */ | ||||
protected function getTestDomain($name, $attrib = []) | protected function getTestDomain($name, $attrib = []) | ||||
{ | { | ||||
// Disable jobs (i.e. skip LDAP oprations) | // Disable jobs (i.e. skip LDAP oprations) | ||||
Queue::fake(); | Queue::fake(); | ||||
return Domain::firstOrCreate(['namespace' => $name], $attrib); | return Domain::firstOrCreate(['namespace' => $name], $attrib); | ||||
} | } | ||||
/** | /** | ||||
* Get Group object by email, create it if needed. | * Get Group object by email, create it if needed. | ||||
* Skip LDAP jobs. | * Skip LDAP jobs. | ||||
*/ | */ | ||||
protected function getTestGroup($email, $attrib = []) | protected function getTestGroup($email, $attrib = []) | ||||
{ | { | ||||
// Disable jobs (i.e. skip LDAP oprations) | // Disable jobs (i.e. skip LDAP oprations) | ||||
Queue::fake(); | Queue::fake(); | ||||
return Group::firstOrCreate(['email' => $email], $attrib); | return Group::firstOrCreate(['email' => $email], $attrib); | ||||
} | } | ||||
/** | /** | ||||
* Get User object by email, create it if needed. | * Get User object by email, create it if needed. | ||||
* Skip LDAP jobs. | * Skip LDAP jobs. | ||||
* | |||||
* @coversNothing | |||||
*/ | */ | ||||
protected function getTestUser($email, $attrib = []) | protected function getTestUser($email, $attrib = []) | ||||
{ | { | ||||
// Disable jobs (i.e. skip LDAP oprations) | // Disable jobs (i.e. skip LDAP oprations) | ||||
Queue::fake(); | Queue::fake(); | ||||
$user = User::firstOrCreate(['email' => $email], $attrib); | $user = User::firstOrCreate(['email' => $email], $attrib); | ||||
if ($user->trashed()) { | if ($user->trashed()) { | ||||
// Note: we do not want to use user restore here | // Note: we do not want to use user restore here | ||||
User::where('id', $user->id)->forceDelete(); | User::where('id', $user->id)->forceDelete(); | ||||
$user = User::create(['email' => $email] + $attrib); | $user = User::create(['email' => $email] + $attrib); | ||||
} | } | ||||
return $user; | return $user; | ||||
} | } | ||||
/** | /** | ||||
* Helper to access protected property of an object | |||||
*/ | |||||
protected static function getObjectProperty($object, $property_name) | |||||
{ | |||||
$reflection = new \ReflectionClass($object); | |||||
$property = $reflection->getProperty($property_name); | |||||
$property->setAccessible(true); | |||||
return $property->getValue($object); | |||||
} | |||||
/** | |||||
* Call protected/private method of a class. | * Call protected/private method of a class. | ||||
* | * | ||||
* @param object $object Instantiated object that we will run method on. | * @param object $object Instantiated object that we will run method on. | ||||
* @param string $methodName Method name to call | * @param string $methodName Method name to call | ||||
* @param array $parameters Array of parameters to pass into method. | * @param array $parameters Array of parameters to pass into method. | ||||
* | * | ||||
* @return mixed Method return. | * @return mixed Method return. | ||||
*/ | */ | ||||
protected function invokeMethod($object, $methodName, array $parameters = array()) | protected function invokeMethod($object, $methodName, array $parameters = array()) | ||||
{ | { | ||||
$reflection = new \ReflectionClass(get_class($object)); | $reflection = new \ReflectionClass(get_class($object)); | ||||
$method = $reflection->getMethod($methodName); | $method = $reflection->getMethod($methodName); | ||||
$method->setAccessible(true); | $method->setAccessible(true); | ||||
return $method->invokeArgs($object, $parameters); | return $method->invokeArgs($object, $parameters); | ||||
} | } | ||||
public function setUp(): void | |||||
{ | |||||
parent::setUp(); | |||||
$this->userPassword = \App\Utils::generatePassphrase(); | |||||
$this->domainHosted = $this->getTestDomain( | |||||
'test.domain', | |||||
[ | |||||
'type' => \App\Domain::TYPE_EXTERNAL, | |||||
'status' => \App\Domain::STATUS_ACTIVE | \App\Domain::STATUS_CONFIRMED | \App\Domain::STATUS_VERIFIED | |||||
] | |||||
); | |||||
$packageKolab = \App\Package::where('title', 'kolab')->first(); | |||||
$this->domainOwner = $this->getTestUser('john@test.domain', ['password' => $this->userPassword]); | |||||
$this->domainOwner->assignPackage($packageKolab); | |||||
$this->domainOwner->setSettings($this->domainOwnerSettings); | |||||
// separate for regular user | |||||
$this->jack = $this->getTestUser('jack@test.domain', ['password' => $this->userPassword]); | |||||
// separate for wallet controller | |||||
$this->jane = $this->getTestUser('jane@test.domain', ['password' => $this->userPassword]); | |||||
$this->joe = $this->getTestUser('joe@test.domain', ['password' => $this->userPassword]); | |||||
$this->domainUsers[] = $this->jack; | |||||
$this->domainUsers[] = $this->jane; | |||||
$this->domainUsers[] = $this->joe; | |||||
$this->domainUsers[] = $this->getTestUser('jill@test.domain', ['password' => $this->userPassword]); | |||||
foreach ($this->domainUsers as $user) { | |||||
$this->domainOwner->assignPackage($packageKolab, $user); | |||||
} | |||||
$this->domainUsers[] = $this->domainOwner; | |||||
// assign second factor to joe | |||||
$this->joe->assignSku(\App\Sku::where('title', '2fa')->first()); | |||||
\App\Auth\SecondFactor::seed($this->joe->email); | |||||
usort( | |||||
$this->domainUsers, | |||||
function ($a, $b) { | |||||
return $a->email > $b->email; | |||||
} | |||||
); | |||||
$this->domainHosted->assignPackage( | |||||
\App\Package::where('title', 'domain-hosting')->first(), | |||||
$this->domainOwner | |||||
); | |||||
$wallet = $this->domainOwner->wallets()->first(); | |||||
$wallet->addController($this->jane); | |||||
$this->publicDomain = \App\Domain::where('type', \App\Domain::TYPE_PUBLIC)->first(); | |||||
$this->publicDomainUser = $this->getTestUser( | |||||
'john@' . $this->publicDomain->namespace, | |||||
['password' => $this->userPassword] | |||||
); | |||||
$this->publicDomainUser->assignPackage($packageKolab); | |||||
} | |||||
public function tearDown(): void | |||||
{ | |||||
foreach ($this->domainUsers as $user) { | |||||
if ($user == $this->domainOwner) { | |||||
continue; | |||||
} | |||||
$this->deleteTestUser($user->email); | |||||
} | |||||
$this->deleteTestUser($this->domainOwner->email); | |||||
$this->deleteTestDomain($this->domainHosted->namespace); | |||||
$this->deleteTestUser($this->publicDomainUser->email); | |||||
parent::tearDown(); | |||||
} | |||||
} | } |