Openemr fhir search (#4349)
[openemr.git] / tests / Tests / Services / FHIR / FhirPatientServiceQueryTest.php
blob757c241a8553a66a5d4751904a7d48444a0295d1
1 <?php
3 namespace OpenEMR\Tests\Services\FHIR;
5 use OpenEMR\Common\Uuid\UuidRegistry;
6 use PHPUnit\Framework\TestCase;
7 use OpenEMR\Tests\Fixtures\FixtureManager;
8 use OpenEMR\Services\FHIR\FhirPatientService;
9 use OpenEMR\FHIR\R4\FHIRDomainResource\FHIRPatient;
11 /**
12 * FHIR Patient Service Query Tests
13 * @coversDefaultClass OpenEMR\Services\FHIR\FhirPatientService
14 * @package OpenEMR
15 * @link http://www.open-emr.org
16 * @author Dixon Whitmire <dixonwh@gmail.com>
17 * @copyright Copyright (c) 2020 Dixon Whitmire <dixonwh@gmail.com>
18 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
21 class FhirPatientServiceQueryTest extends TestCase
23 private $fixtureManager;
24 private $patientFixture;
25 private $fhirPatientFixture;
27 /**
28 * @var FhirPatientService
30 private $fhirPatientService;
32 protected function setUp(): void
34 $this->fixtureManager = new FixtureManager();
35 $this->fixtureManager->installPatientFixtures();
36 $this->fhirPatientService = new FhirPatientService();
39 protected function tearDown(): void
41 $this->fixtureManager->removePatientFixtures();
44 /**
45 * Executes assertions against a 'GetAll' Patient Search processing result
46 * @param $processingResult The OpenEMR Processing Result
47 * @param $isExpectedToHaveAResult Indicates if the result is expected to have at least one search result
49 private function assertGetAllSearchResults($processingResult, $isExpectedToHaveAResult = true)
51 $this->assertTrue($processingResult->isValid());
53 if ($isExpectedToHaveAResult) {
54 $this->assertGreaterThan(0, count($processingResult->getData()));
55 } else {
56 $this->assertEquals(0, count($processingResult->getData()));
60 /**
61 * PHPUnit Data Provider for FHIR patient searches
63 public function searchParameterDataProvider()
66 return [
67 ['identifier', 'test-fixture-789456'],
68 ['gender', 'male'],
69 ['gender', 'female'],
70 ['gender', 'unknown'], // handle unknown gender's
72 // need to do a bunch of identifier tests to make sure our token searching is working.
74 ['address:contains', 'Avenue'],
75 ['address:prefix', '789'],
76 ['address', '789'], // default is :prefix
77 ['address:contains', 'Diego'],
78 ['address:exact', '400 West Broadway'],
80 // name searches
81 ['name', 'Ilias'], // first name
82 ['name', 'Ilias'],
83 ['name', 'Johnny'],
84 ['name', 'Jenane'],
85 ['name', 'Mr.'], // title
87 // if someone does a full timestamp birthdate, this tests the full timestamp parser even though
88 // birthdate is just a date not a datetime
89 ['birthdate', '1960-01-01T13:25:60'],
91 // now combinations of birthdates
92 ['birthdate', '1945'], // search by year
93 ['birthdate', '1945-02'], // search by year, month
94 ['birthdate', '1945-02-14'], // search by year, month, day
96 // now let's do it with our equality search which should be the same
97 ['birthdate', 'eq1945'], // search by year
98 ['birthdate', 'eq1945-02'], // search by year, month
99 ['birthdate', 'eq1945-02-14'], // search by year, month, day
101 // now inequality search
102 ['birthdate', 'ne1945'], // search by year
103 ['birthdate', 'ne1945-02'], // search by year, month
104 ['birthdate', 'ne1945-02-14'], // search by year, month, day
106 // now we will do less than, only 1 patient in data set has DOB of 1933-03-22
107 ['birthdate', 'lt1934'], // search by year
108 ['birthdate', 'lt1933-04'], // search by year, month
109 ['birthdate', 'lt1933-03-23'], // search by year, month, day
111 // now we will do ends before, only 1 patient in data set has DOB of 1933-03-22
112 ['birthdate', 'eb1934'], // search by year
113 ['birthdate', 'eb1933-04'], // search by year, month
114 ['birthdate', 'eb1933-03-23'], // search by year, month, day
116 // now we will do greater than, only 1 patient in data set has DOB of 1977-05-02
117 ['birthdate', 'gt1976'], // search by year
118 ['birthdate', 'gt1977-04'], // search by year, month
119 ['birthdate', 'gt1977-05-01'], // search by year, month, day
121 // now we will do starts after, only 1 patient in data set has DOB of 1977-05-02
122 ['birthdate', 'sa1976'], // search by year
123 ['birthdate', 'sa1977-04'], // search by year, month
124 ['birthdate', 'sa1977-05-01'], // search by year, month, day
126 // now we will do less than or equal to, only 1 patient in data set has DOB of 1933-03-22
127 ['birthdate', 'le1933'], // search by year
128 ['birthdate', 'le1933-03'], // search by year, month
129 ['birthdate', 'le1933-03-22'], // search by year, month, day
131 // now we will do greater than or equal to, only 1 patient in data set has DOB of 1977-05-02
132 ['birthdate', 'ge1977'], // search by year
133 ['birthdate', 'ge1977-05'], // search by year, month
134 ['birthdate', 'ge1977-05-02'], // search by year, month, day
138 // range searches for dates.
140 ['email', 'info@pennfirm.com'],
141 ['family', 'Moses'],
142 ['gender', 'male'],
143 ['given', 'Eduardo'],
144 ['name', 'Mr.'],
145 ['name', 'Ilias'],
146 ['name', 'Johnny'],
147 ['name', 'Jenane'],
148 ['phone', '(619) 555-4859'],
149 ['phone', '(619) 555-7821'],
150 ['phone', '(619) 555-7822'],
151 ['telecom', 'info@pennfirm.com'],
152 ['telecom', '(619) 555-4859'],
153 ['telecom', '(619) 555-7821'],
154 ['telecom', '(619) 555-7822'],
159 * PHPUnit Data Provider for FHIR patient searches
161 public function searchParameterCompoundDataProvider()
163 return [
164 ['birthdate', 'le1960-01-01', 'name:contains', 'lias'], // check operators and comparators work combined
165 ['birthdate', '1945', 'name', 'Moses'], // check defaults work
166 ['birthdate', '1945', 'family', 'Moses'], // check birthdate+family works
167 ['name', 'Ilias', 'birthdate', '1933-03'], // check name+birthdate work
168 ['gender', 'female', 'name', 'Ilias'], // check gender+name works
169 ['birthdate', '1933-03', 'gender', 'female'], // check birthdate+gender works
170 ['name', 'Moses', 'gender', 'male'],
175 * Tests getAll queries
176 * @covers ::getAll
177 * @covers ::searchForOpenEMRRecords
178 * @dataProvider searchParameterDataProvider
180 public function testGetAll($parameterName, $parameterValue)
182 $fhirSearchParameters = [$parameterName => $parameterValue];
183 $processingResult = $this->fhirPatientService->getAll($fhirSearchParameters);
184 $this->assertGetAllSearchResults($processingResult);
188 * Tests getAll queries for the _id search parameter. Since we can't combine a dataProvider with our test fixture
189 * installation, we run this test separately
190 * @covers ::getAll
191 * @covers ::searchForOpenEMRRecords
193 public function testGetAllWithUuid()
195 $select = "SELECT `uuid` FROM `patient_data` WHERE `pubpid`=?";
196 $result = sqlStatement($select, ['test-fixture-789456']);
197 $patient = sqlFetchArray($result);
198 $fhirSearchParameters = ['_id' => UuidRegistry::uuidToString($patient['uuid'])];
199 $processingResult = $this->fhirPatientService->getAll($fhirSearchParameters);
200 $this->assertGetAllSearchResults($processingResult);
204 * Tests getAll compound search queries
205 * @covers ::getAll
206 * @covers ::searchForOpenEMRRecords
207 * @dataProvider searchParameterCompoundDataProvider
209 public function testGetAllCompound($parameter1, $parameter1Value, $parameter2, $parameter2Value)
211 $fhirSearchParameters = [$parameter1 => $parameter1Value, $parameter2 => $parameter2Value];
212 $processingResult = $this->fhirPatientService->getAll($fhirSearchParameters);
213 $this->assertGetAllSearchResults($processingResult);
217 * Uses the getAll method so we can't pass unless that is working.
218 * @covers ::getOne
220 public function testGetOne()
222 $actualResult = $this->fhirPatientService->getAll([]);
223 $this->assertNotEmpty($actualResult->getData(), "Get All should have returned a result");
225 $this->assertInstanceOf(FhirPatient::class, $actualResult->getData()[0], "Instance returned should have been the correct patient class");
226 $expectedId = $actualResult->getData()[0]->getId()->getValue();
228 $actualResult = $this->fhirPatientService->getOne($expectedId);
229 $this->assertGreaterThan(0, $actualResult->getData());
230 $actualId = $actualResult->getData()[0]->getId()->getValue();
232 $this->assertEquals($expectedId, $actualId);
236 * @covers ::getOne with an invalid uuid
238 public function testGetOneInvalidUuid()
240 $actualResult = $this->fhirPatientService->getOne('not-a-uuid');
241 $this->assertGreaterThan(0, count($actualResult->getValidationMessages()));
242 $this->assertEquals(0, count($actualResult->getInternalErrors()));
243 $this->assertEquals(0, count($actualResult->getData()));