import rt 3.8.7
[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 = new RT::Shredder::Plugin::Tickets;
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
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" );
38
39     my $plugin = new RT::Shredder::Plugin::Tickets;
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     my $plugin = new RT::Shredder::Plugin::Tickets;
82     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
83
84     my (@objs);
85     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
86     ok($status, "plugin arguments are ok") or diag "error: $msg";
87
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");
93
94     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
95     ok($status, "plugin arguments are ok") or diag "error: $msg";
96
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");
104
105     my $shredder = shredder_new();
106     $shredder->PutObjects( Objects => \@objs );
107     $shredder->WipeoutAll;
108 }
109 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");
110
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" );
115
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" );
122
123     my $plugin = new RT::Shredder::Plugin::Tickets;
124     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
125
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";
128
129     my @objs;
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");
138
139     my $shredder = shredder_new();
140     $shredder->PutObjects( Objects => \@objs );
141     $shredder->WipeoutAll;
142
143     my $ticket = RT::Ticket->new( $RT::SystemUser );
144     $ticket->Load( $cid1 );
145     is($ticket->id, $cid1, 'loaded ticket');
146
147     $shredder->PutObjects( Objects => $ticket );
148     $shredder->WipeoutAll;
149 }
150 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");