|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * DatabaseClonerTest |
| 5 | + * |
| 6 | + * Unit tests for the database cloning functionality. |
| 7 | + */ |
| 8 | +class DatabaseClonerTest |
| 9 | +{ |
| 10 | + /** |
| 11 | + * Test that cloning validates source database name |
| 12 | + */ |
| 13 | + public function testValidatesSourceDatabaseName(): void |
| 14 | + { |
| 15 | + $validNames = ['mydb', 'my_db', 'mydb123', 'my-db']; |
| 16 | + $invalidNames = ['', ' ', 'my db', "my'db", 'my;db', '../etc']; |
| 17 | + |
| 18 | + foreach ($validNames as $name) { |
| 19 | + $this->assertTrue( |
| 20 | + $this->isValidDatabaseName($name), |
| 21 | + "Expected '$name' to be valid" |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + foreach ($invalidNames as $name) { |
| 26 | + $this->assertFalse( |
| 27 | + $this->isValidDatabaseName($name), |
| 28 | + "Expected '$name' to be invalid" |
| 29 | + ); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Test that sensitive columns are properly masked |
| 35 | + */ |
| 36 | + public function testSensitiveDataMasking(): void |
| 37 | + { |
| 38 | + $sensitiveColumns = ['password', 'ssn', 'credit_card', 'email', 'phone']; |
| 39 | + $normalColumns = ['id', 'name', 'created_at', 'status']; |
| 40 | + |
| 41 | + foreach ($sensitiveColumns as $column) { |
| 42 | + $this->assertTrue( |
| 43 | + $this->isSensitiveColumn($column), |
| 44 | + "Expected '$column' to be flagged as sensitive" |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + foreach ($normalColumns as $column) { |
| 49 | + $this->assertFalse( |
| 50 | + $this->isSensitiveColumn($column), |
| 51 | + "Expected '$column' to NOT be flagged as sensitive" |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Test row sample limit enforcement |
| 58 | + */ |
| 59 | + public function testRowSampleLimitEnforcement(): void |
| 60 | + { |
| 61 | + $maxRows = 10000; |
| 62 | + $testCases = [ |
| 63 | + ['input' => 100, 'expected' => 100], |
| 64 | + ['input' => 500, 'expected' => 500], |
| 65 | + ['input' => 10000, 'expected' => 10000], |
| 66 | + ['input' => 99999, 'expected' => $maxRows], |
| 67 | + ['input' => -1, 'expected' => 100], // Default |
| 68 | + ]; |
| 69 | + |
| 70 | + foreach ($testCases as $case) { |
| 71 | + $result = $this->enforceRowLimit($case['input'], $maxRows); |
| 72 | + $this->assertEquals($case['expected'], $result); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + // Helper methods for testing |
| 77 | + private function isValidDatabaseName(string $name): bool |
| 78 | + { |
| 79 | + return preg_match('/^[a-zA-Z0-9_-]+$/', $name) === 1; |
| 80 | + } |
| 81 | + |
| 82 | + private function isSensitiveColumn(string $column): bool |
| 83 | + { |
| 84 | + $sensitivePatterns = ['password', 'ssn', 'credit', 'email', 'phone', 'secret', 'token']; |
| 85 | + foreach ($sensitivePatterns as $pattern) { |
| 86 | + if (stripos($column, $pattern) !== false) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + } |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + private function enforceRowLimit(int $rows, int $max): int |
| 94 | + { |
| 95 | + if ($rows <= 0) return 100; |
| 96 | + return min($rows, $max); |
| 97 | + } |
| 98 | +} |
0 commit comments