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