diff --git a/core/model/DataObject.php b/core/model/DataObject.php index 39b0f673b..8f47dc1cc 100644 --- a/core/model/DataObject.php +++ b/core/model/DataObject.php @@ -392,10 +392,10 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity * @return string User friendly singular name of this DataObject */ function singular_name() { - $name = $this->stat('singular_name'); - if(!$name) { - $name = ucwords(trim(strtolower(ereg_replace('([A-Z])',' \\1',$this->class)))); + if(!$name = $this->stat('singular_name')) { + $name = ucwords(trim(strtolower(preg_replace('/_?([A-Z])/', ' $1', $this->class)))); } + return $name; } diff --git a/tests/DataObjectTest.php b/tests/DataObjectTest.php index 3c54bb316..08ed53139 100644 --- a/tests/DataObjectTest.php +++ b/tests/DataObjectTest.php @@ -641,7 +641,26 @@ class DataObjectTest extends SapphireTest { 'Position' => 'Varchar(100)' )); } - + + /** + * Tests that singular_name() generates sensible defaults. + */ + public function testSingularName() { + $assertions = array ( + 'DataObjectTest_Player' => 'Data Object Test Player', + 'DataObjectTest_Team' => 'Data Object Test Team', + 'DataObjectTest_WithDefaults' => 'Data Object Test With Defaults' + ); + + foreach($assertions as $class => $expectedSingularName) { + $this->assertEquals ( + $expectedSingularName, + singleton($class)->singular_name(), + "Assert that the singular_name for '$class' is correct." + ); + } + } + } class DataObjectTest_Player extends Member implements TestOnly {