Skip to content
This repository
Browse code

BUGFIX Nested Group records should be removed, along with the parent.

  • Loading branch information...
commit bd95bcaf6149b04ce4478d257098b469646f0f7d 1 parent 86b805d
Sean Harvey authored March 26, 2012
18  security/Group.php
@@ -337,17 +337,21 @@ function onBeforeWrite() {
337 337
 			if(!$this->Code) $this->setCode($this->Title);
338 338
 		}
339 339
 	}
340  
-	
341  
-	function onAfterDelete() {
342  
-		parent::onAfterDelete();
343  
-		
  340
+
  341
+	function onBeforeDelete() {
  342
+		parent::onBeforeDelete();
  343
+
  344
+		// if deleting this group, delete it's children as well
  345
+		foreach($this->Groups() as $group) {
  346
+			$group->delete();
  347
+		}
  348
+
344 349
 		// Delete associated permissions
345  
-		$permissions = $this->Permissions();
346  
-		foreach ( $permissions as $permission ) {
  350
+		foreach($this->Permissions() as $permission) {
347 351
 			$permission->delete();
348 352
 		}
349 353
 	}
350  
-	
  354
+
351 355
 	/**
352 356
 	 * Checks for permission-code CMS_ACCESS_SecurityAdmin.
353 357
 	 * If the group has ADMIN permissions, it requires the user to have ADMIN permissions as well.
22  tests/security/GroupTest.php
@@ -94,15 +94,6 @@ function testMemberGroupRelationForm() {
94 94
 
95 95
 	   }
96 96
 	
97  
-	function testDelete() {
98  
-		$adminGroup = $this->objFromFixture('Group', 'admingroup');
99  
-		
100  
-		$adminGroup->delete();
101  
-		
102  
-		$this->assertEquals(0, DataObject::get('Group', "\"ID\"={$adminGroup->ID}")->count(), 'Group is removed');
103  
-		$this->assertEquals(0, DataObject::get('Permission',"\"GroupID\"={$adminGroup->ID}")->count(), 'Permissions removed along with the group');
104  
-	}
105  
-	
106 97
 	function testCollateAncestorIDs() {
107 98
 		$parentGroup = $this->objFromFixture('Group', 'parentgroup');
108 99
 		$childGroup = $this->objFromFixture('Group', 'childgroup');
@@ -128,6 +119,19 @@ function testCollateAncestorIDs() {
128 119
 			'Orphaned nodes dont contain invalid parent IDs'
129 120
 		);
130 121
 	}
  122
+
  123
+	public function testDelete() {
  124
+		$group = $this->objFromFixture('Group', 'parentgroup');
  125
+		$groupID = $group->ID;
  126
+		$childGroupID = $this->idFromFixture('Group', 'childgroup');
  127
+		$group->delete();
  128
+
  129
+		$this->assertEquals(0, DataObject::get('Group', "\"ID\" = {$groupID}")->Count(), 'Group is removed');
  130
+		$this->assertEquals(0, DataObject::get('Permission', "\"GroupID\" = {$groupID}")->Count(), 'Permissions removed along with the group');
  131
+		$this->assertEquals(0, DataObject::get('Group', "\"ParentID\" = {$groupID}")->Count(), 'Child groups are removed');
  132
+		$this->assertEquals(0, DataObject::get('Group', "\"ParentID\" = {$childGroupID}")->Count(), 'Grandchild groups are removed');
  133
+	}
  134
+
131 135
 }
132 136
 
133 137
 class GroupTest_Member extends Member implements TestOnly {
5  tests/security/GroupTest.yml
@@ -6,6 +6,9 @@ Group:
6 6
    childgroup:
7 7
       Code: childgroup
8 8
       Parent: =>Group.parentgroup
  9
+   grandchildgroup:
  10
+      Code: grandchildgroup
  11
+      Parent: =>Group.childgroup
9 12
    group1:
10 13
       Title: Group 1
11 14
    group2:
@@ -26,4 +29,4 @@ GroupTest_Member:
26 29
 Permission:
27 30
    admincode:
28 31
       Code: ADMIN
29  
-      Group: =>Group.admingroup
  32
+      Group: =>Group.admingroup

0 notes on commit bd95bca

Please sign in to comment.
Something went wrong with that request. Please try again.