'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( [ 'Zutenis | Lietotāju pārvaldība', '
', '
' ], $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( [ 'Zutenis | Lietotāju pārvaldība', '
', '
' ], $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( [ '
', '
', '
Vārds
', '
Lietotājvārds
', '
E-pasts
', '
Tālrunis
', '
Rediģēts
', 'Darbības', ], $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( [ '', '
', '', '', '
', '
', '
', ], $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( [ '', '', '
', '', '', '', '', '
', '
', '
', ], $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( ['
    '], $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( [ '', '', '
    ', '', '', '', ], $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( [ '

    Pierakstiet savu slepeno atslēgu un glabājiet to droši.
    ', 'Tā būs nepieciešama, lai atgūtu 2FA, nozaudēšanas gadījumā.

    ', '', ], $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( [ '

    Ievadiet kodu, lai deaktivizētu 2FA

    ', '

    Ja atslēgsiet 2FA autentifikāciju, daži moduļi var nebūt pieejami!

    ', ], $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( [ '
    ', '

    Pielāgojiet tabulas kolonnas

    ', '

    Tablulas rindas

    ', '

    Atiestatīt izkārtojumu

    ', ], $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); } }