diff --git a/src/Dev/FixtureFactory.php b/src/Dev/FixtureFactory.php index 9016ddb3b..d801be84a 100644 --- a/src/Dev/FixtureFactory.php +++ b/src/Dev/FixtureFactory.php @@ -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]); + } } } } diff --git a/tests/php/Dev/FixtureFactoryTest.php b/tests/php/Dev/FixtureFactoryTest.php index e51ee697f..a816c1611 100644 --- a/tests/php/Dev/FixtureFactoryTest.php +++ b/tests/php/Dev/FixtureFactoryTest.php @@ -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()