Skip to content

Commit bc5ceb3

Browse files
authored
Merge pull request #308 from utopia-php/fix-no-invalid-keys
Add test for invalid keys
2 parents 9ff69a9 + 6c53a71 commit bc5ceb3

2 files changed

Lines changed: 77 additions & 3 deletions

File tree

src/Database/Database.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2920,6 +2920,11 @@ public function updateDocument(string $collection, string $id, Document $documen
29202920

29212921
$time = DateTime::now();
29222922
$old = Authorization::skip(fn () => $this->silent(fn () => $this->getDocument($collection, $id))); // Skip ensures user does not need read permission for this
2923+
$document = \array_merge($old->getArrayCopy(), $document->getArrayCopy());
2924+
$document['$collection'] = $old->getAttribute('$collection'); // Make sure user doesn't switch collectionID
2925+
$document['$createdAt'] = $old->getCreatedAt(); // Make sure user doesn't switch createdAt
2926+
$document['$id'] = $old->getId();
2927+
$document = new Document($document);
29232928

29242929
$collection = $this->silent(fn () => $this->getCollection($collection));
29252930
$relationships = \array_filter($collection->getAttribute('attributes', []), function ($attribute) {

tests/Database/Base.php

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Utopia\Database\Validator\Authorization;
2424
use Utopia\Database\Validator\Datetime as DatetimeValidator;
2525
use Utopia\Database\Validator\Index;
26-
use Utopia\Database\Validator\Query\Filter;
2726
use Utopia\Database\Validator\Structure;
2827
use Utopia\Validator\Range;
2928
use Utopia\Database\Exception\Structure as StructureException;
@@ -4415,6 +4414,76 @@ public function testWritePermissions(): void
44154414
$this->assertEquals('newCat', $docs[0]['type']);
44164415
}
44174416

4417+
public function testNoInvalidKeysWithRelationships(): void
4418+
{
4419+
if (!static::getDatabase()->getAdapter()->getSupportForRelationships()) {
4420+
$this->expectNotToPerformAssertions();
4421+
return;
4422+
}
4423+
static::getDatabase()->createCollection('species');
4424+
static::getDatabase()->createCollection('creatures');
4425+
static::getDatabase()->createCollection('characterstics');
4426+
4427+
static::getDatabase()->createAttribute('species', 'name', Database::VAR_STRING, 255, true);
4428+
static::getDatabase()->createAttribute('creatures', 'name', Database::VAR_STRING, 255, true);
4429+
static::getDatabase()->createAttribute('characterstics', 'name', Database::VAR_STRING, 255, true);
4430+
4431+
static::getDatabase()->createRelationship(
4432+
collection: 'species',
4433+
relatedCollection: 'creatures',
4434+
type: Database::RELATION_ONE_TO_ONE,
4435+
twoWay: true,
4436+
id: 'creature',
4437+
twoWayKey:'species'
4438+
);
4439+
static::getDatabase()->createRelationship(
4440+
collection: 'creatures',
4441+
relatedCollection: 'characterstics',
4442+
type: Database::RELATION_ONE_TO_ONE,
4443+
twoWay: true,
4444+
id: 'characterstic',
4445+
twoWayKey:'creature'
4446+
);
4447+
4448+
$species = static::getDatabase()->createDocument('species', new Document([
4449+
'$id' => ID::custom('1'),
4450+
'$permissions' => [
4451+
Permission::read(Role::any()),
4452+
],
4453+
'name' => 'Canine',
4454+
'creature' => [
4455+
'$id' => ID::custom('1'),
4456+
'$permissions' => [
4457+
Permission::read(Role::any()),
4458+
],
4459+
'name' => 'Dog',
4460+
'characterstic' => [
4461+
'$id' => ID::custom('1'),
4462+
'$permissions' => [
4463+
Permission::read(Role::any()),
4464+
Permission::update(Role::any()),
4465+
],
4466+
'name' => 'active',
4467+
]
4468+
]
4469+
]));
4470+
static::getDatabase()->updateDocument('species', $species->getId(), new Document([
4471+
'$id' => ID::custom('1'),
4472+
'$collection' => 'species',
4473+
'creature' => [
4474+
'$id' => ID::custom('1'),
4475+
'$collection' => 'creatures',
4476+
'characterstic' => [
4477+
'$id' => ID::custom('1'),
4478+
'name' => 'active',
4479+
'$collection' => 'characterstics',
4480+
]
4481+
]
4482+
]));
4483+
$updatedSpecies = static::getDatabase()->getDocument('species', $species->getId());
4484+
$this->assertEquals($species, $updatedSpecies);
4485+
}
4486+
44184487
// Relationships
44194488
public function testOneToOneOneWayRelationship(): void
44204489
{
@@ -4913,12 +4982,12 @@ public function testOneToOneTwoWayRelationship(): void
49134982
$this->assertEquals('London', $country1->getAttribute('city')->getAttribute('name'));
49144983

49154984
// Update a document with non existing related document. It should not get added to the list.
4916-
static::getDatabase()->updateDocument('country', 'country1', $doc->setAttribute('city', 'no-city'));
4985+
static::getDatabase()->updateDocument('country', 'country1', (new Document($doc->getArrayCopy()))->setAttribute('city', 'no-city'));
49174986

49184987
$country1Document = static::getDatabase()->getDocument('country', 'country1');
49194988
// Assert document does not contain non existing relation document.
49204989
$this->assertEquals(null, $country1Document->getAttribute('city'));
4921-
static::getDatabase()->updateDocument('country', 'country1', $doc->setAttribute('city', 'city1'));
4990+
static::getDatabase()->updateDocument('country', 'country1', (new Document($doc->getArrayCopy()))->setAttribute('city', 'city1'));
49224991
try {
49234992
static::getDatabase()->deleteDocument('country', 'country1');
49244993
$this->fail('Failed to throw exception');

0 commit comments

Comments
 (0)