|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Linkage\DoctrineRowLevelSecurity\Tests\Functional; |
| 6 | + |
| 7 | +use Doctrine\Common\EventManager; |
| 8 | +use Doctrine\DBAL\Connection; |
| 9 | +use Doctrine\DBAL\DriverManager; |
| 10 | +use Doctrine\ORM\EntityManager; |
| 11 | +use Doctrine\ORM\Mapping\ClassMetadataFactory; |
| 12 | +use Doctrine\ORM\ORMSetup; |
| 13 | +use Doctrine\ORM\Tools\SchemaTool; |
| 14 | +use Linkage\DoctrineRowLevelSecurity\RowLevelSecurityAwarePostgreSqlConnection; |
| 15 | +use Linkage\DoctrineRowLevelSecurity\RowLevelSecurityListener; |
| 16 | +use Linkage\DoctrineRowLevelSecurity\Tests\Entity\Dog; |
| 17 | +use Linkage\DoctrineRowLevelSecurity\Tests\Entity\DogOwner; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | +use Symfony\Component\Cache\Adapter\ArrayAdapter; |
| 20 | + |
| 21 | +class RowLevelSecurityUsageTest extends TestCase |
| 22 | +{ |
| 23 | + private Connection $conn; |
| 24 | + |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + $connectionParams = [ |
| 28 | + 'dbname' => 'test', |
| 29 | + 'user' => 'test', |
| 30 | + 'password' => 'password', |
| 31 | + 'host' => 'localhost', |
| 32 | + 'driver' => 'pdo_pgsql', |
| 33 | + 'wrapperClass' => RowLevelSecurityAwarePostgreSqlConnection::class, |
| 34 | + ]; |
| 35 | + $conn = DriverManager::getConnection($connectionParams); |
| 36 | + foreach (explode(';', (string) file_get_contents(__DIR__ . '/drop_table.sql')) as $dropSql) { |
| 37 | + if (trim($dropSql) === '') { |
| 38 | + continue; |
| 39 | + } |
| 40 | + $conn->executeQuery($dropSql); |
| 41 | + } |
| 42 | + foreach (explode(';', (string) file_get_contents(__DIR__ . '/create_table.sql')) as $createSql) { |
| 43 | + if (trim($createSql) === '') { |
| 44 | + continue; |
| 45 | + } |
| 46 | + $conn->executeQuery($createSql); |
| 47 | + } |
| 48 | + |
| 49 | + $configuration = ORMSetup::createAttributeMetadataConfiguration( |
| 50 | + paths: [__DIR__."/../Entity"], |
| 51 | + isDevMode: true, |
| 52 | + cache: new ArrayAdapter(), |
| 53 | + ); |
| 54 | + $this->em = new EntityManager( |
| 55 | + $conn, |
| 56 | + $configuration, |
| 57 | + new EventManager(), |
| 58 | + ); |
| 59 | + $this->em->getEventManager()->addEventSubscriber(new RowLevelSecurityListener()); |
| 60 | + } |
| 61 | + |
| 62 | + public function testCreateSchema(): void |
| 63 | + { |
| 64 | + $schemaTool = new SchemaTool($this->em); |
| 65 | + $this->em->getConnection()->getDatabasePlatform()->setEventManager($this->em->getEventManager()); |
| 66 | + $classMetadataFactory = new ClassMetadataFactory(); |
| 67 | + $classMetadataFactory->setEntityManager($this->em); |
| 68 | + $sql = $schemaTool->getCreateSchemaSql([ |
| 69 | + $classMetadataFactory->getMetadataFor(DogOwner::class), |
| 70 | + $classMetadataFactory->getMetadataFor(Dog::class), |
| 71 | + ]); |
| 72 | + |
| 73 | + $this->assertContains('CREATE POLICY dog_policy ON Dog TO dog_owner USING (owner_id = current_user::uuid)', $sql); |
| 74 | + $this->assertContains('GRANT ALL ON TABLE Dog TO dog_owner', $sql); |
| 75 | + $this->assertContains('ALTER TABLE Dog ENABLE ROW LEVEL SECURITY', $sql); |
| 76 | + } |
| 77 | +} |
0 commit comments