fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / approval / basic.t
1 use strict;
2 use warnings;
3 use RT::Test tests => undef;
4
5 use RT::Test::Email;
6
7 RT->Config->Set( LogToSTDERR => 'debug' );
8 RT->Config->Set( UseTransactionBatch => 1 );
9
10 my $q = RT::Queue->new(RT->SystemUser);
11 $q->Load('___Approvals');
12 $q->SetDisabled(0);
13
14 my %users;
15 for my $user_name (qw(minion cfo ceo )) {
16     my $user = $users{$user_name} = RT::User->new(RT->SystemUser);
17     $user->Create( Name => uc($user_name),
18                    Privileged => 1,
19                    EmailAddress => $user_name.'@company.com');
20     my ($val, $msg);
21     ($val, $msg) = $user->PrincipalObj->GrantRight(Object =>$q, Right => $_)
22         for qw(ModifyTicket OwnTicket ShowTicket);
23 }
24
25 # XXX: we need to make the first approval ticket open so notification is sent.
26 my $approvals = 
27 '===Create-Ticket: for-CFO
28 Queue: ___Approvals
29 Type: approval
30 Owner: CFO
31 Refers-To: TOP
32 Subject: CFO Approval for PO: {$Tickets{"TOP"}->Id} - {$Tickets{"TOP"}->Subject}
33 Due: {time + 86400}
34 Content-Type: text/plain
35 Content: Your approval is requested for the PO ticket {$Tickets{"TOP"}->Id}: {$Tickets{"TOP"}->Subject}
36 Blah
37 Blah
38 ENDOFCONTENT
39 ===Create-Ticket: for-CEO
40 Queue: ___Approvals
41 Type: approval
42 Owner: CEO
43 Subject: PO approval request for {$Tickets{"TOP"}->Subject}
44 Refers-To: TOP
45 Depends-On: for-CFO
46 Depended-On-By: {$Tickets{"TOP"}->Id}
47 Content-Type: text/plain
48 Content: 
49 Your CFO approved PO ticket {$Tickets{"TOP"}->Id} for minion. you ok with that?
50 ENDOFCONTENT
51 ';
52
53 my $apptemp = RT::Template->new(RT->SystemUser);
54 $apptemp->Create( Content => $approvals, Name => "PO Approvals", Queue => "0");
55
56 ok($apptemp->Id);
57
58 $q = RT::Queue->new(RT->SystemUser);
59 $q->Create(Name => 'PO');
60 ok ($q->Id, "Created PO queue");
61
62 my $scrip = RT::Scrip->new(RT->SystemUser);
63 my ($sval, $smsg) =$scrip->Create( ScripCondition => 'On Create',
64                 ScripAction => 'Create Tickets',
65                 Template => 'PO Approvals',
66                 Queue => $q->Id,
67                 Description => 'Create Approval Tickets');
68 ok ($sval, $smsg);
69 ok ($scrip->Id, "Created the scrip");
70 ok ($scrip->TemplateObj->Id, "Created the scrip template");
71 ok ($scrip->ConditionObj->Id, "Created the scrip condition");
72 ok ($scrip->ActionObj->Id, "Created the scrip action");
73
74 my $t = RT::Ticket->new(RT->SystemUser);
75 my ($tid, $ttrans, $tmsg);
76
77 mail_ok {
78     ($tid, $ttrans, $tmsg) =
79         $t->Create(Subject => "PO for stationary",
80                    Owner => "root", Requestor => 'minion',
81                    Queue => $q->Id);
82 } { from => qr/PO via RT/,
83     to => 'minion@company.com',
84     subject => qr/PO for stationary/,
85     body => qr/automatically generated in response/
86 },{ from => qr/RT System/,
87     to => 'root@localhost',
88     subject => qr/PO for stationary/,
89 }, { from => qr/RT System/,
90     to => 'cfo@company.com',
91     subject => qr/New Pending Approval: CFO Approval/,
92     body => qr/pending your approval.*Your approval is requested.*Blah/s
93 };
94
95 ok ($tid,$tmsg);
96
97 is ($t->ReferredToBy->Count,2, "referred to by the two tickets");
98
99 my $deps = $t->DependsOn;
100 is ($deps->Count, 1, "The ticket we created depends on one other ticket");
101 my $dependson_ceo= $deps->First->TargetObj;
102 ok ($dependson_ceo->Id, "It depends on a real ticket");
103 like($dependson_ceo->Subject, qr/PO approval request.*stationary/);
104
105 $deps = $dependson_ceo->DependsOn;
106 is ($deps->Count, 1, "The ticket we created depends on one other ticket");
107 my $dependson_cfo = $deps->First->TargetObj;
108 ok ($dependson_cfo->Id, "It depends on a real ticket");
109
110 like($dependson_cfo->Subject, qr/CFO Approval for PO.*stationary/);
111
112 is_deeply([ $t->Status, $dependson_cfo->Status, $dependson_ceo->Status ],
113           [ 'new', 'open', 'new'], 'tickets in correct state');
114
115 mail_ok {
116     my $cfo = RT::CurrentUser->new;
117     $cfo->Load( $users{cfo} );
118
119     $dependson_cfo->CurrentUser($cfo);
120     my $notes = MIME::Entity->build(
121         Data => [ 'Resources exist to be consumed.' ]
122     );
123     RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
124
125     my ( $notesval, $notesmsg ) = $dependson_cfo->Correspond( MIMEObj => $notes );
126     ok($notesval, $notesmsg);
127
128     my ($ok, $msg) = $dependson_cfo->SetStatus( Status => 'resolved' );
129     ok($ok, "cfo can approve - $msg");
130
131 } { from => qr/RT System/,
132     to => 'ceo@company.com',
133     subject => qr/New Pending Approval: PO approval request for PO/,
134     body => qr/pending your approval.*CFO approved.*ok with that\?/s
135 },{ from => qr/RT System/,
136     to => 'root@localhost',
137     subject => qr/Ticket Approved:/,
138 },{ from => qr/RT System/,
139     to => 'minion@company.com',
140     subject => qr/Ticket Approved:/,
141     body => qr/approved by CFO.*notes: Resources exist to be consumed/s
142 };
143
144 is ($t->DependsOn->Count, 1, "still depends only on the CEO approval");
145 is ($t->ReferredToBy->Count,2, "referred to by the two tickets");
146
147 is_deeply([ $t->Status, $dependson_cfo->Status, $dependson_ceo->Status ],
148           [ 'new', 'resolved', 'open'], 'ticket state after cfo approval');
149
150 mail_ok {
151     my $ceo = RT::CurrentUser->new;
152     $ceo->Load( $users{ceo} );
153
154     $dependson_ceo->CurrentUser($ceo);
155     my $notes = MIME::Entity->build(
156         Data => [ 'And consumed they will be.' ]
157     );
158     RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
159
160     my ( $notesval, $notesmsg ) = $dependson_ceo->Correspond( MIMEObj => $notes );
161     ok($notesval, $notesmsg);
162
163     my ($ok, $msg) = $dependson_ceo->SetStatus( Status => 'resolved' );
164     ok($ok, "ceo can approve - $msg");
165
166 } { from => qr/RT System/,
167     to => 'root@localhost',
168     subject => qr/Ticket Approved:/,
169     body => qr/approved by CEO.*Its Owner may now start to act on it.*notes: And consumed they will be/s,
170 },{ from => qr/RT System/,
171     to => 'minion@company.com',
172     subject => qr/Ticket Approved:/,
173     body => qr/approved by CEO.*Its Owner may now start to act on it.*notes: And consumed they will be/s,
174 },{ from => qr/CEO via RT/,
175      to => 'root@localhost',
176      subject => qr/Ticket Approved/,
177      body => qr/The ticket has been approved, you may now start to act on it/,
178 };
179
180
181 is_deeply([ $t->Status, $dependson_cfo->Status, $dependson_ceo->Status ],
182           [ 'new', 'resolved', 'resolved'], 'ticket state after ceo approval');
183
184 $dependson_cfo->_Set(
185     Field => 'Status',
186     Value => 'open');
187
188 $dependson_ceo->_Set(
189     Field => 'Status',
190     Value => 'new');
191
192 mail_ok {
193     my $cfo = RT::CurrentUser->new;
194     $cfo->Load( $users{cfo} );
195
196     $dependson_cfo->CurrentUser($cfo);
197     my $notes = MIME::Entity->build(
198         Data => [ 'sorry, out of resources.' ]
199     );
200     RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
201
202     my ( $notesval, $notesmsg ) = $dependson_cfo->Correspond( MIMEObj => $notes );
203     ok($notesval, $notesmsg);
204
205     my ($ok, $msg) = $dependson_cfo->SetStatus( Status => 'rejected' );
206     ok($ok, "cfo can approve - $msg");
207
208 } { from => qr/RT System/,
209     to => 'root@localhost',
210     subject => qr/Ticket Rejected: PO for stationary/,
211     body => qr/rejected by CFO.*out of resources/s,
212 },{ from => qr/RT System/,
213     to => 'minion@company.com',
214     subject => qr/Ticket Rejected: PO for stationary/,
215     body => qr/rejected by CFO.*out of resources/s,
216 };
217
218 $t->Load($t->id);$dependson_ceo->Load($dependson_ceo->id);
219 is_deeply([ $t->Status, $dependson_cfo->Status, $dependson_ceo->Status ],
220           [ 'rejected', 'rejected', 'deleted'], 'ticket state after cfo rejection');
221
222 done_testing;