7 use RT::Test tests => "no_declare";
10 RT->Config->Set( LogToSTDERR => 'debug' );
11 RT->Config->Set( UseTransactionBatch => 1 );
12 my ($baseurl, $m) = RT::Test->started_ok;
14 my $q = RT::Queue->new($RT::SystemUser);
15 $q->Load('___Approvals');
19 # minion is the requestor, cto is the approval owner, coo and ceo are approval
21 for my $user_name (qw(minion cto coo ceo )) {
22 my $user = $users{$user_name} = RT::User->new($RT::SystemUser);
23 $user->Create( Name => uc($user_name),
25 EmailAddress => $user_name.'@company.com',
26 Password => 'password',
29 ($val, $msg) = $user->PrincipalObj->GrantRight(Object =>$q, Right => $_)
30 for qw(ModifyTicket OwnTicket ShowTicket);
33 # XXX: we need to make the first approval ticket open so notification is sent.
35 '===Create-Ticket: for-CTO
41 Subject: CTO Approval for PO: {$Tickets{"TOP"}->Id} - {$Tickets{"TOP"}->Subject}
43 Content-Type: text/plain
44 Content: Your approval is requested for the PO ticket {$Tickets{"TOP"}->Id}: {$Tickets{"TOP"}->Subject}
50 my $apptemp = RT::Template->new($RT::SystemUser);
51 $apptemp->Create( Content => $approvals, Name => "PO Approvals", Queue => "0");
55 $q = RT::Queue->new($RT::SystemUser);
56 $q->Create(Name => 'PO');
57 ok ($q->Id, "Created PO queue");
59 my $scrip = RT::Scrip->new($RT::SystemUser);
60 my ($sval, $smsg) =$scrip->Create( ScripCondition => 'On Create',
61 ScripAction => 'Create Tickets',
62 Template => 'PO Approvals',
64 Description => 'Create Approval Tickets');
66 ok ($scrip->Id, "Created the scrip");
67 ok ($scrip->TemplateObj->Id, "Created the scrip template");
68 ok ($scrip->ConditionObj->Id, "Created the scrip condition");
69 ok ($scrip->ActionObj->Id, "Created the scrip action");
71 my $t = RT::Ticket->new($RT::SystemUser);
72 my ($tid, $ttrans, $tmsg);
75 ( $tid, $ttrans, $tmsg ) = $t->Create(
76 Subject => "PO for stationary",
78 Requestor => 'minion',
81 } { from => qr/PO via RT/,
82 to => 'minion@company.com',
83 subject => qr/PO for stationary/,
84 body => qr/automatically generated in response/
85 },{ from => qr/RT System/,
86 to => 'root@localhost',
87 subject => qr/PO for stationary/,
88 },{ from => qr/RT System/,
89 to => 'cto@company.com',
90 bcc => qr/ceo.*coo|coo.*ceo/i,
91 subject => qr/New Pending Approval: CTO Approval/,
92 body => qr/pending your approval.*Your approval is requested.*Blah/s
98 is ($t->DependsOn->Count, 1, "depends on one ticket");
99 my $t_cto = RT::Ticket->new( $RT::SystemUser );
100 $t_cto->Load( $t->DependsOn->First->TargetObj->id );
103 [ $t->Status, $t_cto->Status ],
105 'tickets in correct state'
109 my $cto = RT::CurrentUser->new;
110 $cto->Load( $users{cto} );
112 $t_cto->CurrentUser($cto);
113 my $notes = MIME::Entity->build(
114 Data => [ 'Resources exist to be consumed.' ]
116 RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
118 my ( $notesval, $notesmsg ) = $t_cto->Correspond( MIMEObj => $notes );
119 ok($notesval, $notesmsg);
121 my ($ok, $msg) = $t_cto->SetStatus( Status => 'resolved' );
122 ok($ok, "cto can approve - $msg");
126 bcc => qr/ceo.*coo|coo.*ceo/i,
127 body => qr/Resources exist to be consumed/,
130 from => qr/RT System/,
131 to => 'root@localhost',
132 subject => qr/Ticket Approved:/,
135 from => qr/RT System/,
136 to => 'minion@company.com',
137 subject => qr/Ticket Approved:/,
138 body => qr/approved by CTO.*notes: Resources exist to be consumed/s
142 to => 'root@localhost',
143 subject => qr/Ticket Approved:/,
144 body => qr/The ticket has been approved, you may now start to act on it/,
148 [ $t->Status, $t_cto->Status ],
149 [ 'new', 'resolved' ],
150 'ticket state after coo approval'
153 for my $admin (qw/coo ceo/) {
159 RT::Test->clean_caught_mails;
162 my $user = RT::CurrentUser->new;
163 $user->Load( $users{$admin} );
165 $t_cto->CurrentUser($user);
167 MIME::Entity->build( Data => ['Resources exist to be consumed.'] );
168 RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
170 my ( $notesval, $notesmsg ) = $t_cto->Correspond( MIMEObj => $notes );
171 ok( $notesval, $notesmsg );
173 my ( $ok, $msg ) = $t_cto->SetStatus( Status => 'resolved' );
174 ok( $ok, "cto can approve - $msg" );
177 from => qr/\U$admin/,
178 bcc => $admin eq 'coo' ? qr/ceo/i : qr/coo/,
179 body => qr/Resources exist to be consumed/,
182 from => qr/RT System/,
183 to => 'root@localhost',
184 subject => qr/Ticket Approved:/,
185 body => qr/approved by \U$admin\E.*notes: Resources exist to be consumed/s
188 from => qr/RT System/,
189 to => 'minion@company.com',
190 subject => qr/Ticket Approved:/,
191 body => qr/approved by \U$admin\E.*notes: Resources exist to be consumed/s
194 from => qr/\U$admin/,
195 to => 'root@localhost',
196 subject => qr/Ticket Approved:/,
198 qr/The ticket has been approved, you may now start to act on it/,
202 # now we test the web
203 my $approval_link = $baseurl . '/Approvals/';
205 $t = RT::Ticket->new($RT::SystemUser);
206 ( $tid, $ttrans, $tmsg ) = $t->Create(
207 Subject => "first approval",
209 Requestor => 'minion',
214 my $first_ticket = RT::Ticket->new( $RT::SystemUser );
215 $first_ticket->Load( $tid );
216 my $first_approval = $first_ticket->DependsOn->First->TargetObj;
218 $t = RT::Ticket->new($RT::SystemUser);
219 ( $tid, $ttrans, $tmsg ) = $t->Create(
220 Subject => "second approval",
222 Requestor => 'minion',
226 my $second_ticket = RT::Ticket->new( $RT::SystemUser );
227 $second_ticket->Load( $tid );
228 my $second_approval = $second_ticket->DependsOn->First->TargetObj;
231 ok( $m->login( 'cto', 'password' ), 'logged in as coo' );
233 my $m_coo = RT::Test::Web->new;
234 ok( $m_coo->login( 'coo', 'password' ), 'logged in as coo' );
236 my $m_ceo = RT::Test::Web->new;
237 ok( $m_ceo->login( 'ceo', 'password' ), 'logged in as coo' );
240 $m->get_ok( $approval_link );
241 $m_coo->get_ok( $approval_link );
242 $m_ceo->get_ok( $approval_link );
244 $m->content_contains('first approval', 'cto: see both approvals' );
245 $m->content_contains('second approval', 'cto: see both approvals' );
247 $m_coo->content_contains('first approval', 'coo: see both approvals');
248 $m_coo->content_contains('second approval', 'coo: see both approvals');
250 $m_ceo->content_contains('first approval', 'ceo: see both approvals');
251 $m_ceo->content_contains('second approval', 'ceo: see both approvals');
253 # now let's approve the first one via cto
255 form_name => 'Approvals',
256 fields => { 'Approval-' . $first_approval->id . '-Action' => 'approve', },
259 $m->content_lacks( 'first approval', 'cto: first approval is gone' );
260 $m->content_contains( 'second approval', 'cto: second approval is still here' );
261 $m_coo->get_ok( $approval_link );
262 $m_ceo->get_ok( $approval_link );
263 $m_coo->content_lacks( 'first approval', 'coo: first approval is gone' );
264 $m_coo->content_contains( 'second approval', 'coo: second approval is still here' );
265 $m_ceo->content_lacks( 'first approval', 'ceo: first approval is gone' );
266 $m_ceo->content_contains( 'second approval', 'ceo: second approval is still here' );
269 form_name => 'Approvals',
270 fields => { 'Approval-' . $second_approval->id . '-Action' => 'approve', },
273 $m->get_ok( $approval_link );
274 $m_ceo->get_ok( $approval_link );
276 $m->content_lacks( 'second approval', 'cto: second approval is gone too' );
277 $m_coo->content_lacks( 'second approval', 'coo: second approval is gone too' );
278 $m_ceo->content_lacks( 'second approval', 'ceo: second approval is gone too' );
280 RT::Test->clean_caught_mails;