Skip to content

Commit

Permalink
fix: e2e fix user add and patient add (openemr#7779)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradymiller authored Oct 25, 2024
1 parent 13d1752 commit 66875f3
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 41 deletions.
19 changes: 19 additions & 0 deletions tests/Tests/E2e/Base/BaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ private function goToUserMenuLink(string $menuTreeIcon): void
$this->crawler->filterXPath($menuLink2)->click();
}

private function isUserExist(string $username): bool
{
$usernameDatabase = sqlQuery("SELECT `username` FROM `users` WHERE `username` = ?", [$username]);
if (($usernameDatabase['username'] ?? '') == $username) {
return true;
} else {
return false;
}
}

private function isPatientExist(string $firstname, string $lastname, string $dob, string $sex): bool
{
$patientDatabase = sqlQuery("SELECT `fname` FROM `patient_data` WHERE `fname` = ? AND `lname` = ? AND `DOB` = ? AND `sex` = ?", [$firstname, $lastname, $dob, $sex]);
Expand All @@ -162,4 +172,13 @@ private function isEncounterExist(string $firstname, string $lastname, string $d
return false;
}
}

private function logOut(): void
{
$this->client->switchTo()->defaultContent();
$this->goToUserMenuLink('fa-sign-out-alt');
$this->client->waitFor('//input[@id="authUser"]');
$title = $this->client->getTitle();
$this->assertSame('OpenEMR Login', $title, 'Logout FAILED');
}
}
6 changes: 2 additions & 4 deletions tests/Tests/E2e/GgUserMenuLinksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ public function testUserMenuLink(string $menuTreeIcon, string $menuLinkItem, str
$this->base();
try {
$this->login(LoginTestData::username, LoginTestData::password);
$this->goToUserMenuLink($menuTreeIcon);
if ($menuLinkItem == 'Logout') {
// special case for Logout
$this->client->waitFor('//input[@id="authUser"]');
$title = $this->client->getTitle();
$this->assertSame('OpenEMR Login', $title, 'Logout FAILED');
$this->logOut();
} else {
$this->goToUserMenuLink($menuTreeIcon);
$this->assertActiveTab($expectedTabTitle);
}
} catch (\Throwable $e) {
Expand Down
52 changes: 43 additions & 9 deletions tests/Tests/E2e/Patient/PatientAddTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
use OpenEMR\Tests\E2e\Patient\PatientTestData;
use OpenEMR\Tests\E2e\Xpaths\XpathsConstants;
use OpenEMR\Tests\E2e\Xpaths\XpathsConstantsPatientAddTrait;
use PHPUnit\Framework\ExpectationFailedException;

trait PatientAddTrait
{
use BaseTrait;
use LoginTrait;

private int $patientAddAttemptCounter = 1;

/**
* @depends testLoginAuthorized
*/
Expand Down Expand Up @@ -85,31 +88,62 @@ private function patientAddIfNotExist(string $firstname, string $lastname, strin
);
$this->crawler = $this->client->refreshCrawler();
$this->crawler->filterXPath(XpathsConstantsPatientAddTrait::CREATE_CONFIRM_PATIENT_BUTTON_PATIENTADD_TRAIT)->click();
$this->client->switchTo()->defaultContent();
// Note had to use the lower level webdriver directly to ensure iframe has gone away
$this->client->getWebDriver()->wait(10)->until(
WebDriverExpectedCondition::invisibilityOfElementLocated(WebDriverBy::xpath(XpathsConstantsPatientAddTrait::NEW_PATIENT_IFRAME_PATIENTADD_TRAIT))
);
} else {
// Fallback for older versions prior to PHP 8.3
// For some reason, the click is not working like it should in PHP versions less than 8.3, so going to bypass the confirmation screen
$this->crawler = $this->client->submit($newPatient);
}
$this->client->switchTo()->defaultContent();
// assert the new patient is in the database
$this->assertPatientInDatabase($firstname, $lastname, $dob, $sex);
// Note using lower level webdriver directly since seems like a more simple and more consistent way to check for the alert
$alert = $this->client->getWebDriver()->wait(10)->until(
WebDriverExpectedCondition::alertIsPresent()
);
$alert->accept();

// ensure the patient summary screen is shown
$this->client->switchTo()->defaultContent();
$this->client->waitFor(XpathsConstants::PATIENT_IFRAME);
$this->switchToIFrame(XpathsConstants::PATIENT_IFRAME);
// below line will timeout if did not go to the patient summary screen for the new patient
$this->client->waitFor('//*[text()="Medical Record Dashboard - ' . $firstname . " " . $lastname . '"]');
}

// ensure the patient was added
$this->assertTrue($this->isPatientExist($firstname, $lastname, $dob, $sex), 'New patient is not in database, so FAILED');
private function assertPatientInDatabase(string $firstname, string $lastname, string $dob, string $sex): void
{
// assert the new patient is in the database (if this fails, then will try patientAddIfNotExist() up to
// 3 times total before failing)
try {
$this->innerAssertPatientInDatabase($firstname, $lastname, $dob, $sex);
} catch (ExpectationFailedException $e) {
if ($this->patientAddAttemptCounter > 2) {
// re-throw since have failed 3 tries
throw $e;
} else {
// try again since not yet 3 tries
$this->patientAddAttemptCounter++;
echo "TRY " . ($this->patientAddAttemptCounter) . " of 3 to add new patient to database";
$this->logOut();
$this->patientAddIfNotExist($firstname, $lastname, $dob, $sex);
}
}
}

private function innerAssertPatientInDatabase(string $firstname, string $lastname, string $dob, string $sex): void
{
// assert the new patient is in the database (check 3 times with 5 second delay prior each check to
// ensure allow enough time)
$patientExistDatabase = false;
$counter = 0;
while (!$patientExistDatabase && $counter < 3) {
if ($counter > 0) {
echo "TRY " . ($counter + 1) . " of 3 to see if new patient is in database";
}
sleep(5);
if ($this->isPatientExist($firstname, $lastname, $dob, $sex)) {
$patientExistDatabase = true;
}
$counter++;
}
$this->assertTrue($patientExistDatabase, 'New patient is not in database, so FAILED');
}
}
68 changes: 41 additions & 27 deletions tests/Tests/E2e/User/UserAddTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@
use OpenEMR\Tests\E2e\User\UserTestData;
use OpenEMR\Tests\E2e\Xpaths\XpathsConstants;
use OpenEMR\Tests\E2e\Xpaths\XpathsConstantsUserAddTrait;
use PHPUnit\Framework\ExpectationFailedException;

trait UserAddTrait
{
use BaseTrait;
use LoginTrait;

private int $userAddAttemptCounter = 1;

/**
* @depends testLoginAuthorized
*/
Expand All @@ -49,8 +52,7 @@ public function testUserAdd(): void
private function userAddIfNotExist(string $username): void
{
// if user already exists, then skip this
$usernameDatabase = sqlQuery("SELECT `username` FROM `users` WHERE `username` = ?", [$username]);
if (!empty($usernameDatabase['username']) && ($usernameDatabase['username'] == $username)) {
if ($this->isUserExist($username)) {
$this->markTestSkipped('New user test skipped because this user already exists.');
}

Expand All @@ -74,13 +76,45 @@ private function userAddIfNotExist(string $username): void
$this->crawler = $this->client->refreshCrawler();
$newUser = $this->crawler->filterXPath(XpathsConstantsUserAddTrait::NEW_USER_BUTTON_USERADD_TRAIT)->form();
$newUser['rumple'] = $username;
$newUser['stiltskin'] = 'Test12te$t';
$newUser['fname'] = 'Foo';
$newUser['lname'] = 'Bar';
$newUser['adminPass'] = 'pass';
$newUser['stiltskin'] = UserTestData::PASSWORD;
$newUser['fname'] = UserTestData::FIRSTNAME;
$newUser['lname'] = UserTestData::LASTNAME;
$newUser['adminPass'] = LoginTestData::password;
$this->client->waitFor(XpathsConstantsUserAddTrait::CREATE_USER_BUTTON_USERADD_TRAIT);
$this->crawler = $this->client->refreshCrawler();
$this->crawler->filterXPath(XpathsConstantsUserAddTrait::CREATE_USER_BUTTON_USERADD_TRAIT)->click();
// assert the new user is in the database
$this->assertUserInDatabase($username);
// assert the new user can be seen in the gui
$this->client->switchTo()->defaultContent();
$this->client->waitFor(XpathsConstants::ADMIN_IFRAME);
$this->switchToIFrame(XpathsConstants::ADMIN_IFRAME);
// below line will throw a timeout exception and fail if the new user is not listed
$this->client->waitFor("//table//a[text()='$username']");
}

private function assertUserInDatabase(string $username): void
{
// assert the new user is in the database (if this fails, then will try userAddIfNotExist() up to
// 3 times total before failing)
try {
$this->innerAssertUserInDatabase($username);
} catch (ExpectationFailedException $e) {
if ($this->userAddAttemptCounter > 2) {
// re-throw since have failed 3 tries
throw $e;
} else {
// try again since not yet 3 tries
$this->userAddAttemptCounter++;
echo "TRY " . ($this->userAddAttemptCounter) . " of 3 to add new user to database";
$this->logOut();
$this->userAddIfNotExist($username);
}
}
}

private function innerAssertUserInDatabase(string $username): void
{
// assert the new user is in the database (check 3 times with 5 second delay prior each check to
// ensure allow enough time)
$userExistDatabase = false;
Expand All @@ -90,31 +124,11 @@ private function userAddIfNotExist(string $username): void
echo "TRY " . ($counter + 1) . " of 3 to see if new user is in database";
}
sleep(5);
if ($this->userExistDatabase($username)) {
if ($this->isUserExist($username)) {
$userExistDatabase = true;
}
$counter++;
}
$this->assertTrue($userExistDatabase, 'New user is not in database, so FAILED');
// assert the new user can be seen in the gui
$this->client->switchTo()->defaultContent();
$this->client->waitFor(XpathsConstants::ADMIN_IFRAME);
$this->switchToIFrame(XpathsConstants::ADMIN_IFRAME);
// below line will throw a timeout exception and fail if the new user is not listed
$this->client->waitFor("//table//a[text()='$username']");
$this->client->switchTo()->defaultContent();
}

private function userExistDatabase(string $username): bool
{
if (empty($username)) {
return false;
}
$usernameDatabase = sqlQuery("SELECT `username` FROM `users` WHERE `username` = ?", [$username]);
if (($usernameDatabase['username'] ?? '') != $username) {
return false;
} else {
return true;
}
}
}
8 changes: 7 additions & 1 deletion tests/Tests/E2e/User/UserTestData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@

class UserTestData
{
public const USERNAME = "foobar";
public const USERNAME = 'foobar';

public const PASSWORD = 'Test12te$t';

public const FIRSTNAME = 'Foo';

public const LASTNAME = 'Bar';
}

0 comments on commit 66875f3

Please sign in to comment.