diff --git a/src/EventListener/VersionListener.php b/src/EventListener/VersionListener.php index cc734580e..cc3f05141 100644 --- a/src/EventListener/VersionListener.php +++ b/src/EventListener/VersionListener.php @@ -52,10 +52,10 @@ public function preRemove(Version $version, LifecycleEventArgs $event): void public function preUpdate(Version $version, PreUpdateEventArgs $event): void { if (($event->hasChangedField('source') || $event->hasChangedField('dist')) && !$version->isDevelopment()) { - $oldDistRef = $event->getOldValue('dist')['reference'] ?? null; - $oldSourceRef = $event->getOldValue('source')['reference'] ?? null; - $newDistRef = $event->getNewValue('dist')['reference'] ?? null; - $newSourceRef = $event->getNewValue('source')['reference'] ?? null; + $oldDistRef = $event->hasChangedField('dist') ? ($event->getOldValue('dist')['reference'] ?? null) : $version->getDist()['reference'] ?? null; + $oldSourceRef = $event->hasChangedField('source') ? ($event->getOldValue('source')['reference'] ?? null) : $version->getSource()['reference'] ?? null; + $newDistRef = $event->hasChangedField('dist') ? ($event->getNewValue('dist')['reference'] ?? null) : $version->getDist()['reference'] ?? null; + $newSourceRef = $event->hasChangedField('source') ? ($event->getNewValue('source')['reference'] ?? null) : $version->getSource()['reference'] ?? null; if ($oldDistRef !== $newDistRef || $oldSourceRef !== $newSourceRef) { // buffering things to be inserted in postUpdate once we can confirm it is done $this->buffered[] = AuditRecord::versionReferenceChange($version, $oldSourceRef, $oldDistRef); diff --git a/tests/Audit/VersionAuditRecordTest.php b/tests/Audit/VersionAuditRecordTest.php index cd0e70b9a..73f7c63c9 100644 --- a/tests/Audit/VersionAuditRecordTest.php +++ b/tests/Audit/VersionAuditRecordTest.php @@ -68,8 +68,24 @@ public function testVersionChangesGetRecorded(): void self::assertSame(AuditRecordType::VersionReferenceChange->value, $logs[0]['type']); self::assertSame('{"name": "composer/composer", "dist_to": "new-dist-ref", "version": "1.0.0", "dist_from": "old-dist-ref", "source_to": "new-source-ref", "source_from": null}', $logs[0]['attributes']); - // verify that only reference changes triggers a new audit log + // verify that unrelated changes do not create new audit logs + $version->setLicense(['MIT']); + $em->persist($version); + $em->flush(); + + $logs = $container->get(Connection::class)->fetchAllAssociative('SELECT * FROM audit_log ORDER BY id DESC'); + self::assertCount(2, $logs); // package creation + version reference change + + // verify that changing dist only without ref change does not create new audit log and does not crash $version->setDist(['reference' => 'new-dist-ref', 'type' => 'zip2', 'url' => 'https://example.org/dist.zip2']); + $em->persist($version); + $em->flush(); + + $logs = $container->get(Connection::class)->fetchAllAssociative('SELECT * FROM audit_log ORDER BY id DESC'); + self::assertCount(2, $logs); // package creation + version reference change + + // verify that only reference changes triggers a new audit log + $version->setDist(['reference' => 'new-dist-ref', 'type' => 'zip3', 'url' => 'https://example.org/dist.zip2']); $version->setSource(['reference' => 'new-source-ref', 'type' => 'git2', 'url' => 'git://example.org/dist.zip2']); $em->persist($version); $em->flush();