RT 4.0.13
[freeside.git] / rt / t / shredder / 03plugin_tickets.t
1
2 use strict;
3 use warnings;
4
5 use Test::Deep;
6 use File::Spec;
7 use Test::More tests => 44 + 1; # plus one for warnings check
8 use RT::Test ();
9 BEGIN {
10     my $shredder_utils = RT::Test::get_relocatable_file('utils.pl',
11         File::Spec->curdir());
12     require $shredder_utils;
13 }
14
15
16 use_ok('RT::Shredder::Plugin::Tickets');
17 {
18     my $plugin = RT::Shredder::Plugin::Tickets->new;
19     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
20
21     is(lc $plugin->Type, 'search', 'correct type');
22 }
23
24 init_db();
25 create_savepoint('clean');
26 use_ok('RT::Ticket');
27 use_ok('RT::Tickets');
28
29 { # create parent and child and check functionality of 'with_linked' arg
30     my $parent = RT::Ticket->new( RT->SystemUser );
31     my ($pid) = $parent->Create( Subject => 'parent', Queue => 1 );
32     ok( $pid, "created new ticket" );
33     my $child = RT::Ticket->new( RT->SystemUser );
34     my ($cid) = $child->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
35     ok( $cid, "created new ticket" );
36     $_->ApplyTransactionBatch for $parent, $child;
37
38     my $plugin = RT::Shredder::Plugin::Tickets->new;
39     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
40
41     my ($status, $msg, @objs);
42     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
43     ok($status, "plugin arguments are ok") or diag "error: $msg";
44
45     ($status, @objs) = $plugin->Run;
46     ok($status, "executed plugin successfully") or diag "error: @objs";
47     @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
48     is(scalar @objs, 1, "only one object in result set");
49     is($objs[0]->id, $pid, "parent is in result set");
50
51     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
52     ok($status, "plugin arguments are ok") or diag "error: $msg";
53
54     ($status, @objs) = $plugin->Run;
55     ok($status, "executed plugin successfully") or diag "error: @objs";
56     @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
57     my %has = map { $_->id => 1 } @objs;
58     is(scalar @objs, 2, "two objects in the result set");
59     ok($has{$pid}, "parent is in the result set");
60     ok($has{$cid}, "child is in the result set");
61
62     my $shredder = shredder_new();
63     $shredder->PutObjects( Objects => \@objs );
64     $shredder->WipeoutAll;
65 }
66 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");
67
68 { # create parent and child and link them reqursively to check that we don't hang
69     my $parent = RT::Ticket->new( RT->SystemUser );
70     my ($pid) = $parent->Create( Subject => 'parent', Queue => 1 );
71     ok( $pid, "created new ticket" );
72
73     my $child = RT::Ticket->new( RT->SystemUser );
74     my ($cid) = $child->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
75     ok( $cid, "created new ticket" );
76
77     my ($status, $msg) = $child->AddLink( Target => $pid, Type => 'DependsOn' );
78     ok($status, "added reqursive link") or diag "error: $msg";
79
80     $_->ApplyTransactionBatch for $parent, $child;
81
82     my $plugin = RT::Shredder::Plugin::Tickets->new;
83     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
84
85     my (@objs);
86     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
87     ok($status, "plugin arguments are ok") or diag "error: $msg";
88
89     ($status, @objs) = $plugin->Run;
90     ok($status, "executed plugin successfully") or diag "error: @objs";
91     @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
92     is(scalar @objs, 1, "only one object in result set");
93     is($objs[0]->id, $pid, "parent is in result set");
94
95     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
96     ok($status, "plugin arguments are ok") or diag "error: $msg";
97
98     ($status, @objs) = $plugin->Run;
99     ok($status, "executed plugin successfully") or diag "error: @objs";
100     @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
101     is(scalar @objs, 2, "two objects in the result set");
102     my %has = map { $_->id => 1 } @objs;
103     ok($has{$pid}, "parent is in the result set");
104     ok($has{$cid}, "child is in the result set");
105
106     my $shredder = shredder_new();
107     $shredder->PutObjects( Objects => \@objs );
108     $shredder->WipeoutAll;
109 }
110 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");
111
112 { # create parent and child and check functionality of 'apply_query_to_linked' arg
113     my $parent = RT::Ticket->new( RT->SystemUser );
114     my ($pid) = $parent->Create( Subject => 'parent', Queue => 1 );
115     ok( $pid, "created new ticket" );
116     $parent->SetStatus('resolved');
117
118     my $child1 = RT::Ticket->new( RT->SystemUser );
119     my ($cid1) = $child1->Create( Subject => 'child', Queue => 1, MemberOf => $pid );
120     ok( $cid1, "created new ticket" );
121     my $child2 = RT::Ticket->new( RT->SystemUser );
122     my ($cid2) = $child2->Create( Subject => 'child', Queue => 1, MemberOf => $pid);
123     ok( $cid2, "created new ticket" );
124     $child2->SetStatus('resolved');
125
126     $_->ApplyTransactionBatch for $parent, $child1, $child2;
127
128     my $plugin = RT::Shredder::Plugin::Tickets->new;
129     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
130
131     my ($status, $msg) = $plugin->TestArgs( query => 'Status = "resolved"', apply_query_to_linked => 1 );
132     ok($status, "plugin arguments are ok") or diag "error: $msg";
133
134     my @objs;
135     ($status, @objs) = $plugin->Run;
136     ok($status, "executed plugin successfully") or diag "error: @objs";
137     @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
138     is(scalar @objs, 2, "two objects in the result set");
139     my %has = map { $_->id => 1 } @objs;
140     ok($has{$pid}, "parent is in the result set");
141     ok(!$has{$cid1}, "first child is in the result set");
142     ok($has{$cid2}, "second child is in the result set");
143
144     my $shredder = shredder_new();
145     $shredder->PutObjects( Objects => \@objs );
146     $shredder->WipeoutAll;
147
148     my $ticket = RT::Ticket->new( RT->SystemUser );
149     $ticket->Load( $cid1 );
150     is($ticket->id, $cid1, 'loaded ticket');
151
152     $shredder->PutObjects( Objects => $ticket );
153     $shredder->WipeoutAll;
154 }
155 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");