Skip to content
Merged
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
9 changes: 9 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
run: |
echo "running OpenTelemetry Formatter tests"
vendor/bin/phpunit tests/OpenTelemetry/Formatters/ --log-junit results_opentelemetry_tests.xml
echo "running AuditLogFormatterFactoryTest"
vendor/bin/phpunit tests/OpenTelemetry/AuditLogFormatterFactoryTest.php --log-junit results_audit_formatter_tests.xml

- name: Upload OpenTelemetry Tests Output
uses: actions/upload-artifact@v4
Expand All @@ -38,6 +40,13 @@ jobs:
path: results_opentelemetry_tests.xml
retention-days: 5

- name: Upload AuditLogFormatterFactory Tests Output
uses: actions/upload-artifact@v4
with:
name: results_audit_formatter_tests
path: results_audit_formatter_tests.xml
retention-days: 5

integration-tests:
runs-on: ubuntu-latest
strategy:
Expand Down
9 changes: 7 additions & 2 deletions app/Audit/AuditLogFormatterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,13 @@ private function getFormatterByContext(object $subject, string $event_type, Audi

private function matchesStrategy(array $strategy, AuditContext $ctx): bool
{
if (isset($strategy['route']) && !$this->routeMatches($strategy['route'], $ctx->rawRoute)) {
return false;
if (isset($strategy['route'])) {
if ($ctx->rawRoute === null) {
return false;
}
if (!$this->routeMatches($strategy['route'], $ctx->rawRoute)) {
return false;
}
}

return true;
Expand Down
157 changes: 157 additions & 0 deletions tests/OpenTelemetry/AuditLogFormatterFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
namespace Tests\OpenTelemetry;

/**
* 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\AuditContext;
use App\Audit\AuditLogFormatterFactory;
use PHPUnit\Framework\TestCase;

/**
* Class AuditLogFormatterFactoryTest
* Tests for AuditLogFormatterFactory::matchesStrategy() null guard
*/
class AuditLogFormatterFactoryTest extends TestCase
{
private const ROUTE_CREATE_EVENT = 'POST|api/v1/summits/{id}/events';
private const ROUTE_UPDATE_PRESENTATION = 'PUT|api/v1/summits/{id}/presentations';
private const FORMATTER_CLASS = 'TestFormatterClass';

private AuditLogFormatterFactory $factory;

protected function setUp(): void
{
parent::setUp();
$this->factory = new AuditLogFormatterFactory();
}


public function testMatchesStrategyHandlesNullRawRouteWithRouteRequired(): void
{
$strategy = [
'route' => self::ROUTE_CREATE_EVENT,
'formatter' => self::FORMATTER_CLASS
];

$ctx = new AuditContext(
userId: null,
userEmail: null,
userFirstName: null,
userLastName: null,
uiApp: null,
uiFlow: null,
route: null,
rawRoute: null, // null rawRoute from console command
httpMethod: null,
clientIp: null,
userAgent: null
);

$reflection = new \ReflectionClass($this->factory);
$method = $reflection->getMethod('matchesStrategy');
$method->setAccessible(true);

$result = $method->invoke($this->factory, $strategy, $ctx);
$this->assertFalse($result, 'matchesStrategy should return false when rawRoute is null and route is required');
}


public function testMatchesStrategyReturnsTrueWhenNoRouteRequiredAndRawRouteNull(): void
{
$strategy = [
'formatter' => self::FORMATTER_CLASS
];

$ctx = new AuditContext(
userId: null,
userEmail: null,
userFirstName: null,
userLastName: null,
uiApp: null,
uiFlow: null,
route: null,
rawRoute: null,
httpMethod: null,
clientIp: null,
userAgent: null
);

$reflection = new \ReflectionClass($this->factory);
$method = $reflection->getMethod('matchesStrategy');
$method->setAccessible(true);

$result = $method->invoke($this->factory, $strategy, $ctx);
$this->assertTrue($result, 'matchesStrategy should return true when no route is required');
}


public function testMatchesStrategyReturnsTrueWhenRouteMatches(): void
{
$strategy = [
'route' => self::ROUTE_CREATE_EVENT,
'formatter' => self::FORMATTER_CLASS
];

$ctx = new AuditContext(
userId: null,
userEmail: null,
userFirstName: null,
userLastName: null,
uiApp: null,
uiFlow: null,
route: null,
rawRoute: self::ROUTE_CREATE_EVENT, // matching rawRoute
httpMethod: null,
clientIp: null,
userAgent: null
);

$reflection = new \ReflectionClass($this->factory);
$method = $reflection->getMethod('matchesStrategy');
$method->setAccessible(true);

$result = $method->invoke($this->factory, $strategy, $ctx);
$this->assertTrue($result, 'matchesStrategy should return true when routes match');
}


public function testMatchesStrategyReturnsFalseWhenRouteDoesNotMatch(): void
{
$strategy = [
'route' => self::ROUTE_CREATE_EVENT,
'formatter' => self::FORMATTER_CLASS
];

$ctx = new AuditContext(
userId: null,
userEmail: null,
userFirstName: null,
userLastName: null,
uiApp: null,
uiFlow: null,
route: null,
rawRoute: self::ROUTE_UPDATE_PRESENTATION, // non-matching rawRoute
httpMethod: null,
clientIp: null,
userAgent: null
);

$reflection = new \ReflectionClass($this->factory);
$method = $reflection->getMethod('matchesStrategy');
$method->setAccessible(true);

$result = $method->invoke($this->factory, $strategy, $ctx);
$this->assertFalse($result, 'matchesStrategy should return false when routes do not match');
}
}