Completed
Push — CI ( ee6bd7...0f01dd )
by Adam
22:32
created

SecurityGroupTest   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 489
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 32
lcom 2
cbo 3
dl 0
loc 489
rs 9.6
c 1
b 0
f 1

23 Methods

Rating   Name   Duplication   Size   Complexity  
A testSecurityGroup() 0 14 1
A testgetGroupWhere() 0 18 1
A testgetGroupUsersWhere() 0 10 1
A testgetGroupJoin() 0 17 1
A testgetGroupUsersJoin() 0 10 1
A testgroupHasAccess() 0 18 1
A testinherit() 0 22 2
A testassign_default_groups() 0 21 2
A testinherit_creator() 0 20 2
A testinherit_assigned() 0 22 2
A testinherit_parent() 0 22 2
A testinherit_parentQuery() 0 20 2
A testinheritOne() 0 13 1
A testgetMembershipCount() 0 13 1
B testSaveAndRetrieveAndRemoveDefaultGroups() 0 42 2
A testgetSecurityModules() 0 54 1
A testgetLinkName() 0 20 1
A testaddGroupToRecord() 0 19 2
A testremoveGroupFromRecord() 0 19 2
A testgetUserSecurityGroups() 0 14 1
A testgetAllSecurityGroups() 0 14 1
A testgetMembers() 0 14 1
A testgetPrimaryGroupID() 0 14 1
1
<?php
2
3
4
class SecurityGroupTest extends PHPUnit_Framework_TestCase {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
6
7
	public function testSecurityGroup(){
8
9
		//execute the contructor and check for the Object type and  attributes
10
		$securityGroup = new SecurityGroup();
11
		
12
		$this->assertInstanceOf('SecurityGroup',$securityGroup);
13
		$this->assertInstanceOf('Basic',$securityGroup);
14
		$this->assertInstanceOf('SugarBean',$securityGroup);
15
			
16
		$this->assertAttributeEquals('securitygroups', 'table_name', $securityGroup);
17
		$this->assertAttributeEquals('SecurityGroups', 'module_dir', $securityGroup);
18
		$this->assertAttributeEquals('SecurityGroup', 'object_name', $securityGroup);
19
		
20
	}
21
22
23
	public function testgetGroupWhere()
24
	{
25
		error_reporting(E_ERROR | E_PARSE);
26
		
27
		$securityGroup = new SecurityGroup();
28
		
29
		//test with securitygroups module
30
		$expected = " securitygroups.id in (\n                select secg.id from securitygroups secg\n                inner join securitygroups_users secu on secg.id = secu.securitygroup_id and secu.deleted = 0\n                    and secu.user_id = '1'\n                where secg.deleted = 0\n            )";
31
		$actual = $securityGroup->getGroupWhere("securitygroups","SecurityGroups",1);
32
		$this->assertSame($expected,$actual);
33
		
34
		
35
		//test with //test with securitygroups module module
36
		$expected = " EXISTS (SELECT  1\n                  FROM    securitygroups secg\n                          INNER JOIN securitygroups_users secu \n                            ON secg.id = secu.securitygroup_id \n                               AND secu.deleted = 0 \n                               AND secu.user_id = '1'\n                          INNER JOIN securitygroups_records secr \n                            ON secg.id = secr.securitygroup_id \n                               AND secr.deleted = 0 \n                               AND secr.module = 'Users'\n                       WHERE   secr.record_id = users.id\n                               AND secg.deleted = 0) ";
37
		$actual = $securityGroup->getGroupWhere("users","Users",1);
38
		$this->assertSame($expected,$actual);
39
				
40
	}
41
42
	public function testgetGroupUsersWhere()
43
	{
44
		$securityGroup = new SecurityGroup();
45
		
46
		$expected = " users.id in (\n            select sec.user_id from securitygroups_users sec\n            inner join securitygroups_users secu on sec.securitygroup_id = secu.securitygroup_id and secu.deleted = 0\n                and secu.user_id = '1'\n            where sec.deleted = 0\n        )";
47
		$actual = $securityGroup->getGroupUsersWhere(1);
48
	
49
		$this->assertSame($expected,$actual);
50
		
51
	}
52
53
	public function testgetGroupJoin()
54
	{
55
56
		$securityGroup = new SecurityGroup();
57
		
58
		//test with securitygroups module
59
		$expected = " LEFT JOIN (select distinct secg.id from securitygroups secg\n    inner join securitygroups_users secu on secg.id = secu.securitygroup_id and secu.deleted = 0\n            and secu.user_id = '1'\n    where secg.deleted = 0\n) securitygroup_join on securitygroup_join.id = securitygroups.id ";
60
		$actual = $securityGroup->getGroupJoin("securitygroups","SecurityGroups",1);
61
		$this->assertSame($expected,$actual);
62
		
63
		
64
		//test with //test with securitygroups module
65
		$expected = " LEFT JOIN (select distinct secr.record_id as id from securitygroups secg\n    inner join securitygroups_users secu on secg.id = secu.securitygroup_id and secu.deleted = 0\n            and secu.user_id = '1'\n    inner join securitygroups_records secr on secg.id = secr.securitygroup_id and secr.deleted = 0\n             and secr.module = 'Users'\n    where secg.deleted = 0\n) securitygroup_join on securitygroup_join.id = users.id ";
66
		$actual = $securityGroup->getGroupJoin("users","Users",1);
67
		$this->assertSame($expected,$actual);
68
				
69
	}
70
71
72
	public function testgetGroupUsersJoin()
73
	{
74
75
		$securityGroup = new SecurityGroup();
76
		
77
		$expected = " LEFT JOIN (\n            select distinct sec.user_id as id from securitygroups_users sec\n            inner join securitygroups_users secu on sec.securitygroup_id = secu.securitygroup_id and secu.deleted = 0\n                and secu.user_id = '1'\n            where sec.deleted = 0\n        ) securitygroup_join on securitygroup_join.id = users.id ";
78
		$actual = $securityGroup->getGroupUsersJoin(1);
79
		$this->assertSame($expected,$actual);
80
81
	}
82
83
84
	public function testgroupHasAccess()
85
	{
86
		
87
		//test for listview
88
		$result = SecurityGroup::groupHasAccess('', '[SELECT_ID_LIST]');
89
		$this->assertEquals(True,$result);
90
		
91
		
92
		//test with invalid values
93
		$result = SecurityGroup::groupHasAccess('', '');
94
		$this->assertEquals(false,$result);
95
		
96
		
97
		//test with valid values
98
		$result = SecurityGroup::groupHasAccess('Users', '1');
99
		$this->assertEquals(false,$result);
100
				
101
	}
102
103
	public function testinherit()
104
	{
105
		//unset and reconnect Db to resolve mysqli fetch exeception
106
		global $db;
107
		unset ($db->database);
108
		$db->checkConnection();
109
		
110
		$account = new Account();
111
		$account->id = 1;
112
113
		$_REQUEST['subpanel_field_name'] = "id";
114
			
115
		//execute the method and test if it works and does not throws an exception.
116
		try {
117
			SecurityGroup::inherit($account,false);
118
			$this->assertTrue(true);
119
		}
120
		catch (Exception $e) {
121
			$this->fail();
122
		}
123
		
124
	}
125
126
	public function testassign_default_groups()
127
	{
128
	
129
		//unset and reconnect Db to resolve mysqli fetch exeception
130
		global $db;
131
		unset ($db->database);
132
		$db->checkConnection();
133
		
134
		$account = new Account();
135
		$account->id = 1;
136
		
137
		//execute the method and test if it works and does not throws an exception.
138
		try {
139
			SecurityGroup::assign_default_groups($account,false);
140
			$this->assertTrue(true);
141
		}
142
		catch (Exception $e) {
143
			$this->fail();
144
		}
145
		
146
	}
147
148
	public function testinherit_creator()
149
	{
150
		//unset and reconnect Db to resolve mysqli fetch exeception
151
		global $db;
152
		unset ($db->database);
153
		$db->checkConnection();
154
		
155
		$account = new Account();
156
		$account->id = 1;
157
		
158
		//execute the method and test if it works and does not throws an exception.
159
		try {
160
			SecurityGroup::inherit_creator($account,false);
161
			$this->assertTrue(true);
162
		}
163
		catch (Exception $e) {
164
			$this->fail();
165
		}
166
		
167
	}
168
169
	public function testinherit_assigned()
170
	{
171
172
		//unset and reconnect Db to resolve mysqli fetch exeception
173
		global $db;
174
		unset ($db->database);
175
		$db->checkConnection();
176
		
177
		$account = new Account();
178
		$account->id = 1;
179
		$account->assigned_user_id = 1;
180
		
181
		//execute the method and test if it works and does not throws an exception.
182
		try {
183
			SecurityGroup::inherit_assigned($account,false);
184
			$this->assertTrue(true);
185
		}
186
		catch (Exception $e) {
187
			$this->fail();
188
		}
189
		
190
	}
191
192
	public function testinherit_parent()
193
	{
194
195
		//unset and reconnect Db to resolve mysqli fetch exeception
196
		global $db;
197
		unset ($db->database);
198
		$db->checkConnection();
199
		
200
		$account = new Account();
201
		$account->id = 1;
202
		
203
		//execute the method and test if it works and does not throws an exception.
204
		try {
205
			SecurityGroup::inherit_parent($account,false);
206
			$this->assertTrue(true);
207
		}
208
		catch (Exception $e) {
209
			$this->fail();
210
		}
211
		
212
		
213
	}
214
215
	public function testinherit_parentQuery() 
216
	{
217
		//unset and reconnect Db to resolve mysqli fetch exeception
218
		global $db;
219
		unset ($db->database);
220
		$db->checkConnection();
221
		
222
		$account = new Account();
223
		$account->id = 1;
224
225
		//execute the method and test if it works and does not throws an exception.
226
		try {
227
			SecurityGroup::inherit_parentQuery($account, "Accounts", 1, 1 , $account->module_dir);
228
			$this->assertTrue(true);
229
		}
230
		catch (Exception $e) {
231
			$this->fail();
232
		}
233
		
234
	}
235
236
237
	public function testinheritOne() {
238
239
		//unset and reconnect Db to resolve mysqli fetch exeception
240
		global $db;
241
		unset ($db->database);
242
		$db->checkConnection();
243
		
244
		$securityGroup = new SecurityGroup();
245
		
246
		$result = $securityGroup->inheritOne(1, 1, "Accounts");
247
		$this->assertEquals(false, $result);
248
		
249
	}
250
251
252
	public function testgetMembershipCount() {
253
254
		//unset and reconnect Db to resolve mysqli fetch exeception
255
		global $db;
256
		unset ($db->database);
257
		$db->checkConnection();
258
		
259
		$securityGroup = new SecurityGroup();
260
		
261
		$result = $securityGroup->getMembershipCount("1");
262
		$this->assertEquals(0, $result);
263
			
264
	}
265
266
	
267
	public function testSaveAndRetrieveAndRemoveDefaultGroups() {
268
269
270
		//unset and reconnect Db to resolve mysqli fetch exeception
271
		global $db;
272
		unset ($db->database);
273
		$db->checkConnection();
274
		
275
		$securityGroup = new SecurityGroup();
276
		
277
		//create a security group first
278
		$securityGroup->name = "test";
279
		$securityGroup->save();
280
		
281
	
282
		//execute saveDefaultGroup method
283
		$securityGroup->saveDefaultGroup($securityGroup->id, "test_module");
284
		
285
		//execute retrieveDefaultGroups method
286
		$result = $securityGroup->retrieveDefaultGroups();
287
		
288
		//verify that default group is created
289
		$this->assertTrue(is_array($result));
290
		$this->assertGreaterThan(0, count($result));
291
292
		
293
		//execute removeDefaultGroup method for each default group			
294
		foreach($result as $key=>$value){			
295
			$securityGroup->removeDefaultGroup($key);
296
		}
297
		
298
		
299
		//retrieve back and verify that default securith groups are deleted 
300
		$result = $securityGroup->retrieveDefaultGroups();
301
		$this->assertEquals(0, count($result));
302
		
303
		
304
		//delete the security group as well for cleanup
305
		$securityGroup->mark_deleted($securityGroup->id);
306
		
307
308
	}
309
310
311
	public function testgetSecurityModules() {
312
313
		//unset and reconnect Db to resolve mysqli fetch exeception
314
		global $db;
315
		unset ($db->database);
316
		$db->checkConnection();
317
		
318
		$securityGroup = new SecurityGroup();
319
			
320
		$expected = array (
321
            'Meetings',
322
            'Cases',
323
            'AOS_Products',
324
            'Opportunities',
325
            'FP_Event_Locations',
326
            'Tasks',
327
            'jjwg_Markers',
328
            'EmailTemplates',
329
            'Campaigns',
330
            'jjwg_Areas',
331
            'Contacts',
332
            'AOS_Contracts',
333
            'AOS_Quotes',
334
            'Bugs',
335
            'Users',
336
            'Documents',
337
            'AOS_Invoices',
338
            'Notes',
339
            'AOW_WorkFlow',
340
            'ProspectLists',
341
            'AOK_KnowledgeBase',
342
            'AOS_PDF_Templates',
343
            'Calls',
344
            'Accounts',
345
            'Leads',
346
            'Emails',
347
            'ProjectTask',
348
            'Project',
349
            'FP_events',
350
            'AOR_Reports',
351
            'Prospects',
352
            'ACLRoles',
353
            'jjwg_Maps',
354
            'AOS_Product_Categories',
355
				);
356
		
357
		
358
		$actual = $securityGroup->getSecurityModules();
359
        $actualKeys = array_keys($actual);
360
        sort($expected);
361
        sort($actualKeys);
362
		$this->assertSame($expected,$actualKeys);
363
		
364
	}
365
366
367
	public function testgetLinkName() {
368
		
369
		//unset and reconnect Db to resolve mysqli fetch exeception
370
		global $db;
371
		unset ($db->database);
372
		$db->checkConnection();
373
		
374
		
375
		$securityGroup = new SecurityGroup();
376
		
377
		$result = $securityGroup->getLinkName("Accounts","Contacts");
378
		$this->assertEquals("contacts",$result);
379
		
380
		
381
		$result = $securityGroup->getLinkName("SecurityGroups","ACLRoles");
382
		$this->assertEquals("aclroles",$result);
383
		
384
		error_reporting(E_ALL);
385
		//error_reporting(E_ERROR | E_PARSE);
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
386
	}
387
388
389
	public function testaddGroupToRecord() 
390
	{
391
		//unset and reconnect Db to resolve mysqli fetch exeception
392
		global $db;
393
		unset ($db->database);
394
		$db->checkConnection();
395
		
396
		$securityGroup = new SecurityGroup();
397
		
398
		//execute the method and test if it works and does not throws an exception.
399
		try {
400
			$securityGroup->addGroupToRecord("Accounts", 1, 1);
401
			$this->assertTrue(true);
402
		}
403
		catch (Exception $e) {
404
			$this->fail();
405
		}
406
		
407
	}
408
409
410
	public function testremoveGroupFromRecord() 
411
	{
412
		//unset and reconnect Db to resolve mysqli fetch exeception
413
		global $db;
414
		unset ($db->database);
415
		$db->checkConnection();
416
		
417
		$securityGroup = new SecurityGroup();
418
		
419
		//execute the method and test if it works and does not throws an exception.
420
		try {
421
			$securityGroup->removeGroupFromRecord("Accounts", 1, 1);
422
			$this->assertTrue(true);
423
		}
424
		catch (Exception $e) {
425
			$this->fail();
426
		}
427
		
428
	}
429
430
431
	public function testgetUserSecurityGroups()
432
	{
433
		//unset and reconnect Db to resolve mysqli fetch exeception
434
		global $db;
435
		unset ($db->database);
436
		$db->checkConnection();
437
		
438
		$securityGroup = new SecurityGroup();
439
		
440
		$result = $securityGroup->getUserSecurityGroups("1");
441
		
442
		$this->assertTrue(is_array($result));
443
		
444
	}
445
446
	public function testgetAllSecurityGroups()
447
	{
448
		//unset and reconnect Db to resolve mysqli fetch exeception
449
		global $db;
450
		unset ($db->database);
451
		$db->checkConnection();
452
		
453
		$securityGroup = new SecurityGroup();
454
		
455
		$result = $securityGroup->getAllSecurityGroups();
456
		
457
		$this->assertTrue(is_array($result));
458
		
459
	}
460
461
	public function testgetMembers()
462
	{
463
		//unset and reconnect Db to resolve mysqli fetch exeception
464
		global $db;
465
		unset ($db->database);
466
		$db->checkConnection();
467
		
468
		$securityGroup = new SecurityGroup();
469
		
470
		$result = $securityGroup->getMembers();
471
		
472
		$this->assertTrue(is_array($result));
473
		
474
	}
475
476
477
	public function testgetPrimaryGroupID()
478
	{
479
		//unset and reconnect Db to resolve mysqli fetch exeception
480
		global $db;
481
		unset ($db->database);
482
		$db->checkConnection();
483
		
484
		$securityGroup = new SecurityGroup();
485
		
486
		$result = $securityGroup->getPrimaryGroupID();
487
		
488
		$this->assertEquals(null, $result);
489
		
490
	}
491
492
}
493
?>