|
23 | 23 | use Utopia\Database\Validator\Authorization; |
24 | 24 | use Utopia\Database\Validator\Datetime as DatetimeValidator; |
25 | 25 | use Utopia\Database\Validator\Index; |
26 | | -use Utopia\Database\Validator\Query\Filter; |
27 | 26 | use Utopia\Database\Validator\Structure; |
28 | 27 | use Utopia\Validator\Range; |
29 | 28 | use Utopia\Database\Exception\Structure as StructureException; |
@@ -4415,6 +4414,76 @@ public function testWritePermissions(): void |
4415 | 4414 | $this->assertEquals('newCat', $docs[0]['type']); |
4416 | 4415 | } |
4417 | 4416 |
|
| 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 | + |
4418 | 4487 | // Relationships |
4419 | 4488 | public function testOneToOneOneWayRelationship(): void |
4420 | 4489 | { |
@@ -4913,12 +4982,12 @@ public function testOneToOneTwoWayRelationship(): void |
4913 | 4982 | $this->assertEquals('London', $country1->getAttribute('city')->getAttribute('name')); |
4914 | 4983 |
|
4915 | 4984 | // 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')); |
4917 | 4986 |
|
4918 | 4987 | $country1Document = static::getDatabase()->getDocument('country', 'country1'); |
4919 | 4988 | // Assert document does not contain non existing relation document. |
4920 | 4989 | $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')); |
4922 | 4991 | try { |
4923 | 4992 | static::getDatabase()->deleteDocument('country', 'country1'); |
4924 | 4993 | $this->fail('Failed to throw exception'); |
|
0 commit comments