|
21 | 21 | use CodeIgniter\Test\Mock\MockInputOutput; |
22 | 22 | use CodeIgniter\Test\StreamFilterTrait; |
23 | 23 | use PHPUnit\Framework\Attributes\Group; |
| 24 | +use PHPUnit\Framework\Attributes\RequiresOperatingSystem; |
24 | 25 | use PHPUnit\Framework\Attributes\WithoutErrorHandler; |
25 | 26 |
|
26 | 27 | /** |
@@ -113,6 +114,7 @@ public function testRotateMovesCurrentKeyToPreviousKeysAndGeneratesNew(): void |
113 | 114 |
|
114 | 115 | $this->assertSame( |
115 | 116 | <<<'EOT' |
| 117 | +
|
116 | 118 | Encryption key rotated. 1 previous key retained for decryption fallback. |
117 | 119 | Re-encrypt existing data with the new key when ready. |
118 | 120 |
|
@@ -140,6 +142,7 @@ public function testRotatePrependsToExistingPreviousKeysList(): void |
140 | 142 |
|
141 | 143 | $this->assertSame( |
142 | 144 | <<<'EOT' |
| 145 | +
|
143 | 146 | Encryption key rotated. 3 previous keys retained for decryption fallback. |
144 | 147 | Re-encrypt existing data with the new key when ready. |
145 | 148 |
|
@@ -396,6 +399,23 @@ public function testRotateRejectsNonNumericKeepValue(): void |
396 | 399 | $this->assertSame(self::SEED_KEY, env('encryption.key')); |
397 | 400 | } |
398 | 401 |
|
| 402 | + public function testRotateRejectsFractionalKeepValue(): void |
| 403 | + { |
| 404 | + $this->seedEnv(self::SEED_KEY); |
| 405 | + |
| 406 | + command('key:rotate --force --keep=3.5'); |
| 407 | + |
| 408 | + $this->assertSame( |
| 409 | + <<<'EOT' |
| 410 | +
|
| 411 | + The --keep option must be a non-negative integer. |
| 412 | + |
| 413 | + EOT, |
| 414 | + $this->getUndecoratedBuffer(), |
| 415 | + ); |
| 416 | + $this->assertSame(self::SEED_KEY, env('encryption.key')); |
| 417 | + } |
| 418 | + |
399 | 419 | public function testRotateRejectsNegativeLengthValue(): void |
400 | 420 | { |
401 | 421 | $this->seedEnv(self::SEED_KEY); |
@@ -457,6 +477,57 @@ public function testRotateRejectsNonNumericLengthValue(): void |
457 | 477 | $this->assertSame($envContentsBefore, (string) file_get_contents($this->envPath)); |
458 | 478 | } |
459 | 479 |
|
| 480 | + public function testRotateRejectsFractionalLengthValue(): void |
| 481 | + { |
| 482 | + $this->seedEnv(self::SEED_KEY); |
| 483 | + $envContentsBefore = (string) file_get_contents($this->envPath); |
| 484 | + |
| 485 | + command('key:rotate --force --length=3.5'); |
| 486 | + |
| 487 | + $this->assertSame( |
| 488 | + <<<'EOT' |
| 489 | +
|
| 490 | + The --length option must be a positive integer. |
| 491 | + |
| 492 | + EOT, |
| 493 | + $this->getUndecoratedBuffer(), |
| 494 | + ); |
| 495 | + $this->assertSame(self::SEED_KEY, env('encryption.key')); |
| 496 | + $this->assertSame($envContentsBefore, (string) file_get_contents($this->envPath)); |
| 497 | + } |
| 498 | + |
| 499 | + public function testRotateErrorsWhenEnvFileIsMissing(): void |
| 500 | + { |
| 501 | + // No seedEnv() call: `.env` is absent. Populate the env var directly so |
| 502 | + // the up-front `encryption.key` existence check passes. |
| 503 | + putenv('encryption.key=' . self::SEED_KEY); |
| 504 | + $_ENV['encryption.key'] = self::SEED_KEY; |
| 505 | + |
| 506 | + command('key:rotate --force'); |
| 507 | + |
| 508 | + $this->assertStringContainsString('Cannot rotate: `.env` file not found at', $this->getUndecoratedBuffer()); |
| 509 | + $this->assertFileDoesNotExist($this->envPath, 'No `.env` file should have been created.'); |
| 510 | + } |
| 511 | + |
| 512 | + #[RequiresOperatingSystem('Linux|Darwin')] |
| 513 | + public function testRotateErrorsWhenEnvFileIsNotWritable(): void |
| 514 | + { |
| 515 | + $this->seedEnv(self::SEED_KEY); |
| 516 | + $envContentsBefore = (string) file_get_contents($this->envPath); |
| 517 | + chmod($this->envPath, 0o444); |
| 518 | + |
| 519 | + try { |
| 520 | + command('key:rotate --force'); |
| 521 | + |
| 522 | + $output = $this->getUndecoratedBuffer(); |
| 523 | + $this->assertStringContainsString('Cannot rotate: `.env` file at', $output); |
| 524 | + $this->assertStringContainsString('is not writable', $output); |
| 525 | + $this->assertSame($envContentsBefore, (string) file_get_contents($this->envPath)); |
| 526 | + } finally { |
| 527 | + chmod($this->envPath, 0o644); |
| 528 | + } |
| 529 | + } |
| 530 | + |
460 | 531 | public function testRotateIgnoresCommentMentioningPreviousKeysWhenInserting(): void |
461 | 532 | { |
462 | 533 | $envContents = "# encryption.previousKeys is for decryption fallback\nencryption.key = " . self::SEED_KEY . "\n"; |
@@ -514,29 +585,4 @@ public function testRotateInsertsAfterExportPrefixedEncryptionKey(): void |
514 | 585 | ); |
515 | 586 | $this->assertSame(self::SEED_KEY, env('encryption.previousKeys')); |
516 | 587 | } |
517 | | - |
518 | | - public function testRotateErrorsWhenEnvFileIsNotWritable(): void |
519 | | - { |
520 | | - $this->seedEnv(self::SEED_KEY); |
521 | | - chmod($this->envPath, 0o444); |
522 | | - |
523 | | - try { |
524 | | - command('key:rotate --force'); |
525 | | - |
526 | | - $this->assertSame( |
527 | | - sprintf( |
528 | | - <<<'EOT' |
529 | | -
|
530 | | - Failed to write `encryption.previousKeys` to %s. |
531 | | - |
532 | | - EOT, |
533 | | - clean_path($this->envPath), |
534 | | - ), |
535 | | - $this->getUndecoratedBuffer(), |
536 | | - ); |
537 | | - $this->assertSame(self::SEED_KEY, env('encryption.key')); |
538 | | - } finally { |
539 | | - chmod($this->envPath, 0o644); |
540 | | - } |
541 | | - } |
542 | 588 | } |
0 commit comments