Changeset View
Changeset View
Standalone View
Standalone View
src/tests/Feature/Jobs/WalletCheckTest.php
Show First 20 Lines • Show All 91 Lines • ▼ Show 20 Lines | public function testHandleInitial(): void | ||||
Mail::assertSent(\App\Mail\NegativeBalance::class, function ($mail) use ($user) { | Mail::assertSent(\App\Mail\NegativeBalance::class, function ($mail) use ($user) { | ||||
return $mail->hasTo($user->email) && !$mail->hasCc('external@test.com'); | return $mail->hasTo($user->email) && !$mail->hasCc('external@test.com'); | ||||
}); | }); | ||||
$wallet->refresh(); | $wallet->refresh(); | ||||
$today_regexp = '/' . Carbon::now()->toDateString() . ' [0-9]{2}:[0-9]{2}:[0-9]{2}/'; | $today_regexp = '/' . Carbon::now()->toDateString() . ' [0-9]{2}:[0-9]{2}:[0-9]{2}/'; | ||||
$this->assertMatchesRegularExpression($today_regexp, $wallet->getSetting('balance_negative_since')); | $this->assertMatchesRegularExpression($today_regexp, $wallet->getSetting('balance_negative_since')); | ||||
$this->assertMatchesRegularExpression($today_regexp, $wallet->getSetting('balance_warning_initial')); | $this->assertMatchesRegularExpression($today_regexp, $wallet->getSetting('balance_warning_initial')); | ||||
// Test suspended user - no mail sent | |||||
Mail::fake(); | |||||
$wallet->owner->suspend(); | |||||
$wallet->setSetting('balance_warning_initial', null); | |||||
$job = new WalletCheck($wallet); | |||||
$job->handle(); | |||||
Mail::assertNothingSent(); | |||||
} | } | ||||
/** | /** | ||||
* Test job handle, top-up before reminder notification | * Test job handle, top-up before reminder notification | ||||
* | * | ||||
* @depends testHandleInitial | * @depends testHandleInitial | ||||
*/ | */ | ||||
public function testHandleBeforeReminder(): void | public function testHandleBeforeReminder(): void | ||||
{ | { | ||||
Mail::fake(); | Mail::fake(); | ||||
$user = $this->prepareTestUser($wallet); | $user = $this->prepareTestUser($wallet); | ||||
$now = Carbon::now(); | $now = Carbon::now(); | ||||
// Balance turned negative 7-1 days ago | // Balance turned negative 7-1 days ago | ||||
$wallet->setSetting('balance_negative_since', $now->subDays(7 - 1)->toDateTimeString()); | $wallet->setSetting('balance_negative_since', $now->subDays(7 - 1)->toDateTimeString()); | ||||
$job = new WalletCheck($wallet); | $job = new WalletCheck($wallet); | ||||
$res = $job->handle(); | $res = $job->handle(); | ||||
Mail::assertNothingSent(); | Mail::assertNothingSent(); | ||||
// TODO: Test that it actually executed the topUpWallet() | // TODO: Test that it actually executed the topUpWallet() | ||||
$this->assertSame(WalletCheck::THRESHOLD_BEFORE_REMINDER, $res); | $this->assertSame(WalletCheck::THRESHOLD_BEFORE_REMINDER, $res); | ||||
$this->assertFalse($user->fresh()->isSuspended()); | |||||
} | } | ||||
/** | /** | ||||
* Test job handle, reminder notification | * Test job handle, reminder notification | ||||
* | * | ||||
* @depends testHandleBeforeReminder | * @depends testHandleBeforeReminder | ||||
*/ | */ | ||||
public function testHandleReminder(): void | public function testHandleReminder(): void | ||||
Show All 16 Lines | public function testHandleReminder(): void | ||||
}); | }); | ||||
// Run the job again to make sure the notification is not sent again | // Run the job again to make sure the notification is not sent again | ||||
Mail::fake(); | Mail::fake(); | ||||
$job = new WalletCheck($wallet); | $job = new WalletCheck($wallet); | ||||
$job->handle(); | $job->handle(); | ||||
Mail::assertNothingSent(); | Mail::assertNothingSent(); | ||||
} | |||||
/** | |||||
* Test job handle, top-up wallet before account suspending | |||||
* | |||||
* @depends testHandleReminder | |||||
*/ | |||||
/* | |||||
public function testHandleBeforeSuspended(): void | |||||
{ | |||||
Mail::fake(); | |||||
$user = $this->prepareTestUser($wallet); | |||||
$now = Carbon::now(); | |||||
// Balance turned negative 7+14-1 days ago | |||||
$days = 7 + 14 - 1; | |||||
$wallet->setSetting('balance_negative_since', $now->subDays($days)->toDateTimeString()); | |||||
$job = new WalletCheck($wallet); | |||||
$res = $job->handle(); | |||||
Mail::assertNothingSent(); | |||||
// TODO: Test that it actually executed the topUpWallet() | |||||
$this->assertSame(WalletCheck::THRESHOLD_BEFORE_SUSPEND, $res); | |||||
$this->assertFalse($user->fresh()->isSuspended()); | |||||
} | |||||
*/ | |||||
/** | |||||
* Test job handle, account suspending | |||||
* | |||||
* @depends testHandleBeforeSuspended | |||||
*/ | |||||
/* | |||||
public function testHandleSuspended(): void | |||||
{ | |||||
Mail::fake(); | |||||
$user = $this->prepareTestUser($wallet); | |||||
$now = Carbon::now(); | |||||
// Balance turned negative 7+14+1 days ago, expect mail sent | |||||
$days = 7 + 14 + 1; | |||||
$wallet->setSetting('balance_negative_since', $now->subDays($days)->toDateTimeString()); | |||||
$job = new WalletCheck($wallet); | |||||
$job->handle(); | |||||
// Assert the mail was sent to the user's email, but not to his external email | |||||
Mail::assertSent(\App\Mail\NegativeBalanceSuspended::class, 1); | |||||
Mail::assertSent(\App\Mail\NegativeBalanceSuspended::class, function ($mail) use ($user) { | |||||
return $mail->hasTo($user->email) && $mail->hasCc('external@test.com'); | |||||
}); | |||||
// Check that it has been suspended | |||||
$this->assertTrue($user->fresh()->isSuspended()); | |||||
// TODO: Test that group account members/domain are also being suspended | |||||
// Run the job again to make sure the notification is not sent again | |||||
Mail::fake(); | |||||
$job = new WalletCheck($wallet); | |||||
$job->handle(); | |||||
Mail::assertNothingSent(); | |||||
} | |||||
*/ | |||||
/** | |||||
* Test job handle, final warning before delete | |||||
* | |||||
* @depends testHandleSuspended | |||||
*/ | |||||
/* | |||||
public function testHandleBeforeDelete(): void | |||||
{ | |||||
Mail::fake(); | |||||
$user = $this->prepareTestUser($wallet); | // Test suspended user - no mail sent | ||||
$now = Carbon::now(); | |||||
// Balance turned negative 7+14+21-3+1 days ago, expect mail sent | |||||
$days = 7 + 14 + 21 - 3 + 1; | |||||
$wallet->setSetting('balance_negative_since', $now->subDays($days)->toDateTimeString()); | |||||
$job = new WalletCheck($wallet); | |||||
$job->handle(); | |||||
// Assert the mail was sent to the user's email, and his external email | |||||
Mail::assertSent(\App\Mail\NegativeBalanceBeforeDelete::class, 1); | |||||
Mail::assertSent(\App\Mail\NegativeBalanceBeforeDelete::class, function ($mail) use ($user) { | |||||
return $mail->hasTo($user->email) && $mail->hasCc('external@test.com'); | |||||
}); | |||||
// Check that it has not been deleted yet | |||||
$this->assertFalse($user->fresh()->isDeleted()); | |||||
// Run the job again to make sure the notification is not sent again | |||||
Mail::fake(); | Mail::fake(); | ||||
$job = new WalletCheck($wallet); | $wallet->owner->suspend(); | ||||
$job->handle(); | $wallet->setSetting('balance_warning_reminder', null); | ||||
Mail::assertNothingSent(); | |||||
} | |||||
*/ | |||||
/** | |||||
* Test job handle, account delete | |||||
* | |||||
* @depends testHandleBeforeDelete | |||||
*/ | |||||
/* | |||||
public function testHandleDelete(): void | |||||
{ | |||||
Mail::fake(); | |||||
$user = $this->prepareTestUser($wallet); | |||||
$now = Carbon::now(); | |||||
$this->assertFalse($user->isDeleted()); | |||||
$this->assertCount(7, $user->entitlements()->get()); | |||||
// Balance turned negative 7+14+21+1 days ago, expect mail sent | |||||
$days = 7 + 14 + 21 + 1; | |||||
$wallet->setSetting('balance_negative_since', $now->subDays($days)->toDateTimeString()); | |||||
$job = new WalletCheck($wallet); | $job = new WalletCheck($wallet); | ||||
$job->handle(); | $job->handle(); | ||||
Mail::assertNothingSent(); | Mail::assertNothingSent(); | ||||
// Check that it has not been deleted | |||||
$this->assertTrue($user->fresh()->trashed()); | |||||
$this->assertCount(0, $user->entitlements()->get()); | |||||
// TODO: Test it deletes all members of the group account | |||||
} | } | ||||
*/ | |||||
/** | /** | ||||
* Test job handle, account degrade | * Test job handle, account degrade | ||||
* | * | ||||
* @depends testHandleReminder | * @depends testHandleReminder | ||||
*/ | */ | ||||
public function testHandleDegrade(): void | public function testHandleDegrade(): void | ||||
{ | { | ||||
Show All 14 Lines | public function testHandleDegrade(): void | ||||
// Assert the mail was sent to the user's email, and his external email | // Assert the mail was sent to the user's email, and his external email | ||||
Mail::assertSent(\App\Mail\NegativeBalanceDegraded::class, 1); | Mail::assertSent(\App\Mail\NegativeBalanceDegraded::class, 1); | ||||
Mail::assertSent(\App\Mail\NegativeBalanceDegraded::class, function ($mail) use ($user) { | Mail::assertSent(\App\Mail\NegativeBalanceDegraded::class, function ($mail) use ($user) { | ||||
return $mail->hasTo($user->email) && $mail->hasCc('external@test.com'); | return $mail->hasTo($user->email) && $mail->hasCc('external@test.com'); | ||||
}); | }); | ||||
// Check that it has been degraded | // Check that it has been degraded | ||||
$this->assertTrue($user->fresh()->isDegraded()); | $this->assertTrue($user->fresh()->isDegraded()); | ||||
// Test suspended user - no mail sent | |||||
Mail::fake(); | |||||
$wallet->owner->suspend(); | |||||
$wallet->owner->undegrade(); | |||||
$job = new WalletCheck($wallet); | |||||
$job->handle(); | |||||
Mail::assertNothingSent(); | |||||
} | } | ||||
/** | /** | ||||
* Test job handle, periodic reminder to a degraded account | * Test job handle, periodic reminder to a degraded account | ||||
* | * | ||||
* @depends testHandleDegrade | * @depends testHandleDegrade | ||||
*/ | */ | ||||
public function testHandleDegradeReminder(): void | public function testHandleDegradeReminder(): void | ||||
Show All 35 Lines | public function testHandleDegradeReminder(): void | ||||
$wallet->setSetting('degraded_last_reminder', $now->copy()->subDays(14)->setSeconds(0)); | $wallet->setSetting('degraded_last_reminder', $now->copy()->subDays(14)->setSeconds(0)); | ||||
$job = new WalletCheck($wallet); | $job = new WalletCheck($wallet); | ||||
$res = $job->handle(); | $res = $job->handle(); | ||||
// Assert the mail was sent to the user's email, and his external email | // Assert the mail was sent to the user's email, and his external email | ||||
Mail::assertSent(\App\Mail\DegradedAccountReminder::class, 1); | Mail::assertSent(\App\Mail\DegradedAccountReminder::class, 1); | ||||
Mail::assertSent(\App\Mail\DegradedAccountReminder::class, function ($mail) use ($user) { | Mail::assertSent(\App\Mail\DegradedAccountReminder::class, function ($mail) use ($user) { | ||||
return $mail->hasTo($user->email) && $mail->hasCc('external@test.com'); | return $mail->hasTo($user->email) && !$mail->hasCc('external@test.com'); | ||||
}); | }); | ||||
$_last = $wallet->fresh()->getSetting('degraded_last_reminder'); | $_last = $wallet->fresh()->getSetting('degraded_last_reminder'); | ||||
$this->assertSame(Carbon::now()->toDateTimeString(), $_last); | $this->assertSame(Carbon::now()->toDateTimeString(), $_last); | ||||
$this->assertSame(WalletCheck::THRESHOLD_DEGRADE_REMINDER, $res); | $this->assertSame(WalletCheck::THRESHOLD_DEGRADE_REMINDER, $res); | ||||
// Test suspended user - no mail sent | |||||
Mail::fake(); | |||||
$wallet->owner->suspend(); | |||||
$wallet->owner->undegrade(); | |||||
$wallet->setSetting('degraded_last_reminder', null); | |||||
$job = new WalletCheck($wallet); | |||||
$job->handle(); | |||||
Mail::assertNothingSent(); | |||||
} | } | ||||
/** | /** | ||||
* A helper to prepare a user for tests | * A helper to prepare a user for tests | ||||
*/ | */ | ||||
private function prepareTestUser(&$wallet) | private function prepareTestUser(&$wallet) | ||||
{ | { | ||||
$status = User::STATUS_ACTIVE | User::STATUS_LDAP_READY | User::STATUS_IMAP_READY; | $status = User::STATUS_ACTIVE | User::STATUS_LDAP_READY | User::STATUS_IMAP_READY; | ||||
Show All 13 Lines |