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 @@ -23,6 +23,7 @@
class PrePaidSummitRegistrationDiscountCodeAuditLogFormatter extends AbstractAuditLogFormatter
{


private function buildDiscountDetails(PrePaidSummitRegistrationDiscountCode $subject): string
{
$details = [];
Expand Down Expand Up @@ -90,6 +91,10 @@ public function format($subject, array $change_set): ?string
$discount_details,
$this->getUserInfo()
);

case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE:
case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE:
return $this->formatManyToManyCollectionChange($change_set, sprintf("Pre-Paid Discount Code '%s' (%s) for Summit '%s'", $code, $id, $summit_name));
}
} catch (\Exception $ex) {
Log::warning("PrePaidSummitRegistrationDiscountCodeAuditLogFormatter error: " . $ex->getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

class SponsorSummitRegistrationDiscountCodeAuditLogFormatter extends AbstractAuditLogFormatter
{

private function buildDiscountDetails($subject): string
{
$details = [];
Expand Down Expand Up @@ -86,6 +87,10 @@ public function format($subject, array $change_set): ?string
$discount_details,
$this->getUserInfo()
);

case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE:
case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE:
return $this->formatManyToManyCollectionChange($change_set, sprintf("Sponsor Registration Discount Code '%s' (ID: %s) in Summit '%s'", $code, $id, $summit_name));
}
} catch (\Exception $ex) {
Log::warning("SponsorSummitRegistrationDiscountCodeAuditLogFormatter error: " . $ex->getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

class SponsorSummitRegistrationPromoCodeAuditLogFormatter extends AbstractAuditLogFormatter
{

public function format($subject, array $change_set): ?string
{
if (!$subject instanceof SponsorSummitRegistrationPromoCode) {
Expand Down Expand Up @@ -64,6 +65,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("Sponsor Registration Promo Code '%s' (ID: %s) in Summit '%s'", $code, $id, $summit_name));
}
} catch (\Exception $ex) {
Log::warning("SponsorSummitRegistrationPromoCodeAuditLogFormatter error: " . $ex->getMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?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\PrePaidSummitRegistrationDiscountCodeAuditLogFormatter;
use App\Audit\Interfaces\IAuditStrategy;
use Mockery;
use models\main\Tag;
use models\summit\PrePaidSummitRegistrationDiscountCode;
use models\summit\Summit;
use models\summit\SummitBadgeFeatureType;
use models\summit\SummitTicketType;
use Tests\OpenTelemetry\Formatters\Support\AuditContextBuilder;
use Tests\OpenTelemetry\Formatters\Support\PersistentCollectionTestHelper;
use Tests\TestCase;

/**
* PrePaidSummitRegistrationDiscountCode inherits three many-to-many collections from SummitRegistrationPromoCode: badge_features, allowed_ticket_types and tags.
*/
class PrePaidSummitRegistrationDiscountCodeManyToManyAuditLogFormatterTest extends TestCase
{
private const MESSAGE_PREFIX = "Pre-Paid Discount Code 'PREPAID10' (40) for 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('badge_features', SummitBadgeFeatureType::class, [10, 11], [11, 12]),
]);

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " badge_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('allowed_ticket_types', SummitTicketType::class, [], [5, 6]),
]);

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " allowed_ticket_types (SummitTicketType) 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('badge_features', SummitBadgeFeatureType::class, [], []),
'deleted_ids' => [3, 4],
]);

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " badge_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('allowed_ticket_types', SummitTicketType::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('allowed_ticket_types', SummitTicketType::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('badge_features', SummitBadgeFeatureType::class, [], [7]),
]));
}

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

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

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

$code = Mockery::mock(PrePaidSummitRegistrationDiscountCode::class);
$code->shouldReceive('getId')->andReturn(40);
$code->shouldReceive('getCode')->andReturn('PREPAID10');
$code->shouldReceive('getRate')->andReturn(10.0);
$code->shouldReceive('getAmount')->andReturn(0.0);
$code->shouldReceive('getQuantityAvailable')->andReturn(5);
$code->shouldReceive('getSummit')->andReturn($summit);

return $code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?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\SponsorSummitRegistrationDiscountCodeAuditLogFormatter;
use App\Audit\Interfaces\IAuditStrategy;
use Mockery;
use models\main\Tag;
use models\summit\SponsorSummitRegistrationDiscountCode;
use models\summit\Summit;
use models\summit\SummitBadgeFeatureType;
use models\summit\SummitTicketType;
use Tests\OpenTelemetry\Formatters\Support\AuditContextBuilder;
use Tests\OpenTelemetry\Formatters\Support\PersistentCollectionTestHelper;
use Tests\TestCase;

/**
* SponsorSummitRegistrationDiscountCode inherits three many-to-many collections from SummitRegistrationPromoCode: badge_features, allowed_ticket_types and tags.
*/
class SponsorSummitRegistrationDiscountCodeManyToManyAuditLogFormatterTest extends TestCase
{
private const MESSAGE_PREFIX = "Sponsor Registration Discount Code 'SPONSOR20' (ID: 41) 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('badge_features', SummitBadgeFeatureType::class, [10, 11], [11, 12]),
]);

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " badge_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('allowed_ticket_types', SummitTicketType::class, [], [5, 6]),
]);

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " allowed_ticket_types (SummitTicketType) 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('badge_features', SummitBadgeFeatureType::class, [], []),
'deleted_ids' => [3, 4],
]);

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " badge_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('allowed_ticket_types', SummitTicketType::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('allowed_ticket_types', SummitTicketType::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('badge_features', SummitBadgeFeatureType::class, [], [7]),
]));
}

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

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

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

$code = Mockery::mock(SponsorSummitRegistrationDiscountCode::class);
$code->shouldReceive('getId')->andReturn(41);
$code->shouldReceive('getCode')->andReturn('SPONSOR20');
$code->shouldReceive('getSponsorId')->andReturn(7);
$code->shouldReceive('getRate')->andReturn(20.0);
$code->shouldReceive('getAmount')->andReturn(0.0);
$code->shouldReceive('getSummit')->andReturn($summit);

return $code;
}
}
Loading
Loading