From 3e102c75d24d48a0af1802fd8fd714772a5225d6 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 12 Oct 2025 18:33:07 +0300 Subject: [PATCH 01/29] Authorization instance --- src/Database/Adapter.php | 18 +++ src/Database/Adapter/SQL.php | 12 +- src/Database/Database.php | 132 +++++++++-------- src/Database/Validator/Authorization.php | 137 ++++++++++-------- .../Validator/Authorization/Input.php | 49 +++++++ 5 files changed, 220 insertions(+), 128 deletions(-) create mode 100644 src/Database/Validator/Authorization/Input.php diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 2332d9745..2fd8fba2b 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -12,6 +12,7 @@ use Utopia\Database\Exception\Restricted as RestrictedException; use Utopia\Database\Exception\Timeout as TimeoutException; use Utopia\Database\Exception\Transaction as TransactionException; +use Utopia\Database\Validator\Authorization; abstract class Adapter { @@ -47,6 +48,23 @@ abstract class Adapter */ protected array $metadata = []; + /** + * @var Authorization + */ + protected Authorization $authorization; + + /** + * @param Authorization $authorization + * + * @return $this + */ + public function setAuthorization(Authorization $authorization): self + { + $this->authorization = $authorization; + + return $this; + } + /** * @param string $key * @param mixed $value diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 84fae6ce7..73b09af6f 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -2337,7 +2337,7 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25 $collection = $collection->getId(); $name = $this->filter($collection); - $roles = Authorization::getRoles(); + $roles = $this->authorization->getRoles(); $where = []; $orders = []; $alias = Query::DEFAULT_ALIAS; @@ -2421,7 +2421,7 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25 $where[] = $conditions; } - if (Authorization::$status) { + if ($this->authorization->getStatus()) { $where[] = $this->getSQLPermissionsCondition($name, $roles, $alias, $forPermission); } @@ -2527,7 +2527,7 @@ public function count(Document $collection, array $queries = [], ?int $max = nul $attributes = $collection->getAttribute("attributes", []); $collection = $collection->getId(); $name = $this->filter($collection); - $roles = Authorization::getRoles(); + $roles = $this->authorization->getRoles(); $binds = []; $where = []; $alias = Query::DEFAULT_ALIAS; @@ -2545,7 +2545,7 @@ public function count(Document $collection, array $queries = [], ?int $max = nul $where[] = $conditions; } - if (Authorization::$status) { + if ($this->authorization->getStatus()) { $where[] = $this->getSQLPermissionsCondition($name, $roles, $alias); } @@ -2603,7 +2603,7 @@ public function sum(Document $collection, string $attribute, array $queries = [] $collection = $collection->getId(); $name = $this->filter($collection); $attribute = $this->filter($attribute); - $roles = Authorization::getRoles(); + $roles = $this->authorization->getRoles(); $where = []; $alias = Query::DEFAULT_ALIAS; $binds = []; @@ -2621,7 +2621,7 @@ public function sum(Document $collection, string $attribute, array $queries = [] $where[] = $conditions; } - if (Authorization::$status) { + if ($this->authorization->getStatus()) { $where[] = $this->getSQLPermissionsCondition($name, $roles, $alias); } diff --git a/src/Database/Database.php b/src/Database/Database.php index 56a65e724..fc9a76d13 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -396,6 +396,12 @@ class Database */ protected array $relationshipDeleteStack = []; + + /** + * @var Authorization + */ + private Authorization $authorization; + /** * @param Adapter $adapter * @param Cache $cache @@ -410,6 +416,8 @@ public function __construct( $this->cache = $cache; $this->instanceFilters = $filters; + $this->setAuthorization(new Authorization()); + self::addFilter( 'json', /** @@ -849,6 +857,19 @@ public function getMetadata(): array return $this->adapter->getMetadata(); } + /** + * Sets instance of authorization for permission checks + * + * @param Authorization $authorization + * @return self + */ + public function setAuthorization(Authorization $authorization): self + { + $this->adapter->setAuthorization($authorization); + $this->authorization = $authorization; + return $this; + } + /** * Clear metadata * @@ -3482,7 +3503,6 @@ public function getDocument(string $collection, string $id, array $queries = [], $selections = $this->validateSelections($collection, $selects); $nestedSelections = $this->processRelationshipQueries($relationships, $queries); - $validator = new Authorization(self::PERMISSION_READ); $documentSecurity = $collection->getAttribute('documentSecurity', false); [$collectionKey, $documentKey, $hashKey] = $this->getCacheKeys( @@ -3502,10 +3522,11 @@ public function getDocument(string $collection, string $id, array $queries = [], $document = new Document($cached); if ($collection->getId() !== self::METADATA) { - if (!$validator->isValid([ + + if (!$this->authorization->isValid(new Input(self::PERMISSION_READ, [ ...$collection->getRead(), ...($documentSecurity ? $document->getRead() : []) - ])) { + ]))) { return new Document(); } } @@ -3528,10 +3549,10 @@ public function getDocument(string $collection, string $id, array $queries = [], $document->setAttribute('$collection', $collection->getId()); if ($collection->getId() !== self::METADATA) { - if (!$validator->isValid([ + if (!$this->authorization->isValid(new Input(self::PERMISSION_READ, [ ...$collection->getRead(), ...($documentSecurity ? $document->getRead() : []) - ])) { + ]))) { return new Document(); } } @@ -4194,9 +4215,9 @@ public function createDocument(string $collection, Document $document): Document $collection = $this->silent(fn () => $this->getCollection($collection)); if ($collection->getId() !== self::METADATA) { - $authorization = new Authorization(self::PERMISSION_CREATE); - if (!$authorization->isValid($collection->getCreate())) { - throw new AuthorizationException($authorization->getDescription()); + $isValid = $this->authorization->isValid(new Input(self::PERMISSION_CREATE, $collection->getCreate())); + if (!$isValid) { + throw new AuthorizationException($this->authorization->getDescription()); } } @@ -4729,7 +4750,7 @@ public function updateDocument(string $collection, string $id, Document $documen $newUpdatedAt = $document->getUpdatedAt(); $document = $this->withTransaction(function () use ($collection, $id, $document, $newUpdatedAt) { $time = DateTime::now(); - $old = Authorization::skip(fn () => $this->silent( + $old = $this->authorization->skip(fn () => $this->silent( fn () => $this->getDocument($collection->getId(), $id, forUpdate: true) )); if ($old->isEmpty()) { @@ -4762,8 +4783,6 @@ public function updateDocument(string $collection, string $id, Document $documen return $attribute['type'] === Database::VAR_RELATIONSHIP; }); - $updateValidator = new Authorization(self::PERMISSION_UPDATE); - $readValidator = new Authorization(self::PERMISSION_READ); $shouldUpdate = false; if ($collection->getId() !== self::METADATA) { @@ -4856,25 +4875,24 @@ public function updateDocument(string $collection, string $id, Document $documen break; } } + } + + if ($shouldUpdate) { - $updatePermissions = [ + $isUpdateValid = $this->authorization->isValid(new Input(self::PERMISSION_UPDATE, [ ...$collection->getUpdate(), ...($documentSecurity ? $old->getUpdate() : []) - ]; + ])); - $readPermissions = [ + $isReadValid = $this->authorization->isValid(new Input(self::PERMISSION_READ, [ ...$collection->getRead(), ...($documentSecurity ? $old->getRead() : []) - ]; + ])); - if ($shouldUpdate && !$updateValidator->isValid($updatePermissions)) { - throw new AuthorizationException($updateValidator->getDescription()); - } elseif (!$shouldUpdate && !$readValidator->isValid($readPermissions)) { - throw new AuthorizationException($readValidator->getDescription()); + if (!$isReadValid || !$isUpdateValid) { + throw new AuthorizationException($this->authorization->getDescription()); } - } - if ($shouldUpdate) { $document->setAttribute('$updatedAt', ($newUpdatedAt === null || !$this->preserveDates) ? $time : $newUpdatedAt); } @@ -5333,7 +5351,7 @@ private function updateDocumentRelationships(Document $collection, Document $old $removedDocuments = \array_diff($oldIds, $newIds); foreach ($removedDocuments as $relation) { - Authorization::skip(fn () => $this->skipRelationships(fn () => $this->updateDocument( + $this->authorization->skip(fn () => $this->skipRelationships(fn () => $this->updateDocument( $relatedCollection->getId(), $relation, new Document([$twoWayKey => null]) @@ -5461,7 +5479,7 @@ private function updateDocumentRelationships(Document $collection, Document $old ]); foreach ($junctions as $junction) { - Authorization::skip(fn () => $this->deleteDocument($junction->getCollection(), $junction->getId())); + $this->authorization->skip(fn () => $this->deleteDocument($junction->getCollection(), $junction->getId())); } } @@ -5626,12 +5644,12 @@ public function upsertDocumentsWithIncrease( $seenIds = []; foreach ($documents as $key => $document) { if ($this->getSharedTables() && $this->getTenantPerDocument()) { - $old = Authorization::skip(fn () => $this->withTenant($document->getTenant(), fn () => $this->silent(fn () => $this->getDocument( + $old = $this->authorization->skip(fn () => $this->withTenant($document->getTenant(), fn () => $this->silent(fn () => $this->getDocument( $collection->getId(), $document->getId(), )))); } else { - $old = Authorization::skip(fn () => $this->silent(fn () => $this->getDocument( + $old = $this->authorization->skip(fn () => $this->silent(fn () => $this->getDocument( $collection->getId(), $document->getId(), ))); @@ -5664,21 +5682,16 @@ public function upsertDocumentsWithIncrease( // If old is not empty, check if user has update permission on the collection // If old is not empty AND documentSecurity is enabled, check if user has update permission on the collection or document - $validator = new Authorization( - $old->isEmpty() ? - self::PERMISSION_CREATE : - self::PERMISSION_UPDATE - ); if ($old->isEmpty()) { - if (!$validator->isValid($collection->getCreate())) { - throw new AuthorizationException($validator->getDescription()); + if (!$this->authorization->isValid(new Input(self::PERMISSION_CREATE, $collection->getCreate()))) { + throw new AuthorizationException($this->authorization->getDescription()); } - } elseif (!$validator->isValid([ + } elseif (!$this->authorization->isValid(new Input(self::PERMISSION_UPDATE, [ ...$collection->getUpdate(), ...($documentSecurity ? $old->getUpdate() : []) - ])) { - throw new AuthorizationException($validator->getDescription()); + ]))) { + throw new AuthorizationException($this->authorization->getDescription()); } $updatedAt = $document->getUpdatedAt(); @@ -5771,7 +5784,7 @@ public function upsertDocumentsWithIncrease( /** * @var array $chunk */ - $batch = $this->withTransaction(fn () => Authorization::skip(fn () => $this->adapter->upsertDocuments( + $batch = $this->withTransaction(fn () =>$this->authorization->skip(fn () => $this->adapter->upsertDocuments( $collection, $attribute, $chunk @@ -5871,21 +5884,20 @@ public function increaseDocumentAttribute( $document = $this->withTransaction(function () use ($collection, $id, $attribute, $value, $max) { /* @var $document Document */ - $document = Authorization::skip(fn () => $this->silent(fn () => $this->getDocument($collection->getId(), $id, forUpdate: true))); // Skip ensures user does not need read permission for this + $document = $this->authorization->skip(fn () => $this->silent(fn () => $this->getDocument($collection->getId(), $id, forUpdate: true))); // Skip ensures user does not need read permission for this if ($document->isEmpty()) { throw new NotFoundException('Document not found'); } - $validator = new Authorization(self::PERMISSION_UPDATE); - if ($collection->getId() !== self::METADATA) { $documentSecurity = $collection->getAttribute('documentSecurity', false); - if (!$validator->isValid([ + + if (!$this->authorization->isValid(new Input(self::PERMISSION_UPDATE, [ ...$collection->getUpdate(), ...($documentSecurity ? $document->getUpdate() : []) - ])) { - throw new AuthorizationException($validator->getDescription()); + ]))) { + throw new AuthorizationException($this->authorization->getDescription()); } } @@ -5970,21 +5982,20 @@ public function decreaseDocumentAttribute( $document = $this->withTransaction(function () use ($collection, $id, $attribute, $value, $min) { /* @var $document Document */ - $document = Authorization::skip(fn () => $this->silent(fn () => $this->getDocument($collection->getId(), $id, forUpdate: true))); // Skip ensures user does not need read permission for this + $document = $this->authorization->skip(fn () => $this->silent(fn () => $this->getDocument($collection->getId(), $id, forUpdate: true))); // Skip ensures user does not need read permission for this if ($document->isEmpty()) { throw new NotFoundException('Document not found'); } - $validator = new Authorization(self::PERMISSION_UPDATE); - if ($collection->getId() !== self::METADATA) { $documentSecurity = $collection->getAttribute('documentSecurity', false); - if (!$validator->isValid([ + + if (!$this->authorization->isValid(new Input(self::PERMISSION_UPDATE, [ ...$collection->getUpdate(), ...($documentSecurity ? $document->getUpdate() : []) - ])) { - throw new AuthorizationException($validator->getDescription()); + ]))) { + throw new AuthorizationException($this->authorization->getDescription()); } } @@ -6037,7 +6048,7 @@ public function deleteDocument(string $collection, string $id): bool $collection = $this->silent(fn () => $this->getCollection($collection)); $deleted = $this->withTransaction(function () use ($collection, $id, &$document) { - $document = Authorization::skip(fn () => $this->silent( + $document = $this->authorization->skip(fn () => $this->silent( fn () => $this->getDocument($collection->getId(), $id, forUpdate: true) )); @@ -6045,15 +6056,14 @@ public function deleteDocument(string $collection, string $id): bool return false; } - $validator = new Authorization(self::PERMISSION_DELETE); - if ($collection->getId() !== self::METADATA) { $documentSecurity = $collection->getAttribute('documentSecurity', false); - if (!$validator->isValid([ + + if (!$this->authorization->isValid(new Input([ ...$collection->getDelete(), ...($documentSecurity ? $document->getDelete() : []) - ])) { - throw new AuthorizationException($validator->getDescription()); + ]))) { + throw new AuthorizationException($this->authorization->getDescription()); } } @@ -6217,7 +6227,7 @@ private function deleteRestrict( && $side === Database::RELATION_SIDE_CHILD && !$twoWay ) { - Authorization::skip(function () use ($document, $relatedCollection, $twoWayKey) { + $this->authorization->skip(function () use ($document, $relatedCollection, $twoWayKey) { $related = $this->findOne($relatedCollection->getId(), [ Query::select(['$id']), Query::equal($twoWayKey, [$document->getId()]) @@ -6241,7 +6251,7 @@ private function deleteRestrict( $relationType === Database::RELATION_MANY_TO_ONE && $side === Database::RELATION_SIDE_CHILD ) { - $related = Authorization::skip(fn () => $this->findOne($relatedCollection->getId(), [ + $related = $this->authorization->skip(fn () => $this->findOne($relatedCollection->getId(), [ Query::select(['$id']), Query::equal($twoWayKey, [$document->getId()]) ])); @@ -6277,7 +6287,7 @@ private function deleteSetNull(Document $collection, Document $relatedCollection } // Shouldn't need read or update permission to delete - Authorization::skip(function () use ($document, $value, $relatedCollection, $twoWay, $twoWayKey, $side) { + $this->authorization->skip(function () use ($document, $value, $relatedCollection, $twoWay, $twoWayKey, $side) { if (!$twoWay && $side === Database::RELATION_SIDE_CHILD) { $related = $this->findOne($relatedCollection->getId(), [ Query::select(['$id']), @@ -6309,7 +6319,7 @@ private function deleteSetNull(Document $collection, Document $relatedCollection break; } foreach ($value as $relation) { - Authorization::skip(function () use ($relatedCollection, $twoWayKey, $relation) { + $this->authorization->skip(function () use ($relatedCollection, $twoWayKey, $relation) { $this->skipRelationships(fn () => $this->updateDocument( $relatedCollection->getId(), $relation->getId(), @@ -6335,7 +6345,7 @@ private function deleteSetNull(Document $collection, Document $relatedCollection } foreach ($value as $relation) { - Authorization::skip(function () use ($relatedCollection, $twoWayKey, $relation) { + $this->authorization->skip(function () use ($relatedCollection, $twoWayKey, $relation) { $this->skipRelationships(fn () => $this->updateDocument( $relatedCollection->getId(), $relation->getId(), @@ -6805,7 +6815,7 @@ public function find(string $collection, array $queries = [], string $forPermiss $forPermission ); - $results = $skipAuth ? Authorization::skip($getResults) : $getResults(); + $results = $skipAuth ? $this->authorization->skip($getResults) : $getResults(); } if (!$this->inBatchRelationshipPopulation && $this->resolveRelationships && !empty($relationships) && (empty($selects) || !empty($nestedSelections))) { @@ -6969,7 +6979,7 @@ public function count(string $collection, array $queries = [], ?int $max = null) $queries = $queriesOrNull; $getCount = fn () => $this->adapter->count($collection, $queries, $max); - $count = $skipAuth ?? false ? Authorization::skip($getCount) : $getCount(); + $count = $skipAuth ?? false ? $this->authorization->skip($getCount) : $getCount(); $this->trigger(self::EVENT_DOCUMENT_COUNT, $count); diff --git a/src/Database/Validator/Authorization.php b/src/Database/Validator/Authorization.php index 2dd645ba7..d1bc67fa6 100644 --- a/src/Database/Validator/Authorization.php +++ b/src/Database/Validator/Authorization.php @@ -2,34 +2,35 @@ namespace Utopia\Database\Validator; +use Utopia\Database\Validator\Authorization\Input; use Utopia\Validator; class Authorization extends Validator { /** - * @var array + * @var bool */ - private static array $roles = [ - 'any' => true - ]; + protected bool $status = true; /** - * @var string + * Default value in case we need + * to reset Authorization status + * + * @var bool */ - protected string $action = ''; + protected bool $statusDefault = true; /** - * @var string + * @var array */ - protected string $message = 'Authorization Error'; + private array $roles = [ + 'any' => true + ]; /** - * @param string $action + * @var string */ - public function __construct(string $action) - { - $this->action = $action; - } + protected string $message = 'Authorization Error'; /** * Get Description. @@ -43,36 +44,43 @@ public function getDescription(): string return $this->message; } - /** - * Is valid. + /* + * Validation * * Returns true if valid or false if not. - * - * @param mixed $permissions - * - * @return bool - */ - public function isValid($permissions): bool + */ + public function isValid(mixed $input): bool // any, CREATE { - if (!self::$status) { + if(!($input instanceof Input)) { + $this->message = 'Invalid input provided'; + return false; + } + + /** + * @var Input $input + */ + + $permissions = $input->getPermissions(); + $action = $input->getAction(); + + if (!$this->status) { return true; } if (empty($permissions)) { - $this->message = 'No permissions provided for action \''.$this->action.'\''; + $this->message = 'No permissions provided for action \''.$action.'\''; return false; } $permission = '-'; foreach ($permissions as $permission) { - if (\array_key_exists($permission, self::$roles)) { + if (\array_key_exists($permission, $this->roles)) { return true; } } - $this->message = 'Missing "'.$this->action.'" permission for role "'.$permission.'". Only "'.\json_encode(self::getRoles()).'" scopes are allowed and "'.\json_encode($permissions).'" was given.'; - + $this->message = 'Missing "'.$action.'" permission for role "'.$permission.'". Only "'.\json_encode($this->getRoles()).'" scopes are allowed and "'.\json_encode($permissions).'" was given.'; return false; } @@ -80,9 +88,9 @@ public function isValid($permissions): bool * @param string $role * @return void */ - public static function setRole(string $role): void + public function addRole(string $role): void // user:meldiron, users, any { - self::$roles[$role] = true; + $this->roles[$role] = true; } /** @@ -90,25 +98,25 @@ public static function setRole(string $role): void * * @return void */ - public static function unsetRole(string $role): void + public function removeRole(string $role): void { - unset(self::$roles[$role]); + unset($this->roles[$role]); } /** * @return array */ - public static function getRoles(): array + public function getRoles(): array { - return \array_keys(self::$roles); + return \array_keys($this->roles); } /** * @return void */ - public static function cleanRoles(): void + public function cleanRoles(): void { - self::$roles = []; + $this->roles = []; } /** @@ -116,36 +124,43 @@ public static function cleanRoles(): void * * @return bool */ - public static function isRole(string $role): bool + public function isRole(string $role): bool { - return (\array_key_exists($role, self::$roles)); + return (\array_key_exists($role, $this->roles)); } /** - * @var bool + * Change default status. + * This will be used for the + * value set on the $this->reset() method + * @param bool $status + * @return void */ - public static bool $status = true; + public function setDefaultStatus(bool $status): void + { + $this->statusDefault = $status; + $this->status = $status; + } /** - * Default value in case we need - * to reset Authorization status + * Change status * - * @var bool + * @param bool $status + * @return void */ - public static bool $statusDefault = true; + public function setStatus(bool $status): void + { + $this->status = $status; + } /** - * Change default status. - * This will be used for the - * value set on the self::reset() method + * Get status * - * @param bool $status - * @return void + * @return bool */ - public static function setDefaultStatus(bool $status): void + public function getStatus(): bool { - self::$statusDefault = $status; - self::$status = $status; + return $this->status; } /** @@ -157,15 +172,15 @@ public static function setDefaultStatus(bool $status): void * @param callable(): T $callback * @return T */ - public static function skip(callable $callback): mixed + public function skip(callable $callback): mixed { - $initialStatus = self::$status; - self::disable(); + $initialStatus = $this->status; + $this->disable(); try { return $callback(); } finally { - self::$status = $initialStatus; + $this->status = $initialStatus; } } @@ -174,9 +189,9 @@ public static function skip(callable $callback): mixed * * @return void */ - public static function enable(): void + public function enable(): void { - self::$status = true; + $this->status = true; } /** @@ -184,9 +199,9 @@ public static function enable(): void * * @return void */ - public static function disable(): void + public function disable(): void { - self::$status = false; + $this->status = false; } /** @@ -194,9 +209,9 @@ public static function disable(): void * * @return void */ - public static function reset(): void + public function reset(): void { - self::$status = self::$statusDefault; + $this->status = $this->statusDefault; } /** @@ -222,4 +237,4 @@ public function getType(): string { return self::TYPE_ARRAY; } -} +} \ No newline at end of file diff --git a/src/Database/Validator/Authorization/Input.php b/src/Database/Validator/Authorization/Input.php new file mode 100644 index 000000000..3002c5797 --- /dev/null +++ b/src/Database/Validator/Authorization/Input.php @@ -0,0 +1,49 @@ +permissions = $permissions; + $this->action = $action; + } + + /** + * @param string[] $permissions + */ + public function setPermissions(array $permissions): self + { + $this->permissions = $permissions; + return $this; + } + + public function setAction(string $action): self + { + $this->action = $action; + return $this; + } + + /** + * @return string[] + */ + public function getPermissions(): array + { + return $this->permissions; + } + + public function getAction(): string + { + return $this->action; + } +} \ No newline at end of file From 403ffbe863cfb822c835ca2745133dcffccb1dd2 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 12 Oct 2025 19:15:16 +0300 Subject: [PATCH 02/29] Authorization instance --- tests/e2e/Adapter/Base.php | 15 +- tests/e2e/Adapter/Scopes/AttributeTests.php | 80 +++--- tests/e2e/Adapter/Scopes/CollectionTests.php | 54 ++-- tests/e2e/Adapter/Scopes/DocumentTests.php | 240 +++++++++--------- tests/e2e/Adapter/Scopes/GeneralTests.php | 21 +- tests/e2e/Adapter/Scopes/IndexTests.php | 20 +- tests/e2e/Adapter/Scopes/PermissionTests.php | 68 ++--- .../e2e/Adapter/Scopes/RelationshipTests.php | 66 ++--- .../Scopes/Relationships/ManyToManyTests.php | 32 +-- .../Scopes/Relationships/ManyToOneTests.php | 30 +-- .../Scopes/Relationships/OneToManyTests.php | 34 +-- .../Scopes/Relationships/OneToOneTests.php | 36 +-- tests/e2e/Adapter/Scopes/SpatialTests.php | 50 ++-- 13 files changed, 376 insertions(+), 370 deletions(-) diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 37ad7cce3..32a16189e 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -29,11 +29,15 @@ abstract class Base extends TestCase protected static string $namespace; + /** + * @var Authorization + */ + protected static ?Authorization $authorization = null; /** * @return Database */ - abstract protected static function getDatabase(): Database; + abstract protected function getDatabase(): Database; /** * @param string $collection @@ -53,12 +57,17 @@ abstract protected static function deleteIndex(string $collection, string $index public function setUp(): void { - Authorization::setRole('any'); + if (is_null(self::$authorization)) { + self::$authorization = new Authorization(); + } + + self::$authorization->addRole('any'); } public function tearDown(): void { - Authorization::setDefaultStatus(true); + self::$authorization->setDefaultStatus(true); + } protected string $testDatabase = 'utopiaTests'; diff --git a/tests/e2e/Adapter/Scopes/AttributeTests.php b/tests/e2e/Adapter/Scopes/AttributeTests.php index 89ab81a50..8e9c4921d 100644 --- a/tests/e2e/Adapter/Scopes/AttributeTests.php +++ b/tests/e2e/Adapter/Scopes/AttributeTests.php @@ -59,7 +59,7 @@ public function invalidDefaultValues(): array public function testCreateDeleteAttribute(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('attributes'); @@ -181,7 +181,7 @@ public function testCreateDeleteAttribute(): void public function testInvalidDefaultValues(string $type, mixed $default): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectException(\Exception::class); $this->assertEquals(false, $database->createAttribute('attributes', 'bad_default', $type, 256, true, $default)); @@ -192,7 +192,7 @@ public function testInvalidDefaultValues(string $type, mixed $default): void public function testAttributeCaseInsensitivity(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->assertEquals(true, $database->createAttribute('attributes', 'caseSensitive', Database::VAR_STRING, 128, true)); $this->expectException(DuplicateException::class); @@ -202,7 +202,7 @@ public function testAttributeCaseInsensitivity(): void public function testAttributeKeyWithSymbols(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('attributesWithKeys'); @@ -225,7 +225,7 @@ public function testAttributeKeyWithSymbols(): void public function testAttributeNamesWithDots(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('dots.parent'); @@ -289,7 +289,7 @@ public function testAttributeNamesWithDots(): void public function testUpdateAttributeDefault(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $flowers = $database->createCollection('flowers'); $database->createAttribute('flowers', 'name', Database::VAR_STRING, 128, true); @@ -343,7 +343,7 @@ public function testUpdateAttributeDefault(): void public function testRenameAttribute(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $colors = $database->createCollection('colors'); $database->createAttribute('colors', 'name', Database::VAR_STRING, 128, true); @@ -390,7 +390,7 @@ public function testRenameAttribute(): void public function testUpdateAttributeRequired(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->updateAttributeRequired('flowers', 'inStock', true); @@ -413,7 +413,7 @@ public function testUpdateAttributeRequired(): void public function testUpdateAttributeFilter(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createAttribute('flowers', 'cartModel', Database::VAR_STRING, 2000, false); @@ -447,7 +447,7 @@ public function testUpdateAttributeFilter(): void public function testUpdateAttributeFormat(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createAttribute('flowers', 'price', Database::VAR_INTEGER, 0, false); @@ -509,7 +509,7 @@ public function testUpdateAttributeStructure(): void }, Database::VAR_INTEGER); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); // price attribute $collection = $database->getCollection('flowers'); @@ -647,7 +647,7 @@ public function testUpdateAttributeStructure(): void public function testUpdateAttributeRename(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('rename_test'); @@ -771,7 +771,7 @@ public function testUpdateAttributeRename(): void public function textRenameAttributeMissing(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectExceptionMessage('Attribute not found'); $database->renameAttribute('colors', 'name2', 'name3'); @@ -784,7 +784,7 @@ public function textRenameAttributeMissing(): void public function testRenameAttributeExisting(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectExceptionMessage('Attribute name already used'); $database->renameAttribute('colors', 'verbose', 'hex'); @@ -793,7 +793,7 @@ public function testRenameAttributeExisting(): void public function testWidthLimit(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getDocumentSizeLimit() === 0) { $this->expectNotToPerformAssertions(); @@ -874,7 +874,7 @@ public function testWidthLimit(): void public function testExceptionAttributeLimit(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getLimitForAttributes() === 0) { $this->expectNotToPerformAssertions(); @@ -945,7 +945,7 @@ public function testExceptionAttributeLimit(): void public function testExceptionWidthLimit(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getDocumentSizeLimit() === 0) { $this->expectNotToPerformAssertions(); @@ -1023,7 +1023,7 @@ public function testExceptionWidthLimit(): void public function testUpdateAttributeSize(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForAttributeResizing()) { $this->expectNotToPerformAssertions(); @@ -1134,7 +1134,7 @@ public function testUpdateAttributeSize(): void public function testEncryptAttributes(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); // Add custom encrypt filter $database->addFilter( @@ -1185,7 +1185,7 @@ function (mixed $value) { public function updateStringAttributeSize(int $size, Document $document): Document { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->updateAttribute('resize_test', 'resize_me', Database::VAR_STRING, $size, true); @@ -1206,7 +1206,7 @@ public function updateStringAttributeSize(int $size, Document $document): Docume public function testIndexCaseInsensitivity(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->assertEquals(true, $database->createIndex('attributes', 'key_caseSensitive', Database::INDEX_KEY, ['caseSensitive'], [128])); @@ -1225,7 +1225,7 @@ public function testIndexCaseInsensitivity(): void public function testCleanupAttributeTests(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->deleteCollection('attributes'); $this->assertEquals(1, 1); @@ -1243,7 +1243,7 @@ public function testArrayAttribute(): void Authorization::setRole(Role::any()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $collection = 'json'; $permissions = [Permission::read(Role::any())]; @@ -1562,7 +1562,7 @@ public function testArrayAttribute(): void public function testCreateDatetime(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('datetime'); @@ -1694,7 +1694,7 @@ public function testCreateDatetime(): void public function testCreateDateTimeAttributeFailure(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('datetime_fail'); @@ -1709,7 +1709,7 @@ public function testCreateDateTimeAttributeFailure(): void public function testUnknownFormat(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectException(\Exception::class); $this->assertEquals(false, $database->createAttribute('attributes', 'bad_format', Database::VAR_STRING, 256, true, null, true, false, 'url')); @@ -1720,7 +1720,7 @@ public function testUnknownFormat(): void public function testCreateAttributesEmpty(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -1740,7 +1740,7 @@ public function testCreateAttributesEmpty(): void public function testCreateAttributesMissingId(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -1765,7 +1765,7 @@ public function testCreateAttributesMissingId(): void public function testCreateAttributesMissingType(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -1790,7 +1790,7 @@ public function testCreateAttributesMissingType(): void public function testCreateAttributesMissingSize(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -1815,7 +1815,7 @@ public function testCreateAttributesMissingSize(): void public function testCreateAttributesMissingRequired(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -1840,7 +1840,7 @@ public function testCreateAttributesMissingRequired(): void public function testCreateAttributesDuplicateMetadata(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -1868,7 +1868,7 @@ public function testCreateAttributesDuplicateMetadata(): void public function testCreateAttributesInvalidFilter(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -1895,7 +1895,7 @@ public function testCreateAttributesInvalidFilter(): void public function testCreateAttributesInvalidFormat(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -1923,7 +1923,7 @@ public function testCreateAttributesInvalidFormat(): void public function testCreateAttributesDefaultOnRequired(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -1951,7 +1951,7 @@ public function testCreateAttributesDefaultOnRequired(): void public function testCreateAttributesUnknownType(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -1978,7 +1978,7 @@ public function testCreateAttributesUnknownType(): void public function testCreateAttributesStringSizeLimit(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -2007,7 +2007,7 @@ public function testCreateAttributesStringSizeLimit(): void public function testCreateAttributesIntegerSizeLimit(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -2036,7 +2036,7 @@ public function testCreateAttributesIntegerSizeLimit(): void public function testCreateAttributesSuccessMultiple(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); @@ -2081,7 +2081,7 @@ public function testCreateAttributesSuccessMultiple(): void public function testCreateAttributesDelete(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchCreateAttributes()) { $this->expectNotToPerformAssertions(); diff --git a/tests/e2e/Adapter/Scopes/CollectionTests.php b/tests/e2e/Adapter/Scopes/CollectionTests.php index 5178a414d..d88127dd4 100644 --- a/tests/e2e/Adapter/Scopes/CollectionTests.php +++ b/tests/e2e/Adapter/Scopes/CollectionTests.php @@ -23,7 +23,7 @@ trait CollectionTests public function testCreateExistsDelete(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSchemas()) { $this->expectNotToPerformAssertions(); @@ -42,7 +42,7 @@ public function testCreateExistsDelete(): void public function testCreateListExistsDeleteCollection(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->assertInstanceOf('Utopia\Database\Document', $database->createCollection('actors', permissions: [ Permission::create(Role::any()), @@ -77,7 +77,7 @@ public function testCreateListExistsDeleteCollection(): void public function testCreateCollectionWithSchema(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $attributes = [ new Document([ @@ -309,7 +309,7 @@ public function testCreateCollectionValidator(): void ]; /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); foreach ($collections as $id) { $collection = $database->createCollection($id, $attributes, $indexes); @@ -347,7 +347,7 @@ public function testCreateCollectionValidator(): void public function testCollectionNotFound(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); try { $database->find('not_exist', []); @@ -360,7 +360,7 @@ public function testCollectionNotFound(): void public function testSizeCollection(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('sizeTest1'); $database->createCollection('sizeTest2'); @@ -452,7 +452,7 @@ public function testSizeCollectionOnDisk(): void public function testSizeFullText(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); // SQLite does not support fulltext indexes if (!$database->getAdapter()->getSupportForFulltextIndex()) { @@ -493,7 +493,7 @@ public function testSizeFullText(): void public function testPurgeCollectionCache(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('redis'); @@ -535,7 +535,7 @@ public function testSchemaAttributes(): void } $collection = 'schema_attributes'; - $db = static::getDatabase(); + $db = $this->getDatabase(); $this->assertEmpty($db->getSchemaAttributes('no_such_collection')); @@ -594,7 +594,7 @@ public function testSchemaAttributes(): void public function testRowSizeToLarge(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getDocumentSizeLimit() === 0) { $this->expectNotToPerformAssertions(); @@ -650,7 +650,7 @@ public function testRowSizeToLarge(): void public function testCreateCollectionWithSchemaIndexes(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $attributes = [ new Document([ @@ -724,7 +724,7 @@ public function testCreateCollectionWithSchemaIndexes(): void public function testCollectionUpdate(): Document { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $collection = $database->createCollection('collectionUpdate', permissions: [ Permission::create(Role::users()), @@ -759,7 +759,7 @@ public function testCollectionUpdate(): Document public function testUpdateDeleteCollectionNotFound(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); try { $database->deleteCollection('not_found'); @@ -779,7 +779,7 @@ public function testUpdateDeleteCollectionNotFound(): void public function testGetCollectionId(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForGetConnectionId()) { $this->expectNotToPerformAssertions(); @@ -791,7 +791,7 @@ public function testGetCollectionId(): void public function testKeywords(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $keywords = $database->getKeywords(); // Collection name tests @@ -898,7 +898,7 @@ public function testKeywords(): void public function testLabels(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->assertInstanceOf('Utopia\Database\Document', $database->createCollection( 'labels_test', @@ -927,7 +927,7 @@ public function testLabels(): void public function testMetadata(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->setMetadata('key', 'value'); @@ -943,7 +943,7 @@ public function testMetadata(): void public function testDeleteCollectionDeletesRelationships(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -981,7 +981,7 @@ public function testDeleteCollectionDeletesRelationships(): void public function testCascadeMultiDelete(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1061,7 +1061,7 @@ public function testSharedTables(): void * Default mode already tested, we'll test 'schema' and 'table' isolation here */ /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $sharedTables = $database->getSharedTables(); $namespace = $database->getNamespace(); $schema = $database->getDatabase(); @@ -1245,7 +1245,7 @@ public function testSharedTables(): void public function testCreateDuplicates(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('duplicates', permissions: [ Permission::read(Role::any()) @@ -1265,7 +1265,7 @@ public function testCreateDuplicates(): void public function testSharedTablesDuplicates(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $sharedTables = $database->getSharedTables(); $namespace = $database->getNamespace(); $schema = $database->getDatabase(); @@ -1330,7 +1330,7 @@ public function testSharedTablesDuplicates(): void public function testEvents(): void { Authorization::skip(function () { - $database = static::getDatabase(); + $database = $this->getDatabase(); $events = [ Database::EVENT_DATABASE_CREATE, @@ -1460,7 +1460,7 @@ public function testEvents(): void public function testCreatedAtUpdatedAt(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->assertInstanceOf('Utopia\Database\Document', $database->createCollection('created_at')); $database->createAttribute('created_at', 'title', Database::VAR_STRING, 100, false); @@ -1485,7 +1485,7 @@ public function testCreatedAtUpdatedAt(): void public function testCreatedAtUpdatedAtAssert(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->getDocument('created_at', 'uid123'); $this->assertEquals(true, !$document->isEmpty()); @@ -1504,7 +1504,7 @@ public function testCreatedAtUpdatedAtAssert(): void public function testTransformations(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('docs', attributes: [ new Document([ @@ -1531,7 +1531,7 @@ public function testTransformations(): void public function testSetGlobalCollection(): void { - $db = static::getDatabase(); + $db = $this->getDatabase(); $collectionId = 'globalCollection'; diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 81f3eee5b..19f5c8070 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -25,7 +25,7 @@ trait DocumentTests public function testBigintSequence(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection(__FUNCTION__); @@ -50,7 +50,7 @@ public function testBigintSequence(): void public function testCreateDocument(): Document { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('documents'); @@ -333,7 +333,7 @@ public function testCreateDocument(): Document public function testCreateDocumentNumericalId(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('numericalIds'); @@ -365,7 +365,7 @@ public function testCreateDocuments(): void $collection = 'testCreateDocuments'; /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection($collection); @@ -428,7 +428,7 @@ public function testCreateDocuments(): void public function testCreateDocumentsWithAutoIncrement(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection(__FUNCTION__); @@ -470,7 +470,7 @@ public function testCreateDocumentsWithDifferentAttributes(): void $collection = 'testDiffAttributes'; /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection($collection); @@ -540,7 +540,7 @@ public function testCreateDocumentsWithDifferentAttributes(): void public function testSkipPermissions(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForUpserts()) { $this->expectNotToPerformAssertions(); @@ -608,7 +608,7 @@ public function testSkipPermissions(): void public function testUpsertDocuments(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForUpserts()) { $this->expectNotToPerformAssertions(); @@ -727,7 +727,7 @@ public function testUpsertDocuments(): void public function testUpsertDocumentsInc(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForUpserts()) { $this->expectNotToPerformAssertions(); @@ -799,7 +799,7 @@ public function testUpsertDocumentsInc(): void public function testUpsertDocumentsPermissions(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForUpserts()) { $this->expectNotToPerformAssertions(); @@ -888,7 +888,7 @@ public function testUpsertDocumentsPermissions(): void public function testUpsertDocumentsAttributeMismatch(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForUpserts()) { $this->expectNotToPerformAssertions(); @@ -1002,13 +1002,13 @@ public function testUpsertDocumentsAttributeMismatch(): void public function testUpsertDocumentsNoop(): void { - if (!static::getDatabase()->getAdapter()->getSupportForUpserts()) { + if (!$this->getDatabase()->getAdapter()->getSupportForUpserts()) { $this->expectNotToPerformAssertions(); return; } - static::getDatabase()->createCollection(__FUNCTION__); - static::getDatabase()->createAttribute(__FUNCTION__, 'string', Database::VAR_STRING, 128, true); + $this->getDatabase()->createCollection(__FUNCTION__); + $this->getDatabase()->createAttribute(__FUNCTION__, 'string', Database::VAR_STRING, 128, true); $document = new Document([ '$id' => 'first', @@ -1021,17 +1021,17 @@ public function testUpsertDocumentsNoop(): void ], ]); - $count = static::getDatabase()->upsertDocuments(__FUNCTION__, [$document]); + $count = $this->getDatabase()->upsertDocuments(__FUNCTION__, [$document]); $this->assertEquals(1, $count); // No changes, should return 0 - $count = static::getDatabase()->upsertDocuments(__FUNCTION__, [$document]); + $count = $this->getDatabase()->upsertDocuments(__FUNCTION__, [$document]); $this->assertEquals(0, $count); } public function testUpsertDuplicateIds(): void { - $db = static::getDatabase(); + $db = $this->getDatabase(); if (!$db->getAdapter()->getSupportForUpserts()) { $this->expectNotToPerformAssertions(); return; @@ -1053,7 +1053,7 @@ public function testUpsertDuplicateIds(): void public function testUpsertMixedPermissionDelta(): void { - $db = static::getDatabase(); + $db = $this->getDatabase(); if (!$db->getAdapter()->getSupportForUpserts()) { $this->expectNotToPerformAssertions(); return; @@ -1101,7 +1101,7 @@ public function testUpsertMixedPermissionDelta(): void public function testRespectNulls(): Document { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('documents_nulls'); @@ -1140,7 +1140,7 @@ public function testRespectNulls(): Document public function testCreateDocumentDefaults(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('defaults'); @@ -1188,7 +1188,7 @@ public function testCreateDocumentDefaults(): void public function testIncreaseDecrease(): Document { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $collection = 'increase_decrease'; $database->createCollection($collection); @@ -1246,7 +1246,7 @@ public function testIncreaseDecrease(): Document public function testIncreaseLimitMax(Document $document): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectException(Exception::class); $this->assertEquals(true, $database->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase', 10.5, 102.4)); @@ -1258,7 +1258,7 @@ public function testIncreaseLimitMax(Document $document): void public function testDecreaseLimitMin(Document $document): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); try { $database->decreaseDocumentAttribute( @@ -1293,7 +1293,7 @@ public function testDecreaseLimitMin(Document $document): void public function testIncreaseTextAttribute(Document $document): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); try { $this->assertEquals(false, $database->increaseDocumentAttribute('increase_decrease', $document->getId(), 'increase_text')); @@ -1309,7 +1309,7 @@ public function testIncreaseTextAttribute(Document $document): void public function testIncreaseArrayAttribute(Document $document): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); try { $this->assertEquals(false, $database->increaseDocumentAttribute('increase_decrease', $document->getId(), 'sizes')); @@ -1325,7 +1325,7 @@ public function testIncreaseArrayAttribute(Document $document): void public function testGetDocument(Document $document): Document { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->getDocument('documents', $document->getId()); @@ -1355,7 +1355,7 @@ public function testGetDocumentSelect(Document $document): Document $documentId = $document->getId(); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->getDocument('documents', $documentId, [ Query::select(['string', 'integer_signed']), @@ -1401,7 +1401,7 @@ public function testFind(): array Authorization::setRole(Role::any()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('movies', permissions: [ Permission::create(Role::any()), @@ -1580,7 +1580,7 @@ public function testFind(): array public function testFindOne(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->findOne('movies', [ Query::offset(2), @@ -1599,7 +1599,7 @@ public function testFindOne(): void public function testFindBasicChecks(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = $database->find('movies'); $movieDocuments = $documents; @@ -1667,7 +1667,7 @@ public function testFindBasicChecks(): void public function testFindCheckPermissions(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Check Permissions @@ -1681,7 +1681,7 @@ public function testFindCheckPermissions(): void public function testFindCheckInteger(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Query with dash attribute @@ -1713,7 +1713,7 @@ public function testFindCheckInteger(): void public function testFindBoolean(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Boolean condition @@ -1728,7 +1728,7 @@ public function testFindBoolean(): void public function testFindStringQueryEqual(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * String condition @@ -1750,7 +1750,7 @@ public function testFindStringQueryEqual(): void public function testFindNotEqual(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Not Equal query @@ -1777,7 +1777,7 @@ public function testFindNotEqual(): void public function testFindBetween(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = $database->find('movies', [ Query::between('price', 25.94, 25.99), @@ -1803,7 +1803,7 @@ public function testFindBetween(): void public function testFindFloat(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Float condition @@ -1819,7 +1819,7 @@ public function testFindFloat(): void public function testFindContains(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForQueryContains()) { $this->expectNotToPerformAssertions(); @@ -1861,7 +1861,7 @@ public function testFindContains(): void public function testFindFulltext(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Fulltext search @@ -1897,7 +1897,7 @@ public function testFindFulltext(): void public function testFindFulltextSpecialChars(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForFulltextIndex()) { $this->expectNotToPerformAssertions(); @@ -1964,7 +1964,7 @@ public function testFindFulltextSpecialChars(): void public function testFindMultipleConditions(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Multiple conditions @@ -1991,7 +1991,7 @@ public function testFindMultipleConditions(): void public function testFindByID(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * $id condition @@ -2012,7 +2012,7 @@ public function testFindByID(): void public function testFindByInternalID(array $data): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Test that internal ID queries are handled correctly @@ -2027,7 +2027,7 @@ public function testFindByInternalID(array $data): void public function testFindOrderBy(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY @@ -2050,7 +2050,7 @@ public function testFindOrderBy(): void public function testFindOrderByNatural(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY natural @@ -2076,7 +2076,7 @@ public function testFindOrderByNatural(): void public function testFindOrderByMultipleAttributes(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY - Multiple attributes @@ -2100,7 +2100,7 @@ public function testFindOrderByMultipleAttributes(): void public function testFindOrderByCursorAfter(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY - After @@ -2197,7 +2197,7 @@ public function testFindOrderByCursorAfter(): void public function testFindOrderByCursorBefore(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY - Before @@ -2253,7 +2253,7 @@ public function testFindOrderByCursorBefore(): void public function testFindOrderByAfterNaturalOrder(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY - After by natural order @@ -2303,7 +2303,7 @@ public function testFindOrderByAfterNaturalOrder(): void public function testFindOrderByBeforeNaturalOrder(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY - Before by natural order @@ -2365,7 +2365,7 @@ public function testFindOrderByBeforeNaturalOrder(): void public function testFindOrderBySingleAttributeAfter(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY - Single Attribute After @@ -2419,7 +2419,7 @@ public function testFindOrderBySingleAttributeAfter(): void public function testFindOrderBySingleAttributeBefore(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY - Single Attribute Before @@ -2481,7 +2481,7 @@ public function testFindOrderBySingleAttributeBefore(): void public function testFindOrderByMultipleAttributeAfter(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY - Multiple Attribute After @@ -2538,7 +2538,7 @@ public function testFindOrderByMultipleAttributeAfter(): void public function testFindOrderByMultipleAttributeBefore(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY - Multiple Attribute Before @@ -2606,7 +2606,7 @@ public function testFindOrderByMultipleAttributeBefore(): void public function testFindOrderByAndCursor(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY + CURSOR @@ -2628,7 +2628,7 @@ public function testFindOrderByAndCursor(): void public function testFindOrderByIdAndCursor(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY ID + CURSOR @@ -2651,7 +2651,7 @@ public function testFindOrderByIdAndCursor(): void public function testFindOrderByCreateDateAndCursor(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY CREATE DATE + CURSOR @@ -2675,7 +2675,7 @@ public function testFindOrderByCreateDateAndCursor(): void public function testFindOrderByUpdateDateAndCursor(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * ORDER BY UPDATE DATE + CURSOR @@ -2698,7 +2698,7 @@ public function testFindOrderByUpdateDateAndCursor(): void public function testFindCreatedBefore(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Test Query::createdBefore wrapper @@ -2724,7 +2724,7 @@ public function testFindCreatedBefore(): void public function testFindCreatedAfter(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Test Query::createdAfter wrapper @@ -2750,7 +2750,7 @@ public function testFindCreatedAfter(): void public function testFindUpdatedBefore(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Test Query::updatedBefore wrapper @@ -2776,7 +2776,7 @@ public function testFindUpdatedBefore(): void public function testFindUpdatedAfter(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Test Query::updatedAfter wrapper @@ -2802,7 +2802,7 @@ public function testFindUpdatedAfter(): void public function testFindCreatedBetween(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Test Query::createdBetween wrapper @@ -2848,7 +2848,7 @@ public function testFindCreatedBetween(): void public function testFindUpdatedBetween(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Test Query::updatedBetween wrapper @@ -2894,7 +2894,7 @@ public function testFindUpdatedBetween(): void public function testFindLimit(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Limit @@ -2916,7 +2916,7 @@ public function testFindLimit(): void public function testFindLimitAndOffset(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Limit + Offset @@ -2937,7 +2937,7 @@ public function testFindLimitAndOffset(): void public function testFindOrQueries(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Test that OR queries are handled correctly @@ -2955,7 +2955,7 @@ public function testFindOrQueries(): void public function testFindEdgeCases(Document $document): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $collection = 'edgeCases'; @@ -3021,7 +3021,7 @@ public function testFindEdgeCases(Document $document): void public function testOrSingleQuery(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); try { $database->find('movies', [ @@ -3038,7 +3038,7 @@ public function testOrSingleQuery(): void public function testOrMultipleQueries(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $queries = [ Query::or([ @@ -3065,7 +3065,7 @@ public function testOrMultipleQueries(): void public function testOrNested(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $queries = [ Query::select(['director']), @@ -3090,7 +3090,7 @@ public function testOrNested(): void public function testAndSingleQuery(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); try { $database->find('movies', [ @@ -3107,7 +3107,7 @@ public function testAndSingleQuery(): void public function testAndMultipleQueries(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $queries = [ Query::and([ @@ -3122,7 +3122,7 @@ public function testAndMultipleQueries(): void public function testAndNested(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $queries = [ Query::or([ @@ -3144,7 +3144,7 @@ public function testAndNested(): void public function testNestedIDQueries(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); Authorization::setRole(Role::any()->toString()); @@ -3208,7 +3208,7 @@ public function testNestedIDQueries(): void public function testFindNull(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = $database->find('movies', [ Query::isNull('nullable'), @@ -3220,7 +3220,7 @@ public function testFindNull(): void public function testFindNotNull(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = $database->find('movies', [ Query::isNotNull('nullable'), @@ -3232,7 +3232,7 @@ public function testFindNotNull(): void public function testFindStartsWith(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = $database->find('movies', [ Query::startsWith('name', 'Work'), @@ -3256,7 +3256,7 @@ public function testFindStartsWith(): void public function testFindStartsWithWords(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = $database->find('movies', [ Query::startsWith('name', 'Work in Progress'), @@ -3268,7 +3268,7 @@ public function testFindStartsWithWords(): void public function testFindEndsWith(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = $database->find('movies', [ Query::endsWith('name', 'Marvel'), @@ -3280,7 +3280,7 @@ public function testFindEndsWith(): void public function testFindNotContains(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForQueryContains()) { $this->expectNotToPerformAssertions(); @@ -3342,7 +3342,7 @@ public function testFindNotContains(): void public function testFindNotSearch(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); // Only test if fulltext search is supported if ($this->getDatabase()->getAdapter()->getSupportForFulltextIndex()) { @@ -3405,7 +3405,7 @@ public function testFindNotSearch(): void public function testFindNotStartsWith(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); // Test notStartsWith - should return documents that don't start with 'Work' $documents = $database->find('movies', [ @@ -3463,7 +3463,7 @@ public function testFindNotStartsWith(): void public function testFindNotEndsWith(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); // Test notEndsWith - should return documents that don't end with 'Marvel' $documents = $database->find('movies', [ @@ -3516,7 +3516,7 @@ public function testFindNotEndsWith(): void public function testFindOrderRandom(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); // Test orderRandom with default limit $documents = $database->find('movies', [ @@ -3582,7 +3582,7 @@ public function testFindOrderRandom(): void public function testFindNotBetween(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); // Test notBetween with price range - should return documents outside the range $documents = $database->find('movies', [ @@ -3656,7 +3656,7 @@ public function testFindNotBetween(): void public function testFindSelect(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = $database->find('movies', [ Query::select(['name', 'year']) @@ -3789,7 +3789,7 @@ public function testFindSelect(): void public function testForeach(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); /** * Test, foreach goes through all the documents @@ -3842,7 +3842,7 @@ public function testForeach(): void public function testCount(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $count = $database->count('movies'); $this->assertEquals(6, $count); @@ -3886,7 +3886,7 @@ public function testCount(): void public function testSum(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); Authorization::setRole('user:x'); @@ -4106,7 +4106,7 @@ public function testEncodeDecode(): void ]); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $result = $database->encode($collection, $document); @@ -4314,7 +4314,7 @@ public function testDeleteDocument(Document $document): void public function testUpdateDocuments(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); @@ -4508,7 +4508,7 @@ public function testUpdateDocuments(): void public function testUpdateDocumentsWithCallbackSupport(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); @@ -4609,7 +4609,7 @@ public function testReadPermissionsSuccess(Document $document): Document Authorization::setRole(Role::any()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->createDocument('documents', new Document([ '$permissions' => [ @@ -4649,7 +4649,7 @@ public function testWritePermissionsSuccess(Document $document): void Authorization::cleanRoles(); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectException(AuthorizationException::class); $database->createDocument('documents', new Document([ @@ -4682,7 +4682,7 @@ public function testWritePermissionsUpdateFailure(Document $document): Document Authorization::setRole(Role::any()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->createDocument('documents', new Document([ '$permissions' => [ @@ -4732,7 +4732,7 @@ public function testUniqueIndexDuplicate(): void $this->expectException(DuplicateException::class); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->assertEquals(true, $database->createIndex('movies', 'uniqueIndex', Database::INDEX_UNIQUE, ['name'], [128], [Database::ORDER_ASC])); @@ -4766,7 +4766,7 @@ public function testUniqueIndexDuplicate(): void public function testUniqueIndexDuplicateUpdate(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); Authorization::setRole(Role::users()->toString()); // create document then update to conflict with index @@ -4802,7 +4802,7 @@ public function testUniqueIndexDuplicateUpdate(): void public function propagateBulkDocuments(string $collection, int $amount = 10, bool $documentSecurity = false): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); for ($i = 0; $i < $amount; $i++) { $database->createDocument($collection, new Document( @@ -4823,7 +4823,7 @@ public function propagateBulkDocuments(string $collection, int $amount = 10, boo public function testDeleteBulkDocuments(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); @@ -4964,7 +4964,7 @@ public function testDeleteBulkDocuments(): void public function testDeleteBulkDocumentsQueries(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); @@ -5029,7 +5029,7 @@ public function testDeleteBulkDocumentsQueries(): void public function testDeleteBulkDocumentsWithCallbackSupport(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); @@ -5153,7 +5153,7 @@ public function testDeleteBulkDocumentsWithCallbackSupport(): void public function testUpdateDocumentsQueries(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); @@ -5217,7 +5217,7 @@ public function testUpdateDocumentsQueries(): void public function testFulltextIndexWithInteger(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectException(Exception::class); @@ -5232,7 +5232,7 @@ public function testFulltextIndexWithInteger(): void public function testEnableDisableValidation(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('validation', permissions: [ Permission::create(Role::any()), @@ -5295,7 +5295,7 @@ public function testEnableDisableValidation(): void public function testExceptionDuplicate(Document $document): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document->setAttribute('$id', 'duplicated'); $database->createDocument($document->getCollection(), $document); @@ -5309,7 +5309,7 @@ public function testExceptionDuplicate(Document $document): void public function testExceptionCaseInsensitiveDuplicate(Document $document): Document { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document->setAttribute('$id', 'caseSensitive'); $document->setAttribute('$sequence', '200'); @@ -5326,7 +5326,7 @@ public function testExceptionCaseInsensitiveDuplicate(Document $document): Docum public function testEmptyTenant(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSharedTables()) { $documents = $database->find( @@ -5361,7 +5361,7 @@ public function testEmptyTenant(): void public function testEmptyOperatorValues(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); try { $database->findOne('documents', [ @@ -5389,7 +5389,7 @@ public function testDateTimeDocument(): void /** * @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $collection = 'create_modify_dates'; $database->createCollection($collection); $this->assertEquals(true, $database->createAttribute($collection, 'string', Database::VAR_STRING, 128, false)); @@ -5436,7 +5436,7 @@ public function testDateTimeDocument(): void public function testSingleDocumentDateOperations(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $collection = 'normal_date_operations'; $database->createCollection($collection); $this->assertEquals(true, $database->createAttribute($collection, 'string', Database::VAR_STRING, 128, false)); @@ -5607,7 +5607,7 @@ public function testSingleDocumentDateOperations(): void public function testBulkDocumentDateOperations(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $collection = 'bulk_date_operations'; $database->createCollection($collection); $this->assertEquals(true, $database->createAttribute($collection, 'string', Database::VAR_STRING, 128, false)); @@ -5738,7 +5738,7 @@ public function testBulkDocumentDateOperations(): void public function testUpsertDateOperations(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForUpserts()) { return; @@ -6004,7 +6004,7 @@ public function testUpsertDateOperations(): void public function testUpdateDocumentsCount(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForUpserts()) { return; @@ -6062,7 +6062,7 @@ public function testUpdateDocumentsCount(): void public function testCreateUpdateDocumentsMismatch(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); // with different set of attributes $colName = "docs_with_diff"; @@ -6118,7 +6118,7 @@ public function testCreateUpdateDocumentsMismatch(): void public function testDecodeWithDifferentSelectionTypes(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -6285,7 +6285,7 @@ function (mixed $value) { public function testDecodeWithoutRelationships(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->expectNotToPerformAssertions(); @@ -6386,7 +6386,7 @@ function (mixed $value) { public function testDecodeWithMultipleFilters(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->addFilter( 'upperCase', @@ -6437,7 +6437,7 @@ function (mixed $value) { return str_replace('prefix_', '', $value); } public function testUpdateDocumentsSuccessiveCallsDoNotResetDefaults(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); @@ -6494,7 +6494,7 @@ public function testUpdateDocumentsSuccessiveCallsDoNotResetDefaults(): void public function testUpdateDocumentSuccessiveCallsDoNotResetDefaults(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $collectionId = 'successive_update_single'; Authorization::cleanRoles(); diff --git a/tests/e2e/Adapter/Scopes/GeneralTests.php b/tests/e2e/Adapter/Scopes/GeneralTests.php index b4bab2067..37880ad5d 100644 --- a/tests/e2e/Adapter/Scopes/GeneralTests.php +++ b/tests/e2e/Adapter/Scopes/GeneralTests.php @@ -25,10 +25,7 @@ trait GeneralTests { public function testPing(): void { - /** @var Database $database */ - $database = static::getDatabase(); - - $this->assertEquals(true, $database->ping()); + $this->assertEquals(true, $this->getDatabase()->ping()); } /** @@ -47,7 +44,7 @@ public function testQueryTimeout(): void } /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('global-timeouts'); @@ -94,7 +91,7 @@ public function testPreserveDatesUpdate(): void Authorization::disable(); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->setPreserveDates(true); @@ -189,7 +186,7 @@ public function testPreserveDatesCreate(): void Authorization::disable(); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->setPreserveDates(true); @@ -311,7 +308,7 @@ public function testGetId(): void public function testSharedTablesUpdateTenant(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $sharedTables = $database->getSharedTables(); $namespace = $database->getNamespace(); $schema = $database->getDatabase(); @@ -370,7 +367,7 @@ public function testFindOrderByAfterException(): void $this->expectException(Exception::class); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->find('movies', [ Query::limit(2), @@ -425,7 +422,7 @@ public function testNestedQueryValidation(): void public function testSharedTablesTenantPerDocument(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $sharedTables = $database->getSharedTables(); $tenantPerDocument = $database->getTenantPerDocument(); @@ -625,7 +622,7 @@ public function testSharedTablesTenantPerDocument(): void public function testCacheFallback(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForCacheSkipOnFailure()) { $this->expectNotToPerformAssertions(); @@ -634,7 +631,7 @@ public function testCacheFallback(): void Authorization::cleanRoles(); Authorization::setRole(Role::any()->toString()); - $database = static::getDatabase(); + $database = $this->getDatabase(); // Write mock data $database->createCollection('testRedisFallback', attributes: [ diff --git a/tests/e2e/Adapter/Scopes/IndexTests.php b/tests/e2e/Adapter/Scopes/IndexTests.php index ac8b11da7..403866d63 100644 --- a/tests/e2e/Adapter/Scopes/IndexTests.php +++ b/tests/e2e/Adapter/Scopes/IndexTests.php @@ -56,7 +56,7 @@ public function testCreateIndex(): void public function testCreateDeleteIndex(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('indexes'); @@ -160,7 +160,7 @@ public function testIndexValidation(): void ]); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $validator = new Index( $attributes, @@ -301,7 +301,7 @@ public function testIndexValidation(): void public function testIndexLengthZero(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection(__FUNCTION__); @@ -328,7 +328,7 @@ public function testIndexLengthZero(): void public function testRenameIndex(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $numbers = $database->createCollection('numbers'); $database->createAttribute('numbers', 'verbose', Database::VAR_STRING, 128, true); @@ -355,7 +355,7 @@ public function testRenameIndex(): void */ public function testRenameIndexMissing(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectExceptionMessage('Index not found'); $index = $database->renameIndex('numbers', 'index1', 'index4'); } @@ -366,7 +366,7 @@ public function testRenameIndexMissing(): void */ public function testRenameIndexExisting(): void { - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectExceptionMessage('Index name already used'); $index = $database->renameIndex('numbers', 'index3', 'index2'); } @@ -375,7 +375,7 @@ public function testRenameIndexExisting(): void public function testExceptionIndexLimit(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('indexLimit'); @@ -404,7 +404,7 @@ public function testListDocumentSearch(): void } /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createIndex('documents', 'string', Database::INDEX_FULLTEXT, ['string']); $database->createDocument('documents', new Document([ @@ -439,7 +439,7 @@ public function testListDocumentSearch(): void public function testMaxQueriesValues(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $max = $database->getMaxQueryValues(); @@ -468,7 +468,7 @@ public function testEmptySearch(): void } /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = $database->find('documents', [ Query::search('string', ''), diff --git a/tests/e2e/Adapter/Scopes/PermissionTests.php b/tests/e2e/Adapter/Scopes/PermissionTests.php index 50e14c90c..6c51644d2 100644 --- a/tests/e2e/Adapter/Scopes/PermissionTests.php +++ b/tests/e2e/Adapter/Scopes/PermissionTests.php @@ -17,7 +17,7 @@ trait PermissionTests public function testUnsetPermissions(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection(__FUNCTION__); $this->assertTrue($database->createAttribute( @@ -171,7 +171,7 @@ public function testUnsetPermissions(): void public function testCreateDocumentsEmptyPermission(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection(__FUNCTION__); @@ -208,7 +208,7 @@ public function testReadPermissionsFailure(): Document Authorization::setRole(Role::any()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->createDocument('documents', new Document([ '$permissions' => [ @@ -242,7 +242,7 @@ public function testReadPermissionsFailure(): Document public function testNoChangeUpdateDocumentWithoutPermission(): Document { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->createDocument('documents', new Document([ '$id' => ID::unique(), @@ -301,7 +301,7 @@ public function testNoChangeUpdateDocumentWithoutPermission(): Document public function testUpdateDocumentsPermissions(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); @@ -356,7 +356,7 @@ public function testUpdateDocumentsPermissions(): void ])); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = Authorization::skip(function () use ($collection, $database) { return $database->find($collection); @@ -425,7 +425,7 @@ public function testUpdateDocumentsPermissions(): void public function testCollectionPermissions(): Document { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $collection = $database->createCollection('collectionSecurity', permissions: [ Permission::create(Role::users()), @@ -459,7 +459,7 @@ public function testCollectionPermissionsCountThrowsException(array $data): void Authorization::setRole(Role::any()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $count = $database->count( $collection->getId() @@ -480,7 +480,7 @@ public function testCollectionPermissionsCountWorks(array $data): array Authorization::setRole(Role::users()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $count = $database->count( $collection->getId() @@ -501,7 +501,7 @@ public function testCollectionPermissionsCreateThrowsException(Document $collect $this->expectException(AuthorizationException::class); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createDocument($collection->getId(), new Document([ '$id' => ID::unique(), @@ -524,7 +524,7 @@ public function testCollectionPermissionsCreateWorks(Document $collection): arra Authorization::setRole(Role::users()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->createDocument($collection->getId(), new Document([ '$id' => ID::unique(), @@ -554,7 +554,7 @@ public function testCollectionPermissionsDeleteThrowsException(array $data): voi $this->expectException(AuthorizationException::class); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->deleteDocument( $collection->getId(), @@ -574,7 +574,7 @@ public function testCollectionPermissionsDeleteWorks(array $data): void Authorization::setRole(Role::users()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->assertTrue($database->deleteDocument( $collection->getId(), @@ -585,7 +585,7 @@ public function testCollectionPermissionsDeleteWorks(array $data): void public function testCollectionPermissionsExceptions(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->expectException(DatabaseException::class); $database->createCollection('collectionSecurity', permissions: [ @@ -607,7 +607,7 @@ public function testCollectionPermissionsFindThrowsException(array $data): void $this->expectException(AuthorizationException::class); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->find($collection->getId()); } @@ -625,7 +625,7 @@ public function testCollectionPermissionsFindWorks(array $data): array Authorization::setRole(Role::users()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = $database->find($collection->getId()); $this->assertNotEmpty($documents); @@ -654,7 +654,7 @@ public function testCollectionPermissionsGetThrowsException(array $data): void Authorization::setRole(Role::any()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->getDocument( $collection->getId(), @@ -677,7 +677,7 @@ public function testCollectionPermissionsGetWorks(array $data): array Authorization::setRole(Role::users()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->getDocument( $collection->getId(), @@ -695,7 +695,7 @@ public function testCollectionPermissionsGetWorks(array $data): array public function testCollectionPermissionsRelationships(): array { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $collection = $database->createCollection('collectionSecurity.Parent', permissions: [ Permission::create(Role::users()), @@ -779,7 +779,7 @@ public function testCollectionPermissionsRelationshipsCountWorks(array $data): v Authorization::setRole(Role::users()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = $database->count( $collection->getId() @@ -819,7 +819,7 @@ public function testCollectionPermissionsRelationshipsCreateThrowsException(arra $this->expectException(AuthorizationException::class); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createDocument($collection->getId(), new Document([ '$id' => ID::unique(), @@ -845,7 +845,7 @@ public function testCollectionPermissionsRelationshipsDeleteThrowsException(arra $this->expectException(AuthorizationException::class); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->deleteDocument( $collection->getId(), @@ -865,7 +865,7 @@ public function testCollectionPermissionsRelationshipsCreateWorks(array $data): Authorization::setRole(Role::users()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->createDocument($collection->getId(), new Document([ '$id' => ID::unique(), @@ -921,7 +921,7 @@ public function testCollectionPermissionsRelationshipsDeleteWorks(array $data): Authorization::setRole(Role::users()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->assertTrue($database->deleteDocument( $collection->getId(), @@ -941,7 +941,7 @@ public function testCollectionPermissionsRelationshipsFindWorks(array $data): vo Authorization::setRole(Role::users()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $documents = $database->find( $collection->getId() @@ -995,7 +995,7 @@ public function testCollectionPermissionsRelationshipsGetThrowsException(array $ Authorization::setRole(Role::any()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->getDocument( $collection->getId(), @@ -1018,7 +1018,7 @@ public function testCollectionPermissionsRelationshipsGetWorks(array $data): arr Authorization::setRole(Role::users()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->getDocument( $collection->getId(), @@ -1062,7 +1062,7 @@ public function testCollectionPermissionsRelationshipsUpdateThrowsException(arra $this->expectException(AuthorizationException::class); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->updateDocument( $collection->getId(), @@ -1084,7 +1084,7 @@ public function testCollectionPermissionsRelationshipsUpdateWorks(array $data): Authorization::setRole(Role::users()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->updateDocument( $collection->getId(), @@ -1121,7 +1121,7 @@ public function testCollectionPermissionsUpdateThrowsException(array $data): voi $this->expectException(AuthorizationException::class); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $document = $database->updateDocument( $collection->getId(), @@ -1143,7 +1143,7 @@ public function testCollectionPermissionsUpdateWorks(array $data): array Authorization::setRole(Role::users()->toString()); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $this->assertInstanceOf(Document::class, $database->updateDocument( $collection->getId(), @@ -1162,7 +1162,7 @@ public function testCollectionUpdatePermissionsThrowException(Document $collecti $this->expectException(DatabaseException::class); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->updateCollection($collection->getId(), permissions: [ 'i dont work' @@ -1172,7 +1172,7 @@ public function testCollectionUpdatePermissionsThrowException(Document $collecti public function testWritePermissions(): void { Authorization::setRole(Role::any()->toString()); - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('animals', permissions: [ Permission::create(Role::any()), @@ -1246,7 +1246,7 @@ public function testWritePermissions(): void public function testCreateRelationDocumentWithoutUpdatePermission(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); diff --git a/tests/e2e/Adapter/Scopes/RelationshipTests.php b/tests/e2e/Adapter/Scopes/RelationshipTests.php index 2e056238a..14573108b 100644 --- a/tests/e2e/Adapter/Scopes/RelationshipTests.php +++ b/tests/e2e/Adapter/Scopes/RelationshipTests.php @@ -30,7 +30,7 @@ trait RelationshipTests public function testZoo(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -404,7 +404,7 @@ public function testZoo(): void public function testSimpleRelationshipPopulation(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -476,7 +476,7 @@ public function testSimpleRelationshipPopulation(): void public function testDeleteRelatedCollection(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -642,7 +642,7 @@ public function testDeleteRelatedCollection(): void public function testVirtualRelationsAttributes(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1098,7 +1098,7 @@ public function testVirtualRelationsAttributes(): void public function testStructureValidationAfterRelationsAttribute(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1132,7 +1132,7 @@ public function testStructureValidationAfterRelationsAttribute(): void public function testNoChangeUpdateDocumentWithRelationWithoutPermission(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1232,7 +1232,7 @@ public function testNoChangeUpdateDocumentWithRelationWithoutPermission(): void public function testUpdateAttributeRenameRelationshipTwoWay(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1291,7 +1291,7 @@ public function testUpdateAttributeRenameRelationshipTwoWay(): void public function testNoInvalidKeysWithRelationships(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1366,7 +1366,7 @@ public function testNoInvalidKeysWithRelationships(): void public function testSelectRelationshipAttributes(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1660,7 +1660,7 @@ public function testSelectRelationshipAttributes(): void public function testInheritRelationshipPermissions(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1739,7 +1739,7 @@ public function testInheritRelationshipPermissions(): void public function testEnforceRelationshipPermissions(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1898,7 +1898,7 @@ public function testEnforceRelationshipPermissions(): void public function testCreateRelationshipMissingCollection(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1919,7 +1919,7 @@ public function testCreateRelationshipMissingCollection(): void public function testCreateRelationshipMissingRelatedCollection(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1942,7 +1942,7 @@ public function testCreateRelationshipMissingRelatedCollection(): void public function testCreateDuplicateRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1973,7 +1973,7 @@ public function testCreateDuplicateRelationship(): void public function testCreateInvalidRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1998,7 +1998,7 @@ public function testCreateInvalidRelationship(): void public function testDeleteMissingRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -2016,7 +2016,7 @@ public function testDeleteMissingRelationship(): void public function testCreateInvalidIntValueRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -2048,7 +2048,7 @@ public function testCreateInvalidIntValueRelationship(): void public function testCreateInvalidObjectValueRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -2070,7 +2070,7 @@ public function testCreateInvalidObjectValueRelationship(): void public function testCreateInvalidArrayIntValueRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -2098,7 +2098,7 @@ public function testCreateInvalidArrayIntValueRelationship(): void public function testCreateEmptyValueRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -2200,7 +2200,7 @@ public function testCreateEmptyValueRelationship(): void public function testUpdateRelationshipToExistingKey(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -2354,7 +2354,7 @@ public function testUpdateDocumentsRelationships(): void public function testUpdateDocumentWithRelationships(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -2660,7 +2660,7 @@ public function testUpdateDocumentWithRelationships(): void public function testMultiDocumentNestedRelationships(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -2880,7 +2880,7 @@ public function testMultiDocumentNestedRelationships(): void public function testNestedDocumentCreationWithDepthHandling(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -3015,7 +3015,7 @@ public function testNestedDocumentCreationWithDepthHandling(): void public function testRelationshipTypeQueries(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -3258,7 +3258,7 @@ public function testRelationshipTypeQueries(): void public function testRelationshipFilterQueries(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -3425,7 +3425,7 @@ public function testRelationshipFilterQueries(): void public function testRelationshipSpatialQueries(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -3679,7 +3679,7 @@ public function testRelationshipSpatialQueries(): void public function testRelationshipVirtualQueries(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -3788,7 +3788,7 @@ public function testRelationshipVirtualQueries(): void public function testRelationshipQueryEdgeCases(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -3893,7 +3893,7 @@ public function testRelationshipQueryEdgeCases(): void public function testRelationshipManyToManyComplex(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -3994,7 +3994,7 @@ public function testRelationshipManyToManyComplex(): void public function testNestedRelationshipQueriesMultipleDepths(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -4250,7 +4250,7 @@ public function testNestedRelationshipQueriesMultipleDepths(): void public function testCountAndSumWithRelationshipQueries(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -4411,7 +4411,7 @@ public function testCountAndSumWithRelationshipQueries(): void public function testOrderAndCursorWithRelationshipQueries(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('authorsOrder'); $database->createCollection('postsOrder'); diff --git a/tests/e2e/Adapter/Scopes/Relationships/ManyToManyTests.php b/tests/e2e/Adapter/Scopes/Relationships/ManyToManyTests.php index 90d5dcad0..f65b76cee 100644 --- a/tests/e2e/Adapter/Scopes/Relationships/ManyToManyTests.php +++ b/tests/e2e/Adapter/Scopes/Relationships/ManyToManyTests.php @@ -17,7 +17,7 @@ trait ManyToManyTests public function testManyToManyOneWayRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -328,7 +328,7 @@ public function testManyToManyOneWayRelationship(): void public function testManyToManyTwoWayRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -782,7 +782,7 @@ public function testManyToManyTwoWayRelationship(): void public function testNestedManyToMany_OneToOneRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -893,7 +893,7 @@ public function testNestedManyToMany_OneToOneRelationship(): void public function testNestedManyToMany_OneToManyRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -993,7 +993,7 @@ public function testNestedManyToMany_OneToManyRelationship(): void public function testNestedManyToMany_ManyToOneRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1107,7 +1107,7 @@ public function testNestedManyToMany_ManyToOneRelationship(): void public function testNestedManyToMany_ManyToManyRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1211,7 +1211,7 @@ public function testNestedManyToMany_ManyToManyRelationship(): void public function testManyToManyRelationshipKeyWithSymbols(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1254,7 +1254,7 @@ public function testManyToManyRelationshipKeyWithSymbols(): void public function testRecreateManyToManyOneWayRelationshipFromChild(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1320,7 +1320,7 @@ public function testRecreateManyToManyOneWayRelationshipFromChild(): void public function testRecreateManyToManyTwoWayRelationshipFromParent(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1388,7 +1388,7 @@ public function testRecreateManyToManyTwoWayRelationshipFromParent(): void public function testRecreateManyToManyTwoWayRelationshipFromChild(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1456,7 +1456,7 @@ public function testRecreateManyToManyTwoWayRelationshipFromChild(): void public function testRecreateManyToManyOneWayRelationshipFromParent(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1522,7 +1522,7 @@ public function testRecreateManyToManyOneWayRelationshipFromParent(): void public function testSelectManyToMany(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1600,7 +1600,7 @@ public function testSelectManyToMany(): void public function testSelectAcrossMultipleCollections(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1721,7 +1721,7 @@ public function testSelectAcrossMultipleCollections(): void public function testDeleteBulkDocumentsManyToManyRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); @@ -1798,7 +1798,7 @@ public function testDeleteBulkDocumentsManyToManyRelationship(): void public function testUpdateParentAndChild_ManyToMany(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ( !$database->getAdapter()->getSupportForRelationships() || @@ -1883,7 +1883,7 @@ public function testUpdateParentAndChild_ManyToMany(): void public function testDeleteDocumentsRelationshipErrorDoesNotDeleteParent_ManyToMany(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); diff --git a/tests/e2e/Adapter/Scopes/Relationships/ManyToOneTests.php b/tests/e2e/Adapter/Scopes/Relationships/ManyToOneTests.php index 73a1c7a6f..6a6082070 100644 --- a/tests/e2e/Adapter/Scopes/Relationships/ManyToOneTests.php +++ b/tests/e2e/Adapter/Scopes/Relationships/ManyToOneTests.php @@ -17,7 +17,7 @@ trait ManyToOneTests public function testManyToOneOneWayRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -351,7 +351,7 @@ public function testManyToOneOneWayRelationship(): void public function testManyToOneTwoWayRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -819,7 +819,7 @@ public function testManyToOneTwoWayRelationship(): void public function testNestedManyToOne_OneToOneRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -920,7 +920,7 @@ public function testNestedManyToOne_OneToOneRelationship(): void public function testNestedManyToOne_OneToManyRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1031,7 +1031,7 @@ public function testNestedManyToOne_OneToManyRelationship(): void public function testNestedManyToOne_ManyToOne(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1133,7 +1133,7 @@ public function testNestedManyToOne_ManyToOne(): void public function testNestedManyToOne_ManyToManyRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1204,7 +1204,7 @@ public function testNestedManyToOne_ManyToManyRelationship(): void public function testExceedMaxDepthManyToOneParent(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1287,7 +1287,7 @@ public function testExceedMaxDepthManyToOneParent(): void public function testManyToOneRelationshipKeyWithSymbols(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1331,7 +1331,7 @@ public function testManyToOneRelationshipKeyWithSymbols(): void public function testRecreateManyToOneOneWayRelationshipFromParent(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1397,7 +1397,7 @@ public function testRecreateManyToOneOneWayRelationshipFromParent(): void public function testRecreateManyToOneOneWayRelationshipFromChild(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1463,7 +1463,7 @@ public function testRecreateManyToOneOneWayRelationshipFromChild(): void public function testRecreateManyToOneTwoWayRelationshipFromParent(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1530,7 +1530,7 @@ public function testRecreateManyToOneTwoWayRelationshipFromParent(): void public function testRecreateManyToOneTwoWayRelationshipFromChild(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1598,7 +1598,7 @@ public function testRecreateManyToOneTwoWayRelationshipFromChild(): void public function testDeleteBulkDocumentsManyToOneRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); @@ -1681,7 +1681,7 @@ public function testDeleteBulkDocumentsManyToOneRelationship(): void public function testUpdateParentAndChild_ManyToOne(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ( !$database->getAdapter()->getSupportForRelationships() || @@ -1763,7 +1763,7 @@ public function testUpdateParentAndChild_ManyToOne(): void public function testDeleteDocumentsRelationshipErrorDoesNotDeleteParent_ManyToOne(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); diff --git a/tests/e2e/Adapter/Scopes/Relationships/OneToManyTests.php b/tests/e2e/Adapter/Scopes/Relationships/OneToManyTests.php index b29bf2087..f440bf8fd 100644 --- a/tests/e2e/Adapter/Scopes/Relationships/OneToManyTests.php +++ b/tests/e2e/Adapter/Scopes/Relationships/OneToManyTests.php @@ -17,7 +17,7 @@ trait OneToManyTests public function testOneToManyOneWayRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -389,7 +389,7 @@ public function testOneToManyOneWayRelationship(): void public function testOneToManyTwoWayRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -840,7 +840,7 @@ public function testOneToManyTwoWayRelationship(): void public function testNestedOneToMany_OneToOneRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -999,7 +999,7 @@ public function testNestedOneToMany_OneToOneRelationship(): void public function testNestedOneToMany_OneToManyRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1131,7 +1131,7 @@ public function testNestedOneToMany_OneToManyRelationship(): void public function testNestedOneToMany_ManyToOneRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1224,7 +1224,7 @@ public function testNestedOneToMany_ManyToOneRelationship(): void public function testNestedOneToMany_ManyToManyRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1319,7 +1319,7 @@ public function testNestedOneToMany_ManyToManyRelationship(): void public function testExceedMaxDepthOneToMany(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1433,7 +1433,7 @@ public function testExceedMaxDepthOneToMany(): void public function testExceedMaxDepthOneToManyChild(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1525,7 +1525,7 @@ public function testExceedMaxDepthOneToManyChild(): void public function testOneToManyRelationshipKeyWithSymbols(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1568,7 +1568,7 @@ public function testOneToManyRelationshipKeyWithSymbols(): void public function testRecreateOneToManyOneWayRelationshipFromChild(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1634,7 +1634,7 @@ public function testRecreateOneToManyOneWayRelationshipFromChild(): void public function testRecreateOneToManyTwoWayRelationshipFromParent(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1702,7 +1702,7 @@ public function testRecreateOneToManyTwoWayRelationshipFromParent(): void public function testRecreateOneToManyTwoWayRelationshipFromChild(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1770,7 +1770,7 @@ public function testRecreateOneToManyTwoWayRelationshipFromChild(): void public function testRecreateOneToManyOneWayRelationshipFromParent(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1836,7 +1836,7 @@ public function testRecreateOneToManyOneWayRelationshipFromParent(): void public function testDeleteBulkDocumentsOneToManyRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); @@ -2019,7 +2019,7 @@ public function testDeleteBulkDocumentsOneToManyRelationship(): void public function testOneToManyAndManyToOneDeleteRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -2076,7 +2076,7 @@ public function testOneToManyAndManyToOneDeleteRelationship(): void public function testUpdateParentAndChild_OneToMany(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ( !$database->getAdapter()->getSupportForRelationships() || @@ -2158,7 +2158,7 @@ public function testUpdateParentAndChild_OneToMany(): void public function testDeleteDocumentsRelationshipErrorDoesNotDeleteParent_OneToMany(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); diff --git a/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php b/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php index 52881707b..d56052506 100644 --- a/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php +++ b/tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php @@ -20,7 +20,7 @@ trait OneToOneTests public function testOneToOneOneWayRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -445,7 +445,7 @@ public function testOneToOneOneWayRelationship(): void public function testOneToOneTwoWayRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1007,7 +1007,7 @@ public function testOneToOneTwoWayRelationship(): void public function testIdenticalTwoWayKeyRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1107,7 +1107,7 @@ public function testIdenticalTwoWayKeyRelationship(): void public function testNestedOneToOne_OneToOneRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1199,7 +1199,7 @@ public function testNestedOneToOne_OneToOneRelationship(): void public function testNestedOneToOne_OneToManyRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1300,7 +1300,7 @@ public function testNestedOneToOne_OneToManyRelationship(): void public function testNestedOneToOne_ManyToOneRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1393,7 +1393,7 @@ public function testNestedOneToOne_ManyToOneRelationship(): void public function testNestedOneToOne_ManyToManyRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1490,7 +1490,7 @@ public function testNestedOneToOne_ManyToManyRelationship(): void public function testExceedMaxDepthOneToOne(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1572,7 +1572,7 @@ public function testExceedMaxDepthOneToOne(): void public function testExceedMaxDepthOneToOneNull(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1655,7 +1655,7 @@ public function testExceedMaxDepthOneToOneNull(): void public function testOneToOneRelationshipKeyWithSymbols(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1698,7 +1698,7 @@ public function testOneToOneRelationshipKeyWithSymbols(): void public function testRecreateOneToOneOneWayRelationshipFromChild(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1764,7 +1764,7 @@ public function testRecreateOneToOneOneWayRelationshipFromChild(): void public function testRecreateOneToOneTwoWayRelationshipFromParent(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1832,7 +1832,7 @@ public function testRecreateOneToOneTwoWayRelationshipFromParent(): void public function testRecreateOneToOneTwoWayRelationshipFromChild(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1900,7 +1900,7 @@ public function testRecreateOneToOneTwoWayRelationshipFromChild(): void public function testRecreateOneToOneOneWayRelationshipFromParent(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -1966,7 +1966,7 @@ public function testRecreateOneToOneOneWayRelationshipFromParent(): void public function testDeleteBulkDocumentsOneToOneRelationship(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); @@ -2165,7 +2165,7 @@ public function testDeleteBulkDocumentsOneToOneRelationship(): void public function testDeleteTwoWayRelationshipFromChild(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships()) { $this->expectNotToPerformAssertions(); @@ -2292,7 +2292,7 @@ public function testDeleteTwoWayRelationshipFromChild(): void public function testUpdateParentAndChild_OneToOne(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ( !$database->getAdapter()->getSupportForRelationships() || @@ -2375,7 +2375,7 @@ public function testUpdateParentAndChild_OneToOne(): void public function testDeleteDocumentsRelationshipErrorDoesNotDeleteParent_OneToOne(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForBatchOperations()) { $this->expectNotToPerformAssertions(); diff --git a/tests/e2e/Adapter/Scopes/SpatialTests.php b/tests/e2e/Adapter/Scopes/SpatialTests.php index 31093e724..5003126f1 100644 --- a/tests/e2e/Adapter/Scopes/SpatialTests.php +++ b/tests/e2e/Adapter/Scopes/SpatialTests.php @@ -18,7 +18,7 @@ trait SpatialTests public function testSpatialCollection(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); $collectionName = "test_spatial_Col"; if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); @@ -92,7 +92,7 @@ public function testSpatialCollection(): void public function testSpatialTypeDocuments(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -244,7 +244,7 @@ public function testSpatialTypeDocuments(): void public function testSpatialRelationshipOneToOne(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForSpatialAttributes()) { $this->expectNotToPerformAssertions(); @@ -349,7 +349,7 @@ public function testSpatialRelationshipOneToOne(): void public function testSpatialAttributes(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->expectNotToPerformAssertions(); return; @@ -397,7 +397,7 @@ public function testSpatialAttributes(): void public function testSpatialOneToMany(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForSpatialAttributes()) { $this->expectNotToPerformAssertions(); return; @@ -509,7 +509,7 @@ public function testSpatialOneToMany(): void public function testSpatialManyToOne(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForSpatialAttributes()) { $this->expectNotToPerformAssertions(); return; @@ -614,7 +614,7 @@ public function testSpatialManyToOne(): void public function testSpatialManyToMany(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForSpatialAttributes()) { $this->expectNotToPerformAssertions(); return; @@ -719,7 +719,7 @@ public function testSpatialManyToMany(): void public function testSpatialIndex(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->expectNotToPerformAssertions(); return; @@ -916,7 +916,7 @@ public function testSpatialIndex(): void public function testComplexGeometricShapes(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -1346,7 +1346,7 @@ public function testComplexGeometricShapes(): void public function testSpatialQueryCombinations(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -1476,7 +1476,7 @@ public function testSpatialQueryCombinations(): void public function testSpatialBulkOperation(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -1778,7 +1778,7 @@ public function testSpatialBulkOperation(): void public function testSptialAggregation(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -1865,7 +1865,7 @@ public function testSptialAggregation(): void public function testUpdateSpatialAttributes(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -1951,7 +1951,7 @@ public function testUpdateSpatialAttributes(): void public function testSpatialAttributeDefaults(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -2055,7 +2055,7 @@ public function testSpatialAttributeDefaults(): void public function testInvalidSpatialTypes(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -2160,7 +2160,7 @@ public function testInvalidSpatialTypes(): void public function testSpatialDistanceInMeter(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -2230,7 +2230,7 @@ public function testSpatialDistanceInMeter(): void public function testSpatialDistanceInMeterForMultiDimensionGeometry(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -2360,7 +2360,7 @@ public function testSpatialDistanceInMeterForMultiDimensionGeometry(): void public function testSpatialDistanceInMeterError(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -2443,7 +2443,7 @@ public function testSpatialEncodeDecode(): void ]); /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -2484,7 +2484,7 @@ public function testSpatialEncodeDecode(): void public function testSpatialIndexSingleAttributeOnly(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->expectNotToPerformAssertions(); return; @@ -2536,7 +2536,7 @@ public function testSpatialIndexSingleAttributeOnly(): void public function testSpatialIndexRequiredToggling(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->expectNotToPerformAssertions(); return; @@ -2571,7 +2571,7 @@ public function testSpatialIndexRequiredToggling(): void public function testSpatialIndexOnNonSpatial(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->expectNotToPerformAssertions(); return; @@ -2633,7 +2633,7 @@ public function testSpatialIndexOnNonSpatial(): void public function testSpatialDocOrder(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -2664,7 +2664,7 @@ public function testSpatialDocOrder(): void public function testInvalidCoordinateDocuments(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->markTestSkipped('Adapter does not support spatial attributes'); } @@ -2755,7 +2755,7 @@ public function testInvalidCoordinateDocuments(): void public function testCreateSpatialColumnWithExistingData(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if (!$database->getAdapter()->getSupportForSpatialAttributes()) { $this->expectNotToPerformAssertions(); return; From ba9067482ee773f5cf5a7677aed632786e8fd7ba Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 12 Oct 2025 19:21:09 +0300 Subject: [PATCH 03/29] Authorization instance --- tests/e2e/Adapter/MariaDBTest.php | 3 ++- tests/e2e/Adapter/MirrorTest.php | 4 +++- tests/e2e/Adapter/MySQLTest.php | 3 ++- tests/e2e/Adapter/PoolTest.php | 3 ++- tests/e2e/Adapter/PostgresTest.php | 3 ++- tests/e2e/Adapter/SQLiteTest.php | 3 ++- tests/e2e/Adapter/SharedTables/MariaDBTest.php | 3 ++- tests/e2e/Adapter/SharedTables/MySQLTest.php | 3 ++- tests/e2e/Adapter/SharedTables/PostgresTest.php | 3 ++- tests/e2e/Adapter/SharedTables/SQLiteTest.php | 3 ++- 10 files changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/e2e/Adapter/MariaDBTest.php b/tests/e2e/Adapter/MariaDBTest.php index 8a4893af3..cd26401e9 100644 --- a/tests/e2e/Adapter/MariaDBTest.php +++ b/tests/e2e/Adapter/MariaDBTest.php @@ -18,7 +18,7 @@ class MariaDBTest extends Base /** * @return Database */ - public static function getDatabase(bool $fresh = false): Database + public function getDatabase(bool $fresh = false): Database { if (!is_null(self::$database) && !$fresh) { return self::$database; @@ -38,6 +38,7 @@ public static function getDatabase(bool $fresh = false): Database $database = new Database(new MariaDB($pdo), $cache); $database + ->setAuthorization(self::$authorization) ->setDatabase('utopiaTests') ->setNamespace(static::$namespace = 'myapp_' . uniqid()); diff --git a/tests/e2e/Adapter/MirrorTest.php b/tests/e2e/Adapter/MirrorTest.php index b0de36be2..9bd3987bb 100644 --- a/tests/e2e/Adapter/MirrorTest.php +++ b/tests/e2e/Adapter/MirrorTest.php @@ -33,7 +33,7 @@ class MirrorTest extends Base * @throws \RedisException * @throws Exception */ - protected static function getDatabase(bool $fresh = false): Mirror + protected function getDatabase(bool $fresh = false): Mirror { if (!is_null(self::$database) && !$fresh) { return self::$database; @@ -84,9 +84,11 @@ protected static function getDatabase(bool $fresh = false): Mirror */ foreach ($schemas as $schema) { if ($database->getSource()->exists($schema)) { + $database->getSource()->setAuthorization(self::$authorization); $database->getSource()->setDatabase($schema)->delete(); } if ($database->getDestination()->exists($schema)) { + $database->getDestination()->setAuthorization(self::$authorization); $database->getDestination()->setDatabase($schema)->delete(); } } diff --git a/tests/e2e/Adapter/MySQLTest.php b/tests/e2e/Adapter/MySQLTest.php index 31cb6b828..ccba9edca 100644 --- a/tests/e2e/Adapter/MySQLTest.php +++ b/tests/e2e/Adapter/MySQLTest.php @@ -24,7 +24,7 @@ class MySQLTest extends Base * @throws Exception * @throws Limit */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -44,6 +44,7 @@ public static function getDatabase(): Database $database = new Database(new MySQL($pdo), $cache); $database + ->setAuthorization(self::$authorization) ->setDatabase('utopiaTests') ->setNamespace(static::$namespace = 'myapp_' . uniqid()); diff --git a/tests/e2e/Adapter/PoolTest.php b/tests/e2e/Adapter/PoolTest.php index 31efb4b94..668b19175 100644 --- a/tests/e2e/Adapter/PoolTest.php +++ b/tests/e2e/Adapter/PoolTest.php @@ -32,7 +32,7 @@ class PoolTest extends Base * @throws Duplicate * @throws Limit */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -60,6 +60,7 @@ public static function getDatabase(): Database $database = new Database(new Pool($pool), $cache); $database + ->setAuthorization(self::$authorization) ->setDatabase('utopiaTests') ->setNamespace(static::$namespace = 'myapp_' . uniqid()); diff --git a/tests/e2e/Adapter/PostgresTest.php b/tests/e2e/Adapter/PostgresTest.php index 4e63eea81..a63e832c4 100644 --- a/tests/e2e/Adapter/PostgresTest.php +++ b/tests/e2e/Adapter/PostgresTest.php @@ -18,7 +18,7 @@ class PostgresTest extends Base /** * @reture Adapter */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -37,6 +37,7 @@ public static function getDatabase(): Database $database = new Database(new Postgres($pdo), $cache); $database + ->setAuthorization(self::$authorization) ->setDatabase('utopiaTests') ->setNamespace(static::$namespace = 'myapp_' . uniqid()); diff --git a/tests/e2e/Adapter/SQLiteTest.php b/tests/e2e/Adapter/SQLiteTest.php index b40c1259f..2fd3360b1 100644 --- a/tests/e2e/Adapter/SQLiteTest.php +++ b/tests/e2e/Adapter/SQLiteTest.php @@ -18,7 +18,7 @@ class SQLiteTest extends Base /** * @return Database */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -41,6 +41,7 @@ public static function getDatabase(): Database $database = new Database(new SQLite($pdo), $cache); $database + ->setAuthorization(self::$authorization) ->setDatabase('utopiaTests') ->setNamespace(static::$namespace = 'myapp_' . uniqid()); diff --git a/tests/e2e/Adapter/SharedTables/MariaDBTest.php b/tests/e2e/Adapter/SharedTables/MariaDBTest.php index 346775691..49c03c88d 100644 --- a/tests/e2e/Adapter/SharedTables/MariaDBTest.php +++ b/tests/e2e/Adapter/SharedTables/MariaDBTest.php @@ -30,7 +30,7 @@ public static function getAdapterName(): string /** * @return Database */ - public static function getDatabase(bool $fresh = false): Database + public function getDatabase(bool $fresh = false): Database { if (!is_null(self::$database) && !$fresh) { return self::$database; @@ -49,6 +49,7 @@ public static function getDatabase(bool $fresh = false): Database $database = new Database(new MariaDB($pdo), $cache); $database + ->setAuthorization(self::$authorization) ->setDatabase('utopiaTests') ->setSharedTables(true) ->setTenant(999) diff --git a/tests/e2e/Adapter/SharedTables/MySQLTest.php b/tests/e2e/Adapter/SharedTables/MySQLTest.php index 7d72d0635..f9a713e0a 100644 --- a/tests/e2e/Adapter/SharedTables/MySQLTest.php +++ b/tests/e2e/Adapter/SharedTables/MySQLTest.php @@ -30,7 +30,7 @@ public static function getAdapterName(): string /** * @return Database */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -51,6 +51,7 @@ public static function getDatabase(): Database $database = new Database(new MySQL($pdo), $cache); $database + ->setAuthorization(self::$authorization) ->setDatabase('utopiaTests') ->setSharedTables(true) ->setTenant(999) diff --git a/tests/e2e/Adapter/SharedTables/PostgresTest.php b/tests/e2e/Adapter/SharedTables/PostgresTest.php index d3d164ee2..9fcfcb621 100644 --- a/tests/e2e/Adapter/SharedTables/PostgresTest.php +++ b/tests/e2e/Adapter/SharedTables/PostgresTest.php @@ -29,7 +29,7 @@ public static function getAdapterName(): string /** * @reture Adapter */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -48,6 +48,7 @@ public static function getDatabase(): Database $database = new Database(new Postgres($pdo), $cache); $database + ->setAuthorization(self::$authorization) ->setDatabase('utopiaTests') ->setSharedTables(true) ->setTenant(999) diff --git a/tests/e2e/Adapter/SharedTables/SQLiteTest.php b/tests/e2e/Adapter/SharedTables/SQLiteTest.php index b8f3ace70..76d954d66 100644 --- a/tests/e2e/Adapter/SharedTables/SQLiteTest.php +++ b/tests/e2e/Adapter/SharedTables/SQLiteTest.php @@ -30,7 +30,7 @@ public static function getAdapterName(): string /** * @return Database */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -54,6 +54,7 @@ public static function getDatabase(): Database $database = new Database(new SQLite($pdo), $cache); $database + ->setAuthorization(self::$authorization) ->setDatabase('utopiaTests') ->setSharedTables(true) ->setTenant(999) From 742aef695d6da3219b0c18abde23b59d4621710e Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 12 Oct 2025 19:22:56 +0300 Subject: [PATCH 04/29] Authorization instance --- tests/unit/Validator/AuthorizationTest.php | 96 +++++++++++----------- 1 file changed, 50 insertions(+), 46 deletions(-) diff --git a/tests/unit/Validator/AuthorizationTest.php b/tests/unit/Validator/AuthorizationTest.php index 08c1e46f2..bccdd5052 100644 --- a/tests/unit/Validator/AuthorizationTest.php +++ b/tests/unit/Validator/AuthorizationTest.php @@ -9,11 +9,15 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Validator\Authorization; +use Utopia\Database\Validator\Authorization\Input; class AuthorizationTest extends TestCase { + protected Authorization $authorization; + public function setUp(): void { + $this->authorization = new Authorization(); } public function tearDown(): void @@ -22,7 +26,7 @@ public function tearDown(): void public function testValues(): void { - Authorization::setRole(Role::any()->toString()); + $this->authorization->addRole(Role::any()->toString()); $document = new Document([ '$id' => ID::unique(), @@ -35,88 +39,88 @@ public function testValues(): void Permission::delete(Role::any()), ], ]); - $object = new Authorization(Database::PERMISSION_READ); - $this->assertEquals($object->isValid($document->getRead()), false); - $this->assertEquals($object->isValid(''), false); - $this->assertEquals($object->isValid([]), false); + $object = $this->authorization; + + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), false); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, [])), false); $this->assertEquals($object->getDescription(), 'No permissions provided for action \'read\''); - Authorization::setRole(Role::user('456')->toString()); - Authorization::setRole(Role::user('123')->toString()); + $this->authorization->addRole(Role::user('456')->toString()); + $this->authorization->addRole(Role::user('123')->toString()); - $this->assertEquals(Authorization::isRole(Role::user('456')->toString()), true); - $this->assertEquals(Authorization::isRole(Role::user('457')->toString()), false); - $this->assertEquals(Authorization::isRole(''), false); - $this->assertEquals(Authorization::isRole(Role::any()->toString()), true); + $this->assertEquals($this->authorization->isRole(Role::user('456')->toString()), true); + $this->assertEquals($this->authorization->isRole(Role::user('457')->toString()), false); + $this->assertEquals($this->authorization->isRole(''), false); + $this->assertEquals($this->authorization->isRole(Role::any()->toString()), true); - $this->assertEquals($object->isValid($document->getRead()), true); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), true); - Authorization::cleanRoles(); + $this->authorization->cleanRoles(); - $this->assertEquals($object->isValid($document->getRead()), false); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), false); - Authorization::setRole(Role::team('123')->toString()); + $this->authorization->addRole(Role::team('123')->toString()); - $this->assertEquals($object->isValid($document->getRead()), true); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), true); - Authorization::cleanRoles(); - Authorization::disable(); + $this->authorization->cleanRoles(); + $this->authorization->disable(); - $this->assertEquals($object->isValid($document->getRead()), true); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), true); - Authorization::reset(); + $this->authorization->reset(); - $this->assertEquals($object->isValid($document->getRead()), false); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), false); - Authorization::setDefaultStatus(false); - Authorization::disable(); + $this->authorization->setDefaultStatus(false); + $this->authorization->disable(); - $this->assertEquals($object->isValid($document->getRead()), true); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), true); - Authorization::reset(); + $this->authorization->reset(); - $this->assertEquals($object->isValid($document->getRead()), true); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), true); - Authorization::enable(); + $this->authorization->enable(); - $this->assertEquals($object->isValid($document->getRead()), false); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), false); - Authorization::setRole('textX'); + $this->authorization->addRole('textX'); - $this->assertContains('textX', Authorization::getRoles()); + $this->assertContains('textX', $this->authorization->getRoles()); - Authorization::unsetRole('textX'); + $this->authorization->removeRole('textX'); - $this->assertNotContains('textX', Authorization::getRoles()); + $this->assertNotContains('textX', $this->authorization->getRoles()); // Test skip method - $this->assertEquals($object->isValid($document->getRead()), false); - $this->assertEquals(Authorization::skip(function () use ($object, $document) { - return $object->isValid($document->getRead()); + $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), false); + $this->assertEquals($this->authorization->skip(function () use ($object, $document) { + return $object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())); }), true); } public function testNestedSkips(): void { - $this->assertEquals(true, Authorization::$status); + $this->assertEquals(true, $this->authorization->getStatus()); - Authorization::skip(function () { - $this->assertEquals(false, Authorization::$status); + $this->authorization->skip(function () { + $this->assertEquals(false, $this->authorization->getStatus()); - Authorization::skip(function () { - $this->assertEquals(false, Authorization::$status); + $this->authorization->skip(function () { + $this->assertEquals(false, $this->authorization->getStatus()); - Authorization::skip(function () { - $this->assertEquals(false, Authorization::$status); + $this->authorization->skip(function () { + $this->assertEquals(false, $this->authorization->getStatus()); }); - $this->assertEquals(false, Authorization::$status); + $this->assertEquals(false, $this->authorization->getStatus()); }); - $this->assertEquals(false, Authorization::$status); + $this->assertEquals(false, $this->authorization->getStatus()); }); - $this->assertEquals(true, Authorization::$status); + $this->assertEquals(true, $this->authorization->getStatus()); } -} +} \ No newline at end of file From 3dd0f05b89614ac43700ec7772d4a26e88b7a816 Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 15 Oct 2025 11:04:52 +0300 Subject: [PATCH 05/29] Authorization instance --- bin/tasks/load.php | 51 ++++++------ bin/tasks/query.php | 32 ++++---- bin/tasks/relationships.php | 148 ++++++++++++++++++----------------- src/Database/Adapter/SQL.php | 1 - 4 files changed, 122 insertions(+), 110 deletions(-) diff --git a/bin/tasks/load.php b/bin/tasks/load.php index 0a5c99719..895e40181 100644 --- a/bin/tasks/load.php +++ b/bin/tasks/load.php @@ -26,6 +26,8 @@ $genresPool = ['fashion', 'food', 'travel', 'music', 'lifestyle', 'fitness', 'diy', 'sports', 'finance']; $tagsPool = ['short', 'quick', 'easy', 'medium', 'hard']; +$authorization = new Authorization(); + /** * @Example * docker compose exec tests bin/load --adapter=mariadb --limit=1000 @@ -37,7 +39,32 @@ ->param('limit', 0, new Integer(true), 'Total number of records to add to database') ->param('name', 'myapp_' . uniqid(), new Text(0), 'Name of created database.', true) ->param('sharedTables', false, new Boolean(true), 'Whether to use shared tables', true) - ->action(function (string $adapter, int $limit, string $name, bool $sharedTables) { + ->action(function (string $adapter, int $limit, string $name, bool $sharedTables) use ($authorization) { + + + $createSchema = function (Database $database) use ($authorization): void { + if ($database->exists($database->getDatabase())) { + $database->delete($database->getDatabase()); + } + $database->create(); + + $authorization->addRole(Role::any()->toString()); + + $database->createCollection('articles', permissions: [ + Permission::create(Role::any()), + Permission::read(Role::any()), + ]); + + $database->createAttribute('articles', 'author', Database::VAR_STRING, 256, true); + $database->createAttribute('articles', 'created', Database::VAR_DATETIME, 0, true, filters: ['datetime']); + $database->createAttribute('articles', 'text', Database::VAR_STRING, 5000, true); + $database->createAttribute('articles', 'genre', Database::VAR_STRING, 256, true); + $database->createAttribute('articles', 'views', Database::VAR_INTEGER, 0, true); + $database->createAttribute('articles', 'tags', Database::VAR_STRING, 0, true, array: true); + $database->createIndex('articles', 'text', Database::INDEX_FULLTEXT, ['text']); + }; + + $start = null; $namespace = '_ns'; $cache = new Cache(new NoCache()); @@ -95,7 +122,7 @@ $cfg['attrs'] ); - createSchema( + $createSchema( (new Database(new ($cfg['adapter'])($pdo), $cache)) ->setDatabase($name) ->setNamespace($namespace) @@ -138,27 +165,7 @@ Console::success("Completed in {$time} seconds"); }); -function createSchema(Database $database): void -{ - if ($database->exists($database->getDatabase())) { - $database->delete($database->getDatabase()); - } - $database->create(); - Authorization::setRole(Role::any()->toString()); - - $database->createCollection('articles', permissions: [ - Permission::create(Role::any()), - Permission::read(Role::any()), - ]); - - $database->createAttribute('articles', 'author', Database::VAR_STRING, 256, true); - $database->createAttribute('articles', 'created', Database::VAR_DATETIME, 0, true, filters: ['datetime']); - $database->createAttribute('articles', 'text', Database::VAR_STRING, 5000, true); - $database->createAttribute('articles', 'genre', Database::VAR_STRING, 256, true); - $database->createAttribute('articles', 'views', Database::VAR_INTEGER, 0, true); - $database->createAttribute('articles', 'tags', Database::VAR_STRING, 0, true, array: true); -} function createDocuments(Database $database): void { diff --git a/bin/tasks/query.php b/bin/tasks/query.php index 3a8f8b613..03bb0be49 100644 --- a/bin/tasks/query.php +++ b/bin/tasks/query.php @@ -15,15 +15,18 @@ use Utopia\Database\Adapter\Postgres; use Utopia\Database\Database; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; use Utopia\Validator\Boolean; use Utopia\Validator\Integer; use Utopia\Validator\Text; +use Utopia\Database\Validator\Authorization; /** * @Example * docker compose exec tests bin/query --adapter=mariadb --limit=1000 --name=testing */ + +$authorization = new Authorization(); + $cli ->task('query') ->desc('Query mock data') @@ -31,7 +34,15 @@ ->param('name', '', new Text(0), 'Name of created database.') ->param('limit', 25, new Integer(true), 'Limit on queried documents', true) ->param('sharedTables', false, new Boolean(true), 'Whether to use shared tables', true) - ->action(function (string $adapter, string $name, int $limit, bool $sharedTables) { + ->action(function (string $adapter, string $name, int $limit, bool $sharedTables) use ($authorization) { + + $setRoles = function ($faker, $count) use ($authorization): int { + for ($i = 0; $i < $count; $i++) { + $authorization->addRole($faker->numerify('user####')); + } + return \count($authorization->getRoles()); + }; + $namespace = '_ns'; $cache = new Cache(new NoCache()); @@ -91,35 +102,35 @@ $report = []; - $count = setRoles($faker, 1); + $count = $setRoles($faker, 1); Console::info("\nRunning queries with {$count} authorization roles:"); $report[] = [ 'roles' => $count, 'results' => runQueries($database, $limit) ]; - $count = setRoles($faker, 100); + $count = $setRoles($faker, 100); Console::info("\nRunning queries with {$count} authorization roles:"); $report[] = [ 'roles' => $count, 'results' => runQueries($database, $limit) ]; - $count = setRoles($faker, 400); + $count = $setRoles($faker, 400); Console::info("\nRunning queries with {$count} authorization roles:"); $report[] = [ 'roles' => $count, 'results' => runQueries($database, $limit) ]; - $count = setRoles($faker, 500); + $count = $setRoles($faker, 500); Console::info("\nRunning queries with {$count} authorization roles:"); $report[] = [ 'roles' => $count, 'results' => runQueries($database, $limit) ]; - $count = setRoles($faker, 1000); + $count = $setRoles($faker, 1000); Console::info("\nRunning queries with {$count} authorization roles:"); $report[] = [ 'roles' => $count, @@ -136,13 +147,6 @@ \fclose($results); }); -function setRoles($faker, $count): int -{ - for ($i = 0; $i < $count; $i++) { - Authorization::setRole($faker->numerify('user####')); - } - return \count(Authorization::getRoles()); -} function runQueries(Database $database, int $limit): array { diff --git a/bin/tasks/relationships.php b/bin/tasks/relationships.php index 595d01531..128fe61df 100644 --- a/bin/tasks/relationships.php +++ b/bin/tasks/relationships.php @@ -34,6 +34,8 @@ * @Example * docker compose exec tests bin/relationships --adapter=mariadb --limit=1000 */ +$authorization = new Authorization(); + $cli ->task('relationships') ->desc('Load database with mock relationships for testing') @@ -42,13 +44,84 @@ ->param('name', 'myapp_' . uniqid(), new Text(0), 'Name of created database.', true) ->param('sharedTables', false, new Boolean(true), 'Whether to use shared tables', true) ->param('runs', 1, new Integer(true), 'Number of times to run benchmarks', true) - ->action(function (string $adapter, int $limit, string $name, bool $sharedTables, int $runs) { + ->action(function (string $adapter, int $limit, string $name, bool $sharedTables, int $runs) use ($authorization) { $start = null; $namespace = '_ns'; $cache = new Cache(new NoCache()); Console::info("Filling {$adapter} with {$limit} records: {$name}"); + $createRelationshipSchema = function (Database $database) use ($authorization): void { + if ($database->exists($database->getDatabase())) { + $database->delete($database->getDatabase()); + } + $database->create(); + + $authorization->addRole(Role::any()->toString()); + + $database->createCollection('authors', permissions: [ + Permission::create(Role::any()), + Permission::read(Role::any()), + Permission::update(Role::any()), + ]); + $database->createAttribute('authors', 'name', Database::VAR_STRING, 256, true); + $database->createAttribute('authors', 'created', Database::VAR_DATETIME, 0, true, filters: ['datetime']); + $database->createAttribute('authors', 'bio', Database::VAR_STRING, 5000, true); + $database->createAttribute('authors', 'avatar', Database::VAR_STRING, 256, true); + $database->createAttribute('authors', 'website', Database::VAR_STRING, 256, true); + + $database->createCollection('articles', permissions: [ + Permission::create(Role::any()), + Permission::read(Role::any()), + Permission::update(Role::any()), + ]); + $database->createAttribute('articles', 'title', Database::VAR_STRING, 256, true); + $database->createAttribute('articles', 'text', Database::VAR_STRING, 5000, true); + $database->createAttribute('articles', 'genre', Database::VAR_STRING, 256, true); + $database->createAttribute('articles', 'views', Database::VAR_INTEGER, 0, true); + $database->createAttribute('articles', 'tags', Database::VAR_STRING, 0, true, array: true); + + $database->createCollection('users', permissions: [ + Permission::create(Role::any()), + Permission::read(Role::any()), + Permission::update(Role::any()), + ]); + $database->createAttribute('users', 'username', Database::VAR_STRING, 256, true); + $database->createAttribute('users', 'email', Database::VAR_STRING, 256, true); + $database->createAttribute('users', 'password', Database::VAR_STRING, 256, true); + + $database->createCollection('comments', permissions: [ + Permission::create(Role::any()), + Permission::read(Role::any()), + Permission::update(Role::any()), + ]); + $database->createAttribute('comments', 'content', Database::VAR_STRING, 256, true); + $database->createAttribute('comments', 'likes', Database::VAR_INTEGER, 8, true, signed: false); + + $database->createCollection('profiles', permissions: [ + Permission::create(Role::any()), + Permission::read(Role::any()), + Permission::update(Role::any()), + ]); + $database->createAttribute('profiles', 'bio_extended', Database::VAR_STRING, 10000, true); + $database->createAttribute('profiles', 'social_links', Database::VAR_STRING, 256, true, array: true); + $database->createAttribute('profiles', 'verified', Database::VAR_BOOLEAN, 0, true); + + $database->createCollection('categories', permissions: [ + Permission::create(Role::any()), + Permission::read(Role::any()), + Permission::update(Role::any()), + ]); + $database->createAttribute('categories', 'name', Database::VAR_STRING, 256, true); + $database->createAttribute('categories', 'description', Database::VAR_STRING, 1000, true); + + $database->createRelationship('authors', 'articles', Database::RELATION_MANY_TO_MANY, true, onDelete: Database::RELATION_MUTATE_SET_NULL); + $database->createRelationship('articles', 'comments', Database::RELATION_ONE_TO_MANY, true, twoWayKey: 'article', onDelete: Database::RELATION_MUTATE_CASCADE); + $database->createRelationship('users', 'comments', Database::RELATION_ONE_TO_MANY, true, twoWayKey: 'user', onDelete: Database::RELATION_MUTATE_CASCADE); + $database->createRelationship('authors', 'profiles', Database::RELATION_ONE_TO_ONE, true, twoWayKey: 'author', onDelete: Database::RELATION_MUTATE_CASCADE); + $database->createRelationship('articles', 'categories', Database::RELATION_MANY_TO_ONE, true, id: 'category', twoWayKey: 'articles', onDelete: Database::RELATION_MUTATE_SET_NULL); + }; + $dbAdapters = [ 'mariadb' => [ 'host' => 'mariadb', @@ -98,7 +171,7 @@ ->setNamespace($namespace) ->setSharedTables($sharedTables); - createRelationshipSchema($database); + $createRelationshipSchema($database); // Create categories and users once before parallel batch creation $globalDocs = createGlobalDocuments($database, $limit); @@ -165,77 +238,6 @@ displayBenchmarkResults($results, $runs); }); -function createRelationshipSchema(Database $database): void -{ - if ($database->exists($database->getDatabase())) { - $database->delete($database->getDatabase()); - } - $database->create(); - - Authorization::setRole(Role::any()->toString()); - - $database->createCollection('authors', permissions: [ - Permission::create(Role::any()), - Permission::read(Role::any()), - Permission::update(Role::any()), - ]); - $database->createAttribute('authors', 'name', Database::VAR_STRING, 256, true); - $database->createAttribute('authors', 'created', Database::VAR_DATETIME, 0, true, filters: ['datetime']); - $database->createAttribute('authors', 'bio', Database::VAR_STRING, 5000, true); - $database->createAttribute('authors', 'avatar', Database::VAR_STRING, 256, true); - $database->createAttribute('authors', 'website', Database::VAR_STRING, 256, true); - - $database->createCollection('articles', permissions: [ - Permission::create(Role::any()), - Permission::read(Role::any()), - Permission::update(Role::any()), - ]); - $database->createAttribute('articles', 'title', Database::VAR_STRING, 256, true); - $database->createAttribute('articles', 'text', Database::VAR_STRING, 5000, true); - $database->createAttribute('articles', 'genre', Database::VAR_STRING, 256, true); - $database->createAttribute('articles', 'views', Database::VAR_INTEGER, 0, true); - $database->createAttribute('articles', 'tags', Database::VAR_STRING, 0, true, array: true); - - $database->createCollection('users', permissions: [ - Permission::create(Role::any()), - Permission::read(Role::any()), - Permission::update(Role::any()), - ]); - $database->createAttribute('users', 'username', Database::VAR_STRING, 256, true); - $database->createAttribute('users', 'email', Database::VAR_STRING, 256, true); - $database->createAttribute('users', 'password', Database::VAR_STRING, 256, true); - - $database->createCollection('comments', permissions: [ - Permission::create(Role::any()), - Permission::read(Role::any()), - Permission::update(Role::any()), - ]); - $database->createAttribute('comments', 'content', Database::VAR_STRING, 256, true); - $database->createAttribute('comments', 'likes', Database::VAR_INTEGER, 8, true, signed: false); - - $database->createCollection('profiles', permissions: [ - Permission::create(Role::any()), - Permission::read(Role::any()), - Permission::update(Role::any()), - ]); - $database->createAttribute('profiles', 'bio_extended', Database::VAR_STRING, 10000, true); - $database->createAttribute('profiles', 'social_links', Database::VAR_STRING, 256, true, array: true); - $database->createAttribute('profiles', 'verified', Database::VAR_BOOLEAN, 0, true); - - $database->createCollection('categories', permissions: [ - Permission::create(Role::any()), - Permission::read(Role::any()), - Permission::update(Role::any()), - ]); - $database->createAttribute('categories', 'name', Database::VAR_STRING, 256, true); - $database->createAttribute('categories', 'description', Database::VAR_STRING, 1000, true); - - $database->createRelationship('authors', 'articles', Database::RELATION_MANY_TO_MANY, true, onDelete: Database::RELATION_MUTATE_SET_NULL); - $database->createRelationship('articles', 'comments', Database::RELATION_ONE_TO_MANY, true, twoWayKey: 'article', onDelete: Database::RELATION_MUTATE_CASCADE); - $database->createRelationship('users', 'comments', Database::RELATION_ONE_TO_MANY, true, twoWayKey: 'user', onDelete: Database::RELATION_MUTATE_CASCADE); - $database->createRelationship('authors', 'profiles', Database::RELATION_ONE_TO_ONE, true, twoWayKey: 'author', onDelete: Database::RELATION_MUTATE_CASCADE); - $database->createRelationship('articles', 'categories', Database::RELATION_MANY_TO_ONE, true, id: 'category', twoWayKey: 'articles', onDelete: Database::RELATION_MUTATE_SET_NULL); -} function createGlobalDocuments(Database $database, int $limit): array { diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 73b09af6f..d98233e03 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -14,7 +14,6 @@ use Utopia\Database\Exception\Timeout as TimeoutException; use Utopia\Database\Exception\Transaction as TransactionException; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; abstract class SQL extends Adapter { From de79b672786c43ed9e318c9b554114ec57e6df58 Mon Sep 17 00:00:00 2001 From: shimon Date: Wed, 15 Oct 2025 12:42:20 +0300 Subject: [PATCH 06/29] Refactor authorization handling to use instance methods instead of static calls. Update README to reflect changes in role delegation method. --- README.md | 2 +- src/Database/Database.php | 11 ++ src/Database/Mirror.php | 2 +- tests/e2e/Adapter/Scopes/AttributeTests.php | 2 +- tests/e2e/Adapter/Scopes/CollectionTests.php | 6 +- tests/e2e/Adapter/Scopes/DocumentTests.php | 78 +++++----- tests/e2e/Adapter/Scopes/GeneralTests.php | 12 +- tests/e2e/Adapter/Scopes/PermissionTests.php | 144 +++++++++--------- .../e2e/Adapter/Scopes/RelationshipTests.php | 12 +- 9 files changed, 140 insertions(+), 129 deletions(-) diff --git a/README.md b/README.md index 835bee0ee..12e901bb3 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ A list of the utopia/php concepts and their relevant equivalent using the differ - **Document** - A simple JSON object that will be stored in one of the utopia/database collections. For SQL-based adapters, this will be equivalent to a row. For a No-SQL adapter, this will equivalent to a native document. - **Attribute** - A simple document attribute. For SQL-based adapters, this will be equivalent to a column. For a No-SQL adapter, this will equivalent to a native document field. - **Index** - A simple collection index used to improve the performance of your database queries. -- **Permissions** - Using permissions, you can decide which roles have read, create, update and delete access for a specific document. The special attribute `$permissions` is used to store permission metadata for each document in the collection. A permission role can be any string you want. You can use `Authorization::setRole()` to delegate new roles to your users, once obtained a new role a user would gain read, create, update or delete access to a relevant document. +- **Permissions** - Using permissions, you can decide which roles have read, create, update and delete access for a specific document. The special attribute `$permissions` is used to store permission metadata for each document in the collection. A permission role can be any string you want. You can use `$authorization->addRole()` to delegate new roles to your users, once obtained a new role a user would gain read, create, update or delete access to a relevant document. ### Filters diff --git a/src/Database/Database.php b/src/Database/Database.php index fc9a76d13..7ab449681 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -25,6 +25,7 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Validator\Authorization; +use Utopia\Database\Validator\Authorization\Input; use Utopia\Database\Validator\Index as IndexValidator; use Utopia\Database\Validator\IndexDependency as IndexDependencyValidator; use Utopia\Database\Validator\PartialStructure; @@ -870,6 +871,16 @@ public function setAuthorization(Authorization $authorization): self return $this; } + /** + * Get Authorization + * + * @return Authorization + */ + public function getAuthorization(): Authorization + { + return $this->authorization; + } + /** * Clear metadata * diff --git a/src/Database/Mirror.php b/src/Database/Mirror.php index dac23a063..eca43a6e4 100644 --- a/src/Database/Mirror.php +++ b/src/Database/Mirror.php @@ -1084,7 +1084,7 @@ protected function getUpgradeStatus(string $collection): ?Document return new Document(); } - return Authorization::skip(function () use ($collection) { + return $this->getAuthorization()->skip(function () use ($collection) { try { return $this->source->getDocument('upgrades', $collection); } catch (\Throwable) { diff --git a/tests/e2e/Adapter/Scopes/AttributeTests.php b/tests/e2e/Adapter/Scopes/AttributeTests.php index 8e9c4921d..7e251f60d 100644 --- a/tests/e2e/Adapter/Scopes/AttributeTests.php +++ b/tests/e2e/Adapter/Scopes/AttributeTests.php @@ -1240,7 +1240,7 @@ public function testCleanupAttributeTests(): void */ public function testArrayAttribute(): void { - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); /** @var Database $database */ $database = $this->getDatabase(); diff --git a/tests/e2e/Adapter/Scopes/CollectionTests.php b/tests/e2e/Adapter/Scopes/CollectionTests.php index d88127dd4..dad91083a 100644 --- a/tests/e2e/Adapter/Scopes/CollectionTests.php +++ b/tests/e2e/Adapter/Scopes/CollectionTests.php @@ -401,7 +401,7 @@ public function testSizeCollection(): void $this->assertGreaterThan($size1, $size2); - Authorization::skip(function () use ($loopCount) { + $this->getDatabase()->getAuthorization()->skip(function () use ($loopCount) { for ($i = 0; $i < $loopCount; $i++) { $this->getDatabase()->deleteDocument('sizeTest2', 'doc' . $i); } @@ -917,7 +917,7 @@ public function testLabels(): void $this->assertEmpty($documents); - Authorization::setRole(Role::label('reader')->toString()); + $this->getDatabase()->getAuthorization()->addRole(Role::label('reader')->toString()); $documents = $database->find('labels_test'); @@ -1329,7 +1329,7 @@ public function testSharedTablesDuplicates(): void public function testEvents(): void { - Authorization::skip(function () { + $this->getDatabase()->getAuthorization()->skip(function () { $database = $this->getDatabase(); $events = [ diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 19f5c8070..46a884f74 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -583,7 +583,7 @@ public function testSkipPermissions(): void $documents = array_map(fn ($d) => new Document($d), $data); - Authorization::disable(); + $this->getDatabase()->getAuthorization()->disable(); $results = []; $count = $database->upsertDocuments( @@ -594,7 +594,7 @@ public function testSkipPermissions(): void } ); - Authorization::reset(); + $this->getDatabase()->getAuthorization()->reset(); $this->assertEquals(2, \count($results)); $this->assertEquals(2, $count); @@ -1398,7 +1398,7 @@ public function testGetDocumentSelect(Document $document): Document */ public function testFind(): array { - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -1672,7 +1672,7 @@ public function testFindCheckPermissions(): void /** * Check Permissions */ - Authorization::setRole('user:x'); + $this->getDatabase()->getAuthorization()->addRole('user:x'); $documents = $database->find('movies'); $this->assertEquals(6, count($documents)); @@ -3146,7 +3146,7 @@ public function testNestedIDQueries(): void /** @var Database $database */ $database = $this->getDatabase(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $database->createCollection('movies_nested_id', permissions: [ Permission::create(Role::any()), @@ -3854,30 +3854,30 @@ public function testCount(): void $count = $database->count('movies', [Query::equal('with-dash', ['Works2', 'Works3'])]); $this->assertEquals(4, $count); - Authorization::unsetRole('user:x'); + $this->getDatabase()->getAuthorization()->removeRole('user:x'); $count = $database->count('movies'); $this->assertEquals(5, $count); - Authorization::disable(); + $this->getDatabase()->getAuthorization()->disable(); $count = $database->count('movies'); $this->assertEquals(6, $count); - Authorization::reset(); + $this->getDatabase()->getAuthorization()->reset(); - Authorization::disable(); + $this->getDatabase()->getAuthorization()->disable(); $count = $database->count('movies', [], 3); $this->assertEquals(3, $count); - Authorization::reset(); + $this->getDatabase()->getAuthorization()->reset(); /** * Test that OR queries are handled correctly */ - Authorization::disable(); + $this->getDatabase()->getAuthorization()->disable(); $count = $database->count('movies', [ Query::equal('director', ['TBD', 'Joe Johnston']), Query::equal('year', [2025]), ]); $this->assertEquals(1, $count); - Authorization::reset(); + $this->getDatabase()->getAuthorization()->reset(); } /** @@ -3888,7 +3888,7 @@ public function testSum(): void /** @var Database $database */ $database = $this->getDatabase(); - Authorization::setRole('user:x'); + $this->getDatabase()->getAuthorization()->addRole('user:x'); $sum = $database->sum('movies', 'year', [Query::equal('year', [2019]),]); $this->assertEquals(2019 + 2019, $sum); @@ -3902,8 +3902,8 @@ public function testSum(): void $sum = $database->sum('movies', 'year', [Query::equal('year', [2019])], 1); $this->assertEquals(2019, $sum); - Authorization::unsetRole('user:x'); - Authorization::unsetRole('userx'); + $this->getDatabase()->getAuthorization()->removeRole('user:x'); + $this->getDatabase()->getAuthorization()->removeRole('userx'); $sum = $database->sum('movies', 'year', [Query::equal('year', [2019]),]); $this->assertEquals(2019 + 2019, $sum); $sum = $database->sum('movies', 'year'); @@ -4322,8 +4322,8 @@ public function testUpdateDocuments(): void } $collection = 'testUpdateDocuments'; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $database->createCollection($collection, attributes: [ new Document([ @@ -4447,7 +4447,7 @@ public function testUpdateDocuments(): void // Check document level permissions $database->updateCollection($collection, permissions: [], documentSecurity: true); - Authorization::skip(function () use ($collection, $database) { + $this->getDatabase()->getAuthorization()->skip(function () use ($collection, $database) { $database->updateDocument($collection, 'doc0', new Document([ 'string' => 'text📝 updated all', '$permissions' => [ @@ -4459,7 +4459,7 @@ public function testUpdateDocuments(): void ])); }); - Authorization::setRole(Role::user('asd')->toString()); + $this->getDatabase()->getAuthorization()->addRole(Role::user('asd')->toString()); $database->updateDocuments($collection, new Document([ 'string' => 'permission text', @@ -4471,7 +4471,7 @@ public function testUpdateDocuments(): void $this->assertCount(1, $documents); - Authorization::skip(function () use ($collection, $database) { + $this->getDatabase()->getAuthorization()->skip(function () use ($collection, $database) { $unmodifiedDocuments = $database->find($collection, [ Query::equal('string', ['text📝 updated all']), ]); @@ -4479,7 +4479,7 @@ public function testUpdateDocuments(): void $this->assertCount(9, $unmodifiedDocuments); }); - Authorization::skip(function () use ($collection, $database) { + $this->getDatabase()->getAuthorization()->skip(function () use ($collection, $database) { $database->updateDocuments($collection, new Document([ '$permissions' => [ Permission::read(Role::any()), @@ -4501,8 +4501,8 @@ public function testUpdateDocuments(): void $this->assertEquals('batchSize Test', $document->getAttribute('string')); } - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); } public function testUpdateDocumentsWithCallbackSupport(): void @@ -4516,8 +4516,8 @@ public function testUpdateDocumentsWithCallbackSupport(): void } $collection = 'update_callback'; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $database->createCollection($collection, attributes: [ new Document([ @@ -4605,8 +4605,8 @@ public function testUpdateDocumentsWithCallbackSupport(): void */ public function testReadPermissionsSuccess(Document $document): Document { - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -4631,12 +4631,12 @@ public function testReadPermissionsSuccess(Document $document): Document $this->assertEquals(false, $document->isEmpty()); - Authorization::cleanRoles(); + $this->getDatabase()->getAuthorization()->cleanRoles(); $document = $database->getDocument($document->getCollection(), $document->getId()); $this->assertEquals(true, $document->isEmpty()); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); return $document; } @@ -4646,7 +4646,7 @@ public function testReadPermissionsSuccess(Document $document): Document */ public function testWritePermissionsSuccess(Document $document): void { - Authorization::cleanRoles(); + $this->getDatabase()->getAuthorization()->cleanRoles(); /** @var Database $database */ $database = $this->getDatabase(); @@ -4678,8 +4678,8 @@ public function testWritePermissionsUpdateFailure(Document $document): Document { $this->expectException(AuthorizationException::class); - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -4702,7 +4702,7 @@ public function testWritePermissionsUpdateFailure(Document $document): Document 'colors' => ['pink', 'green', 'blue'], ])); - Authorization::cleanRoles(); + $this->getDatabase()->getAuthorization()->cleanRoles(); $document = $database->updateDocument('documents', $document->getId(), new Document([ '$id' => ID::custom($document->getId()), @@ -4768,7 +4768,7 @@ public function testUniqueIndexDuplicateUpdate(): void /** @var Database $database */ $database = $this->getDatabase(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); // create document then update to conflict with index $document = $database->createDocument('movies', new Document([ '$permissions' => [ @@ -4941,7 +4941,7 @@ public function testDeleteBulkDocuments(): void $this->assertEquals(0, $database->deleteDocuments('bulk_delete')); - $documents = Authorization::skip(function () use ($database) { + $documents = $this->getDatabase()->getAuthorization()->skip(function () use ($database) { return $database->find('bulk_delete'); }); @@ -6445,8 +6445,8 @@ public function testUpdateDocumentsSuccessiveCallsDoNotResetDefaults(): void } $collectionId = 'successive_updates'; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); // Create collection with two attributes that have default values $database->createCollection($collectionId); @@ -6497,8 +6497,8 @@ public function testUpdateDocumentSuccessiveCallsDoNotResetDefaults(): void $database = $this->getDatabase(); $collectionId = 'successive_update_single'; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); // Create collection with two attributes that have default values $database->createCollection($collectionId); diff --git a/tests/e2e/Adapter/Scopes/GeneralTests.php b/tests/e2e/Adapter/Scopes/GeneralTests.php index 37880ad5d..c9ae7224a 100644 --- a/tests/e2e/Adapter/Scopes/GeneralTests.php +++ b/tests/e2e/Adapter/Scopes/GeneralTests.php @@ -88,7 +88,7 @@ public function testQueryTimeout(): void public function testPreserveDatesUpdate(): void { - Authorization::disable(); + $this->getDatabase()->getAuthorization()->disable(); /** @var Database $database */ $database = $this->getDatabase(); @@ -178,12 +178,12 @@ public function testPreserveDatesUpdate(): void $database->setPreserveDates(false); - Authorization::reset(); + $this->getDatabase()->getAuthorization()->reset(); } public function testPreserveDatesCreate(): void { - Authorization::disable(); + $this->getDatabase()->getAuthorization()->disable(); /** @var Database $database */ $database = $this->getDatabase(); @@ -283,7 +283,7 @@ public function testPreserveDatesCreate(): void $database->setPreserveDates(false); - Authorization::reset(); + $this->getDatabase()->getAuthorization()->reset(); } public function testGetAttributeLimit(): void @@ -629,8 +629,8 @@ public function testCacheFallback(): void return; } - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $database = $this->getDatabase(); // Write mock data diff --git a/tests/e2e/Adapter/Scopes/PermissionTests.php b/tests/e2e/Adapter/Scopes/PermissionTests.php index 6c51644d2..0b02494f8 100644 --- a/tests/e2e/Adapter/Scopes/PermissionTests.php +++ b/tests/e2e/Adapter/Scopes/PermissionTests.php @@ -155,9 +155,9 @@ public function testUnsetPermissions(): void $documents = $database->find(__FUNCTION__); $this->assertEquals(0, count($documents)); - Authorization::disable(); + $this->getDatabase()->getAuthorization()->disable(); $documents = $database->find(__FUNCTION__); - Authorization::reset(); + $this->getDatabase()->getAuthorization()->reset(); $this->assertEquals(3, count($documents)); @@ -204,8 +204,8 @@ public function testCreateDocumentsEmptyPermission(): void public function testReadPermissionsFailure(): Document { - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -228,13 +228,13 @@ public function testReadPermissionsFailure(): Document 'colors' => ['pink', 'green', 'blue'], ])); - Authorization::cleanRoles(); + $this->getDatabase()->getAuthorization()->cleanRoles(); $document = $database->getDocument($document->getCollection(), $document->getId()); $this->assertEquals(true, $document->isEmpty()); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); return $document; } @@ -320,7 +320,7 @@ public function testUpdateDocumentsPermissions(): void ], permissions: [], documentSecurity: true); // Test we can bulk update permissions we have access to - Authorization::skip(function () use ($collection, $database) { + $this->getDatabase()->getAuthorization()->skip(function () use ($collection, $database) { for ($i = 0; $i < 10; $i++) { $database->createDocument($collection, new Document([ '$id' => 'doc' . $i, @@ -358,7 +358,7 @@ public function testUpdateDocumentsPermissions(): void /** @var Database $database */ $database = $this->getDatabase(); - $documents = Authorization::skip(function () use ($collection, $database) { + $documents = $this->getDatabase()->getAuthorization()->skip(function () use ($collection, $database) { return $database->find($collection); }); @@ -387,7 +387,7 @@ public function testUpdateDocumentsPermissions(): void $this->assertCount(1, $unmodifiedDocuments); - Authorization::setRole(Role::user('user2')->toString()); + $this->getDatabase()->getAuthorization()->addRole(Role::user('user2')->toString()); // Test Bulk permission update with data $modified = $database->updateDocuments($collection, new Document([ @@ -402,7 +402,7 @@ public function testUpdateDocumentsPermissions(): void $this->assertEquals(10, $modified); - $documents = Authorization::skip(function () use ($collection) { + $documents = $this->getDatabase()->getAuthorization()->skip(function () use ($collection) { return $this->getDatabase()->find($collection); }); @@ -455,8 +455,8 @@ public function testCollectionPermissionsCountThrowsException(array $data): void { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -476,8 +476,8 @@ public function testCollectionPermissionsCountWorks(array $data): array { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -496,8 +496,8 @@ public function testCollectionPermissionsCountWorks(array $data): array */ public function testCollectionPermissionsCreateThrowsException(Document $collection): void { - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); /** @var Database $database */ @@ -520,8 +520,8 @@ public function testCollectionPermissionsCreateThrowsException(Document $collect */ public function testCollectionPermissionsCreateWorks(Document $collection): array { - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -548,8 +548,8 @@ public function testCollectionPermissionsDeleteThrowsException(array $data): voi { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); @@ -570,8 +570,8 @@ public function testCollectionPermissionsDeleteWorks(array $data): void { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -601,8 +601,8 @@ public function testCollectionPermissionsFindThrowsException(array $data): void { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); @@ -621,8 +621,8 @@ public function testCollectionPermissionsFindWorks(array $data): array { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -630,8 +630,8 @@ public function testCollectionPermissionsFindWorks(array $data): array $documents = $database->find($collection->getId()); $this->assertNotEmpty($documents); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('random')->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::user('random')->toString()); try { $database->find($collection->getId()); @@ -650,8 +650,8 @@ public function testCollectionPermissionsGetThrowsException(array $data): void { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -673,8 +673,8 @@ public function testCollectionPermissionsGetWorks(array $data): array { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -775,8 +775,8 @@ public function testCollectionPermissionsRelationshipsCountWorks(array $data): v { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -787,8 +787,8 @@ public function testCollectionPermissionsRelationshipsCountWorks(array $data): v $this->assertEquals(1, $documents); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('random')->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::user('random')->toString()); $documents = $database->count( $collection->getId() @@ -796,8 +796,8 @@ public function testCollectionPermissionsRelationshipsCountWorks(array $data): v $this->assertEquals(1, $documents); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('unknown')->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::user('unknown')->toString()); $documents = $database->count( $collection->getId() @@ -814,8 +814,8 @@ public function testCollectionPermissionsRelationshipsCreateThrowsException(arra { [$collection, $collectionOneToOne, $collectionOneToMany] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); /** @var Database $database */ @@ -839,8 +839,8 @@ public function testCollectionPermissionsRelationshipsDeleteThrowsException(arra { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); @@ -861,8 +861,8 @@ public function testCollectionPermissionsRelationshipsDeleteThrowsException(arra public function testCollectionPermissionsRelationshipsCreateWorks(array $data): array { [$collection, $collectionOneToOne, $collectionOneToMany] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -917,8 +917,8 @@ public function testCollectionPermissionsRelationshipsDeleteWorks(array $data): { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -937,8 +937,8 @@ public function testCollectionPermissionsRelationshipsFindWorks(array $data): vo { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -956,8 +956,8 @@ public function testCollectionPermissionsRelationshipsFindWorks(array $data): vo $this->assertCount(2, $document->getAttribute(Database::RELATION_ONE_TO_MANY)); $this->assertFalse($document->isEmpty()); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('random')->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::user('random')->toString()); $documents = $database->find( $collection->getId() @@ -972,8 +972,8 @@ public function testCollectionPermissionsRelationshipsFindWorks(array $data): vo $this->assertCount(1, $document->getAttribute(Database::RELATION_ONE_TO_MANY)); $this->assertFalse($document->isEmpty()); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('unknown')->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::user('unknown')->toString()); $documents = $database->find( $collection->getId() @@ -991,8 +991,8 @@ public function testCollectionPermissionsRelationshipsGetThrowsException(array $ { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -1014,8 +1014,8 @@ public function testCollectionPermissionsRelationshipsGetWorks(array $data): arr { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -1031,8 +1031,8 @@ public function testCollectionPermissionsRelationshipsGetWorks(array $data): arr $this->assertCount(2, $document->getAttribute(Database::RELATION_ONE_TO_MANY)); $this->assertFalse($document->isEmpty()); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('random')->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::user('random')->toString()); $document = $database->getDocument( $collection->getId(), @@ -1056,8 +1056,8 @@ public function testCollectionPermissionsRelationshipsUpdateThrowsException(arra { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); @@ -1080,8 +1080,8 @@ public function testCollectionPermissionsRelationshipsUpdateWorks(array $data): { [$collection, $collectionOneToOne, $collectionOneToMany, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -1094,8 +1094,8 @@ public function testCollectionPermissionsRelationshipsUpdateWorks(array $data): $this->assertTrue(true); - Authorization::cleanRoles(); - Authorization::setRole(Role::user('random')->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::user('random')->toString()); $database->updateDocument( $collection->getId(), @@ -1115,8 +1115,8 @@ public function testCollectionPermissionsRelationshipsUpdateWorks(array $data): public function testCollectionPermissionsUpdateThrowsException(array $data): void { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $this->expectException(AuthorizationException::class); @@ -1139,8 +1139,8 @@ public function testCollectionPermissionsUpdateWorks(array $data): array { [$collection, $document] = $data; - Authorization::cleanRoles(); - Authorization::setRole(Role::users()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::users()->toString()); /** @var Database $database */ $database = $this->getDatabase(); @@ -1171,7 +1171,7 @@ public function testCollectionUpdatePermissionsThrowException(Document $collecti public function testWritePermissions(): void { - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $database = $this->getDatabase(); $database->createCollection('animals', permissions: [ @@ -1237,7 +1237,7 @@ public function testWritePermissions(): void $newCat = $cat->setAttribute('type', 'newCat'); $database->updateDocument('animals', 'cat', $newCat); - $docs = Authorization::skip(fn () => $database->find('animals')); + $docs = $this->getDatabase()->getAuthorization()->skip(fn () => $database->find('animals')); $this->assertCount(1, $docs); $this->assertEquals('cat', $docs[0]['$id']); $this->assertEquals('newCat', $docs[0]['type']); @@ -1253,8 +1253,8 @@ public function testCreateRelationDocumentWithoutUpdatePermission(): void return; } - Authorization::cleanRoles(); - Authorization::setRole(Role::user('a')->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::user('a')->toString()); $database->createCollection('parentRelationTest', [], [], [ Permission::read(Role::user('a')), diff --git a/tests/e2e/Adapter/Scopes/RelationshipTests.php b/tests/e2e/Adapter/Scopes/RelationshipTests.php index 14573108b..57f25d409 100644 --- a/tests/e2e/Adapter/Scopes/RelationshipTests.php +++ b/tests/e2e/Adapter/Scopes/RelationshipTests.php @@ -1745,8 +1745,8 @@ public function testEnforceRelationshipPermissions(): void $this->expectNotToPerformAssertions(); return; } - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $lawn1 = $database->getDocument('lawns', 'lawn1'); $this->assertEquals('Lawn 1', $lawn1['name']); @@ -1823,7 +1823,7 @@ public function testEnforceRelationshipPermissions(): void $this->assertEquals('Missing "delete" permission for role "user:user2". Only "["any"]" scopes are allowed and "["user:user2"]" was given.', $e->getMessage()); } - Authorization::setRole(Role::user('user1')->toString()); + $this->getDatabase()->getAuthorization()->addRole(Role::user('user1')->toString()); $bird1 = $database->getDocument('birds', 'bird1'); @@ -1836,7 +1836,7 @@ public function testEnforceRelationshipPermissions(): void $this->assertEquals('Bird 1 Updated', $bird1['name']); - Authorization::setRole(Role::user('user2')->toString()); + $this->getDatabase()->getAuthorization()->addRole(Role::user('user2')->toString()); // Try delete multi-level nested document $deleted = $database->deleteDocument( @@ -2246,8 +2246,8 @@ public function testUpdateDocumentsRelationships(): void return; } - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $this->getDatabase()->getAuthorization()->cleanRoles(); + $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); $this->getDatabase()->createCollection('testUpdateDocumentsRelationships1', attributes: [ new Document([ From 8007d6f42fcf804cc0d7b824d89679fdd34fe8f7 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 16 Oct 2025 16:39:14 +0300 Subject: [PATCH 07/29] Update PHPUnit configuration to stop on failure and refactor authorization handling in Database class to use instance methods. Adjust tests to reflect changes in method signatures. --- src/Database/Database.php | 66 +++++++++---------- src/Database/Mirror.php | 4 +- tests/e2e/Adapter/Base.php | 4 +- tests/e2e/Adapter/MariaDBTest.php | 10 +-- tests/e2e/Adapter/MirrorTest.php | 23 +++---- tests/e2e/Adapter/MySQLTest.php | 10 +-- tests/e2e/Adapter/PoolTest.php | 10 +-- tests/e2e/Adapter/PostgresTest.php | 12 ++-- tests/e2e/Adapter/SQLiteTest.php | 8 +-- tests/e2e/Adapter/Scopes/AttributeTests.php | 2 +- tests/e2e/Adapter/Scopes/CollectionTests.php | 2 +- tests/e2e/Adapter/Scopes/IndexTests.php | 2 +- tests/e2e/Adapter/Scopes/PermissionTests.php | 3 +- .../e2e/Adapter/SharedTables/MariaDBTest.php | 10 +-- tests/e2e/Adapter/SharedTables/MySQLTest.php | 10 +-- .../e2e/Adapter/SharedTables/PostgresTest.php | 12 ++-- tests/e2e/Adapter/SharedTables/SQLiteTest.php | 10 +-- 17 files changed, 98 insertions(+), 100 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 7ab449681..f9250b5b8 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4333,9 +4333,8 @@ public function createDocuments( $batchSize = \min(Database::INSERT_BATCH_SIZE, \max(1, $batchSize)); $collection = $this->silent(fn () => $this->getCollection($collection)); if ($collection->getId() !== self::METADATA) { - $authorization = new Authorization(self::PERMISSION_CREATE); - if (!$authorization->isValid($collection->getCreate())) { - throw new AuthorizationException($authorization->getDescription()); + if (!$this->authorization->isValid(new Input(self::PERMISSION_CREATE, $collection->getCreate()))) { + throw new AuthorizationException($this->authorization->getDescription()); } } @@ -4886,26 +4885,31 @@ public function updateDocument(string $collection, string $id, Document $documen break; } } - } - - if ($shouldUpdate) { - $isUpdateValid = $this->authorization->isValid(new Input(self::PERMISSION_UPDATE, [ - ...$collection->getUpdate(), - ...($documentSecurity ? $old->getUpdate() : []) - ])); + $updatePermissions = array_merge( + $collection->getUpdate(), + $documentSecurity ? $old->getUpdate() : [] + ); - $isReadValid = $this->authorization->isValid(new Input(self::PERMISSION_READ, [ - ...$collection->getRead(), - ...($documentSecurity ? $old->getRead() : []) - ])); + $readPermissions = array_merge( + $collection->getRead(), + $documentSecurity ? $old->getRead() : [] + ); - if (!$isReadValid || !$isUpdateValid) { - throw new AuthorizationException($this->authorization->getDescription()); + if ($shouldUpdate) { + if(!$this->authorization->isValid(new Input(self::PERMISSION_UPDATE, $updatePermissions))) { + throw new AuthorizationException($this->authorization->getDescription()); + } + } else { + if(!$this->authorization->isValid(new Input(self::PERMISSION_READ, $readPermissions))){ + throw new AuthorizationException($this->authorization->getDescription()); + } } - - $document->setAttribute('$updatedAt', ($newUpdatedAt === null || !$this->preserveDates) ? $time : $newUpdatedAt); } + + if ($shouldUpdate) { + $document->setAttribute('$updatedAt', ($newUpdatedAt === null || !$this->preserveDates) ? $time : $newUpdatedAt); + } // Check if document was updated after the request timestamp $oldUpdatedAt = new \DateTime($old->getUpdatedAt()); @@ -4991,11 +4995,10 @@ public function updateDocuments( } $documentSecurity = $collection->getAttribute('documentSecurity', false); - $authorization = new Authorization(self::PERMISSION_UPDATE); - $skipAuth = $authorization->isValid($collection->getUpdate()); + $skipAuth = $this->authorization->isValid(new Input(self::PERMISSION_UPDATE, $collection->getUpdate())); if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) { - throw new AuthorizationException($authorization->getDescription()); + throw new AuthorizationException($this->authorization->getDescription()); } $attributes = $collection->getAttribute('attributes', []); @@ -6070,7 +6073,7 @@ public function deleteDocument(string $collection, string $id): bool if ($collection->getId() !== self::METADATA) { $documentSecurity = $collection->getAttribute('documentSecurity', false); - if (!$this->authorization->isValid(new Input([ + if (!$this->authorization->isValid(new Input(self::PERMISSION_DELETE, [ ...$collection->getDelete(), ...($documentSecurity ? $document->getDelete() : []) ]))) { @@ -6522,11 +6525,10 @@ public function deleteDocuments( } $documentSecurity = $collection->getAttribute('documentSecurity', false); - $authorization = new Authorization(self::PERMISSION_DELETE); - $skipAuth = $authorization->isValid($collection->getDelete()); + $skipAuth = $this->authorization->isValid(new Input(self::PERMISSION_DELETE, $collection->getDelete())); if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) { - throw new AuthorizationException($authorization->getDescription()); + throw new AuthorizationException($this->authorization->getDescription()); } $attributes = $collection->getAttribute('attributes', []); @@ -6745,12 +6747,12 @@ public function find(string $collection, array $queries = [], string $forPermiss } } - $authorization = new Authorization($forPermission); $documentSecurity = $collection->getAttribute('documentSecurity', false); - $skipAuth = $authorization->isValid($collection->getPermissionsByType($forPermission)); + $skipAuth = $this->authorization->isValid(new Input($forPermission, $collection->getPermissionsByType($forPermission))); + if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) { - throw new AuthorizationException($authorization->getDescription()); + throw new AuthorizationException($this->authorization->getDescription()); } $relationships = \array_filter( @@ -6825,7 +6827,7 @@ public function find(string $collection, array $queries = [], string $forPermiss $cursorDirection, $forPermission ); - + $results = $skipAuth ? $this->authorization->skip($getResults) : $getResults(); } @@ -6968,11 +6970,7 @@ public function count(string $collection, array $queries = [], ?int $max = null) } } - $authorization = new Authorization(self::PERMISSION_READ); - if ($authorization->isValid($collection->getRead())) { - $skipAuth = true; - } - + $skipAuth = $this->authorization->isValid(new Input(self::PERMISSION_READ, $collection->getRead())); $relationships = \array_filter( $collection->getAttribute('attributes', []), fn (Document $attribute) => $attribute->getAttribute('type') === self::VAR_RELATIONSHIP diff --git a/src/Database/Mirror.php b/src/Database/Mirror.php index eca43a6e4..27f9a73a9 100644 --- a/src/Database/Mirror.php +++ b/src/Database/Mirror.php @@ -1083,8 +1083,8 @@ protected function getUpgradeStatus(string $collection): ?Document if ($collection === 'upgrades' || $collection === Database::METADATA) { return new Document(); } - - return $this->getAuthorization()->skip(function () use ($collection) { + + return $this->getSource()->getAuthorization()->skip(function () use ($collection) { try { return $this->source->getDocument('upgrades', $collection); } catch (\Throwable) { diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 32a16189e..00a7384cd 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -45,7 +45,7 @@ abstract protected function getDatabase(): Database; * * @return bool */ - abstract protected static function deleteColumn(string $collection, string $column): bool; + abstract protected function deleteColumn(string $collection, string $column): bool; /** * @param string $collection @@ -53,7 +53,7 @@ abstract protected static function deleteColumn(string $collection, string $colu * * @return bool */ - abstract protected static function deleteIndex(string $collection, string $index): bool; + abstract protected function deleteIndex(string $collection, string $index): bool; public function setUp(): void { diff --git a/tests/e2e/Adapter/MariaDBTest.php b/tests/e2e/Adapter/MariaDBTest.php index cd26401e9..923de242e 100644 --- a/tests/e2e/Adapter/MariaDBTest.php +++ b/tests/e2e/Adapter/MariaDBTest.php @@ -18,7 +18,7 @@ class MariaDBTest extends Base /** * @return Database */ - public function getDatabase(bool $fresh = false): Database + public function getDatabase(bool $fresh = false): Database { if (!is_null(self::$database) && !$fresh) { return self::$database; @@ -52,9 +52,9 @@ public function getDatabase(bool $fresh = false): Database return self::$database = $database; } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { - $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); @@ -62,9 +62,9 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { - $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/MirrorTest.php b/tests/e2e/Adapter/MirrorTest.php index 9bd3987bb..31bf3f3b6 100644 --- a/tests/e2e/Adapter/MirrorTest.php +++ b/tests/e2e/Adapter/MirrorTest.php @@ -33,7 +33,7 @@ class MirrorTest extends Base * @throws \RedisException * @throws Exception */ - protected function getDatabase(bool $fresh = false): Mirror + protected function getDatabase(bool $fresh = false): Mirror { if (!is_null(self::$database) && !$fresh) { return self::$database; @@ -95,6 +95,7 @@ protected function getDatabase(bool $fresh = false): Mirror $database ->setDatabase('utopiaTests') + ->setAuthorization(self::$authorization) ->setNamespace(static::$namespace = 'myapp_' . uniqid()); $database->create(); @@ -108,7 +109,7 @@ protected function getDatabase(bool $fresh = false): Mirror */ public function testGetMirrorSource(): void { - $database = self::getDatabase(); + $database = $this->getDatabase(); $source = $database->getSource(); $this->assertInstanceOf(Database::class, $source); $this->assertEquals(self::$source, $source); @@ -120,7 +121,7 @@ public function testGetMirrorSource(): void */ public function testGetMirrorDestination(): void { - $database = self::getDatabase(); + $database = $this->getDatabase(); $destination = $database->getDestination(); $this->assertInstanceOf(Database::class, $destination); $this->assertEquals(self::$destination, $destination); @@ -134,7 +135,7 @@ public function testGetMirrorDestination(): void */ public function testCreateMirroredCollection(): void { - $database = self::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('testCreateMirroredCollection'); @@ -152,7 +153,7 @@ public function testCreateMirroredCollection(): void */ public function testUpdateMirroredCollection(): void { - $database = self::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('testUpdateMirroredCollection', permissions: [ Permission::read(Role::any()), @@ -182,7 +183,7 @@ public function testUpdateMirroredCollection(): void public function testDeleteMirroredCollection(): void { - $database = self::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('testDeleteMirroredCollection'); @@ -203,7 +204,7 @@ public function testDeleteMirroredCollection(): void */ public function testCreateMirroredDocument(): void { - $database = self::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('testCreateMirroredDocument', attributes: [ new Document([ @@ -245,7 +246,7 @@ public function testCreateMirroredDocument(): void */ public function testUpdateMirroredDocument(): void { - $database = self::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('testUpdateMirroredDocument', attributes: [ new Document([ @@ -285,7 +286,7 @@ public function testUpdateMirroredDocument(): void public function testDeleteMirroredDocument(): void { - $database = self::getDatabase(); + $database = $this->getDatabase(); $database->createCollection('testDeleteMirroredDocument', attributes: [ new Document([ @@ -312,7 +313,7 @@ public function testDeleteMirroredDocument(): void $this->assertTrue($database->getDestination()->getDocument('testDeleteMirroredDocument', $document->getId())->isEmpty()); } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { $sqlTable = "`" . self::$source->getDatabase() . "`.`" . self::$source->getNamespace() . "_" . $collection . "`"; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; @@ -327,7 +328,7 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { $sqlTable = "`" . self::$source->getDatabase() . "`.`" . self::$source->getNamespace() . "_" . $collection . "`"; $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; diff --git a/tests/e2e/Adapter/MySQLTest.php b/tests/e2e/Adapter/MySQLTest.php index ccba9edca..27ecfca9c 100644 --- a/tests/e2e/Adapter/MySQLTest.php +++ b/tests/e2e/Adapter/MySQLTest.php @@ -24,7 +24,7 @@ class MySQLTest extends Base * @throws Exception * @throws Limit */ - public function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -58,9 +58,9 @@ public function getDatabase(): Database return self::$database = $database; } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { - $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); @@ -68,9 +68,9 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { - $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/PoolTest.php b/tests/e2e/Adapter/PoolTest.php index 668b19175..d32aeec05 100644 --- a/tests/e2e/Adapter/PoolTest.php +++ b/tests/e2e/Adapter/PoolTest.php @@ -32,7 +32,7 @@ class PoolTest extends Base * @throws Duplicate * @throws Limit */ - public function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -75,9 +75,9 @@ public function getDatabase(): Database return self::$database = $database; } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { - $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pool->use(function (Adapter $adapter) use ($sql) { @@ -92,9 +92,9 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { - $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; self::$pool->use(function (Adapter $adapter) use ($sql) { diff --git a/tests/e2e/Adapter/PostgresTest.php b/tests/e2e/Adapter/PostgresTest.php index a63e832c4..307e26d41 100644 --- a/tests/e2e/Adapter/PostgresTest.php +++ b/tests/e2e/Adapter/PostgresTest.php @@ -18,7 +18,7 @@ class PostgresTest extends Base /** * @reture Adapter */ - public function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -51,9 +51,9 @@ public function getDatabase(): Database return self::$database = $database; } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { - $sqlTable = '"' . self::getDatabase()->getDatabase() . '"."' . self::getDatabase()->getNamespace() . '_' . $collection . '"'; + $sqlTable = '"' . $this->getDatabase(). '"."' . $this->getDatabase()->getNamespace() . '_' . $collection . '"'; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN \"{$column}\""; self::$pdo->exec($sql); @@ -61,11 +61,11 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { - $key = "\"".self::getDatabase()->getNamespace()."_".self::getDatabase()->getTenant()."_{$collection}_{$index}\""; + $key = "\"".$this->getDatabase()->getNamespace()."_".$this->getDatabase()->getTenant()."_{$collection}_{$index}\""; - $sql = "DROP INDEX \"".self::getDatabase()->getDatabase()."\".{$key}"; + $sql = "DROP INDEX \"".$this->getDatabase()->getDatabase()."\".{$key}"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/SQLiteTest.php b/tests/e2e/Adapter/SQLiteTest.php index 2fd3360b1..6061352e4 100644 --- a/tests/e2e/Adapter/SQLiteTest.php +++ b/tests/e2e/Adapter/SQLiteTest.php @@ -55,9 +55,9 @@ public function getDatabase(): Database return self::$database = $database; } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { - $sqlTable = "`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); @@ -65,9 +65,9 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { - $index = "`".self::getDatabase()->getNamespace()."_".self::getDatabase()->getTenant()."_{$collection}_{$index}`"; + $index = "`".$this->getDatabase()->getNamespace()."_".$this->getDatabase()->getTenant()."_{$collection}_{$index}`"; $sql = "DROP INDEX {$index}"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/Scopes/AttributeTests.php b/tests/e2e/Adapter/Scopes/AttributeTests.php index 7e251f60d..158b60df5 100644 --- a/tests/e2e/Adapter/Scopes/AttributeTests.php +++ b/tests/e2e/Adapter/Scopes/AttributeTests.php @@ -158,7 +158,7 @@ public function testCreateDeleteAttribute(): void $this->assertEquals(true, $database->createAttribute('attributes', 'string1', Database::VAR_STRING, 128, true)); sleep(1); - $this->assertEquals(true, static::deleteColumn('attributes', 'string1')); + $this->assertEquals(true, $this->deleteColumn('attributes', 'string1')); $collection = $database->getCollection('attributes'); $attributes = $collection->getAttribute('attributes'); diff --git a/tests/e2e/Adapter/Scopes/CollectionTests.php b/tests/e2e/Adapter/Scopes/CollectionTests.php index dad91083a..3d2a179ec 100644 --- a/tests/e2e/Adapter/Scopes/CollectionTests.php +++ b/tests/e2e/Adapter/Scopes/CollectionTests.php @@ -16,7 +16,7 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; + trait CollectionTests { diff --git a/tests/e2e/Adapter/Scopes/IndexTests.php b/tests/e2e/Adapter/Scopes/IndexTests.php index 403866d63..08c97c7a2 100644 --- a/tests/e2e/Adapter/Scopes/IndexTests.php +++ b/tests/e2e/Adapter/Scopes/IndexTests.php @@ -99,7 +99,7 @@ public function testCreateDeleteIndex(): void // Test delete index when index does not exist $this->assertEquals(true, $database->createIndex('indexes', 'index1', Database::INDEX_KEY, ['string', 'integer'], [128], [Database::ORDER_ASC])); - $this->assertEquals(true, static::deleteIndex('indexes', 'index1')); + $this->assertEquals(true, $this->deleteIndex('indexes', 'index1')); $this->assertEquals(true, $database->deleteIndex('indexes', 'index1')); // Test delete index when attribute does not exist diff --git a/tests/e2e/Adapter/Scopes/PermissionTests.php b/tests/e2e/Adapter/Scopes/PermissionTests.php index 0b02494f8..0bb6e2cc0 100644 --- a/tests/e2e/Adapter/Scopes/PermissionTests.php +++ b/tests/e2e/Adapter/Scopes/PermissionTests.php @@ -10,7 +10,6 @@ use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; -use Utopia\Database\Validator\Authorization; trait PermissionTests { @@ -1115,9 +1114,9 @@ public function testCollectionPermissionsRelationshipsUpdateWorks(array $data): public function testCollectionPermissionsUpdateThrowsException(array $data): void { [$collection, $document] = $data; + $this->getDatabase()->getAuthorization()->cleanRoles(); $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); - $this->expectException(AuthorizationException::class); /** @var Database $database */ diff --git a/tests/e2e/Adapter/SharedTables/MariaDBTest.php b/tests/e2e/Adapter/SharedTables/MariaDBTest.php index 49c03c88d..5205d8b6b 100644 --- a/tests/e2e/Adapter/SharedTables/MariaDBTest.php +++ b/tests/e2e/Adapter/SharedTables/MariaDBTest.php @@ -30,7 +30,7 @@ public static function getAdapterName(): string /** * @return Database */ - public function getDatabase(bool $fresh = false): Database + public function getDatabase(bool $fresh = false): Database { if (!is_null(self::$database) && !$fresh) { return self::$database; @@ -65,9 +65,9 @@ public function getDatabase(bool $fresh = false): Database return self::$database = $database; } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { - $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); @@ -75,9 +75,9 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { - $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/SharedTables/MySQLTest.php b/tests/e2e/Adapter/SharedTables/MySQLTest.php index f9a713e0a..1824bfcc0 100644 --- a/tests/e2e/Adapter/SharedTables/MySQLTest.php +++ b/tests/e2e/Adapter/SharedTables/MySQLTest.php @@ -30,7 +30,7 @@ public static function getAdapterName(): string /** * @return Database */ - public function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -67,9 +67,9 @@ public function getDatabase(): Database return self::$database = $database; } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { - $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); @@ -77,9 +77,9 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { - $sqlTable = "`" . self::getDatabase()->getDatabase() . "`.`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/SharedTables/PostgresTest.php b/tests/e2e/Adapter/SharedTables/PostgresTest.php index 9fcfcb621..cb9633c01 100644 --- a/tests/e2e/Adapter/SharedTables/PostgresTest.php +++ b/tests/e2e/Adapter/SharedTables/PostgresTest.php @@ -29,7 +29,7 @@ public static function getAdapterName(): string /** * @reture Adapter */ - public function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -64,9 +64,9 @@ public function getDatabase(): Database return self::$database = $database; } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { - $sqlTable = '"' . self::getDatabase()->getDatabase() . '"."' . self::getDatabase()->getNamespace() . '_' . $collection . '"'; + $sqlTable = '"' . $this->getDatabase()->getDatabase() . '"."' . $this->getDatabase()->getNamespace() . '_' . $collection . '"'; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN \"{$column}\""; self::$pdo->exec($sql); @@ -74,11 +74,11 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { - $key = "\"".self::getDatabase()->getNamespace()."_".self::getDatabase()->getTenant()."_{$collection}_{$index}\""; + $key = "\"".$this->getDatabase()->getNamespace()."_".$this->getDatabase()->getTenant()."_{$collection}_{$index}\""; - $sql = "DROP INDEX \"".self::getDatabase()->getDatabase()."\".{$key}"; + $sql = "DROP INDEX \"".$this->getDatabase()->getDatabase()."\".{$key}"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/SharedTables/SQLiteTest.php b/tests/e2e/Adapter/SharedTables/SQLiteTest.php index 76d954d66..ea4a042ea 100644 --- a/tests/e2e/Adapter/SharedTables/SQLiteTest.php +++ b/tests/e2e/Adapter/SharedTables/SQLiteTest.php @@ -30,7 +30,7 @@ public static function getAdapterName(): string /** * @return Database */ - public function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -70,9 +70,9 @@ public function getDatabase(): Database return self::$database = $database; } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { - $sqlTable = "`" . self::getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); @@ -80,9 +80,9 @@ protected static function deleteColumn(string $collection, string $column): bool return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { - $index = "`".self::getDatabase()->getNamespace()."_".self::getDatabase()->getTenant()."_{$collection}_{$index}`"; + $index = "`".$this->getDatabase()->getNamespace()."_".$this->getDatabase()->getTenant()."_{$collection}_{$index}`"; $sql = "DROP INDEX {$index}"; self::$pdo->exec($sql); From 59153b12dedbfd90c4f4db25484426a02e43f0d9 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 16 Oct 2025 18:13:16 +0300 Subject: [PATCH 08/29] linter --- bin/tasks/load.php | 9 +++------ bin/tasks/query.php | 18 ++++++++---------- bin/tasks/relationships.php | 10 +++------- .../mariadb_testing_1000_1760626673.json | 1 + src/Database/Database.php | 14 +++++++------- src/Database/Mirror.php | 3 +-- src/Database/Validator/Authorization.php | 4 ++-- src/Database/Validator/Authorization/Input.php | 2 +- tests/e2e/Adapter/Scopes/AttributeTests.php | 1 - tests/e2e/Adapter/Scopes/CollectionTests.php | 1 - tests/e2e/Adapter/Scopes/DocumentTests.php | 1 - tests/e2e/Adapter/Scopes/GeneralTests.php | 1 - tests/e2e/Adapter/Scopes/RelationshipTests.php | 1 - tests/unit/Validator/AuthorizationTest.php | 2 +- 14 files changed, 27 insertions(+), 41 deletions(-) create mode 100644 bin/view/results/mariadb_testing_1000_1760626673.json diff --git a/bin/tasks/load.php b/bin/tasks/load.php index 895e40181..17029401a 100644 --- a/bin/tasks/load.php +++ b/bin/tasks/load.php @@ -16,7 +16,6 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\PDO; -use Utopia\Database\Validator\Authorization; use Utopia\Validator\Boolean; use Utopia\Validator\Integer; use Utopia\Validator\Text; @@ -26,7 +25,6 @@ $genresPool = ['fashion', 'food', 'travel', 'music', 'lifestyle', 'fitness', 'diy', 'sports', 'finance']; $tagsPool = ['short', 'quick', 'easy', 'medium', 'hard']; -$authorization = new Authorization(); /** * @Example @@ -39,17 +37,16 @@ ->param('limit', 0, new Integer(true), 'Total number of records to add to database') ->param('name', 'myapp_' . uniqid(), new Text(0), 'Name of created database.', true) ->param('sharedTables', false, new Boolean(true), 'Whether to use shared tables', true) - ->action(function (string $adapter, int $limit, string $name, bool $sharedTables) use ($authorization) { + ->action(function (string $adapter, int $limit, string $name, bool $sharedTables) { - $createSchema = function (Database $database) use ($authorization): void { + $createSchema = function (Database $database): void { if ($database->exists($database->getDatabase())) { $database->delete($database->getDatabase()); } + $database->getAuthorization()->addRole(Role::any()->toString()); $database->create(); - $authorization->addRole(Role::any()->toString()); - $database->createCollection('articles', permissions: [ Permission::create(Role::any()), Permission::read(Role::any()), diff --git a/bin/tasks/query.php b/bin/tasks/query.php index 03bb0be49..84c139c9f 100644 --- a/bin/tasks/query.php +++ b/bin/tasks/query.php @@ -15,18 +15,16 @@ use Utopia\Database\Adapter\Postgres; use Utopia\Database\Database; use Utopia\Database\Query; +use Utopia\Database\Validator\Authorization; use Utopia\Validator\Boolean; use Utopia\Validator\Integer; use Utopia\Validator\Text; -use Utopia\Database\Validator\Authorization; /** * @Example * docker compose exec tests bin/query --adapter=mariadb --limit=1000 --name=testing */ -$authorization = new Authorization(); - $cli ->task('query') ->desc('Query mock data') @@ -34,9 +32,9 @@ ->param('name', '', new Text(0), 'Name of created database.') ->param('limit', 25, new Integer(true), 'Limit on queried documents', true) ->param('sharedTables', false, new Boolean(true), 'Whether to use shared tables', true) - ->action(function (string $adapter, string $name, int $limit, bool $sharedTables) use ($authorization) { + ->action(function (string $adapter, string $name, int $limit, bool $sharedTables) { - $setRoles = function ($faker, $count) use ($authorization): int { + $setRoles = function (Authorization $authorization, $faker, $count): int { for ($i = 0; $i < $count; $i++) { $authorization->addRole($faker->numerify('user####')); } @@ -102,35 +100,35 @@ $report = []; - $count = $setRoles($faker, 1); + $count = $setRoles($database->getAuthorization(), $faker, 1); Console::info("\nRunning queries with {$count} authorization roles:"); $report[] = [ 'roles' => $count, 'results' => runQueries($database, $limit) ]; - $count = $setRoles($faker, 100); + $count = $setRoles($database->getAuthorization(), $faker, 100); Console::info("\nRunning queries with {$count} authorization roles:"); $report[] = [ 'roles' => $count, 'results' => runQueries($database, $limit) ]; - $count = $setRoles($faker, 400); + $count = $setRoles($database->getAuthorization(), $faker, 400); Console::info("\nRunning queries with {$count} authorization roles:"); $report[] = [ 'roles' => $count, 'results' => runQueries($database, $limit) ]; - $count = $setRoles($faker, 500); + $count = $setRoles($database->getAuthorization(), $faker, 500); Console::info("\nRunning queries with {$count} authorization roles:"); $report[] = [ 'roles' => $count, 'results' => runQueries($database, $limit) ]; - $count = $setRoles($faker, 1000); + $count = $setRoles($database->getAuthorization(), $faker, 1000); Console::info("\nRunning queries with {$count} authorization roles:"); $report[] = [ 'roles' => $count, diff --git a/bin/tasks/relationships.php b/bin/tasks/relationships.php index 128fe61df..200fce47e 100644 --- a/bin/tasks/relationships.php +++ b/bin/tasks/relationships.php @@ -20,7 +20,6 @@ use Utopia\Database\Helpers\Role; use Utopia\Database\PDO; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; use Utopia\Validator\Boolean; use Utopia\Validator\Integer; use Utopia\Validator\Text; @@ -34,7 +33,6 @@ * @Example * docker compose exec tests bin/relationships --adapter=mariadb --limit=1000 */ -$authorization = new Authorization(); $cli ->task('relationships') @@ -44,21 +42,19 @@ ->param('name', 'myapp_' . uniqid(), new Text(0), 'Name of created database.', true) ->param('sharedTables', false, new Boolean(true), 'Whether to use shared tables', true) ->param('runs', 1, new Integer(true), 'Number of times to run benchmarks', true) - ->action(function (string $adapter, int $limit, string $name, bool $sharedTables, int $runs) use ($authorization) { + ->action(function (string $adapter, int $limit, string $name, bool $sharedTables, int $runs) { $start = null; $namespace = '_ns'; $cache = new Cache(new NoCache()); Console::info("Filling {$adapter} with {$limit} records: {$name}"); - $createRelationshipSchema = function (Database $database) use ($authorization): void { + $createRelationshipSchema = function (Database $database): void { if ($database->exists($database->getDatabase())) { $database->delete($database->getDatabase()); } + $database->getAuthorization()->addRole(Role::any()->toString()); $database->create(); - - $authorization->addRole(Role::any()->toString()); - $database->createCollection('authors', permissions: [ Permission::create(Role::any()), Permission::read(Role::any()), diff --git a/bin/view/results/mariadb_testing_1000_1760626673.json b/bin/view/results/mariadb_testing_1000_1760626673.json new file mode 100644 index 000000000..69557fa93 --- /dev/null +++ b/bin/view/results/mariadb_testing_1000_1760626673.json @@ -0,0 +1 @@ +[{"roles":2,"results":{"Querying greater than, equal[1] and limit":0.03397989273071289,"Querying equal[3] and limit":0.07099103927612305,"Querying greaterThan, limit(1000)":0.18834996223449707,"Query search, limit(1000)":0.0020699501037597656,"Querying contains[1], limit(1000)":0.002312898635864258}},{"roles":101,"results":{"Querying greater than, equal[1] and limit":0.026144027709960938,"Querying equal[3] and limit":0.07204604148864746,"Querying greaterThan, limit(1000)":0.18575501441955566,"Query search, limit(1000)":0.0020110607147216797,"Querying contains[1], limit(1000)":0.002538919448852539}},{"roles":489,"results":{"Querying greater than, equal[1] and limit":0.02759695053100586,"Querying equal[3] and limit":0.07203984260559082,"Querying greaterThan, limit(1000)":0.18977999687194824,"Query search, limit(1000)":0.003882884979248047,"Querying contains[1], limit(1000)":0.0029969215393066406}},{"roles":958,"results":{"Querying greater than, equal[1] and limit":0.029262065887451172,"Querying equal[3] and limit":0.0740351676940918,"Querying greaterThan, limit(1000)":0.18916702270507812,"Query search, limit(1000)":0.0023200511932373047,"Querying contains[1], limit(1000)":0.0027260780334472656}},{"roles":1825,"results":{"Querying greater than, equal[1] and limit":0.028146982192993164,"Querying equal[3] and limit":0.07226085662841797,"Querying greaterThan, limit(1000)":0.18572497367858887,"Query search, limit(1000)":0.002095937728881836,"Querying contains[1], limit(1000)":0.0025589466094970703}}] \ No newline at end of file diff --git a/src/Database/Database.php b/src/Database/Database.php index f9250b5b8..e5347782c 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4897,19 +4897,19 @@ public function updateDocument(string $collection, string $id, Document $documen ); if ($shouldUpdate) { - if(!$this->authorization->isValid(new Input(self::PERMISSION_UPDATE, $updatePermissions))) { + if (!$this->authorization->isValid(new Input(self::PERMISSION_UPDATE, $updatePermissions))) { throw new AuthorizationException($this->authorization->getDescription()); } } else { - if(!$this->authorization->isValid(new Input(self::PERMISSION_READ, $readPermissions))){ + if (!$this->authorization->isValid(new Input(self::PERMISSION_READ, $readPermissions))) { throw new AuthorizationException($this->authorization->getDescription()); } } } - - if ($shouldUpdate) { + + if ($shouldUpdate) { $document->setAttribute('$updatedAt', ($newUpdatedAt === null || !$this->preserveDates) ? $time : $newUpdatedAt); - } + } // Check if document was updated after the request timestamp $oldUpdatedAt = new \DateTime($old->getUpdatedAt()); @@ -5798,7 +5798,7 @@ public function upsertDocumentsWithIncrease( /** * @var array $chunk */ - $batch = $this->withTransaction(fn () =>$this->authorization->skip(fn () => $this->adapter->upsertDocuments( + $batch = $this->withTransaction(fn () => $this->authorization->skip(fn () => $this->adapter->upsertDocuments( $collection, $attribute, $chunk @@ -6827,7 +6827,7 @@ public function find(string $collection, array $queries = [], string $forPermiss $cursorDirection, $forPermission ); - + $results = $skipAuth ? $this->authorization->skip($getResults) : $getResults(); } diff --git a/src/Database/Mirror.php b/src/Database/Mirror.php index 27f9a73a9..2cd438554 100644 --- a/src/Database/Mirror.php +++ b/src/Database/Mirror.php @@ -6,7 +6,6 @@ use Utopia\Database\Exception\Limit; use Utopia\Database\Helpers\ID; use Utopia\Database\Mirroring\Filter; -use Utopia\Database\Validator\Authorization; class Mirror extends Database { @@ -1083,7 +1082,7 @@ protected function getUpgradeStatus(string $collection): ?Document if ($collection === 'upgrades' || $collection === Database::METADATA) { return new Document(); } - + return $this->getSource()->getAuthorization()->skip(function () use ($collection) { try { return $this->source->getDocument('upgrades', $collection); diff --git a/src/Database/Validator/Authorization.php b/src/Database/Validator/Authorization.php index d1bc67fa6..6124ff9e4 100644 --- a/src/Database/Validator/Authorization.php +++ b/src/Database/Validator/Authorization.php @@ -51,7 +51,7 @@ public function getDescription(): string */ public function isValid(mixed $input): bool // any, CREATE { - if(!($input instanceof Input)) { + if (!($input instanceof Input)) { $this->message = 'Invalid input provided'; return false; } @@ -237,4 +237,4 @@ public function getType(): string { return self::TYPE_ARRAY; } -} \ No newline at end of file +} diff --git a/src/Database/Validator/Authorization/Input.php b/src/Database/Validator/Authorization/Input.php index 3002c5797..4c2387097 100644 --- a/src/Database/Validator/Authorization/Input.php +++ b/src/Database/Validator/Authorization/Input.php @@ -46,4 +46,4 @@ public function getAction(): string { return $this->action; } -} \ No newline at end of file +} diff --git a/tests/e2e/Adapter/Scopes/AttributeTests.php b/tests/e2e/Adapter/Scopes/AttributeTests.php index 158b60df5..19724658f 100644 --- a/tests/e2e/Adapter/Scopes/AttributeTests.php +++ b/tests/e2e/Adapter/Scopes/AttributeTests.php @@ -20,7 +20,6 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Datetime as DatetimeValidator; use Utopia\Database\Validator\Structure; use Utopia\Validator\Range; diff --git a/tests/e2e/Adapter/Scopes/CollectionTests.php b/tests/e2e/Adapter/Scopes/CollectionTests.php index 3d2a179ec..435a0d80a 100644 --- a/tests/e2e/Adapter/Scopes/CollectionTests.php +++ b/tests/e2e/Adapter/Scopes/CollectionTests.php @@ -17,7 +17,6 @@ use Utopia\Database\Helpers\Role; use Utopia\Database\Query; - trait CollectionTests { public function testCreateExistsDelete(): void diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 46a884f74..df143b06f 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -18,7 +18,6 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; trait DocumentTests { diff --git a/tests/e2e/Adapter/Scopes/GeneralTests.php b/tests/e2e/Adapter/Scopes/GeneralTests.php index c9ae7224a..2bf315d53 100644 --- a/tests/e2e/Adapter/Scopes/GeneralTests.php +++ b/tests/e2e/Adapter/Scopes/GeneralTests.php @@ -19,7 +19,6 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; trait GeneralTests { diff --git a/tests/e2e/Adapter/Scopes/RelationshipTests.php b/tests/e2e/Adapter/Scopes/RelationshipTests.php index 57f25d409..b6b05d2cb 100644 --- a/tests/e2e/Adapter/Scopes/RelationshipTests.php +++ b/tests/e2e/Adapter/Scopes/RelationshipTests.php @@ -18,7 +18,6 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; trait RelationshipTests { diff --git a/tests/unit/Validator/AuthorizationTest.php b/tests/unit/Validator/AuthorizationTest.php index bccdd5052..37e5a6e22 100644 --- a/tests/unit/Validator/AuthorizationTest.php +++ b/tests/unit/Validator/AuthorizationTest.php @@ -123,4 +123,4 @@ public function testNestedSkips(): void $this->assertEquals(true, $this->authorization->getStatus()); } -} \ No newline at end of file +} From 85a2e71df3511f6519125e7615b5f5a4af3cb0a7 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 16 Oct 2025 18:21:04 +0300 Subject: [PATCH 09/29] codeQl --- src/Database/Database.php | 2 +- tests/e2e/Adapter/MySQLTest.php | 2 +- tests/e2e/Adapter/PoolTest.php | 4 ++-- tests/e2e/Adapter/PostgresTest.php | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index e5347782c..f022da277 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -6988,7 +6988,7 @@ public function count(string $collection, array $queries = [], ?int $max = null) $queries = $queriesOrNull; $getCount = fn () => $this->adapter->count($collection, $queries, $max); - $count = $skipAuth ?? false ? $this->authorization->skip($getCount) : $getCount(); + $count = $skipAuth ? $this->authorization->skip($getCount) : $getCount(); $this->trigger(self::EVENT_DOCUMENT_COUNT, $count); diff --git a/tests/e2e/Adapter/MySQLTest.php b/tests/e2e/Adapter/MySQLTest.php index 27ecfca9c..8e92bb216 100644 --- a/tests/e2e/Adapter/MySQLTest.php +++ b/tests/e2e/Adapter/MySQLTest.php @@ -60,7 +60,7 @@ public function getDatabase(): Database protected function deleteColumn(string $collection, string $column): bool { - $sqlTable = "`" . $this->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pdo->exec($sql); diff --git a/tests/e2e/Adapter/PoolTest.php b/tests/e2e/Adapter/PoolTest.php index d32aeec05..42bea2c68 100644 --- a/tests/e2e/Adapter/PoolTest.php +++ b/tests/e2e/Adapter/PoolTest.php @@ -77,7 +77,7 @@ public function getDatabase(): Database protected function deleteColumn(string $collection, string $column): bool { - $sqlTable = "`" . $this->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`"; self::$pool->use(function (Adapter $adapter) use ($sql) { @@ -94,7 +94,7 @@ protected function deleteColumn(string $collection, string $column): bool protected function deleteIndex(string $collection, string $index): bool { - $sqlTable = "`" . $this->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; + $sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`"; $sql = "DROP INDEX `{$index}` ON {$sqlTable}"; self::$pool->use(function (Adapter $adapter) use ($sql) { diff --git a/tests/e2e/Adapter/PostgresTest.php b/tests/e2e/Adapter/PostgresTest.php index 307e26d41..207c4293e 100644 --- a/tests/e2e/Adapter/PostgresTest.php +++ b/tests/e2e/Adapter/PostgresTest.php @@ -53,7 +53,7 @@ public function getDatabase(): Database protected function deleteColumn(string $collection, string $column): bool { - $sqlTable = '"' . $this->getDatabase(). '"."' . $this->getDatabase()->getNamespace() . '_' . $collection . '"'; + $sqlTable = '"' . $this->getDatabase()->getDatabase(). '"."' . $this->getDatabase()->getNamespace() . '_' . $collection . '"'; $sql = "ALTER TABLE {$sqlTable} DROP COLUMN \"{$column}\""; self::$pdo->exec($sql); From ae1f07cb7a85d57bbd66bed5d5609b678c1d23f0 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 16 Oct 2025 18:58:30 +0300 Subject: [PATCH 10/29] Update PHPUnit configuration to stop on failure and add setAuthorization method in Database Pool class for improved authorization handling. --- phpunit.xml | 2 +- src/Database/Adapter/Pool.php | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 2a0531cfd..34365d48d 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false" + stopOnFailure="true" > diff --git a/src/Database/Adapter/Pool.php b/src/Database/Adapter/Pool.php index 799596d2d..988afcc0c 100644 --- a/src/Database/Adapter/Pool.php +++ b/src/Database/Adapter/Pool.php @@ -66,6 +66,7 @@ public function delegate(string $method, array $args): mixed $adapter->setNamespace($this->getNamespace()); $adapter->setSharedTables($this->getSharedTables()); $adapter->setTenant($this->getTenant()); + $adapter->setAuthorization($this->authorization); if ($this->getTimeout() > 0) { $adapter->setTimeout($this->getTimeout()); @@ -568,4 +569,10 @@ public function decodePolygon(string $wkb): array { return $this->delegate(__FUNCTION__, \func_get_args()); } + + public function setAuthorization(\Utopia\Database\Validator\Authorization $authorization): self + { + $this->authorization = $authorization; + return $this; + } } From 4814e193af2a15d886f721a5a175f2d27d19d1f0 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 16 Oct 2025 19:00:45 +0300 Subject: [PATCH 11/29] phpUnit --- phpunit.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit.xml b/phpunit.xml index 34365d48d..2a0531cfd 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="true" + stopOnFailure="false" > From 4dc0ee7e4da1eb3beaca378195a13a7f96c11240 Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 16 Oct 2025 19:27:46 +0300 Subject: [PATCH 12/29] Add setAuthorization method to Mirror class for improved authorization handling --- src/Database/Mirror.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Database/Mirror.php b/src/Database/Mirror.php index 2cd438554..1cd0c59b5 100644 --- a/src/Database/Mirror.php +++ b/src/Database/Mirror.php @@ -6,6 +6,7 @@ use Utopia\Database\Exception\Limit; use Utopia\Database\Helpers\ID; use Utopia\Database\Mirroring\Filter; +use Utopia\Database\Validator\Authorization; class Mirror extends Database { @@ -1098,4 +1099,19 @@ protected function logError(string $action, \Throwable $err): void $callback($action, $err); } } + + public function setAuthorization(Authorization $authorization): self + { + + parent::setAuthorization($authorization); + + if (isset($this->source)) { + $this->source->setAuthorization($authorization); + } + if (isset($this->destination)) { + $this->destination->setAuthorization($authorization); + } + + return $this; + } } From 63ba415681c1b5dc9319504996c818eed6dd9231 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 19 Oct 2025 09:59:53 +0300 Subject: [PATCH 13/29] composer --- src/Database/Mirror.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Mirror.php b/src/Database/Mirror.php index 1cd0c59b5..a0236695c 100644 --- a/src/Database/Mirror.php +++ b/src/Database/Mirror.php @@ -1102,7 +1102,7 @@ protected function logError(string $action, \Throwable $err): void public function setAuthorization(Authorization $authorization): self { - + parent::setAuthorization($authorization); if (isset($this->source)) { @@ -1111,7 +1111,7 @@ public function setAuthorization(Authorization $authorization): self if (isset($this->destination)) { $this->destination->setAuthorization($authorization); } - + return $this; } } From 11f86ce19f5f712e7455a31e8628829a9339a3be Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 19 Oct 2025 10:58:31 +0300 Subject: [PATCH 14/29] Refactor authorization handling in VectorTests to use database instance methods for role management and permission checks. --- tests/e2e/Adapter/Scopes/VectorTests.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/e2e/Adapter/Scopes/VectorTests.php b/tests/e2e/Adapter/Scopes/VectorTests.php index c0026b634..a3293da91 100644 --- a/tests/e2e/Adapter/Scopes/VectorTests.php +++ b/tests/e2e/Adapter/Scopes/VectorTests.php @@ -1571,7 +1571,7 @@ public function testVectorSearchWithRestrictedPermissions(): void } // Create documents with different permissions inside Authorization::skip - Authorization::skip(function () use ($database) { + $database->getAuthorization()->skip(function () use ($database) { $database->createCollection('vectorPermissions', [], [], [], true); $database->createAttribute('vectorPermissions', 'name', Database::VAR_STRING, 255, true); $database->createAttribute('vectorPermissions', 'embedding', Database::VAR_VECTOR, 3, true); @@ -1602,8 +1602,8 @@ public function testVectorSearchWithRestrictedPermissions(): void }); // Query as user1 - should only see doc1 and doc3 - Authorization::setRole(Role::user('user1')->toString()); - Authorization::setRole(Role::any()->toString()); + $database->getAuthorization()->addRole(Role::user('user1')->toString()); + $database->getAuthorization()->addRole(Role::any()->toString()); $results = $database->find('vectorPermissions', [ Query::vectorCosine('embedding', [1.0, 0.0, 0.0]) ]); @@ -1615,9 +1615,9 @@ public function testVectorSearchWithRestrictedPermissions(): void $this->assertNotContains('Doc 2', $names); // Query as user2 - should only see doc2 and doc3 - Authorization::cleanRoles(); - Authorization::setRole(Role::user('user2')->toString()); - Authorization::setRole(Role::any()->toString()); + $database->getAuthorization()->cleanRoles(); + $database->getAuthorization()->addRole(Role::user('user2')->toString()); + $database->getAuthorization()->addRole(Role::any()->toString()); $results = $database->find('vectorPermissions', [ Query::vectorCosine('embedding', [1.0, 0.0, 0.0]) ]); @@ -1628,8 +1628,8 @@ public function testVectorSearchWithRestrictedPermissions(): void $this->assertContains('Doc 3', $names); $this->assertNotContains('Doc 1', $names); - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $database->getAuthorization()->cleanRoles(); + $database->getAuthorization()->addRole(Role::any()->toString()); // Cleanup $database->deleteCollection('vectorPermissions'); @@ -1663,7 +1663,7 @@ public function testVectorPermissionFilteringAfterScoring(): void } // Query with limit 3 as any user - should skip restricted docs and return accessible ones - Authorization::setRole(Role::any()->toString()); + $database->getAuthorization()->addRole(Role::any()->toString()); $results = $database->find('vectorPermScoring', [ Query::vectorCosine('embedding', [1.0, 0.0, 0.0]), Query::limit(3) @@ -1675,7 +1675,7 @@ public function testVectorPermissionFilteringAfterScoring(): void $this->assertGreaterThanOrEqual(3, $doc->getAttribute('score')); } - Authorization::cleanRoles(); + $database->getAuthorization()->cleanRoles(); // Cleanup $database->deleteCollection('vectorPermScoring'); From 5cd048ef4ab99744e16b7fef37e928c1b4a3002f Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 19 Oct 2025 12:52:47 +0300 Subject: [PATCH 15/29] update --- src/Database/Adapter/Pool.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Database/Adapter/Pool.php b/src/Database/Adapter/Pool.php index 8c2a2497c..f5ce9badf 100644 --- a/src/Database/Adapter/Pool.php +++ b/src/Database/Adapter/Pool.php @@ -7,6 +7,7 @@ use Utopia\Database\Document; use Utopia\Database\Exception as DatabaseException; use Utopia\Pools\Pool as UtopiaPool; +use Utopia\Database\Validator\Authorization; class Pool extends Adapter { @@ -575,7 +576,7 @@ public function decodePolygon(string $wkb): array return $this->delegate(__FUNCTION__, \func_get_args()); } - public function setAuthorization(\Utopia\Database\Validator\Authorization $authorization): self + public function setAuthorization(Authorization $authorization): self { $this->authorization = $authorization; return $this; From 5096b38af2328f73f4d330bbf922555283b2419c Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 19 Oct 2025 16:46:48 +0300 Subject: [PATCH 16/29] linter --- src/Database/Adapter/Pool.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Adapter/Pool.php b/src/Database/Adapter/Pool.php index f5ce9badf..f019bc5ff 100644 --- a/src/Database/Adapter/Pool.php +++ b/src/Database/Adapter/Pool.php @@ -6,8 +6,8 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception as DatabaseException; -use Utopia\Pools\Pool as UtopiaPool; use Utopia\Database\Validator\Authorization; +use Utopia\Pools\Pool as UtopiaPool; class Pool extends Adapter { From f317674ce4188569d71f811aa71e02303e0f2603 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 20 Oct 2025 16:15:32 +0300 Subject: [PATCH 17/29] addresing comments --- src/Database/Database.php | 16 ++++++++-------- src/Database/Validator/Authorization.php | 8 ++------ src/Database/Validator/Authorization/Input.php | 2 +- tests/e2e/Adapter/MongoDBTest.php | 15 ++++++++------- tests/e2e/Adapter/Scopes/GeneralTests.php | 1 - 5 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index f2e087106..e4ecfd2af 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -5050,15 +5050,15 @@ public function updateDocument(string $collection, string $id, Document $documen } } - $updatePermissions = array_merge( - $collection->getUpdate(), - $documentSecurity ? $old->getUpdate() : [] - ); + $updatePermissions = [ + ...$collection->getUpdate(), + ...($documentSecurity ? $old->getUpdate() : []) + ]; - $readPermissions = array_merge( - $collection->getRead(), - $documentSecurity ? $old->getRead() : [] - ); + $readPermissions = [ + ...$collection->getRead(), + ...($documentSecurity ? $old->getRead() : []) + ]; if ($shouldUpdate) { if (!$this->authorization->isValid(new Input(self::PERMISSION_UPDATE, $updatePermissions))) { diff --git a/src/Database/Validator/Authorization.php b/src/Database/Validator/Authorization.php index 6124ff9e4..7fd9e667c 100644 --- a/src/Database/Validator/Authorization.php +++ b/src/Database/Validator/Authorization.php @@ -49,17 +49,13 @@ public function getDescription(): string * * Returns true if valid or false if not. */ - public function isValid(mixed $input): bool // any, CREATE + public function isValid(mixed $input): bool { if (!($input instanceof Input)) { $this->message = 'Invalid input provided'; return false; } - /** - * @var Input $input - */ - $permissions = $input->getPermissions(); $action = $input->getAction(); @@ -124,7 +120,7 @@ public function cleanRoles(): void * * @return bool */ - public function isRole(string $role): bool + public function hasRole(string $role): bool { return (\array_key_exists($role, $this->roles)); } diff --git a/src/Database/Validator/Authorization/Input.php b/src/Database/Validator/Authorization/Input.php index 4c2387097..8db9e8058 100644 --- a/src/Database/Validator/Authorization/Input.php +++ b/src/Database/Validator/Authorization/Input.php @@ -5,7 +5,7 @@ class Input { /** - * @var string[] $permissions + * @var array $permissions */ protected array $permissions; protected string $action; diff --git a/tests/e2e/Adapter/MongoDBTest.php b/tests/e2e/Adapter/MongoDBTest.php index c37b41afe..d2771ed59 100644 --- a/tests/e2e/Adapter/MongoDBTest.php +++ b/tests/e2e/Adapter/MongoDBTest.php @@ -29,7 +29,7 @@ public static function getAdapterName(): string * @return Database * @throws Exception */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -53,6 +53,7 @@ public static function getDatabase(): Database $database = new Database(new Mongo($client), $cache); $database->getAdapter()->setSupportForAttributes(true); $database + ->setAuthorization(self::$authorization) ->setDatabase($schema) ->setNamespace(static::$namespace = 'myapp_' . uniqid()); @@ -71,10 +72,10 @@ public static function getDatabase(): Database public function testCreateExistsDelete(): void { // Mongo creates databases on the fly, so exists would always pass. So we override this test to remove the exists check. - $this->assertNotNull(static::getDatabase()->create()); - $this->assertEquals(true, static::getDatabase()->delete($this->testDatabase)); - $this->assertEquals(true, static::getDatabase()->create()); - $this->assertEquals(static::getDatabase(), static::getDatabase()->setDatabase($this->testDatabase)); + $this->assertNotNull($this->getDatabase()->create()); + $this->assertEquals(true, $this->getDatabase()->delete($this->testDatabase)); + $this->assertEquals(true, $this->getDatabase()->create()); + $this->assertEquals($this->getDatabase(), $this->getDatabase()->setDatabase($this->testDatabase)); } public function testRenameAttribute(): void @@ -97,12 +98,12 @@ public function testKeywords(): void $this->assertTrue(true); } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { return true; } diff --git a/tests/e2e/Adapter/Scopes/GeneralTests.php b/tests/e2e/Adapter/Scopes/GeneralTests.php index ff69e1e7f..bf4280163 100644 --- a/tests/e2e/Adapter/Scopes/GeneralTests.php +++ b/tests/e2e/Adapter/Scopes/GeneralTests.php @@ -640,7 +640,6 @@ public function testCacheFallback(): void $this->getDatabase()->getAuthorization()->cleanRoles(); $this->getDatabase()->getAuthorization()->addRole(Role::any()->toString()); - $database = $this->getDatabase(); // Write mock data $database->createCollection('testRedisFallback', attributes: [ From 63992e908fdf774847bab295f2e0af3cf735cb39 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 20 Oct 2025 16:16:29 +0300 Subject: [PATCH 18/29] linter --- tests/e2e/Adapter/MongoDBTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e/Adapter/MongoDBTest.php b/tests/e2e/Adapter/MongoDBTest.php index d2771ed59..1c7eb9237 100644 --- a/tests/e2e/Adapter/MongoDBTest.php +++ b/tests/e2e/Adapter/MongoDBTest.php @@ -29,7 +29,7 @@ public static function getAdapterName(): string * @return Database * @throws Exception */ - public function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -98,12 +98,12 @@ public function testKeywords(): void $this->assertTrue(true); } - protected function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { return true; } - protected function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { return true; } From c6ad97040faf95e912465f9835ed801fcce36171 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 20 Oct 2025 16:19:16 +0300 Subject: [PATCH 19/29] authorization tests to use hasRole method instead of isRole --- tests/e2e/Adapter/Scopes/DocumentTests.php | 2 +- tests/unit/Validator/AuthorizationTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 85b95937f..1501c0f59 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -3943,7 +3943,7 @@ public function testSum(): void $this->assertEquals(2019, $sum); $this->getDatabase()->getAuthorization()->removeRole('user:x'); - $this->getDatabase()->getAuthorization()->removeRole('userx'); + $sum = $database->sum('movies', 'year', [Query::equal('year', [2019]),]); $this->assertEquals(2019 + 2019, $sum); $sum = $database->sum('movies', 'year'); diff --git a/tests/unit/Validator/AuthorizationTest.php b/tests/unit/Validator/AuthorizationTest.php index 37e5a6e22..e8685549e 100644 --- a/tests/unit/Validator/AuthorizationTest.php +++ b/tests/unit/Validator/AuthorizationTest.php @@ -49,10 +49,10 @@ public function testValues(): void $this->authorization->addRole(Role::user('456')->toString()); $this->authorization->addRole(Role::user('123')->toString()); - $this->assertEquals($this->authorization->isRole(Role::user('456')->toString()), true); - $this->assertEquals($this->authorization->isRole(Role::user('457')->toString()), false); - $this->assertEquals($this->authorization->isRole(''), false); - $this->assertEquals($this->authorization->isRole(Role::any()->toString()), true); + $this->assertEquals($this->authorization->hasRole(Role::user('456')->toString()), true); + $this->assertEquals($this->authorization->hasRole(Role::user('457')->toString()), false); + $this->assertEquals($this->authorization->hasRole(''), false); + $this->assertEquals($this->authorization->hasRole(Role::any()->toString()), true); $this->assertEquals($object->isValid(new Input(Database::PERMISSION_READ, $document->getRead())), true); From 7dbb302326b31ae7f077f778245be1c67e6d88c2 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 20 Oct 2025 16:37:56 +0300 Subject: [PATCH 20/29] Refactor authorization checks to use instance methods in Database and Mongo classes, improving consistency and readability. Update DocumentTests to remove unnecessary whitespace. --- src/Database/Adapter/Mongo.php | 12 ++--- src/Database/Database.php | 7 +-- tests/e2e/Adapter/Schemaless/MongoDBTest.php | 13 +++--- tests/e2e/Adapter/Scopes/DocumentTests.php | 2 +- tests/e2e/Adapter/Scopes/SchemalessTests.php | 48 ++++++++++---------- 5 files changed, 40 insertions(+), 42 deletions(-) diff --git a/src/Database/Adapter/Mongo.php b/src/Database/Adapter/Mongo.php index 82afb0eaa..46693792a 100644 --- a/src/Database/Adapter/Mongo.php +++ b/src/Database/Adapter/Mongo.php @@ -1868,8 +1868,8 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25 } // permissions - if (Authorization::$status) { - $roles = \implode('|', Authorization::getRoles()); + if ($this->authorization->getStatus()) { + $roles = \implode('|', $this->authorization->getRoles()); $filters['_permissions']['$in'] = [new Regex("{$forPermission}\\(\".*(?:{$roles}).*\"\\)", 'i')]; } @@ -2115,8 +2115,8 @@ public function count(Document $collection, array $queries = [], ?int $max = nul } // Add permissions filter if authorization is enabled - if (Authorization::$status) { - $roles = \implode('|', Authorization::getRoles()); + if ($this->authorization->getStatus()) { + $roles = \implode('|', $this->authorization->getRoles()); $filters['_permissions']['$in'] = [new Regex("read\\(\".*(?:{$roles}).*\"\\)", 'i')]; } @@ -2205,8 +2205,8 @@ public function sum(Document $collection, string $attribute, array $queries = [] } // permissions - if (Authorization::$status) { // skip if authorization is disabled - $roles = \implode('|', Authorization::getRoles()); + if ($this->authorization->getStatus()) { // skip if authorization is disabled + $roles = \implode('|', $this->authorization->getRoles()); $filters['_permissions']['$in'] = [new Regex("read\\(\".*(?:{$roles}).*\"\\)", 'i')]; } diff --git a/src/Database/Database.php b/src/Database/Database.php index e4ecfd2af..83802d588 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -7236,10 +7236,7 @@ public function sum(string $collection, string $attribute, array $queries = [], } } - $authorization = new Authorization(self::PERMISSION_READ); - if ($authorization->isValid($collection->getRead())) { - $skipAuth = true; - } + $skipAuth = $this->authorization->isValid(new Input(self::PERMISSION_READ, $collection->getRead())); $relationships = \array_filter( $collection->getAttribute('attributes', []), @@ -7257,7 +7254,7 @@ public function sum(string $collection, string $attribute, array $queries = [], $queries = $queriesOrNull; $getSum = fn () => $this->adapter->sum($collection, $attribute, $queries, $max); - $sum = $skipAuth ?? false ? Authorization::skip($getSum) : $getSum(); + $sum = $skipAuth ? $this->authorization->skip($getSum) : $getSum(); $this->trigger(self::EVENT_DOCUMENT_SUM, $sum); diff --git a/tests/e2e/Adapter/Schemaless/MongoDBTest.php b/tests/e2e/Adapter/Schemaless/MongoDBTest.php index e81a42988..bf5caeb88 100644 --- a/tests/e2e/Adapter/Schemaless/MongoDBTest.php +++ b/tests/e2e/Adapter/Schemaless/MongoDBTest.php @@ -29,7 +29,7 @@ public static function getAdapterName(): string * @return Database * @throws Exception */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -53,6 +53,7 @@ public static function getDatabase(): Database $database = new Database(new Mongo($client), $cache); $database->getAdapter()->setSupportForAttributes(false); $database + ->setAuthorization(self::$authorization) ->setDatabase($schema) ->setNamespace(static::$namespace = 'myapp_' . uniqid()); @@ -73,9 +74,9 @@ public function testCreateExistsDelete(): void { // Mongo creates databases on the fly, so exists would always pass. So we override this test to remove the exists check. $this->assertNotNull(static::getDatabase()->create()); - $this->assertEquals(true, static::getDatabase()->delete($this->testDatabase)); - $this->assertEquals(true, static::getDatabase()->create()); - $this->assertEquals(static::getDatabase(), static::getDatabase()->setDatabase($this->testDatabase)); + $this->assertEquals(true, $this->getDatabase()->delete($this->testDatabase)); + $this->assertEquals(true, $this->getDatabase()->create()); + $this->assertEquals($this->getDatabase(), $this->getDatabase()->setDatabase($this->testDatabase)); } public function testRenameAttribute(): void @@ -98,12 +99,12 @@ public function testKeywords(): void $this->assertTrue(true); } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { return true; } diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 1501c0f59..bb8dcf1c1 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -3943,7 +3943,7 @@ public function testSum(): void $this->assertEquals(2019, $sum); $this->getDatabase()->getAuthorization()->removeRole('user:x'); - + $sum = $database->sum('movies', 'year', [Query::equal('year', [2019]),]); $this->assertEquals(2019 + 2019, $sum); $sum = $database->sum('movies', 'year'); diff --git a/tests/e2e/Adapter/Scopes/SchemalessTests.php b/tests/e2e/Adapter/Scopes/SchemalessTests.php index ee0985682..7002314be 100644 --- a/tests/e2e/Adapter/Scopes/SchemalessTests.php +++ b/tests/e2e/Adapter/Scopes/SchemalessTests.php @@ -20,7 +20,7 @@ trait SchemalessTests public function testSchemalessDocumentOperation(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -117,7 +117,7 @@ public function testSchemalessDocumentOperation(): void public function testSchemalessDocumentInvalidInteralAttributeValidation(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); // test to ensure internal attributes are checked during creating schemaless document if ($database->getAdapter()->getSupportForAttributes()) { @@ -156,7 +156,7 @@ public function testSchemalessDocumentInvalidInteralAttributeValidation(): void public function testSchemalessSelectionOnUnknownAttributes(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -203,7 +203,7 @@ public function testSchemalessSelectionOnUnknownAttributes(): void public function testSchemalessIncrement(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -257,7 +257,7 @@ public function testSchemalessIncrement(): void public function testSchemalessDecrement(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -311,7 +311,7 @@ public function testSchemalessDecrement(): void public function testSchemalessUpdateDocumentWithQuery(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -369,7 +369,7 @@ public function testSchemalessUpdateDocumentWithQuery(): void public function testSchemalessDeleteDocumentWithQuery(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -412,7 +412,7 @@ public function testSchemalessDeleteDocumentWithQuery(): void public function testSchemalessUpdateDocumentsWithQuery(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -507,7 +507,7 @@ public function testSchemalessUpdateDocumentsWithQuery(): void public function testSchemalessDeleteDocumentsWithQuery(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -589,7 +589,7 @@ public function testSchemalessDeleteDocumentsWithQuery(): void public function testSchemalessOperationsWithCallback(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -677,7 +677,7 @@ public function testSchemalessOperationsWithCallback(): void public function testSchemalessIndexCreateListDelete(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -723,7 +723,7 @@ public function testSchemalessIndexCreateListDelete(): void public function testSchemalessIndexDuplicatePrevention(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -754,7 +754,7 @@ public function testSchemalessIndexDuplicatePrevention(): void public function testSchemalessPermissions(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -776,18 +776,18 @@ public function testSchemalessPermissions(): void $this->assertFalse($doc->isEmpty()); // Without roles, cannot read - Authorization::cleanRoles(); + $database->getAuthorization()->cleanRoles(); $empty = $database->getDocument($col, 'd1'); $this->assertTrue($empty->isEmpty()); // With any role, can read - Authorization::setRole(Role::any()->toString()); + $database->getAuthorization()->addRole(Role::any()->toString()); $fetched = $database->getDocument($col, 'd1'); $this->assertEquals('value', $fetched->getAttribute('field')); // Attempt update without update permission - Authorization::cleanRoles(); - Authorization::setRole(Role::any()->toString()); + $database->getAuthorization()->cleanRoles(); + $database->getAuthorization()->addRole(Role::any()->toString()); try { $database->updateDocument($col, 'd1', new Document(['field' => 'updated'])); $this->fail('Failed to throw exception'); @@ -796,7 +796,7 @@ public function testSchemalessPermissions(): void } // Grant update permission and update - Authorization::skip(function () use ($database, $col) { + $database->getAuthorization()->skip(function () use ($database, $col) { $database->updateDocument($col, 'd1', new Document([ '$permissions' => [ Permission::read(Role::any()), @@ -809,7 +809,7 @@ public function testSchemalessPermissions(): void $this->assertEquals('updated', $updated->getAttribute('field')); // Creating without any roles should fail - Authorization::cleanRoles(); + $database->getAuthorization()->cleanRoles(); try { $database->createDocument($col, new Document([ 'field' => 'x' @@ -820,13 +820,13 @@ public function testSchemalessPermissions(): void } $database->deleteCollection($col); - Authorization::cleanRoles(); + $database->getAuthorization()->cleanRoles(); } public function testSchemalessInternalAttributes(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); @@ -836,7 +836,7 @@ public function testSchemalessInternalAttributes(): void $col = uniqid('sl_internal_full'); $database->createCollection($col); - Authorization::setRole(Role::any()->toString()); + $database->getAuthorization()->addRole(Role::any()->toString()); $doc = $database->createDocument($col, new Document([ '$id' => 'i1', @@ -930,13 +930,13 @@ public function testSchemalessInternalAttributes(): void $database->setPreserveDates(false); $database->deleteCollection($col); - Authorization::cleanRoles(); + $database->getAuthorization()->cleanRoles(); } public function testSchemalessDates(): void { /** @var Database $database */ - $database = static::getDatabase(); + $database = $this->getDatabase(); if ($database->getAdapter()->getSupportForAttributes()) { $this->expectNotToPerformAssertions(); From 25af74e8c620e3c6b92768ae46fa185d2e1f579b Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 20 Oct 2025 16:39:39 +0300 Subject: [PATCH 21/29] linter --- tests/e2e/Adapter/Schemaless/MongoDBTest.php | 6 +++--- tests/e2e/Adapter/Scopes/SchemalessTests.php | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/e2e/Adapter/Schemaless/MongoDBTest.php b/tests/e2e/Adapter/Schemaless/MongoDBTest.php index bf5caeb88..4badbedb4 100644 --- a/tests/e2e/Adapter/Schemaless/MongoDBTest.php +++ b/tests/e2e/Adapter/Schemaless/MongoDBTest.php @@ -29,7 +29,7 @@ public static function getAdapterName(): string * @return Database * @throws Exception */ - public function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -99,12 +99,12 @@ public function testKeywords(): void $this->assertTrue(true); } - protected function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { return true; } - protected function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { return true; } diff --git a/tests/e2e/Adapter/Scopes/SchemalessTests.php b/tests/e2e/Adapter/Scopes/SchemalessTests.php index 7002314be..436be6edd 100644 --- a/tests/e2e/Adapter/Scopes/SchemalessTests.php +++ b/tests/e2e/Adapter/Scopes/SchemalessTests.php @@ -13,7 +13,6 @@ use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; trait SchemalessTests { From acaca79eb3d60a8f38daeb3524fea8f27788f50f Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 20 Oct 2025 16:44:03 +0300 Subject: [PATCH 22/29] small fix --- tests/e2e/Adapter/SharedTables/MongoDBTest.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/e2e/Adapter/SharedTables/MongoDBTest.php b/tests/e2e/Adapter/SharedTables/MongoDBTest.php index 9a9f2e749..aaf08c578 100644 --- a/tests/e2e/Adapter/SharedTables/MongoDBTest.php +++ b/tests/e2e/Adapter/SharedTables/MongoDBTest.php @@ -30,7 +30,7 @@ public static function getAdapterName(): string * @return Database * @throws Exception */ - public static function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -53,6 +53,7 @@ public static function getDatabase(): Database $database = new Database(new Mongo($client), $cache); $database + ->setAuthorization(self::$authorization) ->setDatabase($schema) ->setSharedTables(true) ->setTenant(999) @@ -73,10 +74,10 @@ public static function getDatabase(): Database public function testCreateExistsDelete(): void { // Mongo creates databases on the fly, so exists would always pass. So we override this test to remove the exists check. - $this->assertNotNull(static::getDatabase()->create()); - $this->assertEquals(true, static::getDatabase()->delete($this->testDatabase)); - $this->assertEquals(true, static::getDatabase()->create()); - $this->assertEquals(static::getDatabase(), static::getDatabase()->setDatabase($this->testDatabase)); + $this->assertNotNull($this->getDatabase()->create()); + $this->assertEquals(true, $this->getDatabase()->delete($this->testDatabase)); + $this->assertEquals(true, $this->getDatabase()->create()); + $this->assertEquals($this->getDatabase(), $this->getDatabase()->setDatabase($this->testDatabase)); } public function testRenameAttribute(): void @@ -99,12 +100,12 @@ public function testKeywords(): void $this->assertTrue(true); } - protected static function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { return true; } - protected static function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { return true; } From b854100a2679b00ed729a13ccfe51bc548a34636 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 20 Oct 2025 16:45:12 +0300 Subject: [PATCH 23/29] small fix --- src/Database/Validator/Authorization.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Validator/Authorization.php b/src/Database/Validator/Authorization.php index 7fd9e667c..5f5ac179b 100644 --- a/src/Database/Validator/Authorization.php +++ b/src/Database/Validator/Authorization.php @@ -84,7 +84,7 @@ public function isValid(mixed $input): bool * @param string $role * @return void */ - public function addRole(string $role): void // user:meldiron, users, any + public function addRole(string $role): void { $this->roles[$role] = true; } From fc0d9e2034ee414bdf457fdaebc1aedfbaf9d8ff Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 20 Oct 2025 16:47:31 +0300 Subject: [PATCH 24/29] linter --- tests/e2e/Adapter/SharedTables/MongoDBTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e/Adapter/SharedTables/MongoDBTest.php b/tests/e2e/Adapter/SharedTables/MongoDBTest.php index aaf08c578..61904861c 100644 --- a/tests/e2e/Adapter/SharedTables/MongoDBTest.php +++ b/tests/e2e/Adapter/SharedTables/MongoDBTest.php @@ -30,7 +30,7 @@ public static function getAdapterName(): string * @return Database * @throws Exception */ - public function getDatabase(): Database + public function getDatabase(): Database { if (!is_null(self::$database)) { return self::$database; @@ -100,12 +100,12 @@ public function testKeywords(): void $this->assertTrue(true); } - protected function deleteColumn(string $collection, string $column): bool + protected function deleteColumn(string $collection, string $column): bool { return true; } - protected function deleteIndex(string $collection, string $index): bool + protected function deleteIndex(string $collection, string $index): bool { return true; } From 6a91dc5e96d109f6f91fc3503648c8fe1b0ca166 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 20 Oct 2025 16:48:45 +0300 Subject: [PATCH 25/29] utopia-php/mongo ver --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 1ca56d8a4..9717c6434 100755 --- a/composer.json +++ b/composer.json @@ -40,7 +40,7 @@ "utopia-php/framework": "0.33.*", "utopia-php/cache": "0.13.*", "utopia-php/pools": "0.8.*", - "utopia-php/mongo": "0.10.*" + "utopia-php/mongo": "0.11.*" }, "require-dev": { "fakerphp/faker": "1.23.*", From 78eb4f50b0a4a258237b7695efab87152fc41a50 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 20 Oct 2025 16:51:49 +0300 Subject: [PATCH 26/29] composer.lock --- composer.lock | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/composer.lock b/composer.lock index 851a12a7a..8ceef2115 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0b25c35427f7a3653f5c4993507316ee", + "content-hash": "1ef51fb364b438b677b5bce17887153c", "packages": [ { "name": "brick/math", @@ -2166,16 +2166,16 @@ }, { "name": "utopia-php/mongo", - "version": "0.10.0", + "version": "0.11.0", "source": { "type": "git", "url": "https://github.com/utopia-php/mongo.git", - "reference": "ecfad6aad2e2e3fe5899ac2ebf1009a21b4d6b18" + "reference": "34bc0cda8ea368cde68702a6fffe2c3ac625398e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/mongo/zipball/ecfad6aad2e2e3fe5899ac2ebf1009a21b4d6b18", - "reference": "ecfad6aad2e2e3fe5899ac2ebf1009a21b4d6b18", + "url": "https://api.github.com/repos/utopia-php/mongo/zipball/34bc0cda8ea368cde68702a6fffe2c3ac625398e", + "reference": "34bc0cda8ea368cde68702a6fffe2c3ac625398e", "shasum": "" }, "require": { @@ -2221,9 +2221,9 @@ ], "support": { "issues": "https://github.com/utopia-php/mongo/issues", - "source": "https://github.com/utopia-php/mongo/tree/0.10.0" + "source": "https://github.com/utopia-php/mongo/tree/0.11.0" }, - "time": "2025-10-02T04:50:07+00:00" + "time": "2025-10-20T11:11:23+00:00" }, { "name": "utopia-php/pools", @@ -4474,7 +4474,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": {}, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { @@ -4483,6 +4483,6 @@ "ext-mongodb": "*", "ext-mbstring": "*" }, - "platform-dev": {}, - "plugin-api-version": "2.6.0" + "platform-dev": [], + "plugin-api-version": "2.2.0" } From 0a4d6f34ef85b72b343f366f4115d0d52ff31aee Mon Sep 17 00:00:00 2001 From: shimon Date: Thu, 23 Oct 2025 10:02:56 +0300 Subject: [PATCH 27/29] Add authorization setter in Pool adapter initialization --- src/Database/Adapter.php | 4 ++++ src/Database/Adapter/Pool.php | 1 + 2 files changed, 5 insertions(+) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index 8da0b573b..c43585cb9 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -65,6 +65,10 @@ public function setAuthorization(Authorization $authorization): self return $this; } + public function getAuthorization(): Authorization + { + return $this->authorization; + } /** * @param string $key * @param mixed $value diff --git a/src/Database/Adapter/Pool.php b/src/Database/Adapter/Pool.php index f1c845a5d..5a8b3cf96 100644 --- a/src/Database/Adapter/Pool.php +++ b/src/Database/Adapter/Pool.php @@ -30,6 +30,7 @@ public function __construct(UtopiaPool $pool) } // Run setters in case the pooled adapter has its own config + $this->setAuthorization($resource->getAuthorization()); $this->setDatabase($resource->getDatabase()); $this->setNamespace($resource->getNamespace()); $this->setSharedTables($resource->getSharedTables()); From 22123ad30782be131ecad4e9298b843d8daf387b Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 27 Oct 2025 10:34:29 +0200 Subject: [PATCH 28/29] Refactor getAuthorization method formatting and add error handling for authorization initialization in Pool adapter --- src/Database/Adapter.php | 6 +++--- src/Database/Adapter/Pool.php | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Database/Adapter.php b/src/Database/Adapter.php index c43585cb9..d2e7eebe5 100644 --- a/src/Database/Adapter.php +++ b/src/Database/Adapter.php @@ -66,9 +66,9 @@ public function setAuthorization(Authorization $authorization): self } public function getAuthorization(): Authorization - { - return $this->authorization; - } + { + return $this->authorization; + } /** * @param string $key * @param mixed $value diff --git a/src/Database/Adapter/Pool.php b/src/Database/Adapter/Pool.php index 5a8b3cf96..7f381b264 100644 --- a/src/Database/Adapter/Pool.php +++ b/src/Database/Adapter/Pool.php @@ -30,7 +30,11 @@ public function __construct(UtopiaPool $pool) } // Run setters in case the pooled adapter has its own config - $this->setAuthorization($resource->getAuthorization()); + try { + $this->setAuthorization($resource->getAuthorization()); + } catch (\Error $e) { + // Authorization not initialized yet, so skip it. + } $this->setDatabase($resource->getDatabase()); $this->setNamespace($resource->getNamespace()); $this->setSharedTables($resource->getSharedTables()); From 5464da0f13d159096b1ee62acfddf0440be36596 Mon Sep 17 00:00:00 2001 From: shimon Date: Mon, 3 Nov 2025 12:08:55 +0200 Subject: [PATCH 29/29] Authorization::skip to $this->getAuthorization()->skip --- src/Database/Database.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index f4fdd6d67..c343191b5 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -742,7 +742,7 @@ protected function refetchDocuments(Document $collection, array $documents): arr $docIds = array_map(fn ($doc) => $doc->getId(), $documents); // Fetch fresh copies with computed operator values - $refetched = Authorization::skip(fn () => $this->silent( + $refetched = $this->getAuthorization()->skip(fn () => $this->silent( fn () => $this->find($collection->getId(), [Query::equal('$id', $docIds)]) ));