maison_alexandra/tests/UnitTests/App/PubUserManager/pubUserManagerBaseTest.php

745 lines
26 KiB
PHP

<?php
namespace ElixirAudit\Tests\UnitTests\App\PubUserManager;
use BRSystems\Core\Library\cGoogle2FA\cGoogle2FA;
use Exception;
use BRSystems\Core\App\pubUserManager\pubUserManager;
use ElixirAudit\Tests\BasePhpUnitCase;
use ElixirAudit\Tests\DataSetup\Constants\SiteMap;
use ElixirAudit\Tests\DataSetup\Constants\siteUsers;
class pubUserManagerBaseTest extends BasePhpUnitCase
{
private pubUserManager $pubUserManager;
private array $newUserCfg = [
'action' => 'selfSave',
'fields' =>
'{' .
'"pbase":{"u_login":"test@test.lv","u_email":"test@test.lv","u_name":"Test lietotajs","u_surname":"Tests uzvārds","u_phone":"132465789"},' .
'"companies":{"c23":1,"c148":0,"c155":0,"c168":0},' .
'"user_groups":{"g119":1,"g89":1,"g107":0,"g106":0,"g116":0,"g95":0,"g112":0,"g87":0,"g92":0,"g115":0,"g88":0,"g72":0,"g117":0,"g111":0},' .
'"user_permissions":{"g512":1},' .
'"psettings":{"u_blocked":0,"bFullScreen":1,"bUseDarkMode":0,"bShowBtnLabels":1,"u_subscription":1,"u_send_data_mail":1},' .
'"img":""}'
];
protected function setUp(): void
{
// Init base structure
$this->unitTestsAreRunning = true;
$this->resetGetAndPost();
parent::setUp();
$this->reloadDbTables(['mod_users']);
// Create new class
$this->pubUserManager = $this->initBaseClassObjectBySiteMapId(SiteMap::PUB_USER_MANAGER);
}
protected function tearDown(): void
{
$this->reloadDbTables(['mod_users', 'mod_users_permissions']);
$this->logOutUnitTestUser();
parent::tearDown();
}
public function testBaseCase(): void
{
// Default form generation
ob_start();
$this->pubUserManager->run();
$pageHTML = ob_get_contents();
ob_end_clean();
$this->assertExpected(
[
'<title>Zutenis | Lietotāju pārvaldība</title>',
'<div class="table_header_info"></div>',
'<div id="cms-table-drop-marker">'
],
$pageHTML
);
}
public function testBaseCaseActionEdit(): void
{
$this->setPost(['action' => 'edit', 'id' => 12]);
// Default form generation
ob_start();
$this->pubUserManager->run();
$pageHTML = ob_get_contents();
ob_end_clean();
$this->assertExpected(
[
'<title>Zutenis | Lietotāju pārvaldība</title>',
'<div class="table_header_info"></div>',
'<div id="cms-table-drop-marker">'
],
$pageHTML
);
}
public function testBaseCaseActionFilterTable(): void
{
$this->addExtraRecordInTableAndResetPost();
// Filter data table
$this->setPost([
'action' => 'filter_table',
'init' => 1,
'filter' => '{"created_by":"","id":"","user_name":"","user_login":"","user_email":"test","status":""}'
]);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['"jsonok":true'], $error->getMessage());
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertExpected(
[
'<div class="table-rows">',
'<div class="sticky-header-top"></div>',
'<div title="Vārds">Vārds</div>',
'<div title="Lietotājvārds">Lietotājvārds</div>',
'<div title="E-pasts">E-pasts</div>',
'<div title="Tālrunis">Tālrunis</div>',
'<div title="Rediģēts">Rediģēts</div>',
'<a class="tbl-settings">Darbības</a>',
],
$response['data']
);
$this->assertTrue(isset($response['pcount']));
$this->assertTrue(isset($response['rlimit']));
$this->assertTrue(isset($response['header']));
}
}
public function testBaseCaseActionSelfLoad(): void
{
$this->setPost([
'action' => 'selfLoad'
]);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['"jsonok":true'], $error->getMessage());
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['show']));
$this->assertTrue(isset($response['btn']));
$this->assertTrue(isset($response['html']));
$this->assertExpected(
[
'<li class="breadcrumb-item"><a class="fables-second-text-color tback">Lietotāju pārvaldība</a></li>',
'<div id="pbase" class="inner-cnt sbox">',
'<label for="u_email">E-pasts:</label>',
'<input id="u_email" data-validator="u_email" type="text" class="long" value=""/>',
'<div id="user_groups" class="settings sbox">',
'<div id="user_permissions" class="settings sbox">',
'<div id="psettings" class="settings sbox hide">',
],
$response['html']
);
}
$this->setPost([
'id' => 23,
'action' => 'selfLoad',
]);
// Error accessing other user
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(
['{"error":true,"msg":"User doesn\'t exists or you cannot edit this user!"}'],
$error->getMessage()
);
}
// Create user for load
$newUserCfg = $this->newUserCfg;
$newUserCfg['fields'] = base64_encode($newUserCfg['fields']);
$this->setPost($newUserCfg);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['"jsonok":true'], $error->getMessage());
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertIsInt($response['newid']);
}
// Load created user
$this->setPost([
'action' => 'selfLoad',
'id' => $response['newid'],
]);
$prevResponse = $response;
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['"jsonok":true'], $error->getMessage());
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['show']));
$this->assertTrue(isset($response['btn']));
$this->assertTrue(isset($response['html']));
$this->assertExpected(
[
'<input type="hidden" value="' . $prevResponse['newid'] . '" id="id" data-validator="id"/>',
'<li class="breadcrumb-item"><a class="fables-second-text-color tback">Lietotāju pārvaldība</a></li>',
'<div id="pbase" class="inner-cnt sbox">',
'<label for="u_login">Lietotājvārds:</label>',
'<input id="u_login" data-validator="u_login" type="text" class="normal" disabled="" value="test@test.lv" disabled="disabled" />',
'<label for="u_email">E-pasts:</label>',
'<input id="u_email" data-validator="u_email" type="text" class="long" value="test@test.lv"/>',
'<div id="user_groups" class="settings sbox">',
'<div id="user_permissions" class="settings sbox">',
'<div id="psettings" class="settings sbox">',
],
$response['html']
);
}
}
public function testBaseCaseActionSelfSave(): void
{
$newUserCfg = $this->newUserCfg;
$newUserCfg['fields'] = base64_encode($newUserCfg['fields']);
$this->setPost($newUserCfg);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['"jsonok":true'], $error->getMessage());
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['jsonok']));
$this->assertTrue(isset($response['newid']));
$this->assertIsInt($response['newid']);
}
// Check error user already exists with this email
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(
['{"error":true,"data":{"u_email":"User login already exists!"}}'],
$error->getMessage()
);
}
// Self save existing user
$newUserCfg['id'] = $response['newid'];
$this->setPost($newUserCfg);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['"jsonok":true'], $error->getMessage());
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['jsonok']));
$this->assertFalse(isset($response['newid']));
}
}
public function testBaseCaseActionChangeRecordsStatus(): void
{
$newId = $this->addExtraRecordInTableAndResetPost();
$this->setPost(
[
'action' => 'changeRecordsStatus',
'baseField' => 'h_status',
'fields' => '{"1":"' . $newId . '"}'
]
);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['{"jsonok":true,"data":true}'], $error->getMessage());
}
// Error case
// Try to disable self - user admin
$this->setPost(
[
'action' => 'changeRecordsStatus',
'baseField' => 'h_status',
'fields' => '{"1":"12"}'
]
);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['{"error":true,"data":"Something went wrong while updating user(s)!"}'], $error->getMessage());
}
// Prevent self Disable
$this->setPost(
[
'action' => 'changeRecordsStatus',
'baseField' => 'h_status',
'fields' => '{"1":"' . $newId . '","2":"12"}'
]
);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['{"jsonok":true,"data":true}'], $error->getMessage());
}
}
public function testBaseCaseActionDeleteRecords(): void
{
$newId = $this->addExtraRecordInTableAndResetPost();
$this->setPost(
[
'action' => 'deleteRecords',
'fields' => '{"1":"' . $newId . '"}'
]
);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['{"jsonok":true,"data":true}'], $error->getMessage());
}
// Error case
// Try to delete self - user admin
$this->setPost(
[
'action' => 'deleteRecords',
'fields' => '{"1":"12"}'
]
);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['{"error":true,"msg":"Something went wrong while deleting user(s)!"}'], $error->getMessage());
}
// Prevent self delete 2
$this->reloadDbTables(['mod_users']);
$newId = $this->addExtraRecordInTableAndResetPost();
$this->setPost(
[
'action' => 'deleteRecords',
'fields' => '{"1":"' . $newId . '","2":"12"}'
]
);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['{"jsonok":true,"data":true}'], $error->getMessage());
}
}
public function testBaseCaseActionISendRecoveryMail(): void
{
$this->setPost(['action' => 'iSendRecoveryMail']);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['{"jsonok":true,"data":false}'], $error->getMessage());
}
}
public function testBaseCaseActionTablesCfg(): void
{
$loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->setPost(['action' => 'tablescfg', 'id' => $loginData['id']]);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['jsonok']));
$this->assertTrue(isset($response['html']));
$this->assertExpected(
['<ul class="dx tfields sbox" id="pubLursoftDataReq">'],
$response['html']
);
}
}
public function testBaseCaseActionPropSave(): void
{
$this->setPost(
[
'action' => 'propsave',
'prop' => '{"c0":["partner-status","earnings-status"],"b1":["chart-income-base","chart-expanses-categories"' .
',"chart-3","chart-4"],"b2":["subscription-status","system-new-update-log","quick-view-calendar","recent-activity-log"]}'
]
);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['{"jsonok":true,"data":true}'], $error->getMessage());
}
}
public function testBaseCaseActionCompanies(): void
{
$loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->setPost(['action' => 'companies', 'id' => $loginData['id']]);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['jsonok']));
$this->assertTrue(isset($response['html']));
$this->assertExpected(
[
'<input type="text" id="gr-search" class="skip long" />',
'<label for="gr-search">Meklēt kompānijās...</label>',
'<div id="c23" data-validator="c23" class="chk chked"><div></div></div>',
'<label>Mareks Keišs pašn.:</label>',
'<label>Testa Reliģiska organizācija:</label>',
'<label>Testa Biedrība:</label>',
],
$response['html']
);
}
}
public function testBaseCaseActionActivate2FA(): void
{
$oGoogle2FA = new cGoogle2FA();
$loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->setPost([
'action' => 'activate-2FA',
'secret' => $secret = $oGoogle2FA->getNewGoogleAuthSecret(),
'code' => $oGoogle2FA->getGoogleAuthCode($secret)
]);
try {
$this->pubUserManager->controller->selUserId = $loginData['id'];
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['jsonok']));
$this->assertTrue(isset($response['url']));
$this->assertEquals('/gramatvedibas-sistema/sistemas-informacijas-panelis', $response['url']);
}
// Error case
$this->setPost([
'action' => 'activate-2FA',
'secret' => 'KDSALKDSDNNV',
'code' => 123123
]);
try {
$this->pubUserManager->controller->selUserId = $loginData['id'];
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertEquals(
'{"error":true,"data":{"code":"Ievad\u012bts k\u013c\u016bdains 2FA iestat\u012b\u0161anas dro\u0161\u012bbas kods!"}}',
$error->getMessage()
);
}
}
public function testBaseCaseActionDeactivate2FA(): void
{
$oGoogle2FA = new cGoogle2FA();
$loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->setPost([
'action' => 'deactivate-2FA',
'code' => $oGoogle2FA->getGoogleAuthCode($loginData['2fa'])
]);
try {
$this->pubUserManager->controller->selUserId = $loginData['id'];
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['jsonok']));
}
// Error case
$this->setPost([
'action' => 'deactivate-2FA',
'secret' => 'KDSALKDSDNNV',
'code' => 123123
]);
try {
$this->pubUserManager->controller->selUserId = $loginData['id'];
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertEquals(
'{"error":true,"data":{"code":"Login 2FA Security removal code input error!"}}',
$error->getMessage()
);
}
}
public function testBaseCaseActionGet2FAQRCode(): void
{
$loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->setPost(['action' => 'get2FA-QRCode']);
try {
$this->pubUserManager->controller->selUserId = $loginData['id'];
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['jsonok']));
$this->assertTrue(isset($response['html']));
$this->assertTrue(isset($response['msgEnableAuth']));
$this->assertTrue(isset($response['buttonYes']));
$this->assertTrue(isset($response['buttonCancel']));
$this->assertTrue(isset($response['buttonActivate']));
$this->assertExpected(
[
'<p>Pierakstiet savu slepeno atslēgu un glabājiet to droši.<br/>',
'Tā būs nepieciešama, lai atgūtu 2FA, nozaudēšanas gadījumā.</p>',
'<input id="code" value="" data-validator="code" autocomplete="off"/>',
],
$response['html']
);
}
// With reload
$this->setPost([
'action' => 'get2FA-QRCode',
'reload' => true
]);
try {
$this->pubUserManager->controller->selUserId = $loginData['id'];
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['jsonok']));
$this->assertTrue(isset($response['sUrl']));
$this->assertTrue(isset($response['sSecret']));
$this->assertTrue(strlen($response['sSecret']) > 0);
}
}
public function testBaseCaseActionGet2FA4Deactivate(): void
{
$loginData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed);
$this->setPost(['action' => 'get2FA-4Deactivate']);
try {
$this->pubUserManager->controller->selUserId = $loginData['id'];
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['jsonok']));
$this->assertTrue(isset($response['html']));
$this->assertTrue(isset($response['msgDisableAuth']));
$this->assertTrue(isset($response['buttonDeactivate']));
$this->assertExpected(
[
'<p style="margin-bottom: 14px;">Ievadiet kodu, lai deaktivizētu 2FA</p>',
'<p>Ja atslēgsiet 2FA autentifikāciju, daži moduļi var nebūt pieejami!</p>',
],
$response['html']
);
}
}
public function testBaseCaseActionGenRandomPwd(): void
{
$this->setPost(['action' => 'genRandomPwd']);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['"jsonok":true,"key":"'], $error->getMessage());
}
}
public function testBaseCaseActionGetTableSettings(): void
{
$this->setPost(['action' => 'getTableSettings', 'mod' => 'pubUserManager']);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertTrue(isset($response['jsonok']));
$this->assertTrue(isset($response['vCfg']));
$this->assertTrue(isset($response['mod_title']));
$this->assertTrue(isset($response['save_data']));
$this->assertTrue(isset($response['body']));
$this->assertExpected(
[
'<div id="custom-sticky-status-cnt-top"></div>',
'<h4 class="no-border">Pielāgojiet tabulas kolonnas</h4>',
'<h4 class="no-border">Tablulas rindas</h4>',
'<h4 class="no-border">Atiestatīt izkārtojumu</h4>',
],
$response['body']
);
}
}
public function testBaseCaseActionSaveTableSettings(): void
{
$this->setPost([
'action' => 'saveTableSettings',
'mod' => 'pubUserManager',
'tbl' => '{"pubUserManager":["h_created_by","h_user_name","h_id","h_user_login","h_user_email","h_user_phone' .
'","h_user_last_ip","h_user_last_login","h_user_modified","h_user_created","h_status"],"base_only":true}',
'data' => '{"a_rec_per_page":"25"}'
]);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(
['{"jsonok":true,"data":true}'],
$error->getMessage()
);
}
}
public function testBaseCaseActionResetTableSettings(): void
{
$this->setPost([
'action' => 'resetTableSettings',
'mod' => 'pubUserManager'
]);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(
['{"jsonok":true,"data":true}'],
$error->getMessage()
);
}
}
private function addExtraRecordInTableAndResetPost(): int
{
// Create user for load
$newUserCfg = $this->newUserCfg;
$newUserCfg['fields'] = str_replace(
'"u_login":"test@test.lv"',
'"u_login":"extra.record@test.lv"',
$newUserCfg['fields']
);
$newUserCfg['fields'] = base64_encode($newUserCfg['fields']);
$this->setPost($newUserCfg);
try {
$this->pubUserManager->run();
$this->fail("Exception expected!");
} catch (Exception $error) {
$this->assertExpected(['"jsonok":true'], $error->getMessage());
// get JSON response
$response = json_decode($error->getMessage(), true);
$this->assertIsInt($response['newid']);
}
$this->resetGetAndPost();
return $response['newid'];
}
public function testGetUploadConfig(): void
{
$this->pubUserManager = new pubUserManager(getFirstMatchByMapId(SiteMap::PUB_USER_MANAGER));
$data = $this->pubUserManager->controller->getUploadConfig();
$this->assertIsArray($data);
}
public function testGetFileUploadConfig(): void
{
$this->pubUserManager = new pubUserManager(getFirstMatchByMapId(SiteMap::PUB_USER_MANAGER));
$data = $this->pubUserManager->controller->getFileUploadConfig();
$this->assertIsArray($data);
}
}