Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Database/Trait/Documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}

Expand Down
92 changes: 92 additions & 0 deletions tests/unit/FilterCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Tests\Unit;

use PDO;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Utopia\Cache\Adapter\None;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter;
use Utopia\Database\Adapter\Memory;
use Utopia\Database\Adapter\SQLite;
use Utopia\Database\Attribute;
use Utopia\Database\Collection;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Permission;
use Utopia\Database\Query;
use Utopia\Database\Role;

final class FilterCollectionTest extends TestCase
{
private const string COLLECTION = 'resources';

private const string FILTER = 'recordCollection';

/**
* @var array<int, string>
*/
private static array $seen = [];

/**
* @return array<string, array{\Closure(): Adapter}>
*/
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;
}
}
Loading