BUG Make regression in #7839 safer

This commit is contained in:
Damian Mooyman 2018-06-14 15:56:46 +12:00
parent 5fbfd8ff7f
commit d52c4dd602
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A
2 changed files with 20 additions and 7 deletions

View File

@ -201,15 +201,20 @@ class FixtureFactory
* If the $class argument is set, limit clearing to items of this class.
*
* @param string $limitToClass
* @param bool $metadata Clear internal mapping as well as data.
* Set to false by default since sometimes data is rolled back by translations.
*/
public function clear($limitToClass = null)
public function clear($limitToClass = null, $metadata = false)
{
$classes = ($limitToClass) ? array($limitToClass) : array_keys($this->fixtures);
foreach ($classes as $class) {
$ids = $this->fixtures[$class];
foreach ($ids as $id => $dbId) {
if (class_exists($class)) {
$class::get()->byId($dbId)->delete();
$instance = DataObject::get($class)->byId($dbId);
if ($instance) {
$instance->delete();
}
} else {
$table = $class;
$delete = new SQLDelete("\"$table\"", array(
@ -218,7 +223,9 @@ class FixtureFactory
$delete->execute();
}
unset($this->fixtures[$class][$id]);
if ($metadata) {
unset($this->fixtures[$class][$id]);
}
}
}
}

View File

@ -136,19 +136,25 @@ class FixtureFactoryTest extends SapphireTest
public function testClear()
{
$factory = new FixtureFactory();
$table = TestDataObject::singleton()->baseTable();
$obj1Id = $factory->createRaw(
TestDataObject::singleton()->baseTable(),
$table,
'one',
array('Name' => 'My Name')
);
$obj2 = $factory->createObject(TestDataObject::class, 'two');
// Clear models only
$factory->clear();
$this->assertFalse($factory->getId(TestDataObject::class, 'one'));
$this->assertEquals($obj1Id, $factory->getId($table, 'one'));
$this->assertNull(TestDataObject::get()->byID($obj1Id));
$this->assertFalse($factory->getId(TestDataObject::class, 'two'));
$this->assertEquals($obj2->ID, $factory->getId(TestDataObject::class, 'two'));
$this->assertNull(TestDataObject::get()->byID($obj2->ID));
// Force metadata clear
$factory->clear(null, true);
$this->assertFalse($factory->getId($table, 'one'));
$this->assertFalse($factory->getId(TestDataObject::class, 'two'));
}
public function testClearWithClass()