From 246583368bd980d6acd2390a3250a9018a2acae8 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 30 Nov 2026 08:48:06 +0000 Subject: [PATCH 1/5] fix(upsert): authorize case-variant ids as an update of the stored document The engines compare `_uid` without case or accents, so an upsert of `ABC` found the stored `abc` but keyed it under the stored spelling and missed it. It was then authorized as a create and written through the unique `_uid` conflict, replacing the stored row and its permissions. Look up missed ids on their own when the batched read returns a row no request named exactly, and write a variant-id upsert under the stored id, as updateDocument() does. Co-Authored-By: Claude Opus 5.5 --- src/Database/Trait/Documents.php | 51 +++++++++--- tests/e2e/Adapter/MariaDBTest.php | 3 + tests/e2e/Adapter/MySQLTest.php | 2 + tests/e2e/Adapter/PostgresTest.php | 2 + .../Adapter/Scopes/CaseVariantUpsertTests.php | 81 +++++++++++++++++++ .../e2e/Adapter/SharedTables/MariaDBTest.php | 3 + tests/e2e/Adapter/SharedTables/MySQLTest.php | 2 + .../e2e/Adapter/SharedTables/PostgresTest.php | 2 + 8 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 tests/e2e/Adapter/Scopes/CaseVariantUpsertTests.php diff --git a/src/Database/Trait/Documents.php b/src/Database/Trait/Documents.php index 282704010..82509a989 100644 --- a/src/Database/Trait/Documents.php +++ b/src/Database/Trait/Documents.php @@ -2412,6 +2412,11 @@ private function upsert( foreach ($documents as $key => $document) { $old = $existing[$this->upsertKey($document)] ?? new Document(); + // A case- or accent-variant id updates the stored document under its stored id, as updateDocument() does + if (! $old->isEmpty() && $old->getId() !== $document->getId()) { + $document->setAttribute(Document::ID, $old->getId()); + } + $document = $this->removeUnknownAttributes($collection, $document); $unchanged = $this->roundOperators($collection, $document); if (! $old->isEmpty()) { @@ -2716,24 +2721,50 @@ private function findDocumentsToUpsert(string $collection, array $documents): ar $batches[$key]['ids'][] = $document->getId(); } + $find = function (array $ids, int|string|null $tenant) use ($collection, $perTenant): array { + $read = fn (): array => $this->authorization->skip(fn () => $this->silent(fn () => $this->skipQueryHooks(fn () => $this->find($collection, [ + Query::equal(Document::ID, $ids), + Query::limit($this->maxQueryValues), + ], forPermission: PermissionType::Update)))); + + return $perTenant ? $this->withTenant($tenant, $read) : $read(); + }; + $existing = []; foreach ($batches as $batch) { foreach (\array_chunk(\array_values(\array_unique($batch['ids'])), \max(1, $this->maxQueryValues)) as $chunk) { - $read = fn (): array => $this->authorization->skip(fn () => $this->silent(fn () => $this->skipQueryHooks(fn () => $this->find($collection, [ - Query::equal(Document::ID, $chunk), - Query::limit($this->maxQueryValues), - ], forPermission: PermissionType::Update)))); - - $found = $perTenant - ? $this->withTenant($batch['tenant'], $read) - : $read(); - - foreach ($found as $document) { + foreach ($find($chunk, $batch['tenant']) as $document) { $existing[$this->upsertKey($document)] = $document; } } } + // The engine compares ids in its own collation, which can ignore case and accents: a request for + // `ABC` then finds the stored `abc`, keyed under the stored spelling. The request would miss it, + // be authorized as a create, and overwrite `abc` through the unique `_uid` conflict. So, only when + // the read returned a row no request named exactly, look each missed id up on its own. + $requested = []; + foreach ($documents as $document) { + if ($document->getId() !== '') { + $requested[$this->upsertKey($document)] = true; + } + } + if (\array_diff_key($existing, $requested) === []) { + return $existing; + } + + foreach ($documents as $document) { + $key = $this->upsertKey($document); + if ($document->getId() === '' || isset($existing[$key])) { + continue; + } + + $stored = $find([$document->getId()], $perTenant ? $document->getTenant() : null)[0] ?? null; + if ($stored instanceof Document) { + $existing[$key] = $stored; + } + } + return $existing; } diff --git a/tests/e2e/Adapter/MariaDBTest.php b/tests/e2e/Adapter/MariaDBTest.php index dbea51b47..27b225ac6 100644 --- a/tests/e2e/Adapter/MariaDBTest.php +++ b/tests/e2e/Adapter/MariaDBTest.php @@ -6,6 +6,7 @@ use Redis; use Swoole\Coroutine; use Swoole\Runtime; +use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Throwable; use Utopia\Cache\Adapter\None as NoCache; use Utopia\Cache\Adapter\Redis as RedisAdapter; @@ -25,6 +26,8 @@ class MariaDBTest extends Base { + use CaseVariantUpsertTests; + protected static ?Database $database = null; protected static ?PDO $pdo = null; diff --git a/tests/e2e/Adapter/MySQLTest.php b/tests/e2e/Adapter/MySQLTest.php index 5eaf3d034..201172c34 100644 --- a/tests/e2e/Adapter/MySQLTest.php +++ b/tests/e2e/Adapter/MySQLTest.php @@ -3,6 +3,7 @@ namespace Tests\E2E\Adapter; use Redis; +use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Tests\E2E\Adapter\Scopes\MySQLJoinPlanTests; use Utopia\Cache\Adapter\Redis as RedisAdapter; use Utopia\Cache\Cache; @@ -15,6 +16,7 @@ class MySQLTest extends Base { + use CaseVariantUpsertTests; use MySQLJoinPlanTests; public static ?Database $database = null; diff --git a/tests/e2e/Adapter/PostgresTest.php b/tests/e2e/Adapter/PostgresTest.php index 8a3fc1d7a..bcfbb23a1 100644 --- a/tests/e2e/Adapter/PostgresTest.php +++ b/tests/e2e/Adapter/PostgresTest.php @@ -3,6 +3,7 @@ namespace Tests\E2E\Adapter; use Redis; +use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Tests\E2E\Adapter\Scopes\PostgresIdCollationTests; use Utopia\Cache\Adapter\Redis as RedisAdapter; use Utopia\Cache\Cache; @@ -19,6 +20,7 @@ class PostgresTest extends Base { + use CaseVariantUpsertTests; use PostgresIdCollationTests; public static ?Database $database = null; diff --git a/tests/e2e/Adapter/Scopes/CaseVariantUpsertTests.php b/tests/e2e/Adapter/Scopes/CaseVariantUpsertTests.php new file mode 100644 index 000000000..d6f64f32b --- /dev/null +++ b/tests/e2e/Adapter/Scopes/CaseVariantUpsertTests.php @@ -0,0 +1,81 @@ +getDatabase(); + $auth = $database->getAuthorization(); + + $database->createCollection(Collection::create(id: 'caseVariantUpsert', permissions: [ + Permission::create(Role::any()), + Permission::read(Role::any()), + ], documentSecurity: true)); + $database->createAttribute('caseVariantUpsert', Attribute::string('name', 64)); + + $database->createDocument('caseVariantUpsert', new Document([ + '$id' => 'abc', + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::user('owner')), + ], + 'name' => 'original', + ])); + + try { + $auth->cleanRoles(); + $auth->addRole(Role::any()->toString()); + $auth->addRole(Role::user('intruder')->toString()); + + try { + $database->upsertDocuments('caseVariantUpsert', [new Document([ + '$id' => 'ABC', + '$permissions' => [ + Permission::read(Role::user('intruder')), + Permission::update(Role::user('intruder')), + ], + 'name' => 'overwritten', + ])]); + $this->fail('A case-variant upsert must be authorized as an update of the stored document'); + } catch (AuthorizationException) { + } + + $auth->cleanRoles(); + $auth->addRole(Role::any()->toString()); + $auth->addRole(Role::user('owner')->toString()); + + $upserted = []; + $database->upsertDocuments('caseVariantUpsert', [new Document([ + '$id' => 'ABC', + 'name' => 'renamed', + ])], onNext: function (Document $document) use (&$upserted): void { + $upserted[] = $document->getId(); + }); + $this->assertSame(['abc'], $upserted, 'The update must keep the stored id'); + } finally { + $auth->cleanRoles(); + $auth->addRole(Role::any()->toString()); + } + + $stored = $database->getDocument('caseVariantUpsert', 'abc'); + $this->assertSame('renamed', $stored->getAttribute('name')); + $this->assertContains(Permission::update(Role::user('owner')), $stored->getPermissions()); + $this->assertCount(1, $database->find('caseVariantUpsert')); + + $database->deleteCollection('caseVariantUpsert'); + } +} diff --git a/tests/e2e/Adapter/SharedTables/MariaDBTest.php b/tests/e2e/Adapter/SharedTables/MariaDBTest.php index 25278ca71..b8448d080 100644 --- a/tests/e2e/Adapter/SharedTables/MariaDBTest.php +++ b/tests/e2e/Adapter/SharedTables/MariaDBTest.php @@ -4,6 +4,7 @@ use Redis; use Tests\E2E\Adapter\Base; +use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Utopia\Cache\Adapter\Redis as RedisAdapter; use Utopia\Cache\Cache; use Utopia\Database\Adapter\MariaDB; @@ -12,6 +13,8 @@ class MariaDBTest extends Base { + use CaseVariantUpsertTests; + protected static ?Database $database = null; protected static ?PDO $pdo = null; diff --git a/tests/e2e/Adapter/SharedTables/MySQLTest.php b/tests/e2e/Adapter/SharedTables/MySQLTest.php index d910b5928..5b05662ed 100644 --- a/tests/e2e/Adapter/SharedTables/MySQLTest.php +++ b/tests/e2e/Adapter/SharedTables/MySQLTest.php @@ -4,6 +4,7 @@ use Redis; use Tests\E2E\Adapter\Base; +use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Tests\E2E\Adapter\Scopes\MySQLJoinPlanTests; use Utopia\Cache\Adapter\Redis as RedisAdapter; use Utopia\Cache\Cache; @@ -13,6 +14,7 @@ class MySQLTest extends Base { + use CaseVariantUpsertTests; use MySQLJoinPlanTests; public static ?Database $database = null; diff --git a/tests/e2e/Adapter/SharedTables/PostgresTest.php b/tests/e2e/Adapter/SharedTables/PostgresTest.php index 560e65c63..aaca05bc7 100644 --- a/tests/e2e/Adapter/SharedTables/PostgresTest.php +++ b/tests/e2e/Adapter/SharedTables/PostgresTest.php @@ -4,6 +4,7 @@ use Redis; use Tests\E2E\Adapter\Base; +use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Tests\E2E\Adapter\Scopes\PostgresIdCollationTests; use Utopia\Cache\Adapter\Redis as RedisAdapter; use Utopia\Cache\Cache; @@ -18,6 +19,7 @@ class PostgresTest extends Base { + use CaseVariantUpsertTests; use PostgresIdCollationTests; public static ?Database $database = null; From 1669f736c8ef64319eb91b53c3360cdca3790f11 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 30 Nov 2026 08:54:23 +0000 Subject: [PATCH 2/5] fix(upsert): resolve variant ids even when the stored id is requested too An unchanged upsert of the stored `abc` in the same batch made every row the batched read returned look requested, so the lookup of a variant `ABC` was skipped. Read the missed ids again on their own instead: any row that comes back is a variant, and only then is each missed id looked up alone. Co-Authored-By: Claude Opus 5.5 --- src/Database/Trait/Documents.php | 38 ++++++++++--------- .../Adapter/Scopes/CaseVariantUpsertTests.php | 28 ++++++++------ 2 files changed, 37 insertions(+), 29 deletions(-) diff --git a/src/Database/Trait/Documents.php b/src/Database/Trait/Documents.php index 82509a989..23445b815 100644 --- a/src/Database/Trait/Documents.php +++ b/src/Database/Trait/Documents.php @@ -2741,27 +2741,29 @@ private function findDocumentsToUpsert(string $collection, array $documents): ar // The engine compares ids in its own collation, which can ignore case and accents: a request for // `ABC` then finds the stored `abc`, keyed under the stored spelling. The request would miss it, - // be authorized as a create, and overwrite `abc` through the unique `_uid` conflict. So, only when - // the read returned a row no request named exactly, look each missed id up on its own. - $requested = []; - foreach ($documents as $document) { - if ($document->getId() !== '') { - $requested[$this->upsertKey($document)] = true; + // be authorized as a create, and overwrite `abc` through the unique `_uid` conflict. Whether `abc` + // was also requested exactly does not matter, so read the missed ids again on their own: any row + // that comes back is a variant of one of them, and only then is each missed id looked up alone. + foreach ($batches as $batch) { + $missed = []; + foreach (\array_unique($batch['ids']) as $id) { + $key = $perTenant ? $batch['tenant'].':'.$id : $id; + if (! isset($existing[$key])) { + $missed[$key] = $id; + } } - } - if (\array_diff_key($existing, $requested) === []) { - return $existing; - } - foreach ($documents as $document) { - $key = $this->upsertKey($document); - if ($document->getId() === '' || isset($existing[$key])) { - continue; - } + foreach (\array_chunk($missed, \max(1, $this->maxQueryValues), true) as $chunk) { + if ($find(\array_values($chunk), $batch['tenant']) === []) { + continue; + } - $stored = $find([$document->getId()], $perTenant ? $document->getTenant() : null)[0] ?? null; - if ($stored instanceof Document) { - $existing[$key] = $stored; + foreach ($chunk as $key => $id) { + $stored = $find([$id], $batch['tenant'])[0] ?? null; + if ($stored instanceof Document) { + $existing[$key] = $stored; + } + } } } diff --git a/tests/e2e/Adapter/Scopes/CaseVariantUpsertTests.php b/tests/e2e/Adapter/Scopes/CaseVariantUpsertTests.php index d6f64f32b..75c288746 100644 --- a/tests/e2e/Adapter/Scopes/CaseVariantUpsertTests.php +++ b/tests/e2e/Adapter/Scopes/CaseVariantUpsertTests.php @@ -41,17 +41,23 @@ public function testCaseVariantUpsertIsAuthorizedAsAnUpdate(): void $auth->addRole(Role::any()->toString()); $auth->addRole(Role::user('intruder')->toString()); - try { - $database->upsertDocuments('caseVariantUpsert', [new Document([ - '$id' => 'ABC', - '$permissions' => [ - Permission::read(Role::user('intruder')), - Permission::update(Role::user('intruder')), - ], - 'name' => 'overwritten', - ])]); - $this->fail('A case-variant upsert must be authorized as an update of the stored document'); - } catch (AuthorizationException) { + $variant = new Document([ + '$id' => 'ABC', + '$permissions' => [ + Permission::read(Role::user('intruder')), + Permission::update(Role::user('intruder')), + ], + 'name' => 'overwritten', + ]); + // An unchanged copy of the stored document, requested by its exact id, must not hide the variant + $unchanged = new Document(['$id' => 'abc', 'name' => 'original']); + + foreach ([[$unchanged, $variant], [$variant]] as $batch) { + try { + $database->upsertDocuments('caseVariantUpsert', $batch); + $this->fail('A case-variant upsert must be authorized as an update of the stored document'); + } catch (AuthorizationException) { + } } $auth->cleanRoles(); From 1d5939f7cd7ee1db0dda89a98e58f3e4564d346b Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 30 Nov 2026 09:15:12 +0000 Subject: [PATCH 3/5] fix(upsert): re-read missed ids only for batches that found stored rows A case or accent variant is among the rows the batch already read, so a batch that read none has no variant: pure creates keep a single read. Type the lookup's ids for PHPStan, and let the Redis read-count test expect the one extra read a batch that both updates and creates now makes. Co-Authored-By: Claude Opus 5.5 --- src/Database/Trait/Documents.php | 15 +++++++++++++-- tests/unit/Adapter/RedisUniqueIndexTest.php | 3 ++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Database/Trait/Documents.php b/src/Database/Trait/Documents.php index 23445b815..ac2cdf465 100644 --- a/src/Database/Trait/Documents.php +++ b/src/Database/Trait/Documents.php @@ -2721,6 +2721,10 @@ private function findDocumentsToUpsert(string $collection, array $documents): ar $batches[$key]['ids'][] = $document->getId(); } + /** + * @param array $ids + * @return array + */ $find = function (array $ids, int|string|null $tenant) use ($collection, $perTenant): array { $read = fn (): array => $this->authorization->skip(fn () => $this->silent(fn () => $this->skipQueryHooks(fn () => $this->find($collection, [ Query::equal(Document::ID, $ids), @@ -2731,10 +2735,12 @@ private function findDocumentsToUpsert(string $collection, array $documents): ar }; $existing = []; - foreach ($batches as $batch) { + $found = []; + foreach ($batches as $batchKey => $batch) { foreach (\array_chunk(\array_values(\array_unique($batch['ids'])), \max(1, $this->maxQueryValues)) as $chunk) { foreach ($find($chunk, $batch['tenant']) as $document) { $existing[$this->upsertKey($document)] = $document; + $found[$batchKey] = true; } } } @@ -2744,7 +2750,12 @@ private function findDocumentsToUpsert(string $collection, array $documents): ar // be authorized as a create, and overwrite `abc` through the unique `_uid` conflict. Whether `abc` // was also requested exactly does not matter, so read the missed ids again on their own: any row // that comes back is a variant of one of them, and only then is each missed id looked up alone. - foreach ($batches as $batch) { + // A variant is among the rows the batch already read, so a batch that read none has no variant. + foreach ($batches as $batchKey => $batch) { + if (! isset($found[$batchKey])) { + continue; + } + $missed = []; foreach (\array_unique($batch['ids']) as $id) { $key = $perTenant ? $batch['tenant'].':'.$id : $id; diff --git a/tests/unit/Adapter/RedisUniqueIndexTest.php b/tests/unit/Adapter/RedisUniqueIndexTest.php index a9b72b76a..60e2e5e22 100644 --- a/tests/unit/Adapter/RedisUniqueIndexTest.php +++ b/tests/unit/Adapter/RedisUniqueIndexTest.php @@ -141,7 +141,8 @@ public function testAnUpsertBatchReadsTheStoredDocumentsOnceForItsUniqueChecks() new Document(['$id' => 'fifth', 'email' => 'fifth@example.test']), ])); - $this->assertSame($single, $this->memberReads, 'The unique checks must read the collection once per batch, not once per document'); + // A batch that both updates and creates reads its missed ids once more, to find stored case or accent variants + $this->assertSame($single + 1, $this->memberReads, 'The unique checks must read the collection a fixed number of times per batch, not once per document'); $this->assertSame(['fifth@example.test', 'first-moved@example.test', 'fourth@example.test', 'moved@example.test', 'renamed@example.test'], $this->emails($database)); } From 99dc121c7ab92add62983cb7005325b3ac5cdb99 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 30 Nov 2026 09:34:05 +0000 Subject: [PATCH 4/5] chore(upsert): type the ids the variant lookup queries for PHPStan Co-Authored-By: Claude Opus 5.5 --- src/Database/Trait/Documents.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Database/Trait/Documents.php b/src/Database/Trait/Documents.php index ac2cdf465..e9f9b5457 100644 --- a/src/Database/Trait/Documents.php +++ b/src/Database/Trait/Documents.php @@ -2721,11 +2721,8 @@ private function findDocumentsToUpsert(string $collection, array $documents): ar $batches[$key]['ids'][] = $document->getId(); } - /** - * @param array $ids - * @return array - */ $find = function (array $ids, int|string|null $tenant) use ($collection, $perTenant): array { + /** @var list $ids */ $read = fn (): array => $this->authorization->skip(fn () => $this->silent(fn () => $this->skipQueryHooks(fn () => $this->find($collection, [ Query::equal(Document::ID, $ids), Query::limit($this->maxQueryValues), From a3e444f8f11c67b1e9ce29792646e7c208b992f6 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 30 Nov 2026 13:01:05 +0000 Subject: [PATCH 5/5] test(upsert): run the case-variant test on every adapter SQLite, Pool and Mirror read a case variant of an id as the stored document too, and were exploitable the same way. Adapters that keep the variant a separate id skip the test. Redis skips it visibly: it reads `ABC` as `abc` by id but not through find(), which the upsert lookup relies on. Co-Authored-By: Claude Opus 5.5 --- tests/e2e/Adapter/Base.php | 2 ++ tests/e2e/Adapter/MariaDBTest.php | 3 --- tests/e2e/Adapter/MySQLTest.php | 2 -- tests/e2e/Adapter/PostgresTest.php | 2 -- .../Adapter/Scopes/CaseVariantUpsertTests.php | 25 ++++++++++++++++--- .../e2e/Adapter/SharedTables/MariaDBTest.php | 3 --- tests/e2e/Adapter/SharedTables/MySQLTest.php | 2 -- .../e2e/Adapter/SharedTables/PostgresTest.php | 2 -- 8 files changed, 24 insertions(+), 17 deletions(-) diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 7f7a1080f..19bf6a6ed 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -6,6 +6,7 @@ use PHPUnit\Framework\TestCase; use Tests\E2E\Adapter\Scopes\AggregationTests; use Tests\E2E\Adapter\Scopes\AttributeTests; +use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Tests\E2E\Adapter\Scopes\CollectionTests; use Tests\E2E\Adapter\Scopes\CursorTests; use Tests\E2E\Adapter\Scopes\CustomDocumentTypeTests; @@ -51,6 +52,7 @@ abstract class Base extends TestCase { use AggregationTests; use AttributeTests; + use CaseVariantUpsertTests; use CollectionTests; use CursorTests; use CustomDocumentTypeTests; diff --git a/tests/e2e/Adapter/MariaDBTest.php b/tests/e2e/Adapter/MariaDBTest.php index 27b225ac6..dbea51b47 100644 --- a/tests/e2e/Adapter/MariaDBTest.php +++ b/tests/e2e/Adapter/MariaDBTest.php @@ -6,7 +6,6 @@ use Redis; use Swoole\Coroutine; use Swoole\Runtime; -use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Throwable; use Utopia\Cache\Adapter\None as NoCache; use Utopia\Cache\Adapter\Redis as RedisAdapter; @@ -26,8 +25,6 @@ class MariaDBTest extends Base { - use CaseVariantUpsertTests; - protected static ?Database $database = null; protected static ?PDO $pdo = null; diff --git a/tests/e2e/Adapter/MySQLTest.php b/tests/e2e/Adapter/MySQLTest.php index 201172c34..5eaf3d034 100644 --- a/tests/e2e/Adapter/MySQLTest.php +++ b/tests/e2e/Adapter/MySQLTest.php @@ -3,7 +3,6 @@ namespace Tests\E2E\Adapter; use Redis; -use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Tests\E2E\Adapter\Scopes\MySQLJoinPlanTests; use Utopia\Cache\Adapter\Redis as RedisAdapter; use Utopia\Cache\Cache; @@ -16,7 +15,6 @@ class MySQLTest extends Base { - use CaseVariantUpsertTests; use MySQLJoinPlanTests; public static ?Database $database = null; diff --git a/tests/e2e/Adapter/PostgresTest.php b/tests/e2e/Adapter/PostgresTest.php index bcfbb23a1..8a3fc1d7a 100644 --- a/tests/e2e/Adapter/PostgresTest.php +++ b/tests/e2e/Adapter/PostgresTest.php @@ -3,7 +3,6 @@ namespace Tests\E2E\Adapter; use Redis; -use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Tests\E2E\Adapter\Scopes\PostgresIdCollationTests; use Utopia\Cache\Adapter\Redis as RedisAdapter; use Utopia\Cache\Cache; @@ -20,7 +19,6 @@ class PostgresTest extends Base { - use CaseVariantUpsertTests; use PostgresIdCollationTests; public static ?Database $database = null; diff --git a/tests/e2e/Adapter/Scopes/CaseVariantUpsertTests.php b/tests/e2e/Adapter/Scopes/CaseVariantUpsertTests.php index 75c288746..8ac69d869 100644 --- a/tests/e2e/Adapter/Scopes/CaseVariantUpsertTests.php +++ b/tests/e2e/Adapter/Scopes/CaseVariantUpsertTests.php @@ -2,23 +2,31 @@ namespace Tests\E2E\Adapter\Scopes; +use Utopia\Database\Adapter\Feature; use Utopia\Database\Attribute; use Utopia\Database\Collection; use Utopia\Database\Document; use Utopia\Database\Exception\Authorization as AuthorizationException; use Utopia\Database\Permission; +use Utopia\Database\Query; use Utopia\Database\Role; /** - * For engines whose `_uid` index compares ids case-insensitively (MariaDB and MySQL through the default - * collation, Postgres through utf8_ci_ai): an upsert through a case variant of a stored id is an update of - * the stored document, never a create that overwrites it. + * Where an adapter reads a case variant of a stored id as that document (MariaDB and MySQL through the default + * collation, Postgres through utf8_ci_ai, SQLite through its collation), an upsert through the variant is an + * update of the stored document, never a create that overwrites it. Adapters that keep the variant a separate + * id skip the test. */ trait CaseVariantUpsertTests { public function testCaseVariantUpsertIsAuthorizedAsAnUpdate(): void { $database = $this->getDatabase(); + if (! $database->getAdapter()->hasFeature(Feature\Upserts::class)) { + $this->expectNotToPerformAssertions(); + + return; + } $auth = $database->getAuthorization(); $database->createCollection(Collection::create(id: 'caseVariantUpsert', permissions: [ @@ -36,6 +44,17 @@ public function testCaseVariantUpsertIsAuthorizedAsAnUpdate(): void 'name' => 'original', ])); + if ($database->getDocument('caseVariantUpsert', 'ABC')->isEmpty()) { + $database->deleteCollection('caseVariantUpsert'); + $this->expectNotToPerformAssertions(); + + return; + } + if ($database->find('caseVariantUpsert', [Query::equal('$id', ['ABC'])]) === []) { + $database->deleteCollection('caseVariantUpsert'); + $this->markTestSkipped('This adapter reads `ABC` as `abc` by id but not through find(), which the upsert lookup relies on'); + } + try { $auth->cleanRoles(); $auth->addRole(Role::any()->toString()); diff --git a/tests/e2e/Adapter/SharedTables/MariaDBTest.php b/tests/e2e/Adapter/SharedTables/MariaDBTest.php index b8448d080..25278ca71 100644 --- a/tests/e2e/Adapter/SharedTables/MariaDBTest.php +++ b/tests/e2e/Adapter/SharedTables/MariaDBTest.php @@ -4,7 +4,6 @@ use Redis; use Tests\E2E\Adapter\Base; -use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Utopia\Cache\Adapter\Redis as RedisAdapter; use Utopia\Cache\Cache; use Utopia\Database\Adapter\MariaDB; @@ -13,8 +12,6 @@ class MariaDBTest extends Base { - use CaseVariantUpsertTests; - protected static ?Database $database = null; protected static ?PDO $pdo = null; diff --git a/tests/e2e/Adapter/SharedTables/MySQLTest.php b/tests/e2e/Adapter/SharedTables/MySQLTest.php index 5b05662ed..d910b5928 100644 --- a/tests/e2e/Adapter/SharedTables/MySQLTest.php +++ b/tests/e2e/Adapter/SharedTables/MySQLTest.php @@ -4,7 +4,6 @@ use Redis; use Tests\E2E\Adapter\Base; -use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Tests\E2E\Adapter\Scopes\MySQLJoinPlanTests; use Utopia\Cache\Adapter\Redis as RedisAdapter; use Utopia\Cache\Cache; @@ -14,7 +13,6 @@ class MySQLTest extends Base { - use CaseVariantUpsertTests; use MySQLJoinPlanTests; public static ?Database $database = null; diff --git a/tests/e2e/Adapter/SharedTables/PostgresTest.php b/tests/e2e/Adapter/SharedTables/PostgresTest.php index aaca05bc7..560e65c63 100644 --- a/tests/e2e/Adapter/SharedTables/PostgresTest.php +++ b/tests/e2e/Adapter/SharedTables/PostgresTest.php @@ -4,7 +4,6 @@ use Redis; use Tests\E2E\Adapter\Base; -use Tests\E2E\Adapter\Scopes\CaseVariantUpsertTests; use Tests\E2E\Adapter\Scopes\PostgresIdCollationTests; use Utopia\Cache\Adapter\Redis as RedisAdapter; use Utopia\Cache\Cache; @@ -19,7 +18,6 @@ class PostgresTest extends Base { - use CaseVariantUpsertTests; use PostgresIdCollationTests; public static ?Database $database = null;