Skip to content

Commit c0673c0

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

4 files changed

Lines changed: 224 additions & 0 deletions

File tree

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,4 +1595,61 @@ 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+
$database->createCollection('parent');
1609+
$database->createCollection('child');
1610+
$database->createAttribute('parent', 'name', Database::VAR_STRING, 255, true);
1611+
$database->createAttribute('child', 'name', Database::VAR_STRING, 255, true);
1612+
1613+
$database->createRelationship(
1614+
collection: 'parent',
1615+
relatedCollection: 'child',
1616+
type: Database::RELATION_MANY_TO_MANY,
1617+
onDelete: Database::RELATION_MUTATE_RESTRICT
1618+
);
1619+
1620+
$parent = $database->createDocument('parent', new Document([
1621+
'$id' => 'parent1',
1622+
'$permissions' => [
1623+
Permission::read(Role::any()),
1624+
Permission::update(Role::any()),
1625+
Permission::delete(Role::any()),
1626+
],
1627+
'name' => 'Parent 1',
1628+
'child' => [
1629+
[
1630+
'$id' => 'child1',
1631+
'$permissions' => [
1632+
Permission::read(Role::any()),
1633+
Permission::update(Role::any()),
1634+
Permission::delete(Role::any()),
1635+
],
1636+
'name' => 'Child 1',
1637+
]
1638+
]
1639+
]));
1640+
1641+
try {
1642+
$database->deleteDocument('parent', 'parent1');
1643+
$this->fail('Expected exception was not thrown');
1644+
} catch (RestrictedException $e) {
1645+
$this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage());
1646+
}
1647+
$parentDoc = $database->getDocument('parent', 'parent1');
1648+
$childDoc = $database->getDocument('child', 'child1');
1649+
$this->assertFalse($parentDoc->isEmpty(), 'Parent should not be deleted');
1650+
$this->assertFalse($childDoc->isEmpty(), 'Child should not be deleted');
1651+
1652+
$database->deleteCollection('parent');
1653+
$database->deleteCollection('child');
1654+
}
15981655
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,4 +1677,61 @@ 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+
$database->createCollection('parent');
1692+
$database->createCollection('child');
1693+
$database->createAttribute('parent', 'name', Database::VAR_STRING, 255, true);
1694+
$database->createAttribute('child', 'name', Database::VAR_STRING, 255, true);
1695+
1696+
$database->createRelationship(
1697+
collection: 'child',
1698+
relatedCollection: 'parent',
1699+
type: Database::RELATION_MANY_TO_ONE,
1700+
onDelete: Database::RELATION_MUTATE_RESTRICT
1701+
);
1702+
1703+
$parent = $database->createDocument('parent', new Document([
1704+
'$id' => 'parent1',
1705+
'$permissions' => [
1706+
Permission::read(Role::any()),
1707+
Permission::update(Role::any()),
1708+
Permission::delete(Role::any()),
1709+
],
1710+
'name' => 'Parent 1',
1711+
]));
1712+
1713+
$child = $database->createDocument('child', new Document([
1714+
'$id' => 'child1',
1715+
'$permissions' => [
1716+
Permission::read(Role::any()),
1717+
Permission::update(Role::any()),
1718+
Permission::delete(Role::any()),
1719+
],
1720+
'name' => 'Child 1',
1721+
'parent' => 'parent1'
1722+
]));
1723+
1724+
try {
1725+
$database->deleteDocument('parent', 'parent1');
1726+
$this->fail('Expected exception was not thrown');
1727+
} catch (RestrictedException $e) {
1728+
$this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage());
1729+
}
1730+
$parentDoc = $database->getDocument('parent', 'parent1');
1731+
$childDoc = $database->getDocument('child', 'child1');
1732+
$this->assertFalse($parentDoc->isEmpty(), 'Parent should not be deleted');
1733+
$this->assertFalse($childDoc->isEmpty(), 'Child should not be deleted');
1734+
$database->deleteCollection('parent');
1735+
$database->deleteCollection('child');
1736+
}
16801737
}

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,4 +2072,59 @@ 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+
$database->createCollection('parent');
2086+
$database->createCollection('child');
2087+
$database->createAttribute('parent', 'name', Database::VAR_STRING, 255, true);
2088+
$database->createAttribute('child', 'name', Database::VAR_STRING, 255, true);
2089+
2090+
$database->createRelationship(
2091+
collection: 'parent',
2092+
relatedCollection: 'child',
2093+
type: Database::RELATION_ONE_TO_MANY,
2094+
onDelete: Database::RELATION_MUTATE_RESTRICT
2095+
);
2096+
2097+
$parent = $database->createDocument('parent', new Document([
2098+
'$id' => 'parent1',
2099+
'$permissions' => [
2100+
Permission::read(Role::any()),
2101+
Permission::update(Role::any()),
2102+
Permission::delete(Role::any()),
2103+
],
2104+
'name' => 'Parent 1',
2105+
'child' => [
2106+
[
2107+
'$id' => 'child1',
2108+
'$permissions' => [
2109+
Permission::read(Role::any()),
2110+
Permission::update(Role::any()),
2111+
Permission::delete(Role::any()),
2112+
],
2113+
'name' => 'Child 1',
2114+
]
2115+
]
2116+
]));
2117+
2118+
try {
2119+
$database->deleteDocument('parent', 'parent1');
2120+
} catch (RestrictedException $e) {
2121+
$this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage());
2122+
}
2123+
$parentDoc = $database->getDocument('parent', 'parent1');
2124+
$childDoc = $database->getDocument('child', 'child1');
2125+
$this->assertFalse($parentDoc->isEmpty(), 'Parent should not be deleted');
2126+
$this->assertFalse($childDoc->isEmpty(), 'Child should not be deleted');
2127+
$database->deleteCollection('parent');
2128+
$database->deleteCollection('child');
2129+
}
20752130
}

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,4 +2289,59 @@ 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+
$database->createCollection('parent');
2304+
$database->createCollection('child');
2305+
$database->createAttribute('parent', 'name', Database::VAR_STRING, 255, true);
2306+
$database->createAttribute('child', 'name', Database::VAR_STRING, 255, true);
2307+
2308+
$database->createRelationship(
2309+
collection: 'parent',
2310+
relatedCollection: 'child',
2311+
type: Database::RELATION_ONE_TO_ONE,
2312+
onDelete: Database::RELATION_MUTATE_RESTRICT
2313+
);
2314+
2315+
$parent = $database->createDocument('parent', new Document([
2316+
'$id' => 'parent1',
2317+
'$permissions' => [
2318+
Permission::read(Role::any()),
2319+
Permission::update(Role::any()),
2320+
Permission::delete(Role::any()),
2321+
],
2322+
'name' => 'Parent 1',
2323+
'child' => [
2324+
'$id' => 'child1',
2325+
'$permissions' => [
2326+
Permission::read(Role::any()),
2327+
Permission::update(Role::any()),
2328+
Permission::delete(Role::any()),
2329+
],
2330+
'name' => 'Child 1',
2331+
]
2332+
]));
2333+
2334+
try {
2335+
$database->deleteDocument('parent', 'parent1');
2336+
$this->fail('Expected exception was not thrown');
2337+
} catch (RestrictedException $e) {
2338+
$this->assertEquals('Cannot delete document because it has at least one related document.', $e->getMessage());
2339+
}
2340+
$parentDoc = $database->getDocument('parent', 'parent1');
2341+
$childDoc = $database->getDocument('child', 'child1');
2342+
$this->assertFalse($parentDoc->isEmpty(), 'Parent should not be deleted');
2343+
$this->assertFalse($childDoc->isEmpty(), 'Child should not be deleted');
2344+
$database->deleteCollection('parent');
2345+
$database->deleteCollection('child');
2346+
}
22922347
}

0 commit comments

Comments
 (0)