diff --git a/src/Database/Trait/Documents.php b/src/Database/Trait/Documents.php index c7380496d..0e689d6c5 100644 --- a/src/Database/Trait/Documents.php +++ b/src/Database/Trait/Documents.php @@ -4444,6 +4444,9 @@ private function fetch(Collection $collection, array $queries, PermissionType $f foreach ($this->castAfter($collection, $results) as $index => $node) { $node = $this->casting($collection, $node); + if (! $node->isEmpty()) { + $node->setAttribute(Document::COLLECTION, $collectionId); + } $node = $this->decode($collection, $node, $selections); if ($joinedCollections !== []) { $node = $this->decodeJoins($node, $joinedCollections); @@ -4456,10 +4459,6 @@ private function fetch(Collection $collection, array $queries, PermissionType $f $node = $this->newDocument($collectionId, $node->getArrayCopy()); } - if (! $node->isEmpty()) { - $node->setAttribute(Document::COLLECTION, $collectionId); - } - $results[$index] = $node; } diff --git a/tests/unit/FilterCollectionTest.php b/tests/unit/FilterCollectionTest.php new file mode 100644 index 000000000..409c71de3 --- /dev/null +++ b/tests/unit/FilterCollectionTest.php @@ -0,0 +1,92 @@ + + */ + private static array $seen = []; + + /** + * @return array + */ + public static function adapters(): array + { + return [ + 'memory' => [static fn (): Adapter => new Memory()], + 'sqlite' => [static fn (): Adapter => new SQLite(new PDO('sqlite::memory:'))], + ]; + } + + /** + * @param \Closure(): Adapter $adapter + */ + #[DataProvider('adapters')] + public function testDecodeFilterSeesTheCollectionOnEveryRead(\Closure $adapter): void + { + $database = $this->database($adapter()); + $database->createDocument(self::COLLECTION, new Document([Document::ID => 'one', 'name' => 'one', 'derived' => ''])); + + self::$seen = []; + $database->getDocument(self::COLLECTION, 'one'); + $this->assertSame([self::COLLECTION], self::$seen, 'getDocument() decoded without the collection'); + + self::$seen = []; + $found = $database->find(self::COLLECTION); + $this->assertSame([self::COLLECTION], self::$seen, 'find() decoded without the collection'); + $this->assertSame(self::COLLECTION, $found[0]->getCollection()); + + self::$seen = []; + $selected = $database->find(self::COLLECTION, [Query::select(['name'])]); + $this->assertSame(self::COLLECTION, $selected[0]->getCollection(), 'find() with a selection lost the collection'); + } + + private function database(Adapter $adapter): Database + { + Database::addFilter( + self::FILTER, + static fn (mixed $value): mixed => $value, + static function (mixed $value, Document $document): mixed { + self::$seen[] = $document->getCollection(); + + return $value; + }, + ); + + $database = new Database($adapter, new Cache(new None())); + $database->setDatabase('filters')->setNamespace('collection_'.\uniqid()); + $database->create(); + $database->createCollection(Collection::create( + id: self::COLLECTION, + attributes: [ + Attribute::string(key: 'name', size: 64), + Attribute::string(key: 'derived', size: 64, filters: [self::FILTER]), + ], + permissions: [Permission::create(Role::any()), Permission::read(Role::any())], + )); + + return $database; + } +}