unitTestsAreRunning = true; parent::setUp(); $this->clearDbState(); // Set force pass change param $this->cDb->_queryUpdate('mod_users', ['user_fchange' => 1], ['id' => 12]); // Create new class $this->users = $this->initBaseClassObjectBySiteMapId(SiteMap::PUB_USER_PASS_CHANGE, null); $this->loginUserBeforePassChangeRedirect(); } protected function tearDown(): void { $this->clearDbState(); parent::tearDown(); } protected function clearDbState(): void { $this->logOutUnitTestUser(); $this->resetGetAndPost(); $this->reloadDbTables(['mod_users', 'mod_account_manager']); } public function passwordForceChangeActionLoginRequirement(): void { $this->testModuleLoginRequirement(SiteMap::PUB_USER_PASS_CHANGE); } public function testForcePasswordChangeActionDefaultAction(): void { $this->setPost([ 'action' => 'test' ]); // Default form generation ob_start(); $this->users->passwordForceChangeAction(); $pageHTML = ob_get_contents(); ob_end_clean(); $this->assertExpected( [ 'Zutenis | Paroles maiņa', 'Mainīt lietotāja paroli', 'getLoginCredentialsByType(siteUsers::loginAppSelfEmployed); $this->setPost([ 'action' => 'pwd', 'user_name' => '', 'old_password' => $userData['password'], 'new_password' => 'asdasdasd', ]); try { $this->users->passwordForceChangeAction(); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected(['{"error":true,"data":{"new_password":"Lauki nesakr\u012bt!"}}'], $error->getMessage()); } } public function testForcePasswordChangeActionDefaultActionWithPostTestCase2(): void { $this->setPost([ 'action' => 'pwd', 'user_name' => '', 'old_password' => 'asd', 'new_password' => 'asdasdasd', 'rnew_password' => 'asdasdasd', ]); try { $this->users->passwordForceChangeAction(); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected( ['{"error":true,"data":{"msg":"Old user password doesn\'t match!"'], $error->getMessage() ); } } public function testForcePasswordChangeActionDefaultActionWithPostTestCase3(): void { // Update pass time $this->cDb->_queryUpdate( 'mod_users', [ 'user_newpwd' => '$2y$11$N0OyJI/y1gXRsw1eKhU.yu6xZnjlcrASTVIdObLfYI3VsnUnHkBuO', 'user_pwdtime' => time() + 2 ], ['id' => 12] ); // Create new class $this->users = $this->initBaseClassObjectBySiteMapId(SiteMap::PUB_USER_PASS_CHANGE, null); $this->loginUserBeforePassChangeRedirect('baTQCVRQpp'); sleep(5); $this->setPost([ 'action' => 'pwd', 'user_name' => '', 'old_password' => 'baTQCVRQpp', 'new_password' => 'asdasdasd', 'rnew_password' => 'asdasdasd', ]); try { $this->users->passwordForceChangeAction(); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected(['{"error":true,"url":"\/ienakt-gramatvedibas-sistema"}'], $error->getMessage()); } } public function testForcePasswordChangeActionDefaultActionWithPostTestCase4(): void { $userData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed); $this->setPost([ 'action' => 'pwd', 'user_name' => '', 'old_password' => $userData['password'], 'new_password' => $userData['password'], 'rnew_password' => $userData['password'], ]); try { $this->users->passwordForceChangeAction(); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected( ['{"error":true,"data":{"msg":"Jaun\u0101 lietot\u0101ja parole nevar sakrist ar veco!"'], $error->getMessage() ); } } public function testForcePasswordChangeActionDefaultActionWithPostTestCase5(): void { $userData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed); $this->setPost([ 'action' => 'pwd', 'user_name' => '', 'old_password' => $userData['password'], 'new_password' => 'asdasdasd123', 'rnew_password' => 'asdasdasd123', ]); try { $this->users->passwordForceChangeAction(); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected( ['{"jsonok":true,"url":"\/gramatvedibas-sistema\/konta-izvele"}'], $error->getMessage() ); } } public function testForcePasswordChangeActionDefaultActionWithPostTestCase6(): void { // Force user logout after module setup & login $this->logOutUnitTestUser(); // Reset force pass change param $this->cDb->_queryUpdate('mod_users', ['user_fchange' => '0'], ['id' => 12]); // Create new class $this->users = $this->initBaseClassObjectBySiteMapId(SiteMap::PUB_USER_PASS_CHANGE); try { $this->users->passwordForceChangeAction(); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected(['Redirected to Location: /ienakt-gramatvedibas-sistema'], $error->getMessage()); } } private function loginUserBeforePassChangeRedirect(string $tempPassword = null): void { $this->pubUserLogin = new pubUsers(getFirstMatchByMapId(SiteMap::PUB_LOGIN)); $userData = $this->getLoginCredentialsByType(siteUsers::loginAppSelfEmployed); // Log in & catch redirect exception try { $this->pubUserLogin->controller->loginUser( [ 'chkTerms' => 1, 'user_login' => $userData['user'], 'user_password' => $tempPassword ?: $userData['password'], ] ); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected(['Redirected to Location'], $error->getMessage()); } // Submit 2fa security & catch json send data exception try { $oGoogle2FA = new cGoogle2FA(); $next2FACode = $oGoogle2FA->getGoogleAuthCode($userData['2fa']); $_POST['code'] = $next2FACode; $this->pubUserLogin->controller->set2FAForm(); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected( ['Redirected to Location: /gramatvedibas-sistema/paroles-maina'], $error->getMessage() ); } } }