unitTestsAreRunning = true; $this->resetGetAndPost(); parent::setUp(); $this->clearDbState(); // Create new class $this->users = $this->initBaseClassObjectBySiteMapId(SiteMap::PUB_USER_PASS_RECOVERY, null); } protected function tearDown(): void { $this->clearDbState(); parent::tearDown(); } protected function clearDbState(): void { $this->clearMailHogMessages(); $this->reloadDbTables(['mod_users']); } public function testPasswordRecoveryActionDefaultAction(): void { // Default form generation ob_start(); $this->users->passwordRecoveryAction(); $pageHTML = ob_get_contents(); ob_end_clean(); $this->assertExpected( [ 'Zutenis | Paroles atgūšana', '>Paroles atgūšana

', '

Lūdzu ievadiet lietotāja e-pastu vai lietotājvārdu, kuram vēlieties atgūt paroli.

', '
', 'getMessage() ); } } public function testPasswordRecoveryActionWithPostTestCase2(): void { $this->setPost([ 'action' => 'recovery', 'user_data' => 'asdasd', ]); try { $this->users->passwordRecoveryAction(); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected( ['{"error":true,"msg":"Lietot\u0101js nav atrasts vai ir blo\u0137\u0113ts!"}'], $error->getMessage() ); } } public function testPasswordRecoveryActionWithPostTestCase3(): void { $this->setPost([ 'action' => 'recovery', 'user_data' => 'testmareks' ]); try { $this->users->passwordRecoveryAction(); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected( ['{"jsonok":true,"msg":"E-pasts tika nos\u016bt\u012bts"}'], $error->getMessage() ); } // Wait for mail to be sent - otherwise it messes with the secondary mail sent // in call function testPasswordRecoveryActionWithLinkFromEmail - which gets wrong mail for process $this->awaitEmailFromMailHog( 'keiss.mareks@gmail.com', 'Konts - Paroles atjaunošana', 60 * 5, true ); } public function testPasswordRecoveryActionWithPostTestCase4(): void { // Set double email link $this->cDb->_queryUpdate('mod_users', ['user_email' => 'keiss.mareks@gmail.com'], ['id' => 14]); $this->setPost([ 'action' => 'recovery', 'user_data' => 'keiss.mareks@gmail.com' ]); try { $this->users->passwordRecoveryAction(); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected( ['{"error":true,"msg":"There are more then one user registered to this email,'], $error->getMessage() ); } } public function testPasswordRecoveryActionWithLinkFromEmail(): void { // Force user logout if active login pending $this->logOutUnitTestUser(); $this->setPost([ 'action' => 'recovery', 'user_data' => 'testmareks' ]); try { $this->users->passwordRecoveryAction(); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected( ['{"jsonok":true,"msg":"E-pasts tika nos\u016bt\u012bts"}'], $error->getMessage() ); } // Destroy session $this->destroyUserSession(); $this->resetGetAndPost(); // Fetch email from MailHog $messages = $this->awaitEmailFromMailHog( 'keiss.mareks@gmail.com', 'Konts - Paroles atjaunošana', 60 * 5, true ); // Validate mail content $testData = pubUserPwdResetTest::url[siteLanguages::LANG_LV]; $this->assertEquals($testData['email-subject'], $this->emailHtmlDecode($messages->subject)); $this->assertExpected($testData['email-expected-body'], $this->emailHtmlDecode($messages->body)); // Get password reset url from mail $doc = new DOMDocument(); $doc->loadHTML($messages->body); $xpath = new DOMXpath($doc); $activationUrl = $xpath->query("//a[contains(., '{$testData['email-a']}')]") ->item(0)->attributes->item(0)->nodeValue; $urlData = explode('/pwd:', $activationUrl); $this->setGet([ 'pwd' => $urlData[1] ]); try { // Catch output - flaky test, fails from time to time ob_start(); $this->users->loginAction(); $pageHTML = ob_get_contents(); ob_end_clean(); $this->fail('Output generated! Exception Expected!'); } catch (Exception $error) { ob_end_clean(); $this->assertExpected( ['Redirected to Location: /ienakt-gramatvedibas-sistema/divi-faktori'], $error->getMessage() ); } } public function testPasswordRecoveryActionWithLinkFromEmailErrorCase(): void { // Force user logout if active login pending $this->logOutUnitTestUser(); $this->setPost([ 'action' => 'recovery', 'user_data' => 'testmareks' ]); try { $this->users->passwordRecoveryAction(); $this->fail('Exception Expected!'); } catch (Exception $error) { $this->assertExpected( ['{"jsonok":true,"msg":"E-pasts tika nos\u016bt\u012bts"}'], $error->getMessage() ); } // Set double email link $this->cDb->_queryUpdate('mod_users', ['user_pwdtime' => time() - 100], ['id' => 12]); // Fetch email from MailHog $messages = $this->awaitEmailFromMailHog( 'keiss.mareks@gmail.com', 'Konts - Paroles atjaunošana', 60 * 5, true ); // Validate mail content $testData = pubUserPwdResetTest::url[siteLanguages::LANG_LV]; $this->assertEquals($testData['email-subject'], $this->emailHtmlDecode($messages->subject)); $this->assertExpected($testData['email-expected-body'], $this->emailHtmlDecode($messages->body)); // Get password reset url from mail $doc = new DOMDocument(); $doc->loadHTML($messages->body); $xpath = new DOMXpath($doc); $activationUrl = $xpath->query("//a[contains(., '{$testData['email-a']}')]") ->item(0)->attributes->item(0)->nodeValue; $this->resetGetAndPost(); $urlData = explode('/pwd:', $activationUrl); $this->setGet([ 'pwd' => $urlData[1] ]); ob_start(); $this->users->loginAction(); $pageHTML = ob_get_contents(); ob_end_clean(); $this->assertExpected( [ '
Lietotāja paroles atgūšana neizdevās!', '
Lūdzu, mēģiniet vēlreiz atiestatīt paroli,', '
ja ziņojums atkārtojas, lūdzu, sazinieties ar atbalsta dienestu!
', ], $pageHTML ); } }