first pass RT4 merge, RT#13852
[freeside.git] / rt / t / api / groups.t
1 use strict;
2 use warnings;
3 use RT::Test nodata => 1, tests => 27;
4
5 RT::Group->AddRights(
6     'RTxGroupRight' => 'Just a right for testing rights',
7 );
8
9 {
10     my $g = RT::Group->new(RT->SystemUser);
11     my ($id, $msg) = $g->CreateUserDefinedGroup(Name => 'GroupsNotEqualTest');
12     ok ($id, "created group #". $g->id) or diag("error: $msg");
13
14     my $groups = RT::Groups->new(RT->SystemUser);
15     $groups->Limit( FIELD => 'id', OPERATOR => '!=', VALUE => $g->id );
16     $groups->LimitToUserDefinedGroups();
17     my $bug = grep $_->id == $g->id, @{$groups->ItemsArrayRef};
18     ok (!$bug, "didn't find group");
19 }
20
21
22 {
23     my $u = RT::User->new(RT->SystemUser);
24     my ($id, $msg) = $u->Create( Name => 'Membertests'. $$ );
25     ok ($id, 'created user') or diag "error: $msg";
26
27     my $g = RT::Group->new(RT->SystemUser);
28     ($id, $msg) = $g->CreateUserDefinedGroup(Name => 'Membertests');
29     ok ($id, $msg);
30
31     my ($aid, $amsg) =$g->AddMember($u->id);
32     ok ($aid, $amsg);
33     ok($g->HasMember($u->PrincipalObj),"G has member u");
34
35     my $groups = RT::Groups->new(RT->SystemUser);
36     $groups->LimitToUserDefinedGroups();
37     $groups->WithMember(PrincipalId => $u->id);
38     is ($groups->Count , 1,"found the 1 group - " . $groups->Count);
39     is ($groups->First->Id , $g->Id, "it's the right one");
40 }
41
42 no warnings qw/redefine once/;
43
44 my $q = RT::Queue->new(RT->SystemUser);
45 my ($id, $msg) =$q->Create( Name => 'GlobalACLTest');
46 ok ($id, $msg);
47
48 my $testuser = RT::User->new(RT->SystemUser);
49 ($id,$msg) = $testuser->Create(Name => 'JustAnAdminCc');
50 ok ($id,$msg);
51
52 my $global_admin_cc = RT::Group->new(RT->SystemUser);
53 $global_admin_cc->LoadSystemRoleGroup('AdminCc');
54 ok($global_admin_cc->id, "Found the global admincc group");
55 my $groups = RT::Groups->new(RT->SystemUser);
56 $groups->WithRight(Right => 'OwnTicket', Object => $q);
57 is($groups->Count, 1);
58 ($id, $msg) = $global_admin_cc->PrincipalObj->GrantRight(Right =>'OwnTicket', Object=> RT->System);
59 ok ($id,$msg);
60 ok (!$testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does not have the right to own tickets in the test queue");
61 ($id, $msg) = $q->AddWatcher(Type => 'AdminCc', PrincipalId => $testuser->id);
62 ok($id,$msg);
63 ok ($testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does have the right to own tickets now. thank god.");
64
65 $groups = RT::Groups->new(RT->SystemUser);
66 $groups->WithRight(Right => 'OwnTicket', Object => $q);
67 ok ($id,$msg);
68 is($groups->Count, 3);
69
70 my $RTxGroup = RT::Group->new(RT->SystemUser);
71 ($id, $msg) = $RTxGroup->CreateUserDefinedGroup( Name => 'RTxGroup', Description => "RTx extension group");
72 ok ($id,$msg);
73 is ($RTxGroup->id, $id, "group loaded");
74
75 my $RTxSysObj = {};
76 bless $RTxSysObj, 'RTx::System';
77 *RTx::System::Id = sub { 1; };
78 *RTx::System::id = *RTx::System::Id;
79 my $ace = RT::Record->new(RT->SystemUser);
80 $ace->Table('ACL');
81 $ace->_BuildTableAttributes unless ($RT::Record::_TABLE_ATTR->{ref($ace)});
82 ($id, $msg) = $ace->Create( PrincipalId => $RTxGroup->id, PrincipalType => 'Group', RightName => 'RTxGroupRight', ObjectType => 'RTx::System', ObjectId  => 1);
83 ok ($id, "ACL for RTxSysObj created");
84
85 my $RTxObj = {};
86 bless $RTxObj, 'RTx::System::Record';
87 *RTx::System::Record::Id = sub { 4; };
88 *RTx::System::Record::id = *RTx::System::Record::Id;
89
90 $groups = RT::Groups->new(RT->SystemUser);
91 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxSysObj);
92 is($groups->Count, 1, "RTxGroupRight found for RTxSysObj");
93
94 $groups = RT::Groups->new(RT->SystemUser);
95 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj);
96 is($groups->Count, 0, "RTxGroupRight not found for RTxObj");
97
98 $groups = RT::Groups->new(RT->SystemUser);
99 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj, EquivObjects => [ $RTxSysObj ]);
100 is($groups->Count, 1, "RTxGroupRight found for RTxObj using EquivObjects");
101
102 $ace = RT::Record->new(RT->SystemUser);
103 $ace->Table('ACL');
104 $ace->_BuildTableAttributes unless ($RT::Record::_TABLE_ATTR->{ref($ace)});
105 ($id, $msg) = $ace->Create( PrincipalId => $RTxGroup->id, PrincipalType => 'Group', RightName => 'RTxGroupRight', ObjectType => 'RTx::System::Record', ObjectId  => 5 );
106 ok ($id, "ACL for RTxObj created");
107
108 my $RTxObj2 = {};
109 bless $RTxObj2, 'RTx::System::Record';
110 *RTx::System::Record::Id = sub { 5; };
111
112 $groups = RT::Groups->new(RT->SystemUser);
113 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj2);
114 is($groups->Count, 1, "RTxGroupRight found for RTxObj2");
115
116 $groups = RT::Groups->new(RT->SystemUser);
117 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj2, EquivObjects => [ $RTxSysObj ]);
118 is($groups->Count, 1, "RTxGroupRight found for RTxObj2");