import rt 3.8.7
[freeside.git] / rt / t / api / ticket.t
1
2 use strict;
3 use warnings;
4 use RT;
5 use RT::Test tests => 87;
6
7
8 {
9
10 use_ok ('RT::Queue');
11 ok(my $testqueue = RT::Queue->new($RT::SystemUser));
12 ok($testqueue->Create( Name => 'ticket tests'));
13 isnt($testqueue->Id , 0);
14 use_ok('RT::CustomField');
15 ok(my $testcf = RT::CustomField->new($RT::SystemUser));
16 my ($ret, $cmsg) = $testcf->Create( Name => 'selectmulti',
17                     Queue => $testqueue->id,
18                                Type => 'SelectMultiple');
19 ok($ret,"Created the custom field - ".$cmsg);
20 ($ret,$cmsg) = $testcf->AddValue ( Name => 'Value1',
21                         SortOrder => '1',
22                         Description => 'A testing value');
23
24 ok($ret, "Added a value - ".$cmsg);
25
26 ok($testcf->AddValue ( Name => 'Value2',
27                         SortOrder => '2',
28                         Description => 'Another testing value'));
29 ok($testcf->AddValue ( Name => 'Value3',
30                         SortOrder => '3',
31                         Description => 'Yet Another testing value'));
32                        
33 is($testcf->Values->Count , 3);
34
35 use_ok('RT::Ticket');
36
37 my $u = RT::User->new($RT::SystemUser);
38 $u->Load("root");
39 ok ($u->Id, "Found the root user");
40 ok(my $t = RT::Ticket->new($RT::SystemUser));
41 ok(my ($id, $msg) = $t->Create( Queue => $testqueue->Id,
42                Subject => 'Testing',
43                Owner => $u->Id
44               ));
45 isnt($id , 0);
46 is ($t->OwnerObj->Id , $u->Id, "Root is the ticket owner");
47 ok(my ($cfv, $cfm) =$t->AddCustomFieldValue(Field => $testcf->Id,
48                            Value => 'Value1'));
49 isnt($cfv , 0, "Custom field creation didn't return an error: $cfm");
50 is($t->CustomFieldValues($testcf->Id)->Count , 1);
51 ok($t->CustomFieldValues($testcf->Id)->First &&
52     $t->CustomFieldValues($testcf->Id)->First->Content eq 'Value1');
53
54 ok(my ($cfdv, $cfdm) = $t->DeleteCustomFieldValue(Field => $testcf->Id,
55                         Value => 'Value1'));
56 isnt ($cfdv , 0, "Deleted a custom field value: $cfdm");
57 is($t->CustomFieldValues($testcf->Id)->Count , 0);
58
59 ok(my $t2 = RT::Ticket->new($RT::SystemUser));
60 ok($t2->Load($id));
61 is($t2->Subject, 'Testing');
62 is($t2->QueueObj->Id, $testqueue->id);
63 is($t2->OwnerObj->Id, $u->Id);
64
65 my $t3 = RT::Ticket->new($RT::SystemUser);
66 my ($id3, $msg3) = $t3->Create( Queue => $testqueue->Id,
67                                 Subject => 'Testing',
68                                 Owner => $u->Id);
69 my ($cfv1, $cfm1) = $t->AddCustomFieldValue(Field => $testcf->Id,
70  Value => 'Value1');
71 isnt($cfv1 , 0, "Adding a custom field to ticket 1 is successful: $cfm");
72 my ($cfv2, $cfm2) = $t3->AddCustomFieldValue(Field => $testcf->Id,
73  Value => 'Value2');
74 isnt($cfv2 , 0, "Adding a custom field to ticket 2 is successful: $cfm");
75 my ($cfv3, $cfm3) = $t->AddCustomFieldValue(Field => $testcf->Id,
76  Value => 'Value3');
77 isnt($cfv3 , 0, "Adding a custom field to ticket 1 is successful: $cfm");
78 is($t->CustomFieldValues($testcf->Id)->Count , 2,
79    "This ticket has 2 custom field values");
80 is($t3->CustomFieldValues($testcf->Id)->Count , 1,
81    "This ticket has 1 custom field value");
82
83
84 }
85
86 {
87
88
89 ok(require RT::Ticket, "Loading the RT::Ticket library");
90
91
92 }
93
94 {
95
96 my $t = RT::Ticket->new($RT::SystemUser);
97
98 ok( $t->Create(Queue => 'General', Due => '2002-05-21 00:00:00', ReferredToBy => 'http://www.cpan.org', RefersTo => 'http://fsck.com', Subject => 'This is a subject'), "Ticket Created");
99
100 ok ( my $id = $t->Id, "Got ticket id");
101 like ($t->RefersTo->First->Target , qr/fsck.com/, "Got refers to");
102 like ($t->ReferredToBy->First->Base , qr/cpan.org/, "Got referredtoby");
103 is ($t->ResolvedObj->Unix, 0, "It hasn't been resolved - ". $t->ResolvedObj->Unix);
104
105
106 }
107
108 {
109
110 my $ticket = RT::Ticket->new($RT::SystemUser);
111 my ($id, $msg) = $ticket->Create(Subject => "Foo",
112                 Owner => $RT::SystemUser->Id,
113                 Status => 'open',
114                 Requestor => ['jesse@example.com'],
115                 Queue => '1'
116                 );
117 ok ($id, "Ticket $id was created");
118 ok(my $group = RT::Group->new($RT::SystemUser));
119 ok($group->LoadTicketRoleGroup(Ticket => $id, Type=> 'Requestor'));
120 ok ($group->Id, "Found the requestors object for this ticket");
121
122 ok(my $jesse = RT::User->new($RT::SystemUser), "Creating a jesse rt::user");
123 $jesse->LoadByEmail('jesse@example.com');
124 ok($jesse->Id,  "Found the jesse rt user");
125
126
127 ok ($ticket->IsWatcher(Type => 'Requestor', PrincipalId => $jesse->PrincipalId), "The ticket actually has jesse at fsck.com as a requestor");
128 ok (my ($add_id, $add_msg) = $ticket->AddWatcher(Type => 'Requestor', Email => 'bob@fsck.com'), "Added bob at fsck.com as a requestor");
129 ok ($add_id, "Add succeeded: ($add_msg)");
130 ok(my $bob = RT::User->new($RT::SystemUser), "Creating a bob rt::user");
131 $bob->LoadByEmail('bob@fsck.com');
132 ok($bob->Id,  "Found the bob rt user");
133 ok ($ticket->IsWatcher(Type => 'Requestor', PrincipalId => $bob->PrincipalId), "The ticket actually has bob at fsck.com as a requestor");
134 ok ( ($add_id, $add_msg) = $ticket->DeleteWatcher(Type =>'Requestor', Email => 'bob@fsck.com'), "Added bob at fsck.com as a requestor");
135 ok (!$ticket->IsWatcher(Type => 'Requestor', PrincipalId => $bob->PrincipalId), "The ticket no longer has bob at fsck.com as a requestor");
136
137
138 $group = RT::Group->new($RT::SystemUser);
139 ok($group->LoadTicketRoleGroup(Ticket => $id, Type=> 'Cc'));
140 ok ($group->Id, "Found the cc object for this ticket");
141 $group = RT::Group->new($RT::SystemUser);
142 ok($group->LoadTicketRoleGroup(Ticket => $id, Type=> 'AdminCc'));
143 ok ($group->Id, "Found the AdminCc object for this ticket");
144 $group = RT::Group->new($RT::SystemUser);
145 ok($group->LoadTicketRoleGroup(Ticket => $id, Type=> 'Owner'));
146 ok ($group->Id, "Found the Owner object for this ticket");
147 ok($group->HasMember($RT::SystemUser->UserObj->PrincipalObj), "the owner group has the member 'RT_System'");
148
149
150 }
151
152 {
153
154 my $t = RT::Ticket->new($RT::SystemUser);
155 ok($t->Create(Queue => 'general', Subject => 'SquelchTest', SquelchMailTo => 'nobody@example.com'));
156
157 my @returned = $t->SquelchMailTo();
158 is($#returned, 0, "The ticket has one squelched recipients");
159
160 my ($ret, $msg) = $t->UnsquelchMailTo('nobody@example.com');
161 ok($ret, "Removed nobody as a squelched recipient - ".$msg);
162 @returned = $t->SquelchMailTo();
163 is($#returned, -1, "The ticket has no squelched recipients". join(',',@returned));
164
165
166
167 @returned = $t->SquelchMailTo('nobody@example.com');
168 is($#returned, 0, "The ticket has one squelched recipients");
169
170 my @names = $t->Attributes->Names;
171 is(shift @names, 'SquelchMailTo', "The attribute we have is SquelchMailTo");
172 @returned = $t->SquelchMailTo('nobody@example.com');
173
174
175 is($#returned, 0, "The ticket has one squelched recipients");
176
177 @names = $t->Attributes->Names;
178 is(shift @names, 'SquelchMailTo', "The attribute we have is SquelchMailTo");
179
180
181 ($ret, $msg) = $t->UnsquelchMailTo('nobody@example.com');
182 ok($ret, "Removed nobody as a squelched recipient - ".$msg);
183 @returned = $t->SquelchMailTo();
184 is($#returned, -1, "The ticket has no squelched recipients". join(',',@returned));
185
186
187
188 }
189
190 {
191
192 my $t1 = RT::Ticket->new($RT::SystemUser);
193 $t1->Create ( Subject => 'Merge test 1', Queue => 'general', Requestor => 'merge1@example.com');
194 my $t1id = $t1->id;
195 my $t2 = RT::Ticket->new($RT::SystemUser);
196 $t2->Create ( Subject => 'Merge test 2', Queue => 'general', Requestor => 'merge2@example.com');
197 my $t2id = $t2->id;
198 my ($msg, $val) = $t1->MergeInto($t2->id);
199 ok ($msg,$val);
200 $t1 = RT::Ticket->new($RT::SystemUser);
201 is ($t1->id, undef, "ok. we've got a blank ticket1");
202 $t1->Load($t1id);
203
204 is ($t1->id, $t2->id);
205
206 is ($t1->Requestors->MembersObj->Count, 2);
207
208
209
210 }
211
212 {
213
214 my $root = RT::User->new($RT::SystemUser);
215 $root->Load('root');
216 ok ($root->Id, "Loaded the root user");
217 my $t = RT::Ticket->new($RT::SystemUser);
218 $t->Load(1);
219 $t->SetOwner('root');
220 is ($t->OwnerObj->Name, 'root' , "Root owns the ticket");
221 $t->Steal();
222 is ($t->OwnerObj->id, $RT::SystemUser->id , "SystemUser owns the ticket");
223 my $txns = RT::Transactions->new($RT::SystemUser);
224 $txns->OrderBy(FIELD => 'id', ORDER => 'DESC');
225 $txns->Limit(FIELD => 'ObjectId', VALUE => '1');
226 $txns->Limit(FIELD => 'ObjectType', VALUE => 'RT::Ticket');
227 $txns->Limit(FIELD => 'Type', OPERATOR => '!=',  VALUE => 'EmailRecord');
228
229 my $steal  = $txns->First;
230 is($steal->OldValue , $root->Id , "Stolen from root");
231 is($steal->NewValue , $RT::SystemUser->Id , "Stolen by the systemuser");
232
233
234 }
235
236 {
237
238 my $tt = RT::Ticket->new($RT::SystemUser);
239 my ($id, $tid, $msg)= $tt->Create(Queue => 'general',
240             Subject => 'test');
241 ok($id, $msg);
242 is($tt->Status, 'new', "New ticket is created as new");
243
244 ($id, $msg) = $tt->SetStatus('open');
245 ok($id, $msg);
246 like($msg, qr/open/i, "Status message is correct");
247 ($id, $msg) = $tt->SetStatus('resolved');
248 ok($id, $msg);
249 like($msg, qr/resolved/i, "Status message is correct");
250 ($id, $msg) = $tt->SetStatus('resolved');
251 ok(!$id,$msg);
252
253
254
255 }
256
257 1;