2016-11-24 02:11:33 +05:30
|
|
|
<?php
|
2019-02-21 01:28:52 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace console\tests\unit\controllers;
|
2016-11-24 02:11:33 +05:30
|
|
|
|
2017-09-29 04:34:16 +05:30
|
|
|
use common\models\AccountSession;
|
2017-03-07 22:44:54 +05:30
|
|
|
use common\models\EmailActivation;
|
2017-09-27 22:22:28 +05:30
|
|
|
use common\models\MinecraftAccessKey;
|
2018-02-28 03:57:35 +05:30
|
|
|
use common\models\OauthClient;
|
|
|
|
use common\tasks\ClearOauthSessions;
|
2019-02-21 01:28:52 +05:30
|
|
|
use common\tests\fixtures;
|
2017-03-07 22:44:54 +05:30
|
|
|
use console\controllers\CleanupController;
|
2019-02-21 01:28:52 +05:30
|
|
|
use console\tests\unit\TestCase;
|
2017-03-07 22:44:54 +05:30
|
|
|
use Yii;
|
2016-11-24 02:11:33 +05:30
|
|
|
|
|
|
|
class CleanupControllerTest extends TestCase {
|
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
public function _fixtures(): array {
|
2017-03-07 22:44:54 +05:30
|
|
|
return [
|
2018-02-28 03:57:35 +05:30
|
|
|
'emailActivations' => fixtures\EmailActivationFixture::class,
|
|
|
|
'minecraftSessions' => fixtures\MinecraftAccessKeyFixture::class,
|
|
|
|
'accountsSessions' => fixtures\AccountSessionFixture::class,
|
|
|
|
'oauthClients' => fixtures\OauthClientFixture::class,
|
|
|
|
'oauthSessions' => fixtures\OauthSessionFixture::class,
|
2017-03-07 22:44:54 +05:30
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-09-27 22:22:28 +05:30
|
|
|
public function testActionEmailKeys() {
|
2017-03-07 22:44:54 +05:30
|
|
|
/** @var EmailActivation $expiredConfirmation */
|
|
|
|
$expiredConfirmation = $this->tester->grabFixture('emailActivations', 'deeplyExpiredConfirmation');
|
|
|
|
|
|
|
|
$controller = new CleanupController('cleanup', Yii::$app);
|
|
|
|
$this->assertEquals(0, $controller->actionEmailKeys());
|
|
|
|
|
2017-09-27 22:22:28 +05:30
|
|
|
$this->tester->cantSeeRecord(EmailActivation::class, ['key' => $expiredConfirmation->key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testActionMinecraftSessions() {
|
|
|
|
/** @var MinecraftAccessKey $expiredSession */
|
|
|
|
$expiredSession = $this->tester->grabFixture('minecraftSessions', 'expired-token');
|
|
|
|
|
|
|
|
$controller = new CleanupController('cleanup', Yii::$app);
|
|
|
|
$this->assertEquals(0, $controller->actionMinecraftSessions());
|
|
|
|
|
|
|
|
$this->tester->cantSeeRecord(MinecraftAccessKey::class, ['access_token' => $expiredSession->access_token]);
|
2017-03-07 22:44:54 +05:30
|
|
|
}
|
|
|
|
|
2017-09-29 04:34:16 +05:30
|
|
|
public function testActionWebSessions() {
|
|
|
|
/** @var AccountSession $expiredSession */
|
|
|
|
$expiredSession = $this->tester->grabFixture('accountsSessions', 'very-expired-session');
|
|
|
|
/** @var AccountSession $notRefreshedSession */
|
|
|
|
$notRefreshedSession = $this->tester->grabFixture('accountsSessions', 'not-refreshed-session');
|
|
|
|
$totalSessionsCount = AccountSession::find()->count();
|
|
|
|
|
|
|
|
$controller = new CleanupController('cleanup', Yii::$app);
|
|
|
|
$this->assertEquals(0, $controller->actionWebSessions());
|
|
|
|
|
|
|
|
$this->tester->cantSeeRecord(AccountSession::class, ['id' => $expiredSession->id]);
|
|
|
|
$this->tester->cantSeeRecord(AccountSession::class, ['id' => $notRefreshedSession->id]);
|
|
|
|
$this->assertEquals($totalSessionsCount - 2, AccountSession::find()->count());
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:57:35 +05:30
|
|
|
public function testActionOauthClients() {
|
|
|
|
/** @var OauthClient $deletedClient */
|
|
|
|
$totalClientsCount = OauthClient::find()->includeDeleted()->count();
|
|
|
|
|
|
|
|
$controller = new CleanupController('cleanup', Yii::$app);
|
|
|
|
$this->assertEquals(0, $controller->actionOauthClients());
|
|
|
|
|
|
|
|
$this->assertNull(OauthClient::find()->includeDeleted()->andWhere(['id' => 'deleted-oauth-client'])->one());
|
|
|
|
$this->assertNotNull(OauthClient::find()->includeDeleted()->andWhere(['id' => 'deleted-oauth-client-with-sessions'])->one());
|
|
|
|
$this->assertEquals($totalClientsCount - 1, OauthClient::find()->includeDeleted()->count());
|
|
|
|
|
|
|
|
/** @var ClearOauthSessions $job */
|
|
|
|
$job = $this->tester->grabLastQueuedJob();
|
|
|
|
$this->assertInstanceOf(ClearOauthSessions::class, $job);
|
|
|
|
$this->assertSame('deleted-oauth-client-with-sessions', $job->clientId);
|
|
|
|
$this->assertNull($job->notSince);
|
|
|
|
}
|
|
|
|
|
2016-11-24 02:11:33 +05:30
|
|
|
}
|