8 use Test::More tests => 44;
11 my $shredder_utils = RT::Test::get_relocatable_file('utils.pl',
12 File::Spec->curdir());
13 require $shredder_utils;
17 use_ok('RT::Shredder::Plugin::Tickets');
19 my $plugin = new RT::Shredder::Plugin::Tickets;
20 isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
22 is(lc $plugin->Type, 'search', 'correct type');
26 create_savepoint('clean');
28 use_ok('RT::Tickets');
30 { # create parent and child and check functionality of 'with_linked' arg
31 my $parent = RT::Ticket->new( $RT::SystemUser );
32 my ($pid) = $parent->Create( Subject => 'parent', Queue => 1 );
33 ok( $pid, "created new ticket" );
35 my $child = RT::Ticket->new( $RT::SystemUser );
36 my ($cid) = $child->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
37 ok( $cid, "created new ticket" );
39 my $plugin = new RT::Shredder::Plugin::Tickets;
40 isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
42 my ($status, $msg, @objs);
43 ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
44 ok($status, "plugin arguments are ok") or diag "error: $msg";
46 ($status, @objs) = $plugin->Run;
47 ok($status, "executed plugin successfully") or diag "error: @objs";
48 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
49 is(scalar @objs, 1, "only one object in result set");
50 is($objs[0]->id, $pid, "parent is in result set");
52 ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
53 ok($status, "plugin arguments are ok") or diag "error: $msg";
55 ($status, @objs) = $plugin->Run;
56 ok($status, "executed plugin successfully") or diag "error: @objs";
57 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
58 my %has = map { $_->id => 1 } @objs;
59 is(scalar @objs, 2, "two objects in the result set");
60 ok($has{$pid}, "parent is in the result set");
61 ok($has{$cid}, "child is in the result set");
63 my $shredder = shredder_new();
64 $shredder->PutObjects( Objects => \@objs );
65 $shredder->WipeoutAll;
67 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");
69 { # create parent and child and link them reqursively to check that we don't hang
70 my $parent = RT::Ticket->new( $RT::SystemUser );
71 my ($pid) = $parent->Create( Subject => 'parent', Queue => 1 );
72 ok( $pid, "created new ticket" );
74 my $child = RT::Ticket->new( $RT::SystemUser );
75 my ($cid) = $child->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
76 ok( $cid, "created new ticket" );
78 my ($status, $msg) = $child->AddLink( Target => $pid, Type => 'DependsOn' );
79 ok($status, "added reqursive link") or diag "error: $msg";
81 my $plugin = new RT::Shredder::Plugin::Tickets;
82 isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
85 ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
86 ok($status, "plugin arguments are ok") or diag "error: $msg";
88 ($status, @objs) = $plugin->Run;
89 ok($status, "executed plugin successfully") or diag "error: @objs";
90 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
91 is(scalar @objs, 1, "only one object in result set");
92 is($objs[0]->id, $pid, "parent is in result set");
94 ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
95 ok($status, "plugin arguments are ok") or diag "error: $msg";
97 ($status, @objs) = $plugin->Run;
98 ok($status, "executed plugin successfully") or diag "error: @objs";
99 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
100 is(scalar @objs, 2, "two objects in the result set");
101 my %has = map { $_->id => 1 } @objs;
102 ok($has{$pid}, "parent is in the result set");
103 ok($has{$cid}, "child is in the result set");
105 my $shredder = shredder_new();
106 $shredder->PutObjects( Objects => \@objs );
107 $shredder->WipeoutAll;
109 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");
111 { # create parent and child and check functionality of 'apply_query_to_linked' arg
112 my $parent = RT::Ticket->new( $RT::SystemUser );
113 my ($pid) = $parent->Create( Subject => 'parent', Queue => 1, Status => 'resolved' );
114 ok( $pid, "created new ticket" );
116 my $child1 = RT::Ticket->new( $RT::SystemUser );
117 my ($cid1) = $child1->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
118 ok( $cid1, "created new ticket" );
119 my $child2 = RT::Ticket->new( $RT::SystemUser );
120 my ($cid2) = $child2->Create( Subject => 'child', Queue => 1, MemberOf => $pid, Status => 'resolved' );
121 ok( $cid2, "created new ticket" );
123 my $plugin = new RT::Shredder::Plugin::Tickets;
124 isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
126 my ($status, $msg) = $plugin->TestArgs( query => 'Status = "resolved"', apply_query_to_linked => 1 );
127 ok($status, "plugin arguments are ok") or diag "error: $msg";
130 ($status, @objs) = $plugin->Run;
131 ok($status, "executed plugin successfully") or diag "error: @objs";
132 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
133 is(scalar @objs, 2, "two objects in the result set");
134 my %has = map { $_->id => 1 } @objs;
135 ok($has{$pid}, "parent is in the result set");
136 ok(!$has{$cid1}, "first child is in the result set");
137 ok($has{$cid2}, "second child is in the result set");
139 my $shredder = shredder_new();
140 $shredder->PutObjects( Objects => \@objs );
141 $shredder->WipeoutAll;
143 my $ticket = RT::Ticket->new( $RT::SystemUser );
144 $ticket->Load( $cid1 );
145 is($ticket->id, $cid1, 'loaded ticket');
147 $shredder->PutObjects( Objects => $ticket );
148 $shredder->WipeoutAll;
150 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");