BUGFIX Less fragile 'newness' check on Group->Code in Group->onBeforeWrite() (fixes #6595)

This commit is contained in:
Ingo Schommer 2011-04-14 21:38:07 +12:00
parent d02c3cbdba
commit d5efacbf56
2 changed files with 24 additions and 2 deletions

View File

@ -331,8 +331,11 @@ class Group extends DataObject {
function onBeforeWrite() {
parent::onBeforeWrite();
if(stripos($this->Code, _t('SecurityAdmin.NEWGROUPPREFIX','new-')) === 0) {
$this->setCode($this->Title);
// Only set code property when the group has a custom title, and no code exists.
// The "Code" attribute is usually treated as a more permanent identifier than database IDs
// in custom application logic, so can't be changed after its first set.
if(!$this->Code && $this->Title != _t('SecurityAdmin.NEWGROUP',"New Group")) {
if(!$this->Code) $this->setCode($this->Title);
}
}

View File

@ -4,8 +4,27 @@
* @subpackage tests
*/
class GroupTest extends FunctionalTest {
static $fixture_file = 'GroupTest.yml';
function testGroupCodeDefaultsToTitle() {
$g1 = new Group();
$g1->Title = "My Title";
$g1->write();
$this->assertEquals('my-title', $g1->Code, 'Custom title gets converted to code if none exists already');
$g2 = new Group();
$g2->Title = "My Title";
$g2->Code = "my-code";
$g2->write();
$this->assertEquals('my-code', $g2->Code, 'Custom attributes are not overwritten by Title field');
$g3 = new Group();
$g3->Title = _t('SecurityAdmin.NEWGROUP',"New Group");
$g3->write();
$this->assertNull($g3->Code, 'Default title doesnt trigger attribute setting');
}
/**
* Test the Group::map() function
*/