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 @@ -66,6 +66,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("Presentation Type '%s' (%s) for Summit '%s'", $type_name, $id, $summit_name));
}
} catch (\Exception $ex) {
Log::warning("PresentationTypeAuditLogFormatter error: " . $ex->getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,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("Summit Document '%s' (%s) for Summit '%s'", $name, $id, $summit_name));
}
} catch (\Exception $ex) {
Log::warning("SummitDocumentAuditLogFormatter error: " . $ex->getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function format($subject, array $change_set): ?string
$color,
$this->getUserInfo()
);

case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE:
case IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE:
return $this->formatManyToManyCollectionChange($change_set, sprintf("Event Type '%s' (%s) for Summit '%s'", $type, $id, $summit_name));
}
} catch (\Exception $ex) {
Log::warning("SummitEventTypeAuditLogFormatter 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("Media Upload Type '%s' (%s) for Summit '%s'", $name, $id, $summit_name));
}
} catch (\Exception $ex) {
Log::warning("SummitMediaUploadTypeAuditLogFormatter error: " . $ex->getMessage());
Expand Down
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\PresentationTypeAuditLogFormatter;
use App\Audit\Interfaces\IAuditStrategy;
use Mockery;
use models\summit\PresentationType;
use models\summit\Summit;
use models\summit\SummitTicketType;
use Tests\OpenTelemetry\Formatters\Support\AuditContextBuilder;
use Tests\OpenTelemetry\Formatters\Support\PersistentCollectionTestHelper;
use Tests\TestCase;

/**
* PresentationType owns one many-to-many collection: allowed_ticket_types.
*/
class PresentationTypeManyToManyAuditLogFormatterTest extends TestCase
{
private const MESSAGE_PREFIX = "Presentation Type 'Talk' (8) 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('allowed_ticket_types', SummitTicketType::class, [10, 11], [11, 12]),
]);

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

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

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

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

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

$type = Mockery::mock(PresentationType::class);
$type->shouldReceive('getId')->andReturn(8);
$type->shouldReceive('getType')->andReturn('Talk');
$type->shouldReceive('getMaxSpeakers')->andReturn(3);
$type->shouldReceive('isUseSpeakers')->andReturn(true);
$type->shouldReceive('getSummit')->andReturn($summit);

return $type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?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\SummitDocumentAuditLogFormatter;
use App\Audit\Interfaces\IAuditStrategy;
use Mockery;
use models\summit\Summit;
use models\summit\SummitDocument;
use models\summit\SummitEventType;
use Tests\OpenTelemetry\Formatters\Support\AuditContextBuilder;
use Tests\OpenTelemetry\Formatters\Support\PersistentCollectionTestHelper;
use Tests\TestCase;

/**
* SummitDocument owns one many-to-many collection: event_types.
*/
class SummitDocumentManyToManyAuditLogFormatterTest extends TestCase
{
private const MESSAGE_PREFIX = "Summit Document 'Code of Conduct' (21) 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('event_types', SummitEventType::class, [10, 11], [11, 12]),
]);

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

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

$this->assertStringStartsWith(
self::MESSAGE_PREFIX . " event_types (SummitEventType) 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('event_types', SummitEventType::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('event_types', SummitEventType::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('event_types', SummitEventType::class, [], [7]),
]));
}

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

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

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

$doc = Mockery::mock(SummitDocument::class);
$doc->shouldReceive('getId')->andReturn(21);
$doc->shouldReceive('getName')->andReturn('Code of Conduct');
$doc->shouldReceive('getLabel')->andReturn('CoC');
$doc->shouldReceive('getSummit')->andReturn($summit);

return $doc;
}
}
Loading
Loading