fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / shredder / 03plugin_tickets.t
1
2 use strict;
3 use warnings;
4
5 use Test::Deep;
6 use RT::Test::Shredder tests => 49;
7 my $test = "RT::Test::Shredder";
8
9 use_ok('RT::Shredder');
10
11 use_ok('RT::Shredder::Plugin::Tickets');
12 {
13     my $plugin = RT::Shredder::Plugin::Tickets->new;
14     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
15
16     is(lc $plugin->Type, 'search', 'correct type');
17 }
18
19 $test->create_savepoint('clean');
20 use_ok('RT::Ticket');
21 use_ok('RT::Tickets');
22
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;
31
32     my $plugin = RT::Shredder::Plugin::Tickets->new;
33     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
34
35     my ($status, $msg, @objs);
36     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
37     ok($status, "plugin arguments are ok") or diag "error: $msg";
38
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");
44
45     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
46     ok($status, "plugin arguments are ok") or diag "error: $msg";
47
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");
55
56     my $shredder = $test->shredder_new();
57     $shredder->PutObjects( Objects => \@objs );
58     $shredder->WipeoutAll;
59     $test->db_is_valid;
60 }
61 cmp_deeply( $test->dump_current_and_savepoint('clean'), "current DB equal to savepoint");
62
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" );
67
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" );
71
72     my ($status, $msg) = $child->AddLink( Target => $pid, Type => 'DependsOn' );
73     ok($status, "added reqursive link") or diag "error: $msg";
74
75     $_->ApplyTransactionBatch for $parent, $child;
76
77     my $plugin = RT::Shredder::Plugin::Tickets->new;
78     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
79
80     my (@objs);
81     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
82     ok($status, "plugin arguments are ok") or diag "error: $msg";
83
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");
89
90     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
91     ok($status, "plugin arguments are ok") or diag "error: $msg";
92
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");
100
101     my $shredder = $test->shredder_new();
102     $shredder->PutObjects( Objects => \@objs );
103     $shredder->WipeoutAll;
104     $test->db_is_valid;
105 }
106 cmp_deeply( $test->dump_current_and_savepoint('clean'), "current DB equal to savepoint");
107
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');
113
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');
121
122     $_->ApplyTransactionBatch for $parent, $child1, $child2;
123
124     my $plugin = RT::Shredder::Plugin::Tickets->new;
125     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
126
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";
129
130     my @objs;
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");
139
140     my $shredder = $test->shredder_new();
141     $shredder->PutObjects( Objects => \@objs );
142     $shredder->WipeoutAll;
143     $test->db_is_valid;
144
145     my $ticket = RT::Ticket->new( RT->SystemUser );
146     $ticket->Load( $cid1 );
147     is($ticket->id, $cid1, 'loaded ticket');
148
149     $shredder->PutObjects( Objects => $ticket );
150     $shredder->WipeoutAll;
151     $test->db_is_valid;
152 }
153 cmp_deeply( $test->dump_current_and_savepoint('clean'), "current DB equal to savepoint");