diff options
Diffstat (limited to 'rt/lib/t/regression')
-rw-r--r-- | rt/lib/t/regression/06mailgateway.t | 60 | ||||
-rw-r--r-- | rt/lib/t/regression/07acl.t | 10 | ||||
-rw-r--r-- | rt/lib/t/regression/07rights.t | 32 | ||||
-rw-r--r-- | rt/lib/t/regression/09record_cf_api.t | 18 | ||||
-rw-r--r-- | rt/lib/t/regression/14linking.t | 143 | ||||
-rw-r--r-- | rt/lib/t/regression/22search_tix_by_txn.t | 2 | ||||
-rw-r--r-- | rt/lib/t/regression/22search_tix_by_watcher.t | 215 |
7 files changed, 442 insertions, 38 deletions
diff --git a/rt/lib/t/regression/06mailgateway.t b/rt/lib/t/regression/06mailgateway.t index 8486aea9e..1bdc38a69 100644 --- a/rt/lib/t/regression/06mailgateway.t +++ b/rt/lib/t/regression/06mailgateway.t @@ -62,7 +62,7 @@ use RT::I18N; ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url http://this.test.for.non-connection.is.expected.to.generate.an.error"), "Opened the mailgate - The error below is expected - $@"); print MAIL <<EOF; From: root\@localhost -To: rt\@example.com +To: rt\@$RT::rtname Subject: This is a test of new ticket creation Foob! @@ -78,7 +78,7 @@ is ( $? >> 8, 75, "The error message above is expected The mail gateway exited w ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@"); print MAIL <<EOF; From: root\@localhost -To: rt\@example.com +To: rt\@$RT::rtname Subject: This is a test of new ticket creation Blah! @@ -105,8 +105,8 @@ ok ($tick->Subject eq 'This is a test of new ticket creation', "Created the tick ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@"); print MAIL <<EOF; -From: doesnotexist\@example.com -To: rt\@example.com +From: doesnotexist\@$RT::rtname +To: rt\@$RT::rtname Subject: This is a test of new ticket creation as an unknown user Blah! @@ -123,7 +123,7 @@ $tick = $tickets->First(); ok ($tick->Id, "found ticket ".$tick->Id); ok ($tick->Subject ne 'This is a test of new ticket creation as an unknown user', "failed to create the new ticket from an unprivileged account"); my $u = RT::User->new($RT::SystemUser); -$u->Load('doesnotexist@example.com'); +$u->Load("doesnotexist\@$RT::rtname"); ok( $u->Id == 0, " user does not exist and was not created by failed ticket submission"); @@ -141,8 +141,8 @@ ok ($val, "Granted everybody the right to create tickets - $msg"); ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@"); print MAIL <<EOF; -From: doesnotexist\@example.com -To: rt\@example.com +From: doesnotexist\@$RT::rtname +To: rt\@$RT::rtname Subject: This is a test of new ticket creation as an unknown user Blah! @@ -160,7 +160,7 @@ $tick = $tickets->First(); ok ($tick->Id, "found ticket ".$tick->Id); ok ($tick->Subject eq 'This is a test of new ticket creation as an unknown user', "failed to create the new ticket from an unprivileged account"); $u = RT::User->new($RT::SystemUser); -$u->Load('doesnotexist@example.com'); +$u->Load("doesnotexist\@$RT::rtname"); ok( $u->Id != 0, " user does not exist and was created by ticket submission"); # }}} @@ -174,9 +174,9 @@ ok( $u->Id != 0, " user does not exist and was created by ticket submission"); ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@"); print MAIL <<EOF; -From: doesnotexist-2\@example.com -To: rt\@example.com -Subject: [example.com #@{[$tick->Id]}] This is a test of a reply as an unknown user +From: doesnotexist-2\@$RT::rtname +To: rt\@$RT::rtname +Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a reply as an unknown user Blah! (Should not work.) Foob! @@ -186,7 +186,7 @@ close (MAIL); is ($? >> 8, 0, "The mail gateway exited normally. yay"); $u = RT::User->new($RT::SystemUser); -$u->Load('doesnotexist-2@example.com'); +$u->Load('doesnotexist-2@$RT::rtname'); ok( $u->Id == 0, " user does not exist and was not created by ticket correspondence submission"); # }}} @@ -199,9 +199,9 @@ ok ($val, "Granted everybody the right to reply to tickets - $msg"); ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action correspond"), "Opened the mailgate - $@"); print MAIL <<EOF; -From: doesnotexist-2\@example.com -To: rt\@example.com -Subject: [example.com #@{[$tick->Id]}] This is a test of a reply as an unknown user +From: doesnotexist-2\@$RT::rtname +To: rt\@$RT::rtname +Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a reply as an unknown user Blah! Foob! @@ -212,7 +212,7 @@ is ($? >> 8, 0, "The mail gateway exited normally. yay"); $u = RT::User->new($RT::SystemUser); -$u->Load('doesnotexist-2@example.com'); +$u->Load("doesnotexist-2\@$RT::rtname"); ok( $u->Id != 0, " user exists and was created by ticket correspondence submission"); # }}} @@ -225,9 +225,9 @@ ok( $u->Id != 0, " user exists and was created by ticket correspondence submissi ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action comment"), "Opened the mailgate - $@"); print MAIL <<EOF; -From: doesnotexist-3\@example.com -To: rt\@example.com -Subject: [example.com #@{[$tick->Id]}] This is a test of a comment as an unknown user +From: doesnotexist-3\@$RT::rtname +To: rt\@$RT::rtname +Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a comment as an unknown user Blah! (Should not work.) Foob! @@ -238,7 +238,7 @@ close (MAIL); is ($? >> 8, 0, "The mail gateway exited normally. yay"); $u = RT::User->new($RT::SystemUser); -$u->Load('doesnotexist-3@example.com'); +$u->Load("doesnotexist-3\@$RT::rtname"); ok( $u->Id == 0, " user does not exist and was not created by ticket comment submission"); # }}} @@ -250,9 +250,9 @@ ok ($val, "Granted everybody the right to reply to tickets - $msg"); ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action comment"), "Opened the mailgate - $@"); print MAIL <<EOF; -From: doesnotexist-3\@example.com -To: rt\@example.com -Subject: [example.com #@{[$tick->Id]}] This is a test of a comment as an unknown user +From: doesnotexist-3\@$RT::rtname +To: rt\@$RT::rtname +Subject: [$RT::rtname #@{[$tick->Id]}] This is a test of a comment as an unknown user Blah! Foob! @@ -263,7 +263,7 @@ close (MAIL); is ($? >> 8, 0, "The mail gateway exited normally. yay"); $u = RT::User->new($RT::SystemUser); -$u->Load('doesnotexist-3@example.com'); +$u->Load("doesnotexist-3\@$RT::rtname"); ok( $u->Id != 0, " user exists and was created by ticket comment submission"); # }}} @@ -351,7 +351,7 @@ ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --act print MAIL <<EOF; From: root\@localhost -To: rtemail\@example.com +To: rtemail\@$RT::rtname Subject: This is a test of I18N ticket creation Content-Type: text/plain; charset="utf-8" @@ -385,7 +385,7 @@ ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --act print MAIL <<EOF; From: root\@localhost -To: rtemail\@example.com +To: rtemail\@$RT::rtname Subject: This is a test of I18N ticket creation Content-Type: text/plain; charset="utf-8" @@ -436,7 +436,7 @@ is( $tick->Owner, $RT::Nobody->Id, 'owner of the new ticket is nobody' ); ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action take"), "Opened the mailgate - $@"); print MAIL <<EOF; From: root\@localhost -Subject: [example.com \#$id] test +Subject: [$RT::rtname \#$id] test EOF close (MAIL); @@ -461,7 +461,7 @@ is( $tick->Owner, $RT::Nobody->Id, 'set owner back to nobody'); ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action take-correspond"), "Opened the mailgate - $@"); print MAIL <<EOF; From: root\@localhost -Subject: [example.com \#$id] correspondence +Subject: [$RT::rtname \#$id] correspondence test EOF @@ -474,14 +474,14 @@ is( $tick->Id, $id, 'load correct ticket'); is( $tick->OwnerObj->EmailAddress, 'root@localhost', 'successfuly take ticket via email'); my $txns = $tick->Transactions; $txns->Limit( FIELD => 'Type', VALUE => 'Correspond'); -is( $txns->Last->Subject, "[example.com \#$id] correspondence", 'successfuly add correspond within take via email' ); +is( $txns->Last->Subject, "[$RT::rtname \#$id] correspondence", 'successfuly add correspond within take via email' ); # +1 because of auto open is( $tick->Transactions->Count, 6, 'no superfluous transactions'); ok(open(MAIL, "|$RT::BinPath/rt-mailgate --url $RT::WebURL --queue general --action resolve"), "Opened the mailgate - $@"); print MAIL <<EOF; From: root\@localhost -Subject: [example.com \#$id] test +Subject: [$RT::rtname \#$id] test EOF close (MAIL); diff --git a/rt/lib/t/regression/07acl.t b/rt/lib/t/regression/07acl.t index bb1ccdc0b..e30a59bef 100644 --- a/rt/lib/t/regression/07acl.t +++ b/rt/lib/t/regression/07acl.t @@ -49,9 +49,9 @@ is($agent->{'status'}, 200, "Fetched the page ok"); ok($agent->{'content'} =~ /Logout/i, "Found a logout link"); # Test for absence of Configure and Preferences tabs. -ok(!$agent->find_link( url => '/Admin/', +ok(!$agent->find_link( url => "$RT::WebPath/Admin/", text => 'Configuration'), "No config tab" ); -ok(!$agent->find_link( url => '/User/Prefs.html', +ok(!$agent->find_link( url => "$RT::WebPath/User/Prefs.html", text => 'Preferences'), "No prefs pane" ); # Now test for their presence, one at a time. Sleep for a bit after @@ -59,18 +59,18 @@ ok(!$agent->find_link( url => '/User/Prefs.html', $user_obj->PrincipalObj->GrantRight(Right => 'ShowConfigTab'); $agent->reload(); ok($agent->{'content'} =~ /Logout/i, "Reloaded page successfully"); -ok($agent->find_link( url => '/Admin/', +ok($agent->find_link( url => "$RT::WebPath/Admin/", text => 'Configuration'), "Found config tab" ); $user_obj->PrincipalObj->RevokeRight(Right => 'ShowConfigTab'); $user_obj->PrincipalObj->GrantRight(Right => 'ModifySelf'); $agent->reload(); ok($agent->{'content'} =~ /Logout/i, "Reloaded page successfully"); -ok($agent->find_link( url => '/User/Prefs.html', +ok($agent->find_link( url => "$RT::WebPath/User/Prefs.html", text => 'Preferences'), "Found prefs pane" ); $user_obj->PrincipalObj->RevokeRight(Right => 'ModifySelf'); # Good. Now load the search page and test Load/Save Search. -$agent->follow_link( url => '/Search/Build.html', +$agent->follow_link( url => "$RT::WebPath/Search/Build.html", text => 'Tickets'); is($agent->{'status'}, 200, "Fetched search builder page"); ok($agent->{'content'} !~ /Load saved search/i, "No search loading box"); diff --git a/rt/lib/t/regression/07rights.t b/rt/lib/t/regression/07rights.t index 4764b49d4..d34627705 100644 --- a/rt/lib/t/regression/07rights.t +++ b/rt/lib/t/regression/07rights.t @@ -45,7 +45,7 @@ # # END BPS TAGGED BLOCK }}} -use Test::More tests => 14; +use Test::More tests => 26; use RT; RT::LoadConfig(); RT::Init(); @@ -108,3 +108,33 @@ is( $ticket->Owner, $user_id, "set correct owner" ); ok( $user->HasRight( Right => 'ReplyToTicket', Object => $ticket ), "user is owner and can reply to ticket" ); +# Testing of EquivObjects +$group = RT::Group->new( $RT::SystemUser ); +ok( $group->LoadQueueRoleGroup( Queue => $queue_id, Type=> 'AdminCc' ), "load queue AdminCc role group" ); +$ace = RT::ACE->new( $RT::SystemUser ); +my ($ace_id, $msg) = $group->PrincipalObj->GrantRight( Right => 'ModifyTicket', Object => $queue ); +ok( $ace_id, "Granted queue AdminCc role group with ModifyTicket right: $msg" ); +ok( $group->PrincipalObj->HasRight( Right => 'ModifyTicket', Object => $queue ), "role group can modify ticket" ); +ok( !$user->HasRight( Right => 'ModifyTicket', Object => $ticket ), "user is not AdminCc and can't modify ticket" ); +($status, $msg) = $ticket->AddWatcher(Type => 'AdminCc', PrincipalId => $user->PrincipalId); +ok( $status, "successfuly added user as AdminCc"); +ok( $user->HasRight( Right => 'ModifyTicket', Object => $ticket ), "user is AdminCc and can modify ticket" ); + +my $ticket2 = RT::Ticket->new($RT::SystemUser); +my ($ticket2_id) = $ticket2->Create( Queue => $queue_id, Subject => 'test2'); +ok( $ticket2_id, 'new ticket created' ); +ok( !$user->HasRight( Right => 'ModifyTicket', Object => $ticket2 ), "user is not AdminCc and can't modify ticket2" ); + +# now we can finally test EquivObjects +my $equiv = [ $ticket ]; +ok( $user->HasRight( Right => 'ModifyTicket', Object => $ticket2, EquivObjects => $equiv ), + "user is not AdminCc but can modify ticket2 because of EquivObjects" ); + +# the first a third test below are the same, so they should both pass +my $equiv2 = []; +ok( !$user->HasRight( Right => 'ModifyTicket', Object => $ticket2, EquivObjects => $equiv2 ), + "user is not AdminCc and can't modify ticket2" ); +ok( $user->HasRight( Right => 'ModifyTicket', Object => $ticket, EquivObjects => $equiv2 ), + "user is AdminCc and can modify ticket" ); +ok( !$user->HasRight( Right => 'ModifyTicket', Object => $ticket2, EquivObjects => $equiv2 ), + "user is not AdminCc and can't modify ticket2 (same question different answer)" ); diff --git a/rt/lib/t/regression/09record_cf_api.t b/rt/lib/t/regression/09record_cf_api.t index 1428a28db..78f111bd8 100644 --- a/rt/lib/t/regression/09record_cf_api.t +++ b/rt/lib/t/regression/09record_cf_api.t @@ -2,7 +2,7 @@ use strict; use warnings FATAL => 'all'; -use Test::More tests => 131; +use Test::More tests => 133; use RT; RT::LoadConfig(); @@ -21,6 +21,9 @@ my $queue = RT::Queue->new( $RT::SystemUser ); $queue->Create( Name => 'RecordCustomFields-'.$$ ); ok ($queue->id, "Created the queue"); +my $queue2 = RT::Queue->new( $RT::SystemUser ); +$queue2->Create( Name => 'RecordCustomFields2' ); + my $ticket = RT::Ticket->new( $RT::SystemUser ); $ticket->Create( Queue => $queue->Id, @@ -51,6 +54,11 @@ $global_cf3->Create( Name => 'RecordCustomFields3-'.$$, Type => 'SelectSingle', $global_cf3->AddValue( Name => 'RecordCustomFieldValues31' ); $global_cf3->AddValue( Name => 'RecordCustomFieldValues32' ); +my $local_cf4 = RT::CustomField->new( $RT::SystemUser ); +$local_cf4->Create( Name => 'RecordCustomFields4', Type => 'SelectSingle', Queue => $queue2->id ); +$local_cf4->AddValue( Name => 'RecordCustomFieldValues41' ); +$local_cf4->AddValue( Name => 'RecordCustomFieldValues42' ); + my @custom_fields = ($local_cf1, $local_cf2, $global_cf3); @@ -178,6 +186,14 @@ $test_add_delete_cycle->( sub { return $_[0]->id } ); # lets test cycle via CF object reference $test_add_delete_cycle->( sub { return $_[0] } ); +$ticket->AddCustomFieldValue( Field => $local_cf2->id , Value => 'Baz' ); +$ticket->AddCustomFieldValue( Field => $global_cf3->id , Value => 'Baz' ); +# now if we ask for cf values on RecordCustomFields4 we should not get any +$cfvs = $ticket->CustomFieldValues( 'RecordCustomFields4' ); +is( $cfvs->Count, 0, "No custom field values for non-Queue cf" ); +is( $ticket->FirstCustomFieldValue( 'RecordCustomFields4' ), undef, "No first custom field value for non-Queue cf" ); + + #SKIP: { # skip "TODO: should we add CF values to objects via CF Name?", 48; # names are not unique diff --git a/rt/lib/t/regression/14linking.t b/rt/lib/t/regression/14linking.t new file mode 100644 index 000000000..6fdf61405 --- /dev/null +++ b/rt/lib/t/regression/14linking.t @@ -0,0 +1,143 @@ +use Test::More tests => '39'; +use_ok('RT'); +use_ok('RT::Ticket'); +use_ok('RT::ScripConditions'); +use_ok('RT::ScripActions'); +use_ok('RT::Template'); +use_ok('RT::Scrips'); +use_ok('RT::Scrip'); +RT::LoadConfig(); +RT::Init(); + +use File::Temp qw/tempfile/; +my ($fh, $filename) = tempfile( UNLINK => 1, SUFFIX => '.rt'); +my $link_scrips_orig = $RT::LinkTransactionsRun1Scrip; +$RT::LinkTransactionsRun1Scrip = 1; + +my $condition = RT::ScripCondition->new( $RT::SystemUser ); +$condition->Load('User Defined'); +ok($condition->id); +my $action = RT::ScripAction->new( $RT::SystemUser ); +$action->Load('User Defined'); +ok($action->id); +my $template = RT::Template->new( $RT::SystemUser ); +$template->Load('Blank'); +ok($template->id); + +my $q1 = RT::Queue->new($RT::SystemUser); +my ($id,$msg) = $q1->Create(Name => "LinkTest1.$$"); +ok ($id,$msg); +my $q2 = RT::Queue->new($RT::SystemUser); +($id,$msg) = $q2->Create(Name => "LinkTest2.$$"); +ok ($id,$msg); + +my $commit_code = <<END; +open(FILE, "<$filename"); +my \$data = <FILE>; +chomp \$data; +close FILE; +open(FILE, ">$filename"); +if (\$self->TransactionObj->Type eq 'AddLink') { + print FILE \$data+1, "\n"; +} +else { + print FILE \$data-1, "\n"; +} +close FILE; +1; +END + +my $Scrips = RT::Scrips->new( $RT::SystemUser ); +$Scrips->UnLimit; +while ( my $Scrip = $Scrips->Next ) { + $Scrip->Delete if $Scrip->Description =~ /Add or Delete Link \d+/; +} + + +my $scrip = RT::Scrip->new($RT::SystemUser); +($id,$msg) = $scrip->Create( Description => "Add or Delete Link $$", + ScripCondition => $condition->id, + ScripAction => $action->id, + Template => $template->id, + Stage => 'TransactionCreate', + Queue => 0, + CustomIsApplicableCode => '$self->TransactionObj->Type =~ /(Add|Delete)Link/;', + CustomPrepareCode => '1;', + CustomCommitCode => $commit_code, + ); +ok($id, "Scrip created"); + +my $u1 = RT::User->new($RT::SystemUser); +($id,$msg) =$u1->Create(Name => "LinkTestUser.$$"); + +ok ($id,$msg); + +($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q1, Right => 'CreateTicket'); +ok ($id,$msg); +($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q1, Right => 'ModifyTicket'); +ok ($id,$msg); + +my $tid; + +my $creator = RT::CurrentUser->new($u1->id); + +my $ticket = RT::Ticket->new( $creator); +ok($ticket->isa('RT::Ticket')); +($id,$tid, $msg) = $ticket->Create(Subject => 'Link test 1', Queue => $q1->id); +ok ($id,$msg); + + +my $ticket2 = RT::Ticket->new($RT::SystemUser); +($id, $tid, $msg) = $ticket2->Create(Subject => 'Link test 2', Queue => $q2->id); +ok ($id, $msg); + +($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id); +ok(!$id,$msg); +ok(link_count($filename) == 0, "scrips ok"); +($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q2, Right => 'CreateTicket'); +ok ($id,$msg); +($id,$msg) = $u1->PrincipalObj->GrantRight ( Object => $q2, Right => 'ModifyTicket'); +ok ($id,$msg); +($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id); +ok($id,$msg); +ok(link_count($filename) == 1, "scrips ok"); +($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => -1); +ok(!$id,$msg); +ok(link_count($filename) == 1, "scrips ok"); + +my $transactions = $ticket2->Transactions; +$transactions->Limit( FIELD => 'Type', VALUE => 'AddLink' ); +ok( $transactions->Count == 1, "Transaction found in other ticket" ); +ok( $transactions->First->Field eq 'ReferredToBy'); +ok( $transactions->First->NewValue eq $ticket->URI ); + +($id,$msg) =$ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id); +ok($id,$msg); +ok(link_count($filename) == 0, "scrips ok"); +$transactions = $ticket2->Transactions; +$transactions->Limit( FIELD => 'Type', VALUE => 'DeleteLink' ); +ok( $transactions->Count == 1, "Transaction found in other ticket" ); +ok( $transactions->First->Field eq 'ReferredToBy'); +ok( $transactions->First->OldValue eq $ticket->URI ); + +$RT::LinkTransactionsRun1Scrip = 0; +($id,$msg) =$ticket->AddLink(Type => 'RefersTo', Target => $ticket2->id); +ok($id,$msg); +ok(link_count($filename) == 2, "scrips ok"); +($id,$msg) =$ticket->DeleteLink(Type => 'RefersTo', Target => $ticket2->id); +ok($id,$msg); +ok(link_count($filename) == 0, "scrips ok"); + +# restore +$RT::LinkTransactionsRun1Scrip = $link_scrips_orig; + +sub link_count { + + my $file = shift; + open(FILE, "<$file"); + my $data = <FILE>; + chomp $data; + return $data + 0; + close FILE; + +} diff --git a/rt/lib/t/regression/22search_tix_by_txn.t b/rt/lib/t/regression/22search_tix_by_txn.t index 54dad9860..958273c5c 100644 --- a/rt/lib/t/regression/22search_tix_by_txn.t +++ b/rt/lib/t/regression/22search_tix_by_txn.t @@ -1,6 +1,6 @@ #use Test::More tests => 26; use Test::More qw/no_plan/; - +$ENV{'TZ'} = 'GMT'; use RT; RT::LoadConfig(); RT::Init(); diff --git a/rt/lib/t/regression/22search_tix_by_watcher.t b/rt/lib/t/regression/22search_tix_by_watcher.t new file mode 100644 index 000000000..dd87de989 --- /dev/null +++ b/rt/lib/t/regression/22search_tix_by_watcher.t @@ -0,0 +1,215 @@ +#!/usr/bin/perl -w +use strict; +use warnings; + +use Test::More qw/no_plan/; +use_ok('RT'); +RT::LoadConfig(); +RT::Init(); +use RT::Ticket; + +my $q = RT::Queue->new($RT::SystemUser); +my $queue = 'SearchTests-'.rand(200); +$q->Create(Name => $queue); + +my @data = ( + { Subject => '1', Requestor => 'bravo@example.com' }, + { Subject => '2', Cc => 'alpha@example.com' }, +); + +my $total = 0; + +sub add_tix_from_data { + my @res = (); + while (@data) { + my $t = RT::Ticket->new($RT::SystemUser); + my ( $id, undef $msg ) = $t->Create( + Queue => $q->id, + %{ shift(@data) }, + ); + ok( $id, "ticket created" ) or diag("error: $msg"); + push @res, $t; + $total++; + } + return @res; +} +add_tix_from_data(); + +{ + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue'"); + is($tix->Count, $total, "found $total tickets"); +} + +{ + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND Requestor = 'bravo\@example.com'"); + is($tix->Count, 1, "found ticket(s)"); + is($tix->First->RequestorAddresses, 'bravo@example.com',"correct requestor"); +} + +{ + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND Cc = 'alpha\@example.com'"); + is($tix->Count, 1, "found ticket(s)"); + is($tix->First->CcAddresses, 'alpha@example.com', "correct Cc"); +} + +{ + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND (Cc = 'alpha\@example.com' OR Requestor = 'bravo\@example.com')"); + is($tix->Count, 2, "found ticket(s)"); + my @mails; + while (my $t = $tix->Next) { + push @mails, $t->RequestorAddresses; + push @mails, $t->CcAddresses; + } + @mails = sort grep $_, @mails; + is_deeply(\@mails, ['alpha@example.com', 'bravo@example.com'], "correct addresses"); +} + +{ + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND (Cc = 'alpha\@example.com' AND Requestor = 'bravo\@example.com')"); + is($tix->Count, 0, "found ticket(s)"); +} + +{ + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND Cc != 'alpha\@example.com'"); + is($tix->Count, 1, "found ticket(s)"); + is($tix->First->RequestorAddresses, 'bravo@example.com',"correct requestor"); +} + +@data = ( { Subject => '3' } ); +add_tix_from_data(); + +{ + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND Cc != 'alpha\@example.com'"); + is($tix->Count, 2, "found ticket(s)"); + my @mails; + while (my $t = $tix->Next) { push @mails, ($t->CcAddresses||'') } + is( scalar(grep 'alpha@example.com' eq $_, @mails), 0, "no tickets with non required data"); +} + +{ + # has no requestor search + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND Requestor IS NULL"); + is($tix->Count, 2, "found ticket(s)"); + my @mails; + while (my $t = $tix->Next) { push @mails, ($t->RequestorAddresses||'') } + is( scalar(grep $_, @mails), 0, "no tickets with non required data"); +} + +{ + # has at least one requestor search + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND Requestor IS NOT NULL"); + is($tix->Count, 1, "found ticket(s)"); + my @mails; + while (my $t = $tix->Next) { push @mails, ($t->RequestorAddresses||'') } + is( scalar(grep !$_, @mails), 0, "no tickets with non required data"); +} + +@data = ( { Subject => '3', Requestor => 'charly@example.com' } ); +add_tix_from_data(); + +{ + # has no requestor search + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND + (Requestor = 'bravo\@example.com' OR Requestor = 'charly\@example.com')"); + is($tix->Count, 2, "found ticket(s)"); + my @mails; + while (my $t = $tix->Next) { push @mails, ($t->RequestorAddresses||'') } + is_deeply( [sort @mails], + ['bravo@example.com', 'charly@example.com'], + "requestor addresses are correct" + ); +} + +# owner is special watcher because reference is duplicated in two places, +# owner was an ENUM field now it's WATCHERFIELD, but should support old +# style ENUM searches for backward compatibility +my $nobody = RT::Nobody(); +{ + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND Owner = '". $nobody->id ."'"); + is($tix->Count, 4, "found ticket(s)"); +} +{ + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND Owner = '". $nobody->Name ."'"); + is($tix->Count, 4, "found ticket(s)"); +} +{ + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND Owner != '". $nobody->id ."'"); + is($tix->Count, 0, "found ticket(s)"); +} +{ + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND Owner != '". $nobody->Name ."'"); + is($tix->Count, 0, "found ticket(s)"); +} + +{ + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND Owner.Name LIKE 'nob'"); + is($tix->Count, 4, "found ticket(s)"); +} + +{ + # create ticket and force type to not a 'ticket' value + # bug #6898@rt3.fsck.com + # and http://marc.theaimsgroup.com/?l=rt-devel&m=112662934627236&w=2 + @data = ( { Subject => 'not a ticket' } ); + my($t) = add_tix_from_data(); + $t->_Set( Field => 'Type', + Value => 'not a ticket', + CheckACL => 0, + RecordTransaction => 0, + ); + $total--; + + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND Owner = 'Nobody'"); + is($tix->Count, 4, "found ticket(s)"); +} + +{ + my $everyone = RT::Group->new( $RT::SystemUser ); + $everyone->LoadSystemInternalGroup('Everyone'); + ok($everyone->id, "loaded 'everyone' group"); + my($id, $msg) = $everyone->PrincipalObj->GrantRight( Right => 'OwnTicket', + Object => $q + ); + ok($id, "granted OwnTicket right to Everyone on '$queue'") or diag("error: $msg"); + + my $u = RT::User->new( $RT::SystemUser ); + $u->LoadByCols( EmailAddress => 'alpha@example.com' ); + ok($u->id, "loaded user"); + @data = ( { Subject => '4', Owner => $u->id } ); + my($t) = add_tix_from_data(); + is( $t->Owner, $u->id, "created ticket with custom owner" ); + my $u_alpha_id = $u->id; + + $u = RT::User->new( $RT::SystemUser ); + $u->LoadByCols( EmailAddress => 'bravo@example.com' ); + ok($u->id, "loaded user"); + @data = ( { Subject => '5', Owner => $u->id } ); + ($t) = add_tix_from_data(); + is( $t->Owner, $u->id, "created ticket with custom owner" ); + my $u_bravo_id = $u->id; + + my $tix = RT::Tickets->new($RT::SystemUser); + $tix->FromSQL("Queue = '$queue' AND + ( Owner = '$u_alpha_id' OR + Owner = '$u_bravo_id' )" + ); + is($tix->Count, 2, "found ticket(s)"); +} + +exit(0) |