1
0

UserControllerTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Controllers;
  4. use App\Controllers\UserController;
  5. use PHPUnit\Framework\Attributes\DataProvider;
  6. use PHPUnit\Framework\TestCase;
  7. /**
  8. * The demotion guardrails are extracted into a pure static method; this
  9. * test pins the rule matrix without any SessionGuard / PDO setup.
  10. */
  11. final class UserControllerTest extends TestCase
  12. {
  13. /** @return list<array{int,int,bool,bool,int,?string,string}> */
  14. public static function guardrailCases(): array
  15. {
  16. // [actorId, targetId, wasAdmin, willBeAdmin, totalAdmins, expected, label]
  17. return [
  18. [1, 1, true, false, 2, 'self_demote', 'self demote, 2 admins'],
  19. [1, 1, true, false, 1, 'self_demote', 'self demote, only admin — self wins priority'],
  20. [1, 2, true, false, 1, 'last_admin', 'demote the only admin (different user)'],
  21. [1, 2, true, false, 2, null, 'demote someone else when another admin remains'],
  22. [1, 2, false, true, 1, null, 'promote — never blocked'],
  23. [1, 2, true, true, 1, null, 'no-op — never blocked'],
  24. [1, 2, false, false, 1, null, 'no-op on non-admin — never blocked'],
  25. [1, 1, false, true, 1, null, 'actor promoting themselves? odd but not blocked'],
  26. ];
  27. }
  28. #[DataProvider('guardrailCases')]
  29. public function testDemoteGuardrail(
  30. int $actor,
  31. int $target,
  32. bool $was,
  33. bool $will,
  34. int $total,
  35. ?string $expected,
  36. string $label,
  37. ): void {
  38. $this->assertSame(
  39. $expected,
  40. UserController::demoteGuardrail($actor, $target, $was, $will, $total),
  41. $label,
  42. );
  43. }
  44. }