Skip to content

Commit 102865b

Browse files
Add tests to verify parent document deletion restrictions in relationship scenarios
1 parent c0011c4 commit 102865b

4 files changed

Lines changed: 236 additions & 0 deletions

File tree

tests/e2e/Adapter/Scopes/Relationships/ManyToManyTests.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,4 +1595,63 @@ public function testDeleteBulkDocumentsManyToManyRelationship(): void
15951595
$this->getDatabase()->deleteDocuments('bulk_delete_person_m2m');
15961596
$this->assertCount(0, $this->getDatabase()->find('bulk_delete_person_m2m'));
15971597
}
1598+
public function testDeleteDocumentsRelationshipErrorDoesNotDeleteParent_ManyToMany(): void
1599+
{
1600+
/** @var Database $database */
1601+
$database = static::getDatabase();
1602+
1603+
if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForBatchOperations()) {
1604+
$this->expectNotToPerformAssertions();
1605+
return;
1606+
}
1607+
1608+
$parentCollection = 'parent_relationship_error_many_to_many';
1609+
$childCollection = 'child_relationship_error_many_to_many';
1610+
1611+
$database->createCollection($parentCollection);
1612+
$database->createCollection($childCollection);
1613+
$database->createAttribute($parentCollection, 'name', Database::VAR_STRING, 255, true);
1614+
$database->createAttribute($childCollection, 'name', Database::VAR_STRING, 255, true);
1615+
1616+
$database->createRelationship(
1617+
collection: $parentCollection,
1618+
relatedCollection: $childCollection,
1619+
type: Database::RELATION_MANY_TO_MANY,
1620+
onDelete: Database::RELATION_MUTATE_RESTRICT
1621+
);
1622+
1623+
$parent = $database->createDocument($parentCollection, new Document([
1624+
'$id' => 'parent1',
1625+
'$permissions' => [
1626+
Permission::read(Role::any()),
1627+
Permission::update(Role::any()),
1628+
Permission::delete(Role::any()),
1629+
],
1630+
'name' => 'Parent 1',
1631+
$childCollection => [
1632+
[
1633+
'$id' => 'child1',
1634+
'$permissions' => [
1635+
Permission::read(Role::any()),
1636+
Permission::update(Role::any()),
1637+
Permission::delete(Role::any()),
1638+
],
1639+
'name' => 'Child 1',
1640+
]
1641+
]
1642+
]));
1643+
1644+
try {
1645+
$database->deleteDocuments($parentCollection, [Query::equal('$id', ['parent1'])]);
1646+
$this->fail('Expected exception was not thrown');
1647+
} catch (RestrictedException $e) {
1648+
$this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage());
1649+
}
1650+
$parentDoc = $database->getDocument($parentCollection, 'parent1');
1651+
$childDoc = $database->getDocument($childCollection, 'child1');
1652+
$this->assertFalse($parentDoc->isEmpty(), 'Parent should not be deleted');
1653+
$this->assertFalse($childDoc->isEmpty(), 'Child should not be deleted');
1654+
$database->deleteCollection($parentCollection);
1655+
$database->deleteCollection($childCollection);
1656+
}
15981657
}

tests/e2e/Adapter/Scopes/Relationships/ManyToOneTests.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,4 +1677,64 @@ public function testDeleteBulkDocumentsManyToOneRelationship(): void
16771677
$this->getDatabase()->deleteDocuments('bulk_delete_person_m2o');
16781678
$this->assertCount(0, $this->getDatabase()->find('bulk_delete_person_m2o'));
16791679
}
1680+
1681+
public function testDeleteDocumentsRelationshipErrorDoesNotDeleteParent_ManyToOne(): void
1682+
{
1683+
/** @var Database $database */
1684+
$database = static::getDatabase();
1685+
1686+
if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForBatchOperations()) {
1687+
$this->expectNotToPerformAssertions();
1688+
return;
1689+
}
1690+
1691+
$parentCollection = 'parent_relationship_error_many_to_one';
1692+
$childCollection = 'child_relationship_error_many_to_one';
1693+
1694+
$database->createCollection($parentCollection);
1695+
$database->createCollection($childCollection);
1696+
$database->createAttribute($parentCollection, 'name', Database::VAR_STRING, 255, true);
1697+
$database->createAttribute($childCollection, 'name', Database::VAR_STRING, 255, true);
1698+
1699+
$database->createRelationship(
1700+
collection: $childCollection,
1701+
relatedCollection: $parentCollection,
1702+
type: Database::RELATION_MANY_TO_ONE,
1703+
onDelete: Database::RELATION_MUTATE_RESTRICT
1704+
);
1705+
1706+
$parent = $database->createDocument($parentCollection, new Document([
1707+
'$id' => 'parent1',
1708+
'$permissions' => [
1709+
Permission::read(Role::any()),
1710+
Permission::update(Role::any()),
1711+
Permission::delete(Role::any()),
1712+
],
1713+
'name' => 'Parent 1',
1714+
]));
1715+
1716+
$child = $database->createDocument($childCollection, new Document([
1717+
'$id' => 'child1',
1718+
'$permissions' => [
1719+
Permission::read(Role::any()),
1720+
Permission::update(Role::any()),
1721+
Permission::delete(Role::any()),
1722+
],
1723+
'name' => 'Child 1',
1724+
$parentCollection => 'parent1'
1725+
]));
1726+
1727+
try {
1728+
$database->deleteDocuments($parentCollection, [Query::equal('$id', ['parent1'])]);
1729+
$this->fail('Expected exception was not thrown');
1730+
} catch (RestrictedException $e) {
1731+
$this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage());
1732+
}
1733+
$parentDoc = $database->getDocument($parentCollection, 'parent1');
1734+
$childDoc = $database->getDocument($childCollection, 'child1');
1735+
$this->assertFalse($parentDoc->isEmpty(), 'Parent should not be deleted');
1736+
$this->assertFalse($childDoc->isEmpty(), 'Child should not be deleted');
1737+
$database->deleteCollection($parentCollection);
1738+
$database->deleteCollection($childCollection);
1739+
}
16801740
}

tests/e2e/Adapter/Scopes/Relationships/OneToManyTests.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,4 +2072,63 @@ public function testOneToManyAndManyToOneDeleteRelationship(): void
20722072
$this->assertCount(0, $relation2->getAttribute('attributes'));
20732073
$this->assertCount(0, $relation2->getAttribute('indexes'));
20742074
}
2075+
public function testDeleteDocumentsRelationshipErrorDoesNotDeleteParent_OneToMany(): void
2076+
{
2077+
/** @var Database $database */
2078+
$database = static::getDatabase();
2079+
2080+
if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForBatchOperations()) {
2081+
$this->expectNotToPerformAssertions();
2082+
return;
2083+
}
2084+
2085+
$parentCollection = 'parent_relationship_error_one_to_many';
2086+
$childCollection = 'child_relationship_error_one_to_many';
2087+
2088+
$database->createCollection($parentCollection);
2089+
$database->createCollection($childCollection);
2090+
$database->createAttribute($parentCollection, 'name', Database::VAR_STRING, 255, true);
2091+
$database->createAttribute($childCollection, 'name', Database::VAR_STRING, 255, true);
2092+
2093+
$database->createRelationship(
2094+
collection: $parentCollection,
2095+
relatedCollection: $childCollection,
2096+
type: Database::RELATION_ONE_TO_MANY,
2097+
onDelete: Database::RELATION_MUTATE_RESTRICT
2098+
);
2099+
2100+
$parent = $database->createDocument($parentCollection, new Document([
2101+
'$id' => 'parent1',
2102+
'$permissions' => [
2103+
Permission::read(Role::any()),
2104+
Permission::update(Role::any()),
2105+
Permission::delete(Role::any()),
2106+
],
2107+
'name' => 'Parent 1',
2108+
$childCollection => [
2109+
[
2110+
'$id' => 'child1',
2111+
'$permissions' => [
2112+
Permission::read(Role::any()),
2113+
Permission::update(Role::any()),
2114+
Permission::delete(Role::any()),
2115+
],
2116+
'name' => 'Child 1',
2117+
]
2118+
]
2119+
]));
2120+
2121+
try {
2122+
$database->deleteDocuments($parentCollection, [Query::equal('$id', ['parent1'])]);
2123+
$this->fail('Expected exception was not thrown');
2124+
} catch (RestrictedException $e) {
2125+
$this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage());
2126+
}
2127+
$parentDoc = $database->getDocument($parentCollection, 'parent1');
2128+
$childDoc = $database->getDocument($childCollection, 'child1');
2129+
$this->assertFalse($parentDoc->isEmpty(), 'Parent should not be deleted');
2130+
$this->assertFalse($childDoc->isEmpty(), 'Child should not be deleted');
2131+
$database->deleteCollection($parentCollection);
2132+
$database->deleteCollection($childCollection);
2133+
}
20752134
}

tests/e2e/Adapter/Scopes/Relationships/OneToOneTests.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,4 +2289,62 @@ public function testDeleteTwoWayRelationshipFromChild(): void
22892289

22902290
$this->assertEquals(true, $junction->isEmpty());
22912291
}
2292+
2293+
public function testDeleteDocumentsRelationshipErrorDoesNotDeleteParent_OneToOne(): void
2294+
{
2295+
/** @var Database $database */
2296+
$database = static::getDatabase();
2297+
2298+
if (!$database->getAdapter()->getSupportForRelationships() || !$database->getAdapter()->getSupportForBatchOperations()) {
2299+
$this->expectNotToPerformAssertions();
2300+
return;
2301+
}
2302+
2303+
$parentCollection = 'parent_relationship_error_one_to_one';
2304+
$childCollection = 'child_relationship_error_one_to_one';
2305+
2306+
$database->createCollection($parentCollection);
2307+
$database->createCollection($childCollection);
2308+
$database->createAttribute($parentCollection, 'name', Database::VAR_STRING, 255, true);
2309+
$database->createAttribute($childCollection, 'name', Database::VAR_STRING, 255, true);
2310+
2311+
$database->createRelationship(
2312+
collection: $parentCollection,
2313+
relatedCollection: $childCollection,
2314+
type: Database::RELATION_ONE_TO_ONE,
2315+
onDelete: Database::RELATION_MUTATE_RESTRICT
2316+
);
2317+
2318+
$parent = $database->createDocument($parentCollection, new Document([
2319+
'$id' => 'parent1',
2320+
'$permissions' => [
2321+
Permission::read(Role::any()),
2322+
Permission::update(Role::any()),
2323+
Permission::delete(Role::any()),
2324+
],
2325+
'name' => 'Parent 1',
2326+
$childCollection => [
2327+
'$id' => 'child1',
2328+
'$permissions' => [
2329+
Permission::read(Role::any()),
2330+
Permission::update(Role::any()),
2331+
Permission::delete(Role::any()),
2332+
],
2333+
'name' => 'Child 1',
2334+
]
2335+
]));
2336+
2337+
try {
2338+
$database->deleteDocuments($parentCollection, [Query::equal('$id', ['parent1'])]);
2339+
$this->fail('Expected exception was not thrown');
2340+
} catch (RestrictedException $e) {
2341+
$this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage());
2342+
}
2343+
$parentDoc = $database->getDocument($parentCollection, 'parent1');
2344+
$childDoc = $database->getDocument($childCollection, 'child1');
2345+
$this->assertFalse($parentDoc->isEmpty(), 'Parent should not be deleted');
2346+
$this->assertFalse($childDoc->isEmpty(), 'Child should not be deleted');
2347+
$database->deleteCollection($parentCollection);
2348+
$database->deleteCollection($childCollection);
2349+
}
22922350
}

0 commit comments

Comments
 (0)