|
6 | 6 | use Utopia\Database\Database; |
7 | 7 | use Utopia\Database\Document; |
8 | 8 | use Utopia\Database\Exception\Restricted as RestrictedException; |
| 9 | +use Utopia\Database\Exception\Structure; |
9 | 10 | use Utopia\Database\Helpers\ID; |
10 | 11 | use Utopia\Database\Helpers\Permission; |
11 | 12 | use Utopia\Database\Helpers\Role; |
@@ -1595,4 +1596,89 @@ public function testDeleteBulkDocumentsManyToManyRelationship(): void |
1595 | 1596 | $this->getDatabase()->deleteDocuments('bulk_delete_person_m2m'); |
1596 | 1597 | $this->assertCount(0, $this->getDatabase()->find('bulk_delete_person_m2m')); |
1597 | 1598 | } |
| 1599 | + public function testUpdateParentAndChild_ManyToMany(): void |
| 1600 | + { |
| 1601 | + /** @var Database $database */ |
| 1602 | + $database = static::getDatabase(); |
| 1603 | + |
| 1604 | + if ( |
| 1605 | + !$database->getAdapter()->getSupportForRelationships() || |
| 1606 | + !$database->getAdapter()->getSupportForBatchOperations() |
| 1607 | + ) { |
| 1608 | + $this->expectNotToPerformAssertions(); |
| 1609 | + return; |
| 1610 | + } |
| 1611 | + |
| 1612 | + $parentCollection = 'parent_combined_m2m'; |
| 1613 | + $childCollection = 'child_combined_m2m'; |
| 1614 | + |
| 1615 | + $database->createCollection($parentCollection); |
| 1616 | + $database->createCollection($childCollection); |
| 1617 | + |
| 1618 | + $database->createAttribute($parentCollection, 'name', Database::VAR_STRING, 255, true); |
| 1619 | + $database->createAttribute($childCollection, 'name', Database::VAR_STRING, 255, true); |
| 1620 | + $database->createAttribute($childCollection, 'parentNumber', Database::VAR_INTEGER, 0, false); |
| 1621 | + |
| 1622 | + |
| 1623 | + $database->createRelationship( |
| 1624 | + collection: $parentCollection, |
| 1625 | + relatedCollection: $childCollection, |
| 1626 | + type: Database::RELATION_MANY_TO_MANY, |
| 1627 | + id: 'parentNumber' |
| 1628 | + ); |
| 1629 | + |
| 1630 | + $database->createDocument($parentCollection, new Document([ |
| 1631 | + '$id' => 'parent1', |
| 1632 | + '$permissions' => [ |
| 1633 | + Permission::read(Role::any()), |
| 1634 | + Permission::update(Role::any()), |
| 1635 | + Permission::delete(Role::any()), |
| 1636 | + ], |
| 1637 | + 'name' => 'Parent 1', |
| 1638 | + ])); |
| 1639 | + |
| 1640 | + $database->createDocument($childCollection, new Document([ |
| 1641 | + '$id' => 'child1', |
| 1642 | + '$permissions' => [ |
| 1643 | + Permission::read(Role::any()), |
| 1644 | + Permission::update(Role::any()), |
| 1645 | + Permission::delete(Role::any()), |
| 1646 | + ], |
| 1647 | + 'name' => 'Child 1', |
| 1648 | + 'parentNumber' => null, |
| 1649 | + ])); |
| 1650 | + |
| 1651 | + $database->updateDocuments( |
| 1652 | + $parentCollection, |
| 1653 | + new Document(['name' => 'Parent 1 Updated']), |
| 1654 | + [Query::equal('$id', ['parent1'])] |
| 1655 | + ); |
| 1656 | + |
| 1657 | + $parentDoc = $database->getDocument($parentCollection, 'parent1'); |
| 1658 | + $this->assertEquals('Parent 1 Updated', $parentDoc->getAttribute('name'), 'Parent should be updated'); |
| 1659 | + |
| 1660 | + $childDoc = $database->getDocument($childCollection, 'child1'); |
| 1661 | + $this->assertEquals('Child 1', $childDoc->getAttribute('name'), 'Child should remain unchanged'); |
| 1662 | + |
| 1663 | + // invalid update to child |
| 1664 | + try { |
| 1665 | + $database->updateDocuments( |
| 1666 | + $childCollection, |
| 1667 | + new Document(['parentNumber' => 'not-a-number']), |
| 1668 | + [Query::equal('$id', ['child1'])] |
| 1669 | + ); |
| 1670 | + $this->fail('Expected exception was not thrown for invalid parentNumber type'); |
| 1671 | + } catch (\Throwable $e) { |
| 1672 | + $this->assertInstanceOf(Structure::class, $e); |
| 1673 | + } |
| 1674 | + |
| 1675 | + // parent remains unaffected |
| 1676 | + $parentDocAfter = $database->getDocument($parentCollection, 'parent1'); |
| 1677 | + $this->assertEquals('Parent 1 Updated', $parentDocAfter->getAttribute('name'), 'Parent should not be affected by failed child update'); |
| 1678 | + |
| 1679 | + $database->deleteCollection($parentCollection); |
| 1680 | + $database->deleteCollection($childCollection); |
| 1681 | + } |
| 1682 | + |
| 1683 | + |
1598 | 1684 | } |
0 commit comments