3 use RT::Test tests => undef;
5 plan skip_all => 'Email::Abstract and Test::Email required.'
6 unless eval { require Email::Abstract; require Test::Email; 1 };
12 RT->Config->Set( LogToScreen => 'debug' );
13 RT->Config->Set( UseTransactionBatch => 1 );
15 my $q = RT::Queue->new(RT->SystemUser);
16 $q->Load('___Approvals');
20 for my $user_name (qw(minion cfo ceo )) {
21 my $user = $users{$user_name} = RT::User->new(RT->SystemUser);
22 $user->Create( Name => uc($user_name),
24 EmailAddress => $user_name.'@company.com');
26 ($val, $msg) = $user->PrincipalObj->GrantRight(Object =>$q, Right => $_)
27 for qw(ModifyTicket OwnTicket ShowTicket);
30 # XXX: we need to make the first approval ticket open so notification is sent.
32 '===Create-Ticket: for-CFO
36 Requestors: {$Tickets{"TOP"}->Requestors}
38 Subject: CFO Approval for PO: {$Tickets{"TOP"}->Id} - {$Tickets{"TOP"}->Subject}
40 Content-Type: text/plain
41 Content: Your approval is requested for the PO ticket {$Tickets{"TOP"}->Id}: {$Tickets{"TOP"}->Subject}
45 ===Create-Ticket: for-CEO
49 Requestors: {$Tickets{"TOP"}->Requestors}
50 Subject: PO approval request for {$Tickets{"TOP"}->Subject}
53 Depended-On-By: {$Tickets{"TOP"}->Id}
54 Content-Type: text/plain
56 Your CFO approved PO ticket {$Tickets{"TOP"}->Id} for minion. you ok with that?
60 my $apptemp = RT::Template->new(RT->SystemUser);
61 $apptemp->Create( Content => $approvals, Name => "PO Approvals", Queue => "0");
65 $q = RT::Queue->new(RT->SystemUser);
66 $q->Create(Name => 'PO');
67 ok ($q->Id, "Created PO queue");
69 my $scrip = RT::Scrip->new(RT->SystemUser);
70 my ($sval, $smsg) =$scrip->Create( ScripCondition => 'On Create',
71 ScripAction => 'Create Tickets',
72 Template => 'PO Approvals',
74 Description => 'Create Approval Tickets');
76 ok ($scrip->Id, "Created the scrip");
77 ok ($scrip->TemplateObj->Id, "Created the scrip template");
78 ok ($scrip->ConditionObj->Id, "Created the scrip condition");
79 ok ($scrip->ActionObj->Id, "Created the scrip action");
81 my $t = RT::Ticket->new(RT->SystemUser);
82 my ($tid, $ttrans, $tmsg);
85 ($tid, $ttrans, $tmsg) =
86 $t->Create(Subject => "PO for stationary",
87 Owner => "root", Requestor => 'minion',
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 },{ from => qr/PO via RT/,
94 to => 'minion@company.com',
95 subject => qr/PO for stationary/,
96 body => qr/automatically generated in response/
101 is ($t->ReferredToBy->Count,2, "referred to by the two tickets");
103 my $deps = $t->DependsOn;
104 is ($deps->Count, 1, "The ticket we created depends on one other ticket");
105 my $dependson_ceo= $deps->First->TargetObj;
106 ok ($dependson_ceo->Id, "It depends on a real ticket");
107 like($dependson_ceo->Subject, qr/PO approval request.*stationary/);
109 $deps = $dependson_ceo->DependsOn;
110 is ($deps->Count, 1, "The ticket we created depends on one other ticket");
111 my $dependson_cfo = $deps->First->TargetObj;
112 ok ($dependson_cfo->Id, "It depends on a real ticket");
114 like($dependson_cfo->Subject, qr/CFO Approval for PO.*stationary/);
116 is_deeply([ $t->Status, $dependson_cfo->Status, $dependson_ceo->Status ],
117 [ 'new', 'open', 'new'], 'tickets in correct state');
120 my $cfo = RT::CurrentUser->new;
121 $cfo->Load( $users{cfo} );
123 $dependson_cfo->CurrentUser($cfo);
124 my $notes = MIME::Entity->build(
125 Data => [ 'Resources exist to be consumed.' ]
127 RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
129 my ( $notesval, $notesmsg ) = $dependson_cfo->Correspond( MIMEObj => $notes );
130 ok($notesval, $notesmsg);
132 my ($ok, $msg) = $dependson_cfo->SetStatus( Status => 'resolved' );
133 ok($ok, "cfo can approve - $msg");
135 } { from => qr/RT System/,
136 to => 'ceo@company.com',
137 subject => qr/New Pending Approval: PO approval request for PO/,
138 body => qr/pending your approval.*CFO approved.*ok with that\?/s
139 },{ from => qr/RT System/,
140 to => 'minion@company.com',
141 subject => qr/Ticket Approved:/,
142 body => qr/approved by CFO.*notes: Resources exist to be consumed/s
145 is ($t->DependsOn->Count, 1, "still depends only on the CEO approval");
146 is ($t->ReferredToBy->Count,2, "referred to by the two tickets");
148 is_deeply([ $t->Status, $dependson_cfo->Status, $dependson_ceo->Status ],
149 [ 'new', 'resolved', 'open'], 'ticket state after cfo approval');
152 my $ceo = RT::CurrentUser->new;
153 $ceo->Load( $users{ceo} );
155 $dependson_ceo->CurrentUser($ceo);
156 my $notes = MIME::Entity->build(
157 Data => [ 'And consumed they will be.' ]
159 RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
161 my ( $notesval, $notesmsg ) = $dependson_ceo->Correspond( MIMEObj => $notes );
162 ok($notesval, $notesmsg);
164 my ($ok, $msg) = $dependson_ceo->SetStatus( Status => 'resolved' );
165 ok($ok, "ceo can approve - $msg");
167 } { from => qr/RT System/,
168 to => 'minion@company.com',
169 subject => qr/Ticket Approved:/,
170 body => qr/approved by CEO.*Its Owner may now start to act on it.*notes: And consumed they will be/s,
171 }, { from => qr/CEO via RT/,
172 to => 'root@localhost',
173 subject => qr/Ticket Approved/,
174 body => qr/The ticket has been approved, you may now start to act on it/,
178 is_deeply([ $t->Status, $dependson_cfo->Status, $dependson_ceo->Status ],
179 [ 'new', 'resolved', 'resolved'], 'ticket state after ceo approval');
181 $dependson_cfo->_Set(
185 $dependson_ceo->_Set(
190 my $cfo = RT::CurrentUser->new;
191 $cfo->Load( $users{cfo} );
193 $dependson_cfo->CurrentUser($cfo);
194 my $notes = MIME::Entity->build(
195 Data => [ 'sorry, out of resources.' ]
197 RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
199 my ( $notesval, $notesmsg ) = $dependson_cfo->Correspond( MIMEObj => $notes );
200 ok($notesval, $notesmsg);
202 my ($ok, $msg) = $dependson_cfo->SetStatus( Status => 'rejected' );
203 ok($ok, "cfo can approve - $msg");
205 } { from => qr/RT System/,
206 to => 'minion@company.com',
207 subject => qr/Ticket Rejected: PO for stationary/,
208 body => qr/rejected by CFO.*out of resources/s,
211 $t->Load($t->id);$dependson_ceo->Load($dependson_ceo->id);
212 is_deeply([ $t->Status, $dependson_cfo->Status, $dependson_ceo->Status ],
213 [ 'rejected', 'rejected', 'deleted'], 'ticket state after cfo rejection');