fix quantity-less ordering
[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
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     my $plugin = RT::Shredder::Plugin::Tickets->new;
81     isa_ok($plugin, 'RT::Shredder::Plugin::Tickets');
82
83     my (@objs);
84     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"' );
85     ok($status, "plugin arguments are ok") or diag "error: $msg";
86
87     ($status, @objs) = $plugin->Run;
88     ok($status, "executed plugin successfully") or diag "error: @objs";
89     @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
90     is(scalar @objs, 1, "only one object in result set");
91     is($objs[0]->id, $pid, "parent is in result set");
92
93     ($status, $msg) = $plugin->TestArgs( query => 'Subject = "parent"', with_linked => 1 );
94     ok($status, "plugin arguments are ok") or diag "error: $msg";
95
96     ($status, @objs) = $plugin->Run;
97     ok($status, "executed plugin successfully") or diag "error: @objs";
98     @objs = RT::Shredder->CastObjectsToRecords( Objects => \@objs );
99     is(scalar @objs, 2, "two objects in the result set");
100     my %has = map { $_->id => 1 } @objs;
101     ok($has{$pid}, "parent is in the result set");
102     ok($has{$cid}, "child is in the result set");
103
104     my $shredder = shredder_new();
105     $shredder->PutObjects( Objects => \@objs );
106     $shredder->WipeoutAll;
107 }
108 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");
109
110 { # create parent and child and check functionality of 'apply_query_to_linked' arg
111     my $parent = RT::Ticket->new( RT->SystemUser );
112     my ($pid) = $parent->Create( Subject => 'parent', Queue => 1 );
113     ok( $pid, "created new ticket" );
114     $parent->SetStatus('resolved');
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);
121     ok( $cid2, "created new ticket" );
122     $child2->SetStatus('resolved');
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 = shredder_new();
141     $shredder->PutObjects( Objects => \@objs );
142     $shredder->WipeoutAll;
143
144     my $ticket = RT::Ticket->new( RT->SystemUser );
145     $ticket->Load( $cid1 );
146     is($ticket->id, $cid1, 'loaded ticket');
147
148     $shredder->PutObjects( Objects => $ticket );
149     $shredder->WipeoutAll;
150 }
151 cmp_deeply( dump_current_and_savepoint('clean'), "current DB equal to savepoint");