| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
- declare(strict_types=1);
- namespace App\Tests\Controllers;
- use App\Controllers\UserController;
- use PHPUnit\Framework\Attributes\DataProvider;
- use PHPUnit\Framework\TestCase;
- /**
- * The demotion guardrails are extracted into a pure static method; this
- * test pins the rule matrix without any SessionGuard / PDO setup.
- */
- final class UserControllerTest extends TestCase
- {
- /** @return list<array{int,int,bool,bool,int,?string,string}> */
- public static function guardrailCases(): array
- {
- // [actorId, targetId, wasAdmin, willBeAdmin, totalAdmins, expected, label]
- return [
- [1, 1, true, false, 2, 'self_demote', 'self demote, 2 admins'],
- [1, 1, true, false, 1, 'self_demote', 'self demote, only admin — self wins priority'],
- [1, 2, true, false, 1, 'last_admin', 'demote the only admin (different user)'],
- [1, 2, true, false, 2, null, 'demote someone else when another admin remains'],
- [1, 2, false, true, 1, null, 'promote — never blocked'],
- [1, 2, true, true, 1, null, 'no-op — never blocked'],
- [1, 2, false, false, 1, null, 'no-op on non-admin — never blocked'],
- [1, 1, false, true, 1, null, 'actor promoting themselves? odd but not blocked'],
- ];
- }
- #[DataProvider('guardrailCases')]
- public function testDemoteGuardrail(
- int $actor,
- int $target,
- bool $was,
- bool $will,
- int $total,
- ?string $expected,
- string $label,
- ): void {
- $this->assertSame(
- $expected,
- UserController::demoteGuardrail($actor, $target, $was, $will, $total),
- $label,
- );
- }
- }
|