Files
monolog-gdpr-filter/tests/GdprDefaultPatternsTest.php
Ismo Vuorinen 00c6f76c97 feat: performance, integrations, advanced features (#2)
* feat: performance, integrations, advanced features

* chore: fix linting problems

* chore: suppressions and linting

* chore(lint): pre-commit linting, fixes

* feat: comprehensive input validation, security hardening, and regression testing

- Add extensive input validation throughout codebase with proper error handling
- Implement comprehensive security hardening with ReDoS protection and bounds checking
- Add 3 new regression test suites covering critical bugs, security, and validation scenarios
- Enhance rate limiting with memory management and configurable cleanup intervals
- Update configuration security settings and improve Laravel integration
- Fix TODO.md timestamps to reflect actual development timeline
- Strengthen static analysis configuration and improve code quality standards

* feat: configure static analysis tools and enhance development workflow

- Complete configuration of Psalm, PHPStan, and Rector for harmonious static analysis.
- Fix invalid configurations and tool conflicts that prevented proper code quality analysis.
- Add comprehensive safe analysis script with interactive workflow, backup/restore
  capabilities, and dry-run modes. Update documentation with linting policy
  requiring issue resolution over suppression.
- Clean completed items from TODO to focus on actionable improvements.
- All static analysis tools now work together seamlessly to provide
  code quality insights without breaking existing functionality.

* fix(test): update Invalid regex pattern expectation

* chore: phpstan, psalm fixes

* chore: phpstan, psalm fixes, more tests

* chore: tooling tweaks, cleanup

* chore: tweaks to get the tests pass

* fix(lint): rector config tweaks and successful run

* feat: refactoring, more tests, fixes, cleanup

* chore: deduplication, use constants

* chore: psalm fixes

* chore: ignore phpstan deliberate errors in tests

* chore: improve codebase, deduplicate code

* fix: lint

* chore: deduplication, codebase simplification, sonarqube fixes

* fix: resolve SonarQube reliability rating issues

Fix useless object instantiation warnings in test files by assigning
instantiated objects to variables. This resolves the SonarQube reliability
rating issue (was C, now targeting A).

Changes:
- tests/Strategies/MaskingStrategiesTest.php: Fix 3 instances
- tests/Strategies/FieldPathMaskingStrategyTest.php: Fix 1 instance

The tests use expectException() to verify that constructors throw
exceptions for invalid input. SonarQube flagged standalone `new`
statements as useless. Fixed by assigning to variables with explicit
unset() and fail() calls.

All tests pass (623/623) and static analysis tools pass.

* fix: resolve more SonarQube detected issues

* fix: resolve psalm detected issues

* fix: resolve more SonarQube detected issues

* fix: resolve psalm detected issues

* fix: duplications

* fix: resolve SonarQube reliability rating issues

* fix: resolve psalm and phpstan detected issues
2025-10-31 13:59:01 +02:00

153 lines
5.5 KiB
PHP

<?php
namespace Tests;
use Tests\TestConstants;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\TestCase;
use Ivuorinen\MonologGdprFilter\FieldMaskConfig;
use Ivuorinen\MonologGdprFilter\MaskConstants;
use Ivuorinen\MonologGdprFilter\GdprProcessor;
use Ivuorinen\MonologGdprFilter\DefaultPatterns;
/**
* GDPR Default Patterns Test
*
* @api
*/
#[CoversClass(FieldMaskConfig::class)]
#[CoversMethod(DefaultPatterns::class, 'get')]
class GdprDefaultPatternsTest extends TestCase
{
public function testPatternIban(): void
{
$patterns = DefaultPatterns::get();
$processor = new GdprProcessor($patterns);
// Finnish IBAN with spaces
$iban = TestConstants::IBAN_FI;
$masked = $processor->maskMessage($iban);
$this->assertSame(MaskConstants::MASK_IBAN, $masked);
// Finnish IBAN without spaces
$ibanWithoutSpaces = 'FI2112345600000785';
$this->assertSame(MaskConstants::MASK_IBAN, $processor->maskMessage($ibanWithoutSpaces));
$this->assertNotSame($ibanWithoutSpaces, $processor->maskMessage($ibanWithoutSpaces));
// Edge: not an IBAN
$notIban = 'FI21 1234 5600 000 85A';
$this->assertSame($notIban, $processor->maskMessage($notIban));
}
public function testPatternPhone(): void
{
$patterns = DefaultPatterns::get();
$processor = new GdprProcessor($patterns);
$phone = '+358 40 1234567';
$masked = $processor->maskMessage($phone);
$this->assertSame(MaskConstants::MASK_PHONE, $masked);
// Edge: not a phone
$notPhone = 'Call me maybe';
$this->assertSame($notPhone, $processor->maskMessage($notPhone));
}
public function testPatternUsSsn(): void
{
$patterns = DefaultPatterns::get();
$processor = new GdprProcessor($patterns);
$ssn = TestConstants::SSN_US;
$masked = $processor->maskMessage($ssn);
$this->assertSame(MaskConstants::MASK_USSSN, $masked);
// Edge: not a SSN
$notSsn = '123456789';
$this->assertSame($notSsn, $processor->maskMessage($notSsn));
}
public function testPatternDob(): void
{
$patterns = DefaultPatterns::get();
$processor = new GdprProcessor($patterns);
$dob1 = '1990-12-31';
$dob2 = '31/12/1990';
$masked1 = $processor->maskMessage($dob1);
$masked2 = $processor->maskMessage($dob2);
$this->assertSame(MaskConstants::MASK_DOB, $masked1);
$this->assertSame(MaskConstants::MASK_DOB, $masked2);
// Edge: not a DOB
$notDob = '1990/31/12';
$this->assertSame($notDob, $processor->maskMessage($notDob));
}
public function testPatternPassport(): void
{
$patterns = DefaultPatterns::get();
$processor = new GdprProcessor($patterns);
$passport = 'A123456';
$masked = $processor->maskMessage($passport);
$this->assertSame(MaskConstants::MASK_PASSPORT, $masked);
// Edge: too short
$notPassport = 'A1234';
$this->assertSame($notPassport, $processor->maskMessage($notPassport));
}
public function testPatternCreditCard(): void
{
$patterns = DefaultPatterns::get();
$processor = new GdprProcessor($patterns);
$cc1 = '4111 1111 1111 1111'; // Visa
$cc2 = '5500-0000-0000-0004'; // MasterCard
$cc3 = '340000000000009'; // Amex (15 digits)
$cc4 = '6011000000000004'; // Discover
$masked1 = $processor->maskMessage($cc1);
$masked2 = $processor->maskMessage($cc2);
$masked3 = $processor->maskMessage($cc3);
$masked4 = $processor->maskMessage($cc4);
$this->assertSame(MaskConstants::MASK_CC, $masked1);
$this->assertSame(MaskConstants::MASK_CC, $masked2);
$this->assertSame(MaskConstants::MASK_CC, $masked3);
$this->assertSame(MaskConstants::MASK_CC, $masked4);
// Edge: not a CC
$notCc = '1234 5678 9012';
$this->assertSame($notCc, $processor->maskMessage($notCc));
}
public function testPatternBearerToken(): void
{
$patterns = DefaultPatterns::get();
$processor = new GdprProcessor($patterns);
$token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9';
$masked = $processor->maskMessage($token);
$this->assertSame(MaskConstants::MASK_TOKEN, $masked);
// Edge: not a token
$notToken = 'bearer token';
$this->assertSame($notToken, $processor->maskMessage($notToken));
}
public function testPatternApiKey(): void
{
$patterns = DefaultPatterns::get();
$processor = new GdprProcessor($patterns);
$apiKey = 'sk_test_4eC39HqLyjWDarj';
$masked = $processor->maskMessage($apiKey);
$this->assertSame(MaskConstants::MASK_APIKEY, $masked);
// Edge: short string
$notApiKey = 'shortkey';
$this->assertSame($notApiKey, $processor->maskMessage($notApiKey));
}
public function testPatternMac(): void
{
$patterns = DefaultPatterns::get();
$processor = new GdprProcessor($patterns);
$mac = '00:1A:2B:3C:4D:5E';
$masked = $processor->maskMessage($mac);
$this->assertSame(MaskConstants::MASK_MAC, $masked);
$mac2 = '00-1A-2B-3C-4D-5E';
$masked2 = $processor->maskMessage($mac2);
$this->assertSame(MaskConstants::MASK_MAC, $masked2);
// Edge: not a MAC
$notMac = '001A2B3C4D5E';
$this->assertSame($notMac, $processor->maskMessage($notMac));
}
}