|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\OpenTelemetry\Formatters; |
| 4 | + |
| 5 | +/** |
| 6 | + * Copyright 2026 OpenStack Foundation |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + **/ |
| 17 | + |
| 18 | +use App\Audit\ConcreteFormatters\PresentationCategoryGroupAuditLogFormatter; |
| 19 | +use App\Audit\Interfaces\IAuditStrategy; |
| 20 | +use Mockery; |
| 21 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 22 | +use Tests\OpenTelemetry\Formatters\Support\AuditContextBuilder; |
| 23 | +use Tests\OpenTelemetry\Formatters\Support\PersistentCollectionTestHelper; |
| 24 | +use Tests\TestCase; |
| 25 | +use models\summit\PresentationCategory; |
| 26 | +use models\summit\PresentationCategoryGroup; |
| 27 | +use models\summit\Summit; |
| 28 | + |
| 29 | +class PresentationCategoryGroupAuditLogFormatterManyToManyTest extends TestCase |
| 30 | +{ |
| 31 | + private const GROUP_ID = 7; |
| 32 | + private const GROUP_NAME = 'Core Tracks'; |
| 33 | + private const SUMMIT_ID = 1; |
| 34 | + private const SUMMIT_NAME = 'Test Summit'; |
| 35 | + private const GROUP_COLOR = '#00AAFF'; |
| 36 | + private const GROUP_MAX_ATTENDEE_VOTES = 5; |
| 37 | + private const FIELD_NAME = 'categories'; |
| 38 | + private const TARGET_ENTITY = PresentationCategory::class; |
| 39 | + private const DP_UPDATE_WITHOUT_CONTEXT = 'update without context'; |
| 40 | + private const DP_DELETE_WITHOUT_CONTEXT = 'delete without context'; |
| 41 | + private const DP_UPDATE_WITHOUT_COLLECTION = 'update without collection'; |
| 42 | + private const DP_DELETE_WITHOUT_COLLECTION = 'delete without collection'; |
| 43 | + private const LOG_DELETED_M2M = 'deleted M2M'; |
| 44 | + private const LOG_UPDATED_M2M = 'updated M2M'; |
| 45 | + private const LOG_REMOVED_IDS_EMPTY = 'Removed IDs: []'; |
| 46 | + private const LOG_REMOVED_IDS_PAYLOAD = 'Removed IDs: [10,11,12]'; |
| 47 | + private const LOG_REMOVED_IDS_DIFF = 'Removed IDs: [1]'; |
| 48 | + private const LOG_ADDED_IDS_DIFF = 'Added IDs: [3]'; |
| 49 | + private const DELETED_IDS_PAYLOAD = [10, 11, 12]; |
| 50 | + private const SNAPSHOT_IDS_EMPTY = []; |
| 51 | + private const CURRENT_IDS_EMPTY = []; |
| 52 | + private const SNAPSHOT_IDS_REMOVE_ONE = [1, 2, 3]; |
| 53 | + private const CURRENT_IDS_REMOVE_ONE = [2, 3]; |
| 54 | + private const SNAPSHOT_IDS_UPDATE = [1, 2]; |
| 55 | + private const CURRENT_IDS_UPDATE = [2, 3]; |
| 56 | + |
| 57 | + protected function tearDown(): void |
| 58 | + { |
| 59 | + Mockery::close(); |
| 60 | + parent::tearDown(); |
| 61 | + } |
| 62 | + |
| 63 | + #[DataProvider('providesNullCasesForManyToMany')] |
| 64 | + public function testManyToManyReturnsNullWithoutRequiredContextOrCollection( |
| 65 | + string $eventType, |
| 66 | + bool $withContext |
| 67 | + ): void { |
| 68 | + $formatter = $this->makeFormatter($eventType, $withContext); |
| 69 | + $group = $this->makeGroup(); |
| 70 | + |
| 71 | + $result = $formatter->format($group, []); |
| 72 | + $this->assertNull($result); |
| 73 | + } |
| 74 | + |
| 75 | + public function testFormatterReturnsNullForInvalidSubject(): void |
| 76 | + { |
| 77 | + $formatter = new PresentationCategoryGroupAuditLogFormatter( |
| 78 | + IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE |
| 79 | + ); |
| 80 | + $formatter->setContext(AuditContextBuilder::default()->build()); |
| 81 | + |
| 82 | + $result = $formatter->format(new \stdClass(), []); |
| 83 | + |
| 84 | + $this->assertNull($result); |
| 85 | + } |
| 86 | + |
| 87 | + public function testManyToManyDeleteReturnsEmptyRemovedIdsWhenPayloadMissing(): void |
| 88 | + { |
| 89 | + $group = $this->makeGroup(); |
| 90 | + $formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE); |
| 91 | + $collection = $this->makeCollection(self::SNAPSHOT_IDS_EMPTY, self::CURRENT_IDS_EMPTY); |
| 92 | + |
| 93 | + $result = $formatter->format($group, [ |
| 94 | + 'collection' => $collection, |
| 95 | + ]); |
| 96 | + |
| 97 | + $this->assertNotNull($result); |
| 98 | + $this->assertStringContainsString(self::LOG_DELETED_M2M, $result); |
| 99 | + $this->assertStringContainsString(self::LOG_REMOVED_IDS_EMPTY, $result); |
| 100 | + } |
| 101 | + |
| 102 | + public function testManyToManyDeleteUsesDeletedIdsFromPayload(): void |
| 103 | + { |
| 104 | + $group = $this->makeGroup(); |
| 105 | + $formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE); |
| 106 | + $collection = $this->makeCollection(self::SNAPSHOT_IDS_EMPTY, self::CURRENT_IDS_EMPTY); |
| 107 | + |
| 108 | + $result = $formatter->format($group, [ |
| 109 | + 'collection' => $collection, |
| 110 | + 'deleted_ids' => self::DELETED_IDS_PAYLOAD, |
| 111 | + ]); |
| 112 | + |
| 113 | + $this->assertNotNull($result); |
| 114 | + $this->assertStringContainsString(self::LOG_REMOVED_IDS_PAYLOAD, $result); |
| 115 | + } |
| 116 | + |
| 117 | + public function testManyToManyDeleteUsesRemovedIdsFromCollectionDiff(): void |
| 118 | + { |
| 119 | + $group = $this->makeGroup(); |
| 120 | + $formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE); |
| 121 | + $collection = $this->makeCollection(self::SNAPSHOT_IDS_REMOVE_ONE, self::CURRENT_IDS_REMOVE_ONE); |
| 122 | + |
| 123 | + $result = $formatter->format($group, [ |
| 124 | + 'collection' => $collection, |
| 125 | + ]); |
| 126 | + |
| 127 | + $this->assertNotNull($result); |
| 128 | + $this->assertStringContainsString(self::LOG_REMOVED_IDS_DIFF, $result); |
| 129 | + } |
| 130 | + |
| 131 | + public function testManyToManyUpdateUsesAddedAndRemovedIdsFromCollectionDiff(): void |
| 132 | + { |
| 133 | + $group = $this->makeGroup(); |
| 134 | + $formatter = $this->makeFormatter(IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE); |
| 135 | + $collection = $this->makeCollection(self::SNAPSHOT_IDS_UPDATE, self::CURRENT_IDS_UPDATE); |
| 136 | + |
| 137 | + $result = $formatter->format($group, [ |
| 138 | + 'collection' => $collection, |
| 139 | + ]); |
| 140 | + |
| 141 | + $this->assertNotNull($result); |
| 142 | + $this->assertStringContainsString(self::LOG_UPDATED_M2M, $result); |
| 143 | + $this->assertStringContainsString(self::LOG_ADDED_IDS_DIFF, $result); |
| 144 | + $this->assertStringContainsString(self::LOG_REMOVED_IDS_DIFF, $result); |
| 145 | + } |
| 146 | + |
| 147 | + public static function providesNullCasesForManyToMany(): array |
| 148 | + { |
| 149 | + return [ |
| 150 | + self::DP_UPDATE_WITHOUT_CONTEXT => [IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE, false], |
| 151 | + self::DP_DELETE_WITHOUT_CONTEXT => [IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE, false], |
| 152 | + self::DP_UPDATE_WITHOUT_COLLECTION => [IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_UPDATE, true], |
| 153 | + self::DP_DELETE_WITHOUT_COLLECTION => [IAuditStrategy::EVENT_COLLECTION_MANYTOMANY_DELETE, true], |
| 154 | + ]; |
| 155 | + } |
| 156 | + |
| 157 | + private function makeFormatter(string $eventType, bool $withContext = true): PresentationCategoryGroupAuditLogFormatter |
| 158 | + { |
| 159 | + $formatter = new PresentationCategoryGroupAuditLogFormatter($eventType); |
| 160 | + if ($withContext) { |
| 161 | + $formatter->setContext(AuditContextBuilder::default()->build()); |
| 162 | + } |
| 163 | + return $formatter; |
| 164 | + } |
| 165 | + |
| 166 | + private function makeCollection(array $snapshotIds, array $currentIds) |
| 167 | + { |
| 168 | + return PersistentCollectionTestHelper::buildManyToManyCollection( |
| 169 | + PresentationCategoryGroup::class, |
| 170 | + self::FIELD_NAME, |
| 171 | + self::TARGET_ENTITY, |
| 172 | + $snapshotIds, |
| 173 | + $currentIds |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + private function makeGroup(): PresentationCategoryGroup |
| 178 | + { |
| 179 | + $group = Mockery::mock(PresentationCategoryGroup::class, [ |
| 180 | + 'getId' => self::GROUP_ID, |
| 181 | + 'getName' => self::GROUP_NAME, |
| 182 | + 'getColor' => self::GROUP_COLOR, |
| 183 | + 'getMaxAttendeeVotes' => self::GROUP_MAX_ATTENDEE_VOTES, |
| 184 | + ])->makePartial(); |
| 185 | + |
| 186 | + $summit = Mockery::mock(Summit::class, [ |
| 187 | + 'getId' => self::SUMMIT_ID, |
| 188 | + 'getName' => self::SUMMIT_NAME, |
| 189 | + ])->makePartial(); |
| 190 | + |
| 191 | + $group->shouldReceive('getSummit')->andReturn($summit); |
| 192 | + |
| 193 | + return $group; |
| 194 | + } |
| 195 | +} |
0 commit comments