3 use RT::Test tests => undef;
7 RT->Config->Set( LogToSTDERR => 'debug' );
8 RT->Config->Set( UseTransactionBatch => 1 );
10 my $q = RT::Queue->new(RT->SystemUser);
11 $q->Load('___Approvals');
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),
19 EmailAddress => $user_name.'@company.com');
21 ($val, $msg) = $user->PrincipalObj->GrantRight(Object =>$q, Right => $_)
22 for qw(ModifyTicket OwnTicket ShowTicket);
25 # XXX: we need to make the first approval ticket open so notification is sent.
27 '===Create-Ticket: for-CFO
32 Subject: CFO Approval for PO: {$Tickets{"TOP"}->Id} - {$Tickets{"TOP"}->Subject}
34 Content-Type: text/plain
35 Content: Your approval is requested for the PO ticket {$Tickets{"TOP"}->Id}: {$Tickets{"TOP"}->Subject}
39 ===Create-Ticket: for-CEO
43 Subject: PO approval request for {$Tickets{"TOP"}->Subject}
46 Depended-On-By: {$Tickets{"TOP"}->Id}
47 Content-Type: text/plain
49 Your CFO approved PO ticket {$Tickets{"TOP"}->Id} for minion. you ok with that?
53 my $apptemp = RT::Template->new(RT->SystemUser);
54 $apptemp->Create( Content => $approvals, Name => "PO Approvals", Queue => "0");
58 $q = RT::Queue->new(RT->SystemUser);
59 $q->Create(Name => 'PO');
60 ok ($q->Id, "Created PO queue");
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',
67 Description => 'Create Approval Tickets');
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");
74 my $t = RT::Ticket->new(RT->SystemUser);
75 my ($tid, $ttrans, $tmsg);
78 ($tid, $ttrans, $tmsg) =
79 $t->Create(Subject => "PO for stationary",
80 Owner => "root", Requestor => 'minion',
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
97 is ($t->ReferredToBy->Count,2, "referred to by the two tickets");
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/);
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");
110 like($dependson_cfo->Subject, qr/CFO Approval for PO.*stationary/);
112 is_deeply([ $t->Status, $dependson_cfo->Status, $dependson_ceo->Status ],
113 [ 'new', 'open', 'new'], 'tickets in correct state');
116 my $cfo = RT::CurrentUser->new;
117 $cfo->Load( $users{cfo} );
119 $dependson_cfo->CurrentUser($cfo);
120 my $notes = MIME::Entity->build(
121 Data => [ 'Resources exist to be consumed.' ]
123 RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
125 my ( $notesval, $notesmsg ) = $dependson_cfo->Correspond( MIMEObj => $notes );
126 ok($notesval, $notesmsg);
128 my ($ok, $msg) = $dependson_cfo->SetStatus( Status => 'resolved' );
129 ok($ok, "cfo can approve - $msg");
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
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");
147 is_deeply([ $t->Status, $dependson_cfo->Status, $dependson_ceo->Status ],
148 [ 'new', 'resolved', 'open'], 'ticket state after cfo approval');
151 my $ceo = RT::CurrentUser->new;
152 $ceo->Load( $users{ceo} );
154 $dependson_ceo->CurrentUser($ceo);
155 my $notes = MIME::Entity->build(
156 Data => [ 'And consumed they will be.' ]
158 RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
160 my ( $notesval, $notesmsg ) = $dependson_ceo->Correspond( MIMEObj => $notes );
161 ok($notesval, $notesmsg);
163 my ($ok, $msg) = $dependson_ceo->SetStatus( Status => 'resolved' );
164 ok($ok, "ceo can approve - $msg");
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/,
181 is_deeply([ $t->Status, $dependson_cfo->Status, $dependson_ceo->Status ],
182 [ 'new', 'resolved', 'resolved'], 'ticket state after ceo approval');
184 $dependson_cfo->_Set(
188 $dependson_ceo->_Set(
193 my $cfo = RT::CurrentUser->new;
194 $cfo->Load( $users{cfo} );
196 $dependson_cfo->CurrentUser($cfo);
197 my $notes = MIME::Entity->build(
198 Data => [ 'sorry, out of resources.' ]
200 RT::I18N::SetMIMEEntityToUTF8($notes); # convert text parts into utf-8
202 my ( $notesval, $notesmsg ) = $dependson_cfo->Correspond( MIMEObj => $notes );
203 ok($notesval, $notesmsg);
205 my ($ok, $msg) = $dependson_cfo->SetStatus( Status => 'rejected' );
206 ok($ok, "cfo can approve - $msg");
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,
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');