6 use RT::Test::Shredder tests => 49;
7 my $test = "RT::Test::Shredder";
9 use_ok('RT::Shredder');
11 use_ok('RT::Shredder::Plugin::Tickets');
13 my $plugin = RT::Shredder::Plugin::Tickets->new;
14 isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
16 is(lc $plugin->Type, 'search', 'correct type');
19 $test->create_savepoint('clean');
21 use_ok('RT::Tickets');
23 { # create parent and child and check functionality of 'with_linked' arg
24 my $parent = RT::Ticket->new( RT->SystemUser );
25 my ($pid) = $parent->Create( Subject => 'parent', Queue => 1 );
26 ok( $pid, "created new ticket" );
27 my $child = RT::Ticket->new( RT->SystemUser );
28 my ($cid) = $child->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
29 ok( $cid, "created new ticket" );
30 $_->ApplyTransactionBatch for $parent, $child;
32 my $plugin = RT::Shredder::Plugin::Tickets->new;
33 isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
35 my ($status, $msg, @objs);
36 ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
37 ok($status, "plugin arguments are ok") or diag "error: $msg";
39 ($status, @objs) = $plugin->Run;
40 ok($status, "executed plugin successfully") or diag "error: @objs";
41 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
42 is(scalar @objs, 1, "only one object in result set");
43 is($objs[0]->id, $pid, "parent is in result set");
45 ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
46 ok($status, "plugin arguments are ok") or diag "error: $msg";
48 ($status, @objs) = $plugin->Run;
49 ok($status, "executed plugin successfully") or diag "error: @objs";
50 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
51 my %has = map { $_->id => 1 } @objs;
52 is(scalar @objs, 2, "two objects in the result set");
53 ok($has{$pid}, "parent is in the result set");
54 ok($has{$cid}, "child is in the result set");
56 my $shredder = $test->shredder_new();
57 $shredder->PutObjects( Objects => \@objs );
58 $shredder->WipeoutAll;
61 cmp_deeply( $test->dump_current_and_savepoint('clean'), "current DB equal to savepoint");
63 { # create parent and child and link them reqursively to check that we don't hang
64 my $parent = RT::Ticket->new( RT->SystemUser );
65 my ($pid) = $parent->Create( Subject => 'parent', Queue => 1 );
66 ok( $pid, "created new ticket" );
68 my $child = RT::Ticket->new( RT->SystemUser );
69 my ($cid) = $child->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
70 ok( $cid, "created new ticket" );
72 my ($status, $msg) = $child->AddLink( Target => $pid, Type => 'DependsOn' );
73 ok($status, "added reqursive link") or diag "error: $msg";
75 $_->ApplyTransactionBatch for $parent, $child;
77 my $plugin = RT::Shredder::Plugin::Tickets->new;
78 isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
81 ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
82 ok($status, "plugin arguments are ok") or diag "error: $msg";
84 ($status, @objs) = $plugin->Run;
85 ok($status, "executed plugin successfully") or diag "error: @objs";
86 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
87 is(scalar @objs, 1, "only one object in result set");
88 is($objs[0]->id, $pid, "parent is in result set");
90 ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
91 ok($status, "plugin arguments are ok") or diag "error: $msg";
93 ($status, @objs) = $plugin->Run;
94 ok($status, "executed plugin successfully") or diag "error: @objs";
95 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
96 is(scalar @objs, 2, "two objects in the result set");
97 my %has = map { $_->id => 1 } @objs;
98 ok($has{$pid}, "parent is in the result set");
99 ok($has{$cid}, "child is in the result set");
101 my $shredder = $test->shredder_new();
102 $shredder->PutObjects( Objects => \@objs );
103 $shredder->WipeoutAll;
106 cmp_deeply( $test->dump_current_and_savepoint('clean'), "current DB equal to savepoint");
108 { # create parent and child and check functionality of 'apply_query_to_linked' arg
109 my $parent = RT::Ticket->new( RT->SystemUser );
110 my ($pid) = $parent->Create( Subject => 'parent', Queue => 1 );
111 ok( $pid, "created new ticket" );
112 $parent->SetStatus('resolved');
114 my $child1 = RT::Ticket->new( RT->SystemUser );
115 my ($cid1) = $child1->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
116 ok( $cid1, "created new ticket" );
117 my $child2 = RT::Ticket->new( RT->SystemUser );
118 my ($cid2) = $child2->Create( Subject => 'child', Queue => 1, MemberOf => $pid);
119 ok( $cid2, "created new ticket" );
120 $child2->SetStatus('resolved');
122 $_->ApplyTransactionBatch for $parent, $child1, $child2;
124 my $plugin = RT::Shredder::Plugin::Tickets->new;
125 isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
127 my ($status, $msg) = $plugin->TestArgs( query => 'Status = "resolved"', apply_query_to_linked => 1 );
128 ok($status, "plugin arguments are ok") or diag "error: $msg";
131 ($status, @objs) = $plugin->Run;
132 ok($status, "executed plugin successfully") or diag "error: @objs";
133 @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
134 is(scalar @objs, 2, "two objects in the result set");
135 my %has = map { $_->id => 1 } @objs;
136 ok($has{$pid}, "parent is in the result set");
137 ok(!$has{$cid1}, "first child is in the result set");
138 ok($has{$cid2}, "second child is in the result set");
140 my $shredder = $test->shredder_new();
141 $shredder->PutObjects( Objects => \@objs );
142 $shredder->WipeoutAll;
145 my $ticket = RT::Ticket->new( RT->SystemUser );
146 $ticket->Load( $cid1 );
147 is($ticket->id, $cid1, 'loaded ticket');
149 $shredder->PutObjects( Objects => $ticket );
150 $shredder->WipeoutAll;
153 cmp_deeply( $test->dump_current_and_savepoint('clean'), "current DB equal to savepoint");