RT 4.0.13
[freeside.git] / rt / t / api / cron.t
1 use strict;
2 use warnings;
3
4 use RT;
5 use RT::Test nodata => 1, tests => 18;
6
7
8 ### Set up some testing data.  Test the testing data because why not?
9
10 # Create a user with rights, a queue, and some tickets.
11 my $user_obj = RT::User->new(RT->SystemUser);
12 my ($ret, $msg) = $user_obj->LoadOrCreateByEmail('tara@example.com');
13 ok($ret, 'record test user creation');
14 $user_obj->SetName('tara');
15 $user_obj->PrincipalObj->GrantRight(Right => 'SuperUser');
16 my $CurrentUser = RT::CurrentUser->new('tara');
17
18 # Create our template, which will be used for tests of RT::Action::Record*.
19
20 my $template_content = 'RT-Send-Cc: tla@example.com
21 RT-Send-Bcc: jesse@example.com
22
23 This is a content string with no content.';
24
25 my $template_obj = RT::Template->new($CurrentUser);
26 $template_obj->Create(Queue       => '0',
27                       Name        => 'recordtest',
28                       Description => 'testing Record actions',
29                       Content     => $template_content,
30                      );
31
32 # Create a queue and some tickets.
33
34 my $queue_obj = RT::Queue->new($CurrentUser);
35 ($ret, $msg) = $queue_obj->Create(Name => 'recordtest', Description => 'queue for Action::Record testing');
36 ok($ret, 'record test queue creation');
37
38 my $ticket1 = RT::Ticket->new($CurrentUser);
39 my ($id, $tobj, $msg2) = $ticket1->Create(Queue    => $queue_obj,
40                                          Requestor => ['tara@example.com'],
41                                          Subject   => 'bork bork bork',
42                                          Priority  => 22,
43                                         );
44 ok($id, 'record test ticket creation 1');
45 my $ticket2 = RT::Ticket->new($CurrentUser);
46 ($id, $tobj, $msg2) = $ticket2->Create(Queue     => $queue_obj,
47                                       Requestor => ['root@localhost'],
48                                       Subject   => 'hurdy gurdy'
49                                       );
50 ok($id, 'record test ticket creation 2');
51
52
53 ### OK.  Have data, will travel.
54
55 # First test the search.
56
57 ok(require RT::Search::FromSQL, "Search::FromSQL loaded");
58 my $ticketsqlstr = "Requestor.EmailAddress = '" . $CurrentUser->EmailAddress .
59     "' AND Priority > '20'";
60 my $search = RT::Search::FromSQL->new(Argument => $ticketsqlstr, TicketsObj => RT::Tickets->new($CurrentUser),
61                                       );
62 is(ref($search), 'RT::Search::FromSQL', "search created");
63 ok($search->Prepare(), "fromsql search run");
64 my $counter = 0;
65 while(my $t = $search->TicketsObj->Next() ) {
66     is($t->Id(), $ticket1->Id(), "fromsql search results 1");
67     $counter++;
68 }
69 is ($counter, 1, "fromsql search results 2");
70
71 # Right.  Now test the actions.
72
73 ok(require RT::Action::RecordComment);
74 ok(require RT::Action::RecordCorrespondence);
75
76 my ($comment_act, $correspond_act);
77 ok($comment_act = RT::Action::RecordComment->new(TicketObj => $ticket1, TemplateObj => $template_obj, CurrentUser => $CurrentUser), "RecordComment created");
78 ok($correspond_act = RT::Action::RecordCorrespondence->new(TicketObj => $ticket2, TemplateObj => $template_obj, CurrentUser => $CurrentUser), "RecordCorrespondence created");
79 ok($comment_act->Prepare(), "Comment prepared");
80 ok($correspond_act->Prepare(), "Correspond prepared");
81 ok($comment_act->Commit(), "Comment committed");
82 ok($correspond_act->Commit(), "Correspondence committed");
83
84 # Now test for loop suppression.
85 my ($trans, $desc, $transaction) = $ticket2->Comment(MIMEObj => $template_obj->MIMEObj);
86 my $bogus_action = RT::Action::RecordComment->new(TicketObj => $ticket1, TemplateObj => $template_obj, TransactionObj => $transaction, CurrentUser => $CurrentUser);
87 ok(!$bogus_action->Prepare(), "Comment aborted to prevent loop");
88