Permalink
Browse files

BUGFIX Less fragile 'newness' check on Group->Code in Group->onBefore…

…Write() (fixes #6595)
  • Loading branch information...
chillu committed Apr 14, 2011
1 parent d02c3cb commit d5efacbf56120b6546d73af1892718fce8ced510
Showing with 24 additions and 2 deletions.
  1. +5 −2 security/Group.php
  2. +19 −0 tests/security/GroupTest.php
View
@@ -331,8 +331,11 @@ public function setCode($val){
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);
}
}
@@ -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
*/

0 comments on commit d5efacb

Please sign in to comment.