4 * FHIR Allergy Intolerance Service Query Tests
5 * @coversDefaultClass OpenEMR\Services\FHIR\FhirPatientService
7 * @link http://www.open-emr.org
8 * @author Stephen Nielson <stephen@nielson.org>
9 * @copyright Copyright (c) 2021 Stephen Nielson <stephen@nielson.org>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 namespace OpenEMR\Tests\Services\FHIR
;
15 use OpenEMR\Common\Database\QueryUtils
;
16 use OpenEMR\Common\Uuid\UuidRegistry
;
17 use OpenEMR\FHIR\R4\FHIRDomainResource\FHIRAllergyIntolerance
;
18 use OpenEMR\Services\FHIR\FhirAllergyIntoleranceService
;
19 use OpenEMR\Services\FHIR\FhirUrlResolver
;
20 use PHPUnit\Framework\TestCase
;
21 use OpenEMR\Tests\Fixtures\FixtureManager
;
23 class FhirAllergyIntoleranceServiceQueryTest
extends TestCase
28 private $fixtureManager;
29 private $patientFixture;
30 private $fhirPatientFixture;
33 * @var FhirAllergyIntoleranceService
37 const FHIR_BASE_URL
= "/api/fhirs/default";
41 public function __construct(?
string $name = null, array $data = [], $dataName = '')
43 parent
::__construct($name, $data, $dataName);
45 $baseUrl = getenv("OPENEMR_BASE_URL_API", true) ?
: "https://localhost";
46 $fhirUrl = $baseUrl . "/apis/default/fhir/";
47 $this->apiBaseURL
= $fhirUrl;
50 protected function setUp(): void
53 $this->fixtureManager
= new FixtureManager();
54 $this->fixtureManager
->installAllergyIntoleranceFixtures();
55 $this->fhirService
= new FhirAllergyIntoleranceService($this->apiBaseURL
);
58 protected function tearDown(): void
60 $this->fixtureManager
->removeAllergyIntoleranceFixtures();
64 * Executes assertions against a 'GetAll' AllergyIntolerance Search processing result
65 * @param $processingResult The OpenEMR Processing Result
66 * @param $isExpectedToHaveAResult Indicates if the result is expected to have at least one search result
68 private function assertGetAllSearchResults($processingResult, $isExpectedToHaveAResult = true)
70 $this->assertTrue($processingResult->isValid());
72 if ($isExpectedToHaveAResult) {
73 $this->assertGreaterThan(0, count($processingResult->getData()));
75 $this->assertEquals(0, count($processingResult->getData()));
79 private function getReferenceURL($reference)
81 $url = $this->apiBaseURL
. $reference;
86 * PHPUnit Data Provider for FHIR AllergyIntolerance searches
88 public function searchParameterPatientReferenceDataProvider()
91 ['patient', "Patient/:uuid1"],
92 ['patient', ":uuid1"],
93 // make sure we can handle different ids
94 ['patient', "Patient/:uuid2"],
95 ['patient', ":uuid2"],
97 // full URL resolution
98 ["patient", $this->getReferenceURL("Patient/:uuid1")],
99 ["patient", $this->getReferenceURL("Patient/:uuid2")],
101 // select reference value on multiple voices
102 ['patient', "Patient/:uuid1,Patient/:uuid2"],
103 ['patient', ":uuid1,:uuid2"],
108 * Tests getAll queries
110 * @covers ::searchForOpenEMRRecords
111 * @dataProvider searchParameterPatientReferenceDataProvider
113 public function testGetAllPatientReference($parameterName, $parameterValue)
115 $pubpid = FixtureManager
::PATIENT_FIXTURE_PUBPID_PREFIX
. "%";
116 $select = "SELECT `lists`.`pid`,`patient_data`.`uuid` FROM `lists` INNER JOIN `patient_data` ON `patient_data`.`pid` = "
117 . "`lists`.`pid` WHERE `type`='allergy' and `patient_data`.`pubpid` LIKE ? LIMIT 2";
118 $records = QueryUtils
::fetchTableColumn($select, 'uuid', [$pubpid]);
119 $uuids = array_map(function ($v) {
120 return UuidRegistry
::uuidToString($v);
122 list($uuidPatient1, $uuidPatient2) = $uuids;
124 // replace any values that we will use for searching
125 $parameterValue = str_replace(":uuid1", $uuidPatient1, $parameterValue);
126 $parameterValue = str_replace(":uuid2", $uuidPatient2, $parameterValue);
128 $fhirSearchParameters = [$parameterName => $parameterValue];
129 $processingResult = $this->fhirService
->getAll($fhirSearchParameters);
130 $this->assertGetAllSearchResults($processingResult);
134 * Tests getAll queries for the _id search parameter. Since we can't combine a dataProvider with our test fixture
135 * installation, we run this test separately
137 * @covers ::searchForOpenEMRRecords
139 public function testGetAllWithUuid()
141 $select = "SELECT `uuid` FROM `lists` WHERE `type`='allergy' LIMIT 1";
142 $allergy_uuid = QueryUtils
::fetchSingleValue($select, 'uuid');
143 $fhirSearchParameters = ['_id' => UuidRegistry
::uuidToString($allergy_uuid)];
144 $processingResult = $this->fhirService
->getAll($fhirSearchParameters);
145 $this->assertGetAllSearchResults($processingResult);
149 * Uses the getAll method so we can't pass unless that is working.
152 public function testGetOne()
154 $actualResult = $this->fhirService
->getAll([]);
155 $this->assertNotEmpty($actualResult->getData(), "Get All should have returned a result");
157 $this->assertInstanceOf(FHIRAllergyIntolerance
::class, $actualResult->getData()[0], "Instance returned should have been the correct AllergyIntolerance class");
158 $expectedId = $actualResult->getData()[0]->getId()->getValue();
160 $actualResult = $this->fhirService
->getOne($expectedId);
161 $this->assertGreaterThan(0, count($actualResult->getData()), "Data array should have at least one record");
162 $actualId = $actualResult->getData()[0]->getId()->getValue();
164 $this->assertEquals($expectedId, $actualId);
168 * @covers ::getOne with an invalid uuid
170 public function testGetOneInvalidUuid()
172 $actualResult = $this->fhirService
->getOne('not-a-uuid');
173 $this->assertGreaterThan(0, count($actualResult->getValidationMessages()));
174 $this->assertEquals(0, count($actualResult->getInternalErrors()));
175 $this->assertEquals(0, count($actualResult->getData()));