1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-22 23:17:46 +00:00
Files
matomo/plugins/TwoFactorAuth/tests/Integration/Dao/RecoveryCodeDaoTest.php
Stefan Giehl d6d72d1fa7 [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps (#22144)
* [Coding Style] Enable rule PSR1.Methods.CamelCapsMethodName.NotCamelCaps

* [Coding Style] Use camel case for method names in API plugin tests (#22145)

* [Coding Style] Use camel case for method names in Core* plugin tests (#22147)

* [Coding Style] Use camel case for method names in core Unit tests (#22149)

* [Coding Style] Use camel case for method names in Actions and BulkTracking plugin tests (#22146)

* [Coding Style] Use camel case for method names in CustomDimensions and CustomJSTracker plugin tests (#22148)

* [Coding Style] Use camel case for method names in core Integration tests (#22151)

* [Coding Style] Use camel case for method names in more core plugin tests (#22153)

* [Coding Style] Use camel case for method names in more core plugin tests (#22157)

* [Coding Style] Use camel case for method names in more core plugin tests

* Update plugins/Monolog/tests/Unit/Processor/ExceptionToTextProcessorTest.php

Co-authored-by: Michal Kleiner <michal@innocraft.com>

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>

* [Coding Style] Use camel case for method names in more core plugin tests (#22159)

* [Coding Style] Use camel case for method names in remaining tests (#22160)

* [Coding Style] Use camel case for method names in remaining tests

* rename expected test files

---------

Co-authored-by: Michal Kleiner <michal@innocraft.com>
2024-04-25 20:57:55 +02:00

167 خطوط
6.6 KiB
PHP

<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\TwoFactorAuth\tests\Integration\Dao;
use Piwik\Container\StaticContainer;
use Piwik\DbHelper;
use Piwik\Plugins\TwoFactorAuth\Dao\RecoveryCodeDao;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* @group TwoFactorAuth
* @group RecoveryCodeDaoTest
* @group Plugins
*/
class RecoveryCodeDaoTest extends IntegrationTestCase
{
/**
* @var RecoveryCodeDao
*/
private $dao;
public function setUp(): void
{
parent::setUp();
$this->dao = StaticContainer::get(RecoveryCodeDao::class);
}
public function testShouldInstallTable()
{
$columns = DbHelper::getTableColumns($this->dao->getPrefixedTableName());
$columns = array_keys($columns);
$this->assertEquals(['idrecoverycode', 'login', 'recovery_code'], $columns);
}
public function testGetAllRecoveryCodesForLoginEmptyByDefault()
{
$this->assertEquals([], $this->dao->getAllRecoveryCodesForLogin('login1'));
}
public function testInsertRecoveryCodeGetAllRecoveryCodesForLogin()
{
$this->dao->insertRecoveryCode('login1', '123456');
$this->dao->insertRecoveryCode('login1', '654321');
$this->dao->insertRecoveryCode('login2', '333111');
$this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
$this->assertEquals(['333111'], $this->dao->getAllRecoveryCodesForLogin('login2'));
}
public function testDeleteRecoveryCode()
{
$this->insertManyCodesDifferentLogins();
$this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
$this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login2'));
$this->assertEquals(1, $this->dao->deleteRecoveryCode('login2', '654321')); // this one should be deleted
$this->assertEquals(0, $this->dao->deleteRecoveryCode('login2', 'xya123')); // cannot be found
$this->assertEquals(0, $this->dao->deleteRecoveryCode('login999', '123456')); // cannot be found
$this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
$this->assertEquals(['123456'], $this->dao->getAllRecoveryCodesForLogin('login2'));
$this->dao->deleteRecoveryCode('login2', '123456'); // delete last code for this login
$this->assertEquals([], $this->dao->getAllRecoveryCodesForLogin('login2'));
$this->assertEquals(0, $this->dao->deleteRecoveryCode('login2', '654321')); // cannot be deleted again
}
public function testDeleteAllRecoveryCodesForLogin()
{
$this->insertManyCodesDifferentLogins();
$this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
$this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login2'));
$this->dao->deleteAllRecoveryCodesForLogin('login2'); // this one should be deleted
$this->dao->deleteAllRecoveryCodesForLogin('login999'); // login cannot be found
$this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
$this->assertEquals([], $this->dao->getAllRecoveryCodesForLogin('login2'));
}
public function testUseRecoveryCode()
{
$this->insertManyCodesDifferentLogins();
$this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
$this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login2'));
$this->assertTrue($this->dao->useRecoveryCode('login2', '654321')); // this one should be used and deleted
$this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
$this->assertEquals(['123456'], $this->dao->getAllRecoveryCodesForLogin('login2'));
$this->assertFalse($this->dao->useRecoveryCode('login2', '654321')); // cannot be used again
$this->assertFalse($this->dao->useRecoveryCode('login2', 'xya123')); // cannot be found
$this->assertFalse($this->dao->useRecoveryCode('login999', '123456')); // cannot be found
$this->assertEquals(['123456', '654321'], $this->dao->getAllRecoveryCodesForLogin('login1'));
$this->assertEquals(['123456'], $this->dao->getAllRecoveryCodesForLogin('login2'));
$this->assertTrue($this->dao->useRecoveryCode('login2', '123456')); // cannot be used again
$this->assertEquals([], $this->dao->getAllRecoveryCodesForLogin('login2'));
}
public function testCreateRecoveryCodesForLogin()
{
$this->assertEquals([], $this->dao->getAllRecoveryCodesForLogin('login1'));
$this->dao->createRecoveryCodesForLogin('login1');
$codes1 = $this->dao->getAllRecoveryCodesForLogin('login1');
$this->assertCount(10, $codes1);
// generating new codes will remove the old codes
$this->dao->createRecoveryCodesForLogin('login1');
$codes2 = $this->dao->getAllRecoveryCodesForLogin('login1');
$this->assertCount(10, $codes2);
// not the same
$this->assertCount(10, array_diff($codes1, $codes2));
foreach ($codes1 as $code) {
// none of the old codes can be used
$this->assertFalse($this->dao->useRecoveryCode('login1', $code));
}
foreach ($codes2 as $code) {
// all new codes can be used
$this->assertTrue($this->dao->useRecoveryCode('login1', $code));
}
}
public function testCreateRecoveryCodesForLoginDifferentPerLogin()
{
$this->dao->createRecoveryCodesForLogin('login1');
$this->dao->createRecoveryCodesForLogin('login2');
$codes1 = $this->dao->getAllRecoveryCodesForLogin('login1');
$codes2 = $this->dao->getAllRecoveryCodesForLogin('login2');
// not the same
$this->assertCount(10, array_diff($codes1, $codes2));
foreach ($codes1 as $code) {
// all new codes can be used
$this->assertTrue($this->dao->useRecoveryCode('login1', $code));
}
foreach ($codes2 as $code) {
// all new codes can be used
$this->assertTrue($this->dao->useRecoveryCode('login2', $code));
}
}
private function insertManyCodesDifferentLogins()
{
$this->dao->insertRecoveryCode('login1', '123456');
$this->dao->insertRecoveryCode('login1', '654321');
$this->dao->insertRecoveryCode('login2', '123456');
$this->dao->insertRecoveryCode('login2', '654321');
}
}