+sub delete_queue_watchers {
+ my $self = shift;
+ my @queues = @_;
+
+ foreach my $q ( @queues ) {
+ foreach my $t (qw(Cc AdminCc) ) {
+ $q->DeleteWatcher( Type => $t, PrincipalId => $_->MemberId )
+ foreach @{ $q->$t()->MembersObj->ItemsArrayRef };
+ }
+ }
+}
+
+sub create_tickets {
+ local $Test::Builder::Level = $Test::Builder::Level + 1;
+
+ my $self = shift;
+ my $defaults = shift;
+ my @data = @_;
+ @data = sort { rand(100) <=> rand(100) } @data
+ if delete $defaults->{'RandomOrder'};
+
+ $defaults->{'Queue'} ||= 'General';
+
+ my @res = ();
+ while ( @data ) {
+ my %args = %{ shift @data };
+ $args{$_} = $res[ $args{$_} ]->id foreach
+ grep $args{ $_ }, keys %RT::Link::TYPEMAP;
+ push @res, $self->create_ticket( %$defaults, %args );
+ }
+ return @res;
+}
+
+sub create_ticket {
+ local $Test::Builder::Level = $Test::Builder::Level + 1;
+
+ my $self = shift;
+ my %args = @_;
+
+ if ( blessed $args{'Queue'} ) {
+ $args{Queue} = $args{'Queue'}->id;
+ }
+ elsif ($args{Queue} && $args{Queue} =~ /\D/) {
+ my $queue = RT::Queue->new(RT->SystemUser);
+ if (my $id = $queue->Load($args{Queue}) ) {
+ $args{Queue} = $id;
+ } else {
+ die ("Error: Invalid queue $args{Queue}");
+ }
+ }
+
+ if ( my $content = delete $args{'Content'} ) {
+ $args{'MIMEObj'} = MIME::Entity->build(
+ From => Encode::encode( "UTF-8", $args{'Requestor'} ),
+ Subject => RT::Interface::Email::EncodeToMIME( String => $args{'Subject'} ),
+ Type => (defined $args{ContentType} ? $args{ContentType} : "text/plain"),
+ Charset => "UTF-8",
+ Data => Encode::encode( "UTF-8", $content ),
+ );
+ }
+
+ if ( my $cfs = delete $args{'CustomFields'} ) {
+ my $q = RT::Queue->new( RT->SystemUser );
+ $q->Load( $args{'Queue'} );
+ while ( my ($k, $v) = each %$cfs ) {
+ my $cf = $q->CustomField( $k );
+ unless ($cf->id) {
+ RT->Logger->error("Couldn't load custom field $k");
+ next;
+ }
+
+ $args{'CustomField-'. $cf->id} = $v;
+ }
+ }
+
+ my $ticket = RT::Ticket->new( RT->SystemUser );
+ my ( $id, undef, $msg ) = $ticket->Create( %args );
+ Test::More::ok( $id, "ticket created" )
+ or Test::More::diag("error: $msg");
+
+ # hackish, but simpler
+ if ( $args{'LastUpdatedBy'} ) {
+ $ticket->__Set( Field => 'LastUpdatedBy', Value => $args{'LastUpdatedBy'} );
+ }
+
+
+ for my $field ( keys %args ) {
+ #TODO check links and watchers
+
+ if ( $field =~ /CustomField-(\d+)/ ) {
+ my $cf = $1;
+ my $got = join ',', sort map $_->Content,
+ @{ $ticket->CustomFieldValues($cf)->ItemsArrayRef };
+ my $expected = ref $args{$field}
+ ? join( ',', sort @{ $args{$field} } )
+ : $args{$field};
+ Test::More::is( $got, $expected, 'correct CF values' );
+ }
+ else {
+ next if ref $args{$field};
+ next unless $ticket->can($field) or $ticket->_Accessible($field,"read");
+ next if ref $ticket->$field();
+ Test::More::is( $ticket->$field(), $args{$field}, "$field is correct" );
+ }
+ }
+
+ return $ticket;
+}
+
+sub delete_tickets {
+ my $self = shift;
+ my $query = shift;
+ my $tickets = RT::Tickets->new( RT->SystemUser );
+ if ( $query ) {
+ $tickets->FromSQL( $query );
+ }
+ else {
+ $tickets->UnLimit;
+ }
+ while ( my $ticket = $tickets->Next ) {
+ $ticket->Delete;
+ }
+}
+