BUG Prevent unnecessary reconstruction of ClassName field after default records are generated

This commit is contained in:
Damian Mooyman 2014-03-04 11:46:53 +13:00
parent ccb791995e
commit 53b5adbcd9
2 changed files with 52 additions and 1 deletions

View File

@ -237,7 +237,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$db = DB::getConn(); $db = DB::getConn();
if($db->hasField($class, 'ClassName')) { if($db->hasField($class, 'ClassName')) {
$existing = $db->query("SELECT DISTINCT \"ClassName\" FROM \"$class\"")->column(); $existing = $db->query("SELECT DISTINCT \"ClassName\" FROM \"$class\"")->column();
$classNames = array_unique(array_merge($existing, $classNames)); $classNames = array_unique(array_merge($classNames, $existing));
} }
self::$classname_spec_cache[$class] = "Enum('" . implode(', ', $classNames) . "')"; self::$classname_spec_cache[$class] = "Enum('" . implode(', ', $classNames) . "')";

View File

@ -124,6 +124,57 @@ class DataObjectSchemaGenerationTest extends SapphireTest {
// Restore old indexes // Restore old indexes
Config::unnest(); Config::unnest();
} }
/**
* Tests the generation of the ClassName spec and ensure it's not unnecessarily influenced
* by the order of classnames of existing records
*/
public function testClassNameSpecGeneration() {
// Test with blank entries
DataObject::clear_classname_spec_cache();
$fields = DataObject::database_fields('DataObjectSchemaGenerationTest_DO');
$this->assertEquals(
"Enum('DataObjectSchemaGenerationTest_DO, DataObjectSchemaGenerationTest_IndexDO')",
$fields['ClassName']
);
// Test with instance of subclass
$item1 = new DataObjectSchemaGenerationTest_IndexDO();
$item1->write();
DataObject::clear_classname_spec_cache();
$fields = DataObject::database_fields('DataObjectSchemaGenerationTest_DO');
$this->assertEquals(
"Enum('DataObjectSchemaGenerationTest_DO, DataObjectSchemaGenerationTest_IndexDO')",
$fields['ClassName']
);
$item1->delete();
// Test with instance of main class
$item2 = new DataObjectSchemaGenerationTest_DO();
$item2->write();
DataObject::clear_classname_spec_cache();
$fields = DataObject::database_fields('DataObjectSchemaGenerationTest_DO');
$this->assertEquals(
"Enum('DataObjectSchemaGenerationTest_DO, DataObjectSchemaGenerationTest_IndexDO')",
$fields['ClassName']
);
$item2->delete();
// Test with instances of both classes
$item1 = new DataObjectSchemaGenerationTest_IndexDO();
$item1->write();
$item2 = new DataObjectSchemaGenerationTest_DO();
$item2->write();
DataObject::clear_classname_spec_cache();
$fields = DataObject::database_fields('DataObjectSchemaGenerationTest_DO');
$this->assertEquals(
"Enum('DataObjectSchemaGenerationTest_DO, DataObjectSchemaGenerationTest_IndexDO')",
$fields['ClassName']
);
$item1->delete();
$item2->delete();
}
} }
class DataObjectSchemaGenerationTest_DO extends DataObject implements TestOnly { class DataObjectSchemaGenerationTest_DO extends DataObject implements TestOnly {