diff --git a/html/modules/custom/ghi_plans/src/ApiObjects/Attachments/DataAttachment.php b/html/modules/custom/ghi_plans/src/ApiObjects/Attachments/DataAttachment.php index 49c801a06..f61eac653 100644 --- a/html/modules/custom/ghi_plans/src/ApiObjects/Attachments/DataAttachment.php +++ b/html/modules/custom/ghi_plans/src/ApiObjects/Attachments/DataAttachment.php @@ -717,8 +717,8 @@ public function getMeasurements() { if (!$attachment || !is_object($attachment)) { return NULL; } - $measurements = $attachment->measurements ?? []; - if (empty($measurements) && $measurements_query = $this->getEndpointQueryManager()->createInstance('measurement_query')) { + $measurements = property_exists($attachment, 'measurements') ? $attachment->measurements : []; + if (!property_exists($attachment, 'measurements') && $measurements_query = $this->getEndpointQueryManager()->createInstance('measurement_query')) { /** @var \Drupal\ghi_plans\Plugin\EndpointQuery\MeasurementQuery $measurements_query */ $measurements_query->setPlaceholder('attachment_id', $attachment->id); $measurements = $measurements_query->getUnprocessedMeasurements($this, TRUE); diff --git a/html/modules/custom/ghi_plans/tests/src/Unit/AttachmentTest.php b/html/modules/custom/ghi_plans/tests/src/Unit/AttachmentTest.php index fcb4c2f00..544bd8839 100644 --- a/html/modules/custom/ghi_plans/tests/src/Unit/AttachmentTest.php +++ b/html/modules/custom/ghi_plans/tests/src/Unit/AttachmentTest.php @@ -75,6 +75,38 @@ public function testAttachmentEmptyData() { $this->assertEmpty($attachment->getSourceEntity()); } + /** + * Test that missing measurements on DataAttachment does not create a loop. + * + * This tests against a potential loop in DataAttachment::getMeasurements(). + */ + public function testAttachmentEmptyMeasurementLoop() { + $measurement_query = $this->getMockBuilder('Drupal\ghi_plans\Plugin\EndpointQuery\MeasurementQuery') + ->disableOriginalConstructor() + ->getMock(); + $measurement_query->method('setPlaceholder')->willReturn(NULL); + $measurement_query->method('getUnprocessedMeasurements')->willReturn([]); + $measurement_query->expects($this->once())->method('getUnprocessedMeasurements'); + + $endpoint_query_manager = $this->getMockBuilder('Drupal\hpc_api\Query\EndpointQueryManager') + ->disableOriginalConstructor() + ->getMock(); + $endpoint_query_manager->method('createInstance')->with('measurement_query')->willReturn($measurement_query); + + $container = \Drupal::getContainer(); + $container->set('plugin.manager.endpoint_query_manager', $endpoint_query_manager); + \Drupal::setContainer($container); + + /** @var \Drupal\ghi_plans\ApiObjects\Attachments\DataAttachment $attachment */ + $attachment = AttachmentHelper::processAttachment((object) [ + 'id' => 38529, + 'type' => 'caseLoad', + 'attachmentPrototype' => $this->getApiObjectFixture('AttachmentPrototype', 'caseload'), + ]); + $this->assertInstanceOf(DataAttachment::class, $attachment); + $this->assertEmpty($attachment->getSourceEntity()); + } + /** * Test value retrieval from DataAttachments. */