Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public function format($subject, array $change_set): ?string
return $this->formatUpdateMessage($change_set, fn($details) => sprintf("Attendee Badge (%s) for '%s' updated: %s by user %s", $id, $owner_name, $details, $this->getUserInfo()));
case IAuditStrategy::EVENT_ENTITY_DELETION:
return sprintf("Attendee Badge (%s) for '%s' deleted by user %s", $id, $owner_name, $this->getUserInfo());

case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE:
case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE:
return $this->formatManyToManyCollectionChange($change_set, sprintf("Attendee Badge (%s) for '%s'", $id, $owner_name));
}
} catch (\Exception $ex) {
Log::warning("SummitAttendeeBadgeAuditLogFormatter error: " . $ex->getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ public function format($subject, array $change_set): ?string
$summit_name,
$this->getUserInfo()
);

case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE:
case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE:
return $this->formatManyToManyCollectionChange($change_set, sprintf("Badge Type '%s' (%s) in Summit '%s'", $name, $id, $summit_name));
}
} catch (\Exception $ex) {
Log::warning("SummitBadgeTypeAuditLogFormatter error: " . $ex->getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function format($subject, array $change_set): ?string
return $this->formatUpdateMessage($change_set, fn($details) => sprintf("Order Extra Question '%s' (%s) in Summit '%s' updated: %s by user %s", $label, $id, $summit_name, $details, $this->getUserInfo()));
case IAuditStrategy::EVENT_ENTITY_DELETION:
return sprintf("Order Extra Question '%s' (%s) in Summit '%s' deleted by user %s", $label, $id, $summit_name, $this->getUserInfo());

case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE:
case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE:
return $this->formatManyToManyCollectionChange($change_set, sprintf("Order Extra Question '%s' (%s) in Summit '%s'", $label, $id, $summit_name));
}
} catch (\Exception $ex) {
Log::warning("SummitOrderExtraQuestionTypeAuditLogFormatter error: " . $ex->getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public function format($subject, array $change_set): ?string
($rate !== null ? sprintf("%.2f%%", $rate) : 'N/A'),
$this->getUserInfo()
);

case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE:
case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE:
return $this->formatManyToManyCollectionChange($change_set, sprintf("Tax Type '%s' (%s) for Summit '%s'", $name, $id, $summit_name));
}
} catch (\Exception $ex) {
Log::warning("SummitTaxTypeAuditLogFormatter error: " . $ex->getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

namespace Tests\OpenTelemetry\Formatters;

/**
* Copyright 2026 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use App\Audit\ConcreteFormatters\SummitAttendeeBadgeAuditLogFormatter;
use App\Audit\Interfaces\IAuditStrategy;
use Mockery;
use models\summit\SummitAttendee;
use models\summit\SummitAttendeeBadge;
use models\summit\SummitAttendeeTicket;
use models\summit\SummitBadgeFeatureType;
use Tests\OpenTelemetry\Formatters\Support\AuditContextBuilder;
use Tests\OpenTelemetry\Formatters\Support\PersistentCollectionTestHelper;
use Tests\TestCase;

/**
* SummitAttendeeBadge owns one many-to-many collection: features.
*/
class SummitAttendeeBadgeManyToManyAuditLogFormatterTest extends TestCase
{
private const MESSAGE_PREFIX = "Attendee Badge (6) for 'Jane Doe'";

protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}

public function testUpdateEventReportsAddedAndRemovedIdsFromDiff(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE);

$result = $formatter->format($this->makeSubject(), [
'collection' => $this->makeCollection('features', SummitBadgeFeatureType::class, [10, 11], [11, 12]),
]);

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " features (SummitBadgeFeatureType) updated: Added IDs: [12], Removed IDs: [10] by user ",
$result
);
}

public function testUpdateEventReportsAddedIds(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE);

$result = $formatter->format($this->makeSubject(), [
'collection' => $this->makeCollection('features', SummitBadgeFeatureType::class, [], [5, 6]),
]);

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " features (SummitBadgeFeatureType) updated: Added IDs: [5,6] by user ",
$result
);
}

public function testDeleteEventReportsRemovedIdsFromPayload(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE);

$result = $formatter->format($this->makeSubject(), [
'collection' => $this->makeCollection('features', SummitBadgeFeatureType::class, [], []),
'deleted_ids' => [3, 4],
]);

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " features (SummitBadgeFeatureType) deleted: Removed IDs: [3,4] by user ",
$result
);
}

public function testUpdateEventWithoutDiffIsSuppressed(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE);

$result = $formatter->format($this->makeSubject(), [
'collection' => $this->makeCollection('features', SummitBadgeFeatureType::class, [3], [3]),
]);

$this->assertNull($result);
}

public function testDeleteEventWithNothingToRemoveIsSuppressed(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE);

$result = $formatter->format($this->makeSubject(), [
'collection' => $this->makeCollection('features', SummitBadgeFeatureType::class, [], []),
'deleted_ids' => [],
]);

$this->assertNull($result);
}

public function testManyToManyEventWithoutCollectionReturnsNull(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE);

$this->assertNull($formatter->format($this->makeSubject(), []));
}

public function testManyToManyEventWithInvalidSubjectReturnsNull(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE);

$this->assertNull($formatter->format(new \stdClass(), [
'collection' => $this->makeCollection('features', SummitBadgeFeatureType::class, [], [7]),
]));
}

private function makeFormatter(string $eventType): SummitAttendeeBadgeAuditLogFormatter
{
$formatter = new SummitAttendeeBadgeAuditLogFormatter($eventType);
$formatter->setContext(AuditContextBuilder::default()->build());
return $formatter;
}

private function makeCollection(string $field, string $targetEntity, array $snapshotIds, array $currentIds)
{
return PersistentCollectionTestHelper::buildManyToManyCollection(
SummitAttendeeBadge::class,
$field,
$targetEntity,
$snapshotIds,
$currentIds
);
}

private function makeSubject(): SummitAttendeeBadge
{
$owner = Mockery::mock(SummitAttendee::class);
$owner->shouldReceive('getFirstName')->andReturn('Jane');
$owner->shouldReceive('getSurname')->andReturn('Doe');

$ticket = Mockery::mock(SummitAttendeeTicket::class);
$ticket->shouldReceive('getOwner')->andReturn($owner);

$badge = Mockery::mock(SummitAttendeeBadge::class);
$badge->shouldReceive('getId')->andReturn(6);
$badge->shouldReceive('getTicket')->andReturn($ticket);

return $badge;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace Tests\OpenTelemetry\Formatters;

/**
* Copyright 2026 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use App\Audit\ConcreteFormatters\SummitBadgeTypeAuditLogFormatter;
use App\Audit\Interfaces\IAuditStrategy;
use Mockery;
use models\summit\Summit;
use models\summit\SummitAccessLevelType;
use models\summit\SummitBadgeFeatureType;
use models\summit\SummitBadgeType;
use models\summit\SummitBadgeViewType;
use Tests\OpenTelemetry\Formatters\Support\AuditContextBuilder;
use Tests\OpenTelemetry\Formatters\Support\PersistentCollectionTestHelper;
use Tests\TestCase;

/**
* SummitBadgeType owns three many-to-many collections: access_levels, badge_features and allowed_view_types.
*/
class SummitBadgeTypeManyToManyAuditLogFormatterTest extends TestCase
{
private const MESSAGE_PREFIX = "Badge Type 'General' (2) in Summit 'Test Summit'";

protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}

public function testUpdateEventReportsAddedAndRemovedIdsFromDiff(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE);

$result = $formatter->format($this->makeSubject(), [
'collection' => $this->makeCollection('access_levels', SummitAccessLevelType::class, [10, 11], [11, 12]),
]);

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " access_levels (SummitAccessLevelType) updated: Added IDs: [12], Removed IDs: [10] by user ",
$result
);
}

public function testUpdateEventReportsAddedIds(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE);

$result = $formatter->format($this->makeSubject(), [
'collection' => $this->makeCollection('badge_features', SummitBadgeFeatureType::class, [], [5, 6]),
]);

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " badge_features (SummitBadgeFeatureType) updated: Added IDs: [5,6] by user ",
$result
);
}

public function testDeleteEventReportsRemovedIdsFromPayload(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE);

$result = $formatter->format($this->makeSubject(), [
'collection' => $this->makeCollection('access_levels', SummitAccessLevelType::class, [], []),
'deleted_ids' => [3, 4],
]);

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " access_levels (SummitAccessLevelType) deleted: Removed IDs: [3,4] by user ",
$result
);
}

public function testUpdateEventWithoutDiffIsSuppressed(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE);

$result = $formatter->format($this->makeSubject(), [
'collection' => $this->makeCollection('badge_features', SummitBadgeFeatureType::class, [3], [3]),
]);

$this->assertNull($result);
}

public function testDeleteEventWithNothingToRemoveIsSuppressed(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE);

$result = $formatter->format($this->makeSubject(), [
'collection' => $this->makeCollection('badge_features', SummitBadgeFeatureType::class, [], []),
'deleted_ids' => [],
]);

$this->assertNull($result);
}

public function testManyToManyEventWithoutCollectionReturnsNull(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE);

$this->assertNull($formatter->format($this->makeSubject(), []));
}

public function testManyToManyEventWithInvalidSubjectReturnsNull(): void
{
$formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE);

$this->assertNull($formatter->format(new \stdClass(), [
'collection' => $this->makeCollection('access_levels', SummitAccessLevelType::class, [], [7]),
]));
}

private function makeFormatter(string $eventType): SummitBadgeTypeAuditLogFormatter
{
$formatter = new SummitBadgeTypeAuditLogFormatter($eventType);
$formatter->setContext(AuditContextBuilder::default()->build());
return $formatter;
}

private function makeCollection(string $field, string $targetEntity, array $snapshotIds, array $currentIds)
{
return PersistentCollectionTestHelper::buildManyToManyCollection(
SummitBadgeType::class,
$field,
$targetEntity,
$snapshotIds,
$currentIds
);
}

private function makeSubject(): SummitBadgeType
{
$summit = Mockery::mock(Summit::class);
$summit->shouldReceive('getName')->andReturn('Test Summit');

$type = Mockery::mock(SummitBadgeType::class);
$type->shouldReceive('getId')->andReturn(2);
$type->shouldReceive('getName')->andReturn('General');
$type->shouldReceive('getSummit')->andReturn($summit);

return $type;
}
}
Loading
Loading