From d5efacbf56120b6546d73af1892718fce8ced510 Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Thu, 14 Apr 2011 21:38:07 +1200 Subject: [PATCH] BUGFIX Less fragile 'newness' check on Group->Code in Group->onBeforeWrite() (fixes #6595) --- security/Group.php | 7 +++++-- tests/security/GroupTest.php | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/security/Group.php b/security/Group.php index 375225e81..84d7b9870 100644 --- a/security/Group.php +++ b/security/Group.php @@ -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); } } diff --git a/tests/security/GroupTest.php b/tests/security/GroupTest.php index a16facd51..009511dee 100644 --- a/tests/security/GroupTest.php +++ b/tests/security/GroupTest.php @@ -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 */