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 = RT::Shredder::Plugin::Tickets->new;
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" );
34 my $child = RT::Ticket->new( RT->SystemUser );
35 my ($cid) = $child->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
36 ok( $cid, "created new ticket" );
37 $_->ApplyTransactionBatch for $parent, $child;
39 my $plugin = RT::Shredder::Plugin::Tickets->new;
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 $_->ApplyTransactionBatch for $parent, $child;
83 my $plugin = RT::Shredder::Plugin::Tickets->new;
84 isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
87 ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
88 ok($status, "plugin arguments are ok") or diag "error: $msg";
90 ($status, @objs) = $plugin->Run;
91 ok($status, "executed plugin successfully") or diag "error: @objs";
92 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
93 is(scalar @objs, 1, "only one object in result set");
94 is($objs[0]->id, $pid, "parent is in result set");
96 ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
97 ok($status, "plugin arguments are ok") or diag "error: $msg";
99 ($status, @objs) = $plugin->Run;
100 ok($status, "executed plugin successfully") or diag "error: @objs";
101 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
102 is(scalar @objs, 2, "two objects in the result set");
103 my %has = map { $_->id => 1 } @objs;
104 ok($has{$pid}, "parent is in the result set");
105 ok($has{$cid}, "child is in the result set");
107 my $shredder = shredder_new();
108 $shredder->PutObjects( Objects => \@objs );
109 $shredder->WipeoutAll;
111 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");
113 { # create parent and child and check functionality of 'apply_query_to_linked' arg
114 my $parent = RT::Ticket->new( RT->SystemUser );
115 my ($pid) = $parent->Create( Subject => 'parent', Queue => 1 );
116 ok( $pid, "created new ticket" );
117 $parent->SetStatus('resolved');
119 my $child1 = RT::Ticket->new( RT->SystemUser );
120 my ($cid1) = $child1->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
121 ok( $cid1, "created new ticket" );
122 my $child2 = RT::Ticket->new( RT->SystemUser );
123 my ($cid2) = $child2->Create( Subject => 'child', Queue => 1, MemberOf => $pid);
124 ok( $cid2, "created new ticket" );
125 $child2->SetStatus('resolved');
127 $_->ApplyTransactionBatch for $parent, $child1, $child2;
129 my $plugin = RT::Shredder::Plugin::Tickets->new;
130 isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
132 my ($status, $msg) = $plugin->TestArgs( query => 'Status = "resolved"', apply_query_to_linked => 1 );
133 ok($status, "plugin arguments are ok") or diag "error: $msg";
136 ($status, @objs) = $plugin->Run;
137 ok($status, "executed plugin successfully") or diag "error: @objs";
138 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
139 is(scalar @objs, 2, "two objects in the result set");
140 my %has = map { $_->id => 1 } @objs;
141 ok($has{$pid}, "parent is in the result set");
142 ok(!$has{$cid1}, "first child is in the result set");
143 ok($has{$cid2}, "second child is in the result set");
145 my $shredder = shredder_new();
146 $shredder->PutObjects( Objects => \@objs );
147 $shredder->WipeoutAll;
149 my $ticket = RT::Ticket->new( RT->SystemUser );
150 $ticket->Load( $cid1 );
151 is($ticket->id, $cid1, 'loaded ticket');
153 $shredder->PutObjects( Objects => $ticket );
154 $shredder->WipeoutAll;
156 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");