Skip to content

Commit ff3b041

Browse files
fix: new test and remove unused parameters
1 parent 2311779 commit ff3b041

3 files changed

Lines changed: 59 additions & 37 deletions

File tree

app/Audit/AbstractAuditLogFormatter.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use App\Audit\Utils\DateFormatter;
66
use Doctrine\ORM\PersistentCollection;
7-
use Illuminate\Support\Facades\Log;
87

98
/**
109
* Copyright 2025 OpenStack Foundation
@@ -194,36 +193,6 @@ protected function formatFieldChange(string $prop_name, $old_value, $new_value):
194193
return sprintf("Property \"%s\" has changed from \"%s\" to \"%s\"", $prop_name, $old_display, $new_display);
195194
}
196195

197-
/**
198-
* Build detailed message for many-to-many collection changes
199-
*/
200-
protected function buildManyToManyDetailedMessage(PersistentCollection $collection, array $insertDiff, array $deleteDiff): array
201-
{
202-
$fieldName = 'unknown';
203-
$targetEntity = 'unknown';
204-
205-
try {
206-
$mapping = $collection->getMapping();
207-
$fieldName = $mapping->fieldName ?? 'unknown';
208-
$targetEntity = $mapping->targetEntity ?? 'unknown';
209-
if ($targetEntity) {
210-
$targetEntity = class_basename($targetEntity);
211-
}
212-
} catch (\Exception $e) {
213-
Log::debug("AbstractAuditLogFormatter::Could not extract collection metadata: " . $e->getMessage());
214-
}
215-
216-
$addedIds = $this->extractCollectionEntityIds($insertDiff);
217-
$removedIds = $this->extractCollectionEntityIds($deleteDiff);
218-
219-
return [
220-
'field' => $fieldName,
221-
'target_entity' => $targetEntity,
222-
'added_ids' => $addedIds,
223-
'removed_ids' => $removedIds,
224-
];
225-
}
226-
227196
/**
228197
* Format detailed message for many-to-many collection changes
229198
*/

app/Audit/AuditEventListener.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ public function onFlush(OnFlushEventArgs $eventArgs): void
5656
$strategy->audit($entity, [], IAuditStrategy::EVENT_ENTITY_DELETION, $ctx);
5757
}
5858
foreach ($uow->getScheduledCollectionDeletions() as $col) {
59-
[$subject, $payload, $eventType] = $this->auditCollection($col, $uow, IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE);
59+
[$subject, $payload, $eventType] = $this->auditCollection($col, IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE);
6060

6161
if (!is_null($subject)) {
6262
$strategy->audit($subject, $payload, $eventType, $ctx);
6363
}
6464
}
6565

6666
foreach ($uow->getScheduledCollectionUpdates() as $col) {
67-
[$subject, $payload, $eventType] = $this->auditCollection($col, $uow, IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE);
67+
[$subject, $payload, $eventType] = $this->auditCollection($col, IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE);
6868

6969
if (!is_null($subject)) {
7070
$strategy->audit($subject, $payload, $eventType, $ctx);
@@ -148,11 +148,10 @@ private function buildAuditContext(): AuditContext
148148
* Subject will be null if collection should not be audited
149149
*
150150
* @param object $subject The collection
151-
* @param mixed $uow The UnitOfWork
152151
* @param string $eventType The event type constant (EVENT_COLLECTION_MANYTOMANY_DELETE or EVENT_COLLECTION_MANYTOMANY_UPDATE)
153152
* @return array [$subject, $payload, $eventType]
154153
*/
155-
private function auditCollection($subject, $uow, string $eventType): array
154+
private function auditCollection($subject, string $eventType): array
156155
{
157156
if (!$subject instanceof PersistentCollection) {
158157
return [null, null, null];

tests/OpenTelemetry/Formatters/AuditEventListenerTest.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public function testAuditCollectionNonManyToManyReturnsCollectionAsSubject(): vo
5353
[$subject, $payload, $eventType] = $method->invoke(
5454
$listener,
5555
$collection,
56-
new \stdClass(),
5756
IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE
5857
);
5958

@@ -155,7 +154,62 @@ public function testAuditCollectionDeleteInitializedWithoutDiffUsesJoinTableQuer
155154
[$subject, $payload, $eventType] = $method->invoke(
156155
$listener,
157156
$collection,
158-
new \stdClass(),
157+
IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE
158+
);
159+
160+
$this->assertSame($owner, $subject);
161+
$this->assertSame([10, 11], $payload['deleted_ids']);
162+
$this->assertSame(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE, $eventType);
163+
}
164+
165+
public function testAuditCollectionDeleteUninitializedUsesJoinTableQuery(): void
166+
{
167+
$listener = new AuditEventListener();
168+
$owner = new \stdClass();
169+
170+
$mapping = ManyToManyOwningSideMapping::fromMappingArrayAndNamingStrategy([
171+
'fieldName' => 'tags',
172+
'sourceEntity' => \stdClass::class,
173+
'targetEntity' => \stdClass::class,
174+
'isOwningSide' => true,
175+
'joinTable' => [
176+
'name' => 'owner_tags',
177+
'joinColumns' => [['name' => 'owner_id', 'referencedColumnName' => 'id']],
178+
'inverseJoinColumns' => [['name' => 'tag_id', 'referencedColumnName' => 'id']],
179+
],
180+
], new DefaultNamingStrategy());
181+
182+
$em = $this->createMock(EntityManagerInterface::class);
183+
$meta = new ClassMetadata(\stdClass::class);
184+
$collection = new PersistentCollection($em, $meta, new ArrayCollection());
185+
$collection->setOwner($owner, $mapping);
186+
$collection->setInitialized(false);
187+
188+
$this->assertFalse($collection->isInitialized());
189+
190+
$ownerMeta = $this->getMockBuilder(ClassMetadata::class)
191+
->disableOriginalConstructor()
192+
->getMock();
193+
$ownerMeta->method('getIdentifierValues')->with($owner)->willReturn(['id' => 123]);
194+
195+
$conn = $this->getMockBuilder(Connection::class)
196+
->disableOriginalConstructor()
197+
->getMock();
198+
$conn->method('fetchFirstColumn')->willReturn(['10', '11']);
199+
200+
$em->method('getConnection')->willReturn($conn);
201+
$em->method('getClassMetadata')->with(get_class($owner))->willReturn($ownerMeta);
202+
203+
$emProp = new \ReflectionProperty(AuditEventListener::class, 'em');
204+
$emProp->setAccessible(true);
205+
$emProp->setValue($listener, $em);
206+
207+
$method = new \ReflectionMethod(AuditEventListener::class, 'auditCollection');
208+
$method->setAccessible(true);
209+
210+
[$subject, $payload, $eventType] = $method->invoke(
211+
$listener,
212+
$collection,
159213
IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE
160214
);
161215

0 commit comments

Comments
 (0)