packages/sql-semantics-postgres/tests/Unit/Rules/Access/PrivilegeChecksTest.php
1<?php
2
3declare(strict_types=1);
4
5namespace Tests\Unit\Rules\Access;
6
7use PHPUnit\Framework\Attributes\CoversClass;
8use PHPUnit\Framework\Attributes\Medium;
9use PHPUnit\Framework\TestCase;
10
11#[CoversClass(\SqlSemantics\Platform\PostgreSql\Rules\Access\PrivilegeChecks::class)]
12#[Medium]
13final class PrivilegeChecksTest extends TestCase
14{
15 public function testPrivilegesAcceptsTheRightsOfEachKind(): void
16 {
17 self::assertSame([], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT insert, SELECT, update, delete, truncate, REFERENCES, trigger, maintain, usage, rule ON t TO joe')->facts->diagnostics));
18 }
19
20 public function testPrivilegesReportsARightOfAnotherKind(): void
21 {
22 self::assertSame(['invalid privilege type TEMP for sequence', 'invalid privilege type CONNECT for sequence'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT temporary, connect ON SEQUENCE s TO joe')->facts->diagnostics));
23 }
24
25 public function testPrivilegesLimitsDefaultsToTheKindItself(): void
26 {
27 self::assertSame(['invalid privilege type USAGE for relation'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('ALTER DEFAULT PRIVILEGES GRANT usage ON TABLES TO joe')->facts->diagnostics));
28 }
29
30 public function testColumnsReportsAKindWithoutColumns(): void
31 {
32 self::assertSame(['column privileges are only valid for relations'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT SELECT (a) ON SCHEMA s TO joe')->facts->diagnostics));
33 }
34
35 public function testColumnsReportsARightThatCannotBeLimited(): void
36 {
37 self::assertSame(['invalid privilege type DELETE for column'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT delete (a), update (a) ON t TO joe')->facts->diagnostics));
38 }
39
40 public function testNamedReportsAnUnknownName(): void
41 {
42 self::assertSame(['unrecognized privilege type "SELECT"'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT "SELECT" ON t TO joe')->facts->diagnostics));
43 }
44
45 public function testKnownMaintainFromRelease17(): void
46 {
47 self::assertSame(['unrecognized privilege type "maintain"'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics('pg-16.6'))->inspect('GRANT maintain ON t TO joe')->facts->diagnostics));
48 }
49
50 public function testGrantOptionReportsPublic(): void
51 {
52 self::assertSame(['grant options can only be granted to roles'], array_map(static fn (\SqlSemantics\Platform\PostgreSql\Model\Statement\Fact\Diagnostic $diagnostic): string => $diagnostic->message(), (new \SqlSemantics\Platform\PostgreSql\Semantics())->inspect('GRANT SELECT ON t TO joe, PUBLIC WITH GRANT OPTION')->facts->diagnostics));
53 }
54}
55