fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / ticket / link_search.t
1 use strict;
2 use warnings;
3 use RT;
4
5 # Load the config file
6 use RT::Test tests => 63;
7
8 #Connect to the database and get RT::SystemUser and RT::Nobody loaded
9
10
11 #Get the current user all loaded
12 my $CurrentUser = RT->SystemUser;
13
14 my $queue = RT::Queue->new($CurrentUser);
15 $queue->Load('General') || Abort(loc("Queue could not be loaded."));
16
17 my $child_ticket = RT::Ticket->new( $CurrentUser );
18 my ($childid) = $child_ticket->Create(
19     Subject => 'test child',
20     Queue => $queue->Id,
21 );
22 ok($childid, "We created a child ticket");
23
24 my $parent_ticket = RT::Ticket->new( $CurrentUser );
25 my ($parentid) = $parent_ticket->Create(
26     Subject => 'test parent',
27     Children => [ $childid ],
28     Queue => $queue->Id,
29 );
30 ok($parentid, "We created a parent ticket");
31
32
33 my $Collection = RT::Tickets->new($CurrentUser);
34 $Collection->LimitMemberOf( $parentid );
35 is($Collection->Count,1, "We found only one result");
36 ok($Collection->First);
37 is($Collection->First->id, $childid, "We found the collection of all children of $parentid with Limit");
38
39 $Collection = RT::Tickets->new($CurrentUser);
40 $Collection->FromSQL("MemberOf = $parentid");
41 is($Collection->Count, 1, "We found only one result");
42 ok($Collection->First);
43 is($Collection->First->id, $childid, "We found the collection of all children of $parentid with TicketSQL");
44
45
46 $Collection = RT::Tickets->new($CurrentUser);
47 $Collection->LimitHasMember ($childid);
48 is($Collection->Count,1, "We found only one result");
49 ok($Collection->First);
50 is($Collection->First->id, $parentid, "We found the collection of all parents of $childid with Limit");
51
52
53 $Collection = RT::Tickets->new($CurrentUser);
54 $Collection->FromSQL("HasMember = $childid");
55 is($Collection->Count,1, "We found only one result");
56 ok($Collection->First);
57 is($Collection->First->id, $parentid, "We found the collection of all parents of $childid with TicketSQL");
58
59
60 # Now we find a collection of all the tickets which have no members. they should have no children.
61 $Collection = RT::Tickets->new($CurrentUser);
62 $Collection->LimitHasMember('');
63 # must contain child; must not contain parent
64 my %has;
65 while (my $t = $Collection->Next) {
66     ++$has{$t->id};
67 }
68 ok( $has{$childid}, "The collection has our child - $childid");
69 ok( !$has{$parentid}, "The collection doesn't have our parent - $parentid");
70
71
72 # Now we find a collection of all the tickets which are not members of anything. they should have no parents.
73 $Collection = RT::Tickets->new($CurrentUser);
74 $Collection->LimitMemberOf('');
75 # must contain parent; must not contain child
76 %has = ();
77 while (my $t = $Collection->Next) {
78     ++$has{$t->id};
79 }
80 ok ($has{$parentid} , "The collection has our parent - $parentid");
81 ok( !$has{$childid}, "The collection doesn't have our child - $childid");
82
83
84 #  Do it all over with TicketSQL
85 #
86
87
88
89 # Now we find a collection of all the tickets which have no members. they should have no children.
90 $Collection = RT::Tickets->new($CurrentUser);
91 $Collection->FromSQL ("HasMember IS NULL");
92 # must contain parent; must not contain child
93 %has = ();
94 while (my $t = $Collection->Next) {
95     ++$has{$t->id};
96 }
97 ok( !$has{$parentid}, "The collection doesn't have our parent - $parentid");
98 ok( $has{$childid}, "The collection has our child - $childid");
99
100
101 # Now we find a collection of all the tickets which have no members. they should have no children.
102 # Alternate syntax
103 $Collection = RT::Tickets->new($CurrentUser);
104 $Collection->FromSQL("HasMember = ''");
105 # must contain parent; must not contain child
106 %has = ();
107 while (my $t = $Collection->Next) {
108     ++$has{$t->id};
109 }
110 ok( !$has{$parentid}, "The collection doesn't have our parent - $parentid");
111 ok( $has{$childid}, "The collection has our child - $childid");
112
113
114 # Now we find a collection of all the tickets which are not members of anything. they should have no parents.
115 $Collection = RT::Tickets->new($CurrentUser);
116 $Collection->FromSQL("MemberOf IS NULL");
117 # must not  contain parent; must contain parent
118 %has = ();
119 while (my $t = $Collection->Next) {
120     ++$has{$t->id};
121 }
122 ok( $has{$parentid}, "The collection has our parent - $parentid");
123 ok( !$has{$childid}, "The collection doesn't have our child - $childid");
124
125
126 # Now we find a collection of all the tickets which are not members of anything. they should have no parents.
127 $Collection = RT::Tickets->new($CurrentUser);
128 $Collection->FromSQL("MemberOf = ''");
129 # must not  contain parent; must contain parent
130 %has = ();
131 while (my $t = $Collection->Next) {
132     ++$has{$t->id};
133 }
134 ok( $has{$parentid}, "The collection has our parent - $parentid");
135 ok( !$has{$childid}, "The collection doesn't have our child - $childid");
136
137
138 # Now we find a collection of all the tickets which are not members of the parent ticket
139 $Collection = RT::Tickets->new($CurrentUser);
140 $Collection->FromSQL("MemberOf != $parentid");
141 %has = ();
142 while (my $t = $Collection->Next) {
143     ++$has{$t->id};
144 }
145 ok( $has{$parentid}, "The collection has our parent - $parentid");
146 ok( !$has{$childid}, "The collection doesn't have our child - $childid");
147
148 $Collection = RT::Tickets->new($CurrentUser);
149 $Collection->LimitMemberOf($parentid, OPERATOR => '!=');
150 %has = ();
151 while (my $t = $Collection->Next) {
152     ++$has{$t->id};
153 }
154 ok( $has{$parentid}, "The collection has our parent - $parentid");
155 ok( !$has{$childid}, "The collection doesn't have our child - $childid");
156
157 my $grand_child_ticket = RT::Ticket->new( $CurrentUser );
158 my ($grand_childid) = $child_ticket->Create(
159     Subject => 'test child',
160     Queue   => $queue->Id,
161     MemberOf => $childid,
162 );
163 ok($childid, "We created a grand child ticket");
164
165 my $unlinked_ticket = RT::Ticket->new( $CurrentUser );
166 my ($unlinked_id) = $child_ticket->Create(
167     Subject => 'test unlinked',
168     Queue   => $queue->Id,
169 );
170 ok($unlinked_id, "We created a grand child ticket");
171
172 $Collection = RT::Tickets->new($CurrentUser);
173 $Collection->FromSQL( "LinkedTo = $childid" );
174 is($Collection->Count,1, "We found only one result");
175 ok($Collection->First);
176 is($Collection->First->id, $grand_childid, "We found all tickets linked to ticket #$childid");
177
178 $Collection = RT::Tickets->new($CurrentUser);
179 $Collection->FromSQL( "LinkedFrom = $childid" );
180 is($Collection->Count,1, "We found only one result");
181 ok($Collection->First);
182 is($Collection->First->id, $parentid, "We found all tickets linked from ticket #$childid");
183
184 $Collection = RT::Tickets->new($CurrentUser);
185 $Collection->FromSQL( "LinkedTo IS NULL" );
186 ok($Collection->Count, "Result is set is not empty");
187 %has = ();
188 while (my $t = $Collection->Next) {
189     ++$has{$t->id};
190 }
191 ok( $has{$parentid}, "parent is in collection");
192 ok( $has{$unlinked_id}, "unlinked is in collection");
193 ok( !$has{$childid}, "child is NOT in collection");
194 ok( !$has{$grand_childid}, "grand child too is not in collection");
195
196 $Collection = RT::Tickets->new($CurrentUser);
197 $Collection->FromSQL( "LinkedTo IS NOT NULL" );
198 ok($Collection->Count, "Result set is not empty");
199 %has = ();
200 while (my $t = $Collection->Next) {
201     ++$has{$t->id};
202 }
203 ok( !$has{$parentid}, "The collection has no our parent - $parentid");
204 ok( !$has{$unlinked_id}, "unlinked is not in collection");
205 ok( $has{$childid}, "The collection have our child - $childid");
206 ok( $has{$grand_childid}, "The collection have our grand child - $grand_childid");
207
208 $Collection = RT::Tickets->new($CurrentUser);
209 $Collection->FromSQL( "LinkedFrom IS NULL" );
210 ok($Collection->Count, "Result is set is not empty");
211 %has = ();
212 while (my $t = $Collection->Next) {
213     ++$has{$t->id};
214 }
215 ok( !$has{$parentid}, "parent is NOT in collection");
216 ok( !$has{$childid}, "child is NOT in collection");
217 ok( $has{$grand_childid}, "grand child is in collection");
218 ok( $has{$unlinked_id}, "unlinked is in collection");
219
220 $Collection = RT::Tickets->new($CurrentUser);
221 $Collection->FromSQL( "LinkedFrom IS NOT NULL" );
222 ok($Collection->Count, "Result set is not empty");
223 %has = ();
224 while (my $t = $Collection->Next) {
225     ++$has{$t->id};
226 }
227 ok( $has{$parentid}, "The collection has our parent - $parentid");
228 ok( $has{$childid}, "The collection have our child - $childid");
229 ok( !$has{$grand_childid}, "The collection have no our grand child - $grand_childid");
230 ok( !$has{$unlinked_id}, "unlinked is not in collection");
231
232 $Collection = RT::Tickets->new($CurrentUser);
233 $Collection->FromSQL( "Linked = $childid" );
234 is($Collection->Count, 2, "We found two tickets: parent and child");
235 %has = ();
236 while (my $t = $Collection->Next) {
237     ++$has{$t->id};
238 }
239 ok( !$has{$childid}, "Ticket is not linked to itself");
240 ok( $has{$parentid}, "The collection has our parent");
241 ok( $has{$grand_childid}, "The collection have our child");
242 ok( !$has{$unlinked_id}, "unlinked is not in collection");
243