silverstripe-framework/tests/php/ORM/CascadeDeleteTest.php

127 lines
3.8 KiB
PHP
Raw Normal View History

2017-08-08 06:46:34 +02:00
<?php
namespace SilverStripe\ORM\Tests;
use SilverStripe\Dev\SapphireTest;
/**
* Test cascade delete objects
*/
class CascadeDeleteTest extends SapphireTest
{
protected static $fixture_file = 'CascadeDeleteTest.yml';
protected static $extra_dataobjects = [
CascadeDeleteTest\ParentObject::class,
CascadeDeleteTest\ChildObject::class,
CascadeDeleteTest\GrandChildObject::class,
CascadeDeleteTest\RelatedObject::class,
];
public function testFindCascadeDeletes()
{
/** @var CascadeDeleteTest\ChildObject $child1 */
$child1 = $this->objFromFixture(CascadeDeleteTest\ChildObject::class, 'child1');
$this->assertDOSEquals(
[
[ 'Title' => 'Grandchild 1'],
[ 'Title' => 'Grandchild 2'],
],
$child1->findCascadeDeletes(true)
);
$this->assertDOSEquals(
[
[ 'Title' => 'Grandchild 1'],
[ 'Title' => 'Grandchild 2'],
],
$child1->findCascadeDeletes(false)
);
/** @var CascadeDeleteTest\ParentObject $parent1 */
$parent1 = $this->objFromFixture(CascadeDeleteTest\ParentObject::class, 'parent1');
$this->assertDOSEquals(
[
[ 'Title' => 'Child 1'],
[ 'Title' => 'Grandchild 1'],
[ 'Title' => 'Grandchild 2'],
],
$parent1->findCascadeDeletes(true)
);
$this->assertDOSEquals(
[
[ 'Title' => 'Child 1'],
],
$parent1->findCascadeDeletes(false)
);
}
public function testRecursiveDelete()
{
/** @var CascadeDeleteTest\ChildObject $child1 */
$child1 = $this->objFromFixture(CascadeDeleteTest\ChildObject::class, 'child1');
$child1->delete();
// Parent, related, and non-relational objects undeleted
$this->assertNotEmpty($this->objFromFixture(CascadeDeleteTest\ParentObject::class, 'parent1'));
// Related objects never deleted
$this->assertDOSEquals(
[
['Title' => 'Related 1'],
['Title' => 'Related 2'],
['Title' => 'Related 3'],
],
CascadeDeleteTest\RelatedObject::get()
);
// Ensure only remaining grandchild are those outside the relation
$this->assertDOSEquals(
[
['Title' => 'Grandchild 3'],
],
CascadeDeleteTest\GrandChildObject::get()
);
}
public function testDeepRecursiveDelete()
{
/** @var CascadeDeleteTest\ParentObject $parent1 */
$parent1 = $this->objFromFixture(CascadeDeleteTest\ParentObject::class, 'parent1');
$parent1->delete();
// Ensure affected cascading tables have expected content
$this->assertDOSEquals(
[
['Title' => 'Child 2'],
],
CascadeDeleteTest\ChildObject::get()
);
$this->assertDOSEquals(
[
['Title' => 'Grandchild 3'],
],
CascadeDeleteTest\GrandChildObject::get()
);
// Related objects never deleted
$this->assertDOSEquals(
[
['Title' => 'Related 1'],
['Title' => 'Related 2'],
['Title' => 'Related 3'],
],
CascadeDeleteTest\RelatedObject::get()
);
// Ensure that other parents which share cascade deleted objects have the correct result
/** @var CascadeDeleteTest\ChildObject $child2 */
$child2 = $this->objFromFixture(CascadeDeleteTest\ChildObject::class, 'child2');
$this->assertDOSEquals(
[
['Title' => 'Grandchild 3'],
],
$child2->Children()
);
}
}