accounts/common/tests/unit/models/AccountTest.php
Octol1ttle 57d492da8a
Upgrade project to PHP 8.3, add PHPStan, upgrade almost every dependency (#36)
* start updating to PHP 8.3

* taking off!

Co-authored-by: ErickSkrauch <erickskrauch@yandex.ru>
Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* dropped this

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* migrate to symfonymailer

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* this is so stupid 😭

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* ah, free, at last.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* oh, Gabriel.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* now dawns thy reckoning.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* and thy gore shall GLISTEN before the temples of man.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* creature of steel.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* my gratitude upon thee for my freedom.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* but the crimes thy kind has committed against humanity

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* Upgrade PHP-CS-Fixer and do fix the codebase

* First review round (maybe I have broken something)

* are NOT forgotten.

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>

* Enable parallel PHP-CS-Fixer runner

* PHPStan level 1

* PHPStan level 2

* PHPStan level 3

* PHPStan level 4

* PHPStan level 5

* Levels 6 and 7 takes too much effort. Generate a baseline and fix them eventually

* Resolve TODO's related to the php-mock

* Drastically reduce baseline size with the Rector

* More code modernization with help of the Rector

* Update GitLab CI

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
Co-authored-by: ErickSkrauch <erickskrauch@yandex.ru>
2024-12-02 11:10:55 +01:00

167 lines
7.1 KiB
PHP

<?php
declare(strict_types=1);
namespace common\tests\unit\models;
use Codeception\Util\ReflectionHelper;
use common\models\Account;
use common\notifications\AccountDeletedNotification;
use common\notifications\AccountEditNotification;
use common\tasks\CreateWebHooksDeliveries;
use common\tests\fixtures\MojangUsernameFixture;
use common\tests\unit\TestCase;
use const common\LATEST_RULES_VERSION;
/**
* @covers \common\models\Account
*/
class AccountTest extends TestCase {
public function testSetPassword(): void {
$model = new Account();
$model->setPassword('12345678');
$this->assertNotEmpty($model->password_hash, 'hash should be set');
$this->assertTrue($model->validatePassword('12345678'), 'validation should be passed');
$this->assertSame(Account::PASS_HASH_STRATEGY_YII2, $model->password_hash_strategy, 'latest password hash should be used');
}
public function testValidatePassword(): void {
// Use old hashing algorithm
$model = new Account();
$model->email = 'erick@skrauch.net';
$model->password_hash = '2cfdb29eb354af970865a923335d17d9'; // 12345678
$this->assertTrue($model->validatePassword('12345678', Account::PASS_HASH_STRATEGY_OLD_ELY), 'valid password should pass');
$this->assertFalse($model->validatePassword('87654321', Account::PASS_HASH_STRATEGY_OLD_ELY), 'invalid password should fail');
// Modern hash algorithm should also work
$model = new Account();
$model->password_hash = '$2y$04$N0q8DaHzlYILCnLYrpZfEeWKEqkPZzbawiS07GbSr/.xbRNweSLU6'; // 12345678
$this->assertTrue($model->validatePassword('12345678', Account::PASS_HASH_STRATEGY_YII2), 'valid password should pass');
$this->assertFalse($model->validatePassword('87654321', Account::PASS_HASH_STRATEGY_YII2), 'invalid password should fail');
// If the second arg isn't passed model's value should be used
$model = new Account();
$model->email = 'erick@skrauch.net';
$model->password_hash = '2cfdb29eb354af970865a923335d17d9'; // 12345678
$model->password_hash_strategy = Account::PASS_HASH_STRATEGY_OLD_ELY;
$this->assertTrue($model->validatePassword('12345678'), 'valid password should pass');
$this->assertFalse($model->validatePassword('87654321'), 'invalid password should fail');
// The same case for modern algorithm
$model = new Account();
$model->password_hash = '$2y$04$N0q8DaHzlYILCnLYrpZfEeWKEqkPZzbawiS07GbSr/.xbRNweSLU6'; // 12345678
$model->password_hash_strategy = Account::PASS_HASH_STRATEGY_YII2;
$this->assertTrue($model->validatePassword('12345678'), 'valid password should pass');
$this->assertFalse($model->validatePassword('87654321'), 'invalid password should fail');
}
public function testHasMojangUsernameCollision(): void {
$model = new Account();
$model->username = 'ErickSkrauch';
$this->assertFalse($model->hasMojangUsernameCollision());
$this->tester->haveFixtures([
'mojangUsernames' => MojangUsernameFixture::class,
]);
$this->assertTrue($model->hasMojangUsernameCollision());
}
public function testGetProfileLink(): void {
$model = new Account();
$model->id = 123;
$this->assertSame('http://ely.by/u123', $model->getProfileLink());
}
public function testIsAgreedWithActualRules(): void {
$model = new Account();
$this->assertFalse($model->isAgreedWithActualRules(), 'field is null');
$model->rules_agreement_version = 0;
$this->assertFalse($model->isAgreedWithActualRules(), 'actual version is greater than zero');
$model->rules_agreement_version = LATEST_RULES_VERSION;
$this->assertTrue($model->isAgreedWithActualRules());
}
public function testSetRegistrationIp(): void {
$account = new Account();
$account->setRegistrationIp('42.72.205.204');
$this->assertSame('42.72.205.204', inet_ntop($account->registration_ip));
$account->setRegistrationIp('2001:1620:28:1:b6f:8bca:93:a116');
$this->assertSame('2001:1620:28:1:b6f:8bca:93:a116', inet_ntop($account->registration_ip));
$account->setRegistrationIp(null);
$this->assertNull($account->registration_ip);
}
public function testGetRegistrationIp(): void {
$account = new Account();
$account->setRegistrationIp('42.72.205.204');
$this->assertSame('42.72.205.204', $account->getRegistrationIp());
$account->setRegistrationIp('2001:1620:28:1:b6f:8bca:93:a116');
$this->assertSame('2001:1620:28:1:b6f:8bca:93:a116', $account->getRegistrationIp());
$account->setRegistrationIp(null);
$this->assertNull($account->getRegistrationIp());
}
public function testAfterSaveInsertEvent(): void {
$account = new Account();
$account->afterSave(true, [
'username' => 'old-username',
]);
$this->assertNull($this->tester->grabLastQueuedJob());
}
public function testAfterSaveNotMeaningfulAttributes(): void {
$account = new Account();
$account->afterSave(false, [
'updatedAt' => time(),
]);
$this->assertNull($this->tester->grabLastQueuedJob());
}
public function testAfterSavePushEvent(): void {
$changedAttributes = [
'username' => 'old-username',
'email' => 'old-email@ely.by',
'uuid' => 'c3cc0121-fa87-4818-9c0e-4acb7f9a28c5',
'status' => 10,
'lang' => 'en',
];
$account = new Account();
$account->id = 123;
$account->afterSave(false, $changedAttributes);
/** @var CreateWebHooksDeliveries $job */
$job = $this->tester->grabLastQueuedJob();
$this->assertInstanceOf(CreateWebHooksDeliveries::class, $job);
/** @var AccountEditNotification $notification */
$notification = ReflectionHelper::readPrivateProperty($job, 'notification');
$this->assertInstanceOf(AccountEditNotification::class, $notification);
$this->assertSame(123, $notification->getPayloads()['id']);
$this->assertSame($changedAttributes, $notification->getPayloads()['changedAttributes']);
}
public function testAfterDeletePushEvent(): void {
$account = new Account();
$account->id = 1;
$account->status = Account::STATUS_REGISTERED;
$account->created_at = time() - 60 * 60 * 24;
$account->deleted_at = time();
$account->afterDelete();
$this->assertNull($this->tester->grabLastQueuedJob());
$account->status = Account::STATUS_ACTIVE;
$account->afterDelete();
/** @var CreateWebHooksDeliveries $job */
$job = $this->tester->grabLastQueuedJob();
$this->assertInstanceOf(CreateWebHooksDeliveries::class, $job);
/** @var AccountDeletedNotification $notification */
$notification = ReflectionHelper::readPrivateProperty($job, 'notification');
$this->assertInstanceOf(AccountDeletedNotification::class, $notification);
$this->assertSame(1, $notification->getPayloads()['id']);
}
}