Blame view

framework/tests/security/PermissionCheckboxSetFieldTest.php 3.17 KB
0084d336   Administrator   Importers CRUD
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  <?php
  /**
   * @package framework
   * @subpackage tests
   */
  class PermissionCheckboxSetFieldTest extends SapphireTest {
  	protected static $fixture_file = 'PermissionCheckboxSetFieldTest.yml';
  	
  	public function testHiddenPermissions() {
  		$f = new PermissionCheckboxSetField(
  			'Permissions',
  			'Permissions',
  			'Permission',
  			'GroupID'
  		);
  		$f->setHiddenPermissions(
  			array('NON-ADMIN')
  		);
  		$this->assertEquals(
  			$f->getHiddenPermissions(),
  			array('NON-ADMIN')
  		);
  		$this->assertContains('ADMIN', $f->Field());
  		$this->assertNotContains('NON-ADMIN', $f->Field());
  	}
  	
  	public function testSaveInto() {
  		$group = $this->objFromFixture('Group', 'group');  // tested group
  		$untouchable = $this->objFromFixture('Group', 'untouchable');  // group that should not change
  		
  		$field = new PermissionCheckboxSetField(
  			'Permissions',
  			'Permissions',
  			'Permission',
  			'GroupID',
  			$group
  		);
  
  		// get the number of permissions before we start
  		$baseCount = DataObject::get('Permission')->Count();
  		
  		// there are currently no permissions, save empty checkbox
  		$field->saveInto($group);
  		$group->flushCache();
  		$untouchable->flushCache();
  		$this->assertEquals($group->Permissions()->Count(), 0, 'The tested group has no permissions');
  
  		$this->assertEquals($untouchable->Permissions()->Count(), 1, 'The other group has one permission');
  		$this->assertEquals($untouchable->Permissions("\"Code\"='ADMIN'")->Count(), 1,
  			'The other group has ADMIN permission');
  
  		$this->assertEquals(DataObject::get('Permission')->Count(), $baseCount, 'There are no orphaned permissions');
  				
  		// add some permissions
  		$field->setValue(array(
  			'ADMIN'=>true,
  			'NON-ADMIN'=>true
  		));
  
  		$field->saveInto($group);
  		$group->flushCache();
  		$untouchable->flushCache();
  		$this->assertEquals($group->Permissions()->Count(), 2,
  			'The tested group has two permissions permission');
  		$this->assertEquals($group->Permissions("\"Code\"='ADMIN'")->Count(), 1,
  			'The tested group has ADMIN permission');
  		$this->assertEquals($group->Permissions("\"Code\"='NON-ADMIN'")->Count(), 1,
  			'The tested group has CMS_ACCESS_AssetAdmin permission');
  
  		$this->assertEquals($untouchable->Permissions()->Count(), 1,
  			'The other group has one permission');
  		$this->assertEquals($untouchable->Permissions("\"Code\"='ADMIN'")->Count(), 1,
  			'The other group has ADMIN permission');
  
  		$this->assertEquals(DataObject::get('Permission')->Count(), $baseCount+2,
  			'There are no orphaned permissions');
  		
  		// remove permission
  		$field->setValue(array(
  			'ADMIN'=>true,
  		));
  
  		$field->saveInto($group);
  		$group->flushCache();
  		$untouchable->flushCache();
  		$this->assertEquals($group->Permissions()->Count(), 1,
  			'The tested group has 1 permission');
  		$this->assertEquals($group->Permissions("\"Code\"='ADMIN'")->Count(), 1,
  			'The tested group has ADMIN permission');
  
  		$this->assertEquals($untouchable->Permissions()->Count(), 1,
  			'The other group has one permission');
  		$this->assertEquals($untouchable->Permissions("\"Code\"='ADMIN'")->Count(), 1,
  			'The other group has ADMIN permission');
  
  		$this->assertEquals(DataObject::get('Permission')->Count(), $baseCount+1,
  			'There are no orphaned permissions');
  	}
  }