Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / t / shredder / 03plugin_tickets.t
1 #!/usr/bin/perl -w
2
3 use strict;
4 use warnings;
5
6 use Test::Deep;
7 use File::Spec;
8 use Test::More tests => 44;
9 use RT::Test ();
10 BEGIN {
11     my $shredder_utils = RT::Test::get_relocatable_file('utils.pl',
12         File::Spec->curdir());
13     require $shredder_utils;
14 }
15
16
17 use_ok('RT::Shredder::Plugin::Tickets');
18 {
19     my $plugin = RT::Shredder::Plugin::Tickets->new;
20     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
21
22     is(lc $plugin->Type, 'search', 'correct type');
23 }
24
25 init_db();
26 create_savepoint('clean');
27 use_ok('RT::Ticket');
28 use_ok('RT::Tickets');
29
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;
38
39     my $plugin = RT::Shredder::Plugin::Tickets->new;
40     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
41
42     my ($status, $msg, @objs);
43     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
44     ok($status, "plugin arguments are ok") or diag "error: $msg";
45
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");
51
52     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
53     ok($status, "plugin arguments are ok") or diag "error: $msg";
54
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");
62
63     my $shredder = shredder_new();
64     $shredder->PutObjects( Objects => \@objs );
65     $shredder->WipeoutAll;
66 }
67 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");
68
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" );
73
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" );
77
78     my ($status, $msg) = $child->AddLink( Target => $pid, Type => 'DependsOn' );
79     ok($status, "added reqursive link") or diag "error: $msg";
80
81     $_->ApplyTransactionBatch for $parent, $child;
82
83     my $plugin = RT::Shredder::Plugin::Tickets->new;
84     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
85
86     my (@objs);
87     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
88     ok($status, "plugin arguments are ok") or diag "error: $msg";
89
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");
95
96     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
97     ok($status, "plugin arguments are ok") or diag "error: $msg";
98
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");
106
107     my $shredder = shredder_new();
108     $shredder->PutObjects( Objects => \@objs );
109     $shredder->WipeoutAll;
110 }
111 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");
112
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');
118
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');
126
127     $_->ApplyTransactionBatch for $parent, $child1, $child2;
128
129     my $plugin = RT::Shredder::Plugin::Tickets->new;
130     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
131
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";
134
135     my @objs;
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");
144
145     my $shredder = shredder_new();
146     $shredder->PutObjects( Objects => \@objs );
147     $shredder->WipeoutAll;
148
149     my $ticket = RT::Ticket->new( RT->SystemUser );
150     $ticket->Load( $cid1 );
151     is($ticket->id, $cid1, 'loaded ticket');
152
153     $shredder->PutObjects( Objects => $ticket );
154     $shredder->WipeoutAll;
155 }
156 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");