4 use RT::Test tests => 'no_declare';
6 my $initialdata = RT::Test::get_relocatable_file("transaction-cfs" => "..", "data", "initialdata");
7 my ($rv, $msg) = RT->DatabaseHandle->InsertData( $initialdata, undef, disconnect_after => 0 );
8 ok($rv, "Inserted test data from $initialdata")
13 Coffee => { Billable => "No", },
14 Phone => { Billable => "Yes", Who => ["Telecom", "Information Technology"], When => "2013-06-25", Location => "Geology" },
15 Stacks => { Billable => "Yes", Who => "Library", When => "2013-06-01" },
16 Benches => { Billable => "Yes", Location => "Outdoors" },
20 results_are("CF.Location IS NOT NULL", [qw( Phone Benches )]);
21 results_are("CF.Location IS NULL", [qw( Spam Coffee Stacks )]);
23 # TODO: Ideal behaviour of TxnCF IS NULL not yet determined
24 #results_are("TxnCF.Billable IS NULL", [qw( Spam )]);
26 results_are("TxnCF.Billable IS NOT NULL", [qw( Coffee Phone Stacks Benches )]);
27 results_are("TxnCF.Billable = 'No'", [qw( Coffee )]);
28 results_are("TxnCF.Billable = 'Yes'", [qw( Phone Stacks Benches )]);
29 results_are("TxnCF.Billable = 'Yes' AND CF.Location IS NOT NULL", [qw( Phone Benches )]);
30 results_are("TxnCF.Billable = 'Yes' AND CF.Location = 'Outdoors'", [qw( Benches )]);
31 results_are("TxnCF.Billable = 'Yes' AND CF.Location LIKE 'o'", [qw( Phone Benches )]);
33 results_are("TxnCF.Who = 'Telecom' OR TxnCF.Who = 'Library'", [qw( Phone Stacks )]);
35 # TODO: Negative searching finds tickets with at least one txn doesn't have the value
36 #results_are("TxnCF.Who != 'Library'", [qw( Spam Coffee Phone Benches )]);
38 results_are("TxnCF.When > '2013-06-24'", [qw( Phone )]);
39 results_are("TxnCF.When < '2013-06-24'", [qw( Stacks )]);
40 results_are("TxnCF.When >= '2013-06-01' and TxnCF.When <= '2013-06-30'", [qw( Phone Stacks )]);
42 results_are("TxnCF.Who LIKE 'e'", [qw( Phone )]);
44 # TODO: Negative searching finds tickets with at least one txn doesn't have the value
45 #results_are("TxnCF.Who NOT LIKE 'e'", [qw( Spam Coffee Stacks Benches )]);
47 results_are("TxnCF.Who NOT LIKE 'e' and TxnCF.Who IS NOT NULL", [qw( Stacks )]);
50 # Multiple CFs with same name applied to different queues
53 BlueNone => { Queue => "Blues" },
54 PurpleNone => { Queue => "Purples" },
56 Blue => { Queue => "Blues", Color => "Blue" },
57 Purple => { Queue => "Purples", Color => "Purple" },
60 # Queue-specific txn CFs
61 results_are("TxnCF.Blues.{Color} = 'Blue'", [qw( Blue )]);
62 results_are("TxnCF.Blues.{Color} = 'Purple'", []);
64 # Multiple transaction CFs by name
65 results_are("TxnCF.{Color} IS NOT NULL", [qw( Blue Purple )]);
66 results_are("TxnCF.{Color} = 'Blue'", [qw( Blue )]);
67 results_are("TxnCF.{Color} = 'Purple'", [qw( Purple )]);
68 results_are("TxnCF.{Color} LIKE 'e'", [qw( Blue Purple )]);
73 local $Test::Builder::Level = $Test::Builder::Level + 1;
77 my %expected = map { $_ => 1 } @$expected;
80 my $tickets = RT::Tickets->new(RT->SystemUser);
81 my ($ok, $msg) = $tickets->FromSQL($query);
82 ok($ok, "Searched: $query")
84 for my $t (@{$tickets->ItemsArrayRef || []}) {
85 if (delete $expected{$t->Subject}) {
86 ok(1, "Found expected ticket ".$t->Subject);
88 push @unexpected, $t->Subject;
91 ok(0, "Didn't find expected ticket $_")
92 for grep $expected{$_}, @$expected;
93 ok(0, "Found unexpected tickets: ".join ", ", @unexpected)
99 for my $subject (sort keys %ticket) {
100 my %data = %{$ticket{$subject}};
101 my $location = delete $data{Location};
102 my $queue = delete $data{Queue} || "General";
104 my $ticket = RT::Ticket->new( RT->SystemUser );
105 my ($ok, $msg) = $ticket->Create(
109 ok($ticket->id, "Created ticket: $msg") or next;
112 ($ok, $msg) = $ticket->AddCustomFieldValue( Field => "Location", Value => $location );
113 ok($ok, "Added Location: $msg") or next;
116 my ($txnid, $txnmsg, $txn) = $ticket->Correspond( Content => "test transaction" );
118 RT->Logger->error("Unable to correspond on ticket $ok: $txnmsg");
121 for my $name (sort keys %data) {
122 my $values = ref $data{$name} ? $data{$name} : [$data{$name}];
123 for my $v (@$values) {
124 ($ok, $msg) = $txn->_AddCustomFieldValue(
127 RecordTransaction => 0
129 ok($ok, "Added txn CF $name value '$v'")
137 my $tickets = RT::Tickets->new( RT->SystemUser );
138 $tickets->FromSQL("id > 0");
139 $_->SetStatus("deleted") for @{$tickets->ItemsArrayRef};