6 eval { require Email::Abstract; require Test::Email; 1 }
7 or plan skip_all => 'require Email::Abstract and Test::Email';
12 use RT::Test tests => 62;
15 RT->Config->Set( LogToScreen => 'debug' );
16 RT->Config->Set( UseTransactionBatch => 1 );
17 my ($baseurl, $m) = RT::Test->started_ok;
19 my $q = RT::Queue->new($RT::SystemUser);
20 $q->Load('___Approvals');
24 # minion is the requestor, cto is the approval owner, coo and ceo are approval
26 for my $user_name (qw(minion cto coo ceo )) {
27 my $user = $users{$user_name} = RT::User->new($RT::SystemUser);
28 $user->Create( Name => uc($user_name),
30 EmailAddress => $user_name.'@company.com',
31 Password => 'password',
34 ($val, $msg) = $user->PrincipalObj->GrantRight(Object =>$q, Right => $_)
35 for qw(ModifyTicket OwnTicket ShowTicket);
38 # XXX: we need to make the first approval ticket open so notification is sent.
40 '===Create-Ticket: for-CTO
45 Requestors: {$Tickets{"TOP"}->Requestors}
47 Subject: CTO Approval for PO: {$Tickets{"TOP"}->Id} - {$Tickets{"TOP"}->Subject}
49 Content-Type: text/plain
50 Content: Your approval is requested for the PO ticket {$Tickets{"TOP"}->Id}: {$Tickets{"TOP"}->Subject}
56 my $apptemp = RT::Template->new($RT::SystemUser);
57 $apptemp->Create( Content => $approvals, Name => "PO Approvals", Queue => "0");
61 $q = RT::Queue->new($RT::SystemUser);
62 $q->Create(Name => 'PO');
63 ok ($q->Id, "Created PO queue");
65 my $scrip = RT::Scrip->new($RT::SystemUser);
66 my ($sval, $smsg) =$scrip->Create( ScripCondition => 'On Create',
67 ScripAction => 'Create Tickets',
68 Template => 'PO Approvals',
70 Description => 'Create Approval Tickets');
72 ok ($scrip->Id, "Created the scrip");
73 ok ($scrip->TemplateObj->Id, "Created the scrip template");
74 ok ($scrip->ConditionObj->Id, "Created the scrip condition");
75 ok ($scrip->ActionObj->Id, "Created the scrip action");
77 my $t = RT::Ticket->new($RT::SystemUser);
78 my ($tid, $ttrans, $tmsg);
81 ( $tid, $ttrans, $tmsg ) = $t->Create(
82 Subject => "PO for stationary",
84 Requestor => 'minion',
87 } { from => qr/RT System/,
88 bcc => qr/ceo.*coo|coo.*ceo/i,
89 subject => qr/PO for stationary/i,
91 { from => qr/RT System/,
92 to => 'cto@company.com',
93 subject => qr/New Pending Approval: CTO Approval/,
94 body => qr/pending your approval.*Your approval is requested.*Blah/s
96 { from => qr/PO via RT/,
97 to => 'minion@company.com',
98 subject => qr/PO for stationary/,
99 body => qr/automatically generated in response/
104 is ($t->DependsOn->Count, 1, "depends on one ticket");
105 my $t_cto = RT::Ticket->new( $RT::SystemUser );
106 $t_cto->Load( $t->DependsOn->First->TargetObj->id );
109 [ $t->Status, $t_cto->Status ],
111 'tickets in correct state'
115 my $cto = RT::CurrentUser->new;
116 $cto->Load( $users{cto} );
118 $t_cto->CurrentUser($cto);
119 my $notes = MIME::Entity->build(
120 Data => [ 'Resources exist to be consumed.' ]
122 RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
124 my ( $notesval, $notesmsg ) = $t_cto->Correspond( MIMEObj => $notes );
125 ok($notesval, $notesmsg);
127 my ($ok, $msg) = $t_cto->SetStatus( Status => 'resolved' );
128 ok($ok, "cto can approve - $msg");
132 bcc => qr/ceo.*coo|coo.*ceo/i,
133 body => qr/Resources exist to be consumed/,
136 from => qr/RT System/,
137 to => 'minion@company.com',
138 subject => qr/Ticket Approved:/,
139 body => qr/approved by CTO.*notes: Resources exist to be consumed/s
143 to => 'root@localhost',
144 subject => qr/Ticket Approved:/,
145 body => qr/The ticket has been approved, you may now start to act on it/,
149 [ $t->Status, $t_cto->Status ],
150 [ 'new', 'resolved' ],
151 'ticket state after coo approval'
154 for my $admin (qw/coo ceo/) {
160 RT::Test->clean_caught_mails;
163 my $user = RT::CurrentUser->new;
164 $user->Load( $users{$admin} );
166 $t_cto->CurrentUser($user);
168 MIME::Entity->build( Data => ['Resources exist to be consumed.'] );
169 RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
171 my ( $notesval, $notesmsg ) = $t_cto->Correspond( MIMEObj => $notes );
172 ok( $notesval, $notesmsg );
174 my ( $ok, $msg ) = $t_cto->SetStatus( Status => 'resolved' );
175 ok( $ok, "cto can approve - $msg" );
178 from => qr/\U$admin/,
179 bcc => $admin eq 'coo' ? qr/ceo/i : qr/coo/,
180 body => qr/Resources exist to be consumed/,
183 from => qr/RT System/,
184 to => 'minion@company.com',
185 subject => qr/Ticket Approved:/,
186 body => qr/approved by \U$admin\E.*notes: Resources exist to be consumed/s
189 from => qr/\U$admin/,
190 to => 'root@localhost',
191 subject => qr/Ticket Approved:/,
193 qr/The ticket has been approved, you may now start to act on it/,
197 # now we test the web
198 my $approval_link = $baseurl . '/Approvals/';
200 $t = RT::Ticket->new($RT::SystemUser);
201 ( $tid, $ttrans, $tmsg ) = $t->Create(
202 Subject => "first approval",
204 Requestor => 'minion',
209 my $first_ticket = RT::Ticket->new( $RT::SystemUser );
210 $first_ticket->Load( $tid );
211 my $first_approval = $first_ticket->DependsOn->First->TargetObj;
213 $t = RT::Ticket->new($RT::SystemUser);
214 ( $tid, $ttrans, $tmsg ) = $t->Create(
215 Subject => "second approval",
217 Requestor => 'minion',
221 my $second_ticket = RT::Ticket->new( $RT::SystemUser );
222 $second_ticket->Load( $tid );
223 my $second_approval = $second_ticket->DependsOn->First->TargetObj;
226 ok( $m->login( 'cto', 'password' ), 'logged in as coo' );
228 my $m_coo = RT::Test::Web->new;
229 ok( $m_coo->login( 'coo', 'password' ), 'logged in as coo' );
231 my $m_ceo = RT::Test::Web->new;
232 ok( $m_ceo->login( 'ceo', 'password' ), 'logged in as coo' );
235 $m->get_ok( $approval_link );
236 $m_coo->get_ok( $approval_link );
237 $m_ceo->get_ok( $approval_link );
239 $m->content_contains('first approval', 'cto: see both approvals' );
240 $m->content_contains('second approval', 'cto: see both approvals' );
242 $m_coo->content_contains('first approval', 'coo: see both approvals');
243 $m_coo->content_contains('second approval', 'coo: see both approvals');
245 $m_ceo->content_contains('first approval', 'ceo: see both approvals');
246 $m_ceo->content_contains('second approval', 'ceo: see both approvals');
248 # now let's approve the first one via cto
250 form_name => 'Approvals',
251 fields => { 'Approval-' . $first_approval->id . '-Action' => 'approve', },
254 $m->content_lacks( 'first approval', 'cto: first approval is gone' );
255 $m->content_contains( 'second approval', 'cto: second approval is still here' );
256 $m_coo->get_ok( $approval_link );
257 $m_ceo->get_ok( $approval_link );
258 $m_coo->content_lacks( 'first approval', 'coo: first approval is gone' );
259 $m_coo->content_contains( 'second approval', 'coo: second approval is still here' );
260 $m_ceo->content_lacks( 'first approval', 'ceo: first approval is gone' );
261 $m_ceo->content_contains( 'second approval', 'ceo: second approval is still here' );
264 form_name => 'Approvals',
265 fields => { 'Approval-' . $second_approval->id . '-Action' => 'approve', },
268 $m->get_ok( $approval_link );
269 $m_ceo->get_ok( $approval_link );
271 $m->content_lacks( 'second approval', 'cto: second approval is gone too' );
272 $m_coo->content_lacks( 'second approval', 'coo: second approval is gone too' );
273 $m_ceo->content_lacks( 'second approval', 'ceo: second approval is gone too' );
275 RT::Test->clean_caught_mails;