RT 4.0.13
[freeside.git] / rt / t / articles / article.t
1
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 67;
6
7 use_ok 'RT::Articles';
8 use_ok 'RT::Classes';
9 use_ok 'RT::Class';
10
11 my $CLASS = 'ArticleTest-'.$$;
12
13 my $user = RT::CurrentUser->new('root');
14
15 my $class = RT::Class->new($user);
16
17
18 my ($id, $msg) = $class->Create(Name =>$CLASS);
19 ok ($id, $msg);
20
21
22
23 my $article = RT::Article->new($user);
24 ok (UNIVERSAL::isa($article, 'RT::Article'));
25 ok (UNIVERSAL::isa($article, 'RT::Record'));
26 ok (UNIVERSAL::isa($article, 'RT::Record'));
27 ok (UNIVERSAL::isa($article, 'DBIx::SearchBuilder::Record') , "It's a searchbuilder record!");
28
29
30 ($id, $msg) = $article->Create( Class => $CLASS, Summary => $CLASS);
31 ok ($id, $msg);
32 $article->Load($id);
33 is ($article->Summary, $CLASS, "The summary is set correct");
34 my $at = RT::Article->new($RT::SystemUser);
35 $at->Load($id);
36 is ($at->id , $id);
37 is ($at->Summary, $article->Summary);
38
39
40
41
42 my  $a1 = RT::Article->new($RT::SystemUser);
43  ($id, $msg)  = $a1->Create(Class => $class->id, Name => 'ValidateNameTest'.$$);
44 ok ($id, $msg);
45
46
47
48 my  $a2 = RT::Article->new($RT::SystemUser);
49 ($id, $msg)  = $a2->Create(Class => $class->id, Name => 'ValidateNameTest'.$$);
50 ok (!$id, $msg);
51
52 my  $a3 = RT::Article->new($RT::SystemUser);
53 ($id, $msg)  = $a3->Create(Class => $class->id, Name => 'ValidateNameTest2'.$$);
54 ok ($id, $msg);
55 ($id, $msg) =$a3->SetName('ValidateNameTest'.$$);
56
57 ok (!$id, $msg);
58
59 ($id, $msg) =$a3->SetName('ValidateNametest2'.$$);
60
61 ok ($id, $msg);
62
63
64
65
66
67 my $newart = RT::Article->new($RT::SystemUser);
68 $newart->Create(Name => 'DeleteTest'.$$, Class => '1');
69 $id = $newart->Id;
70
71 ok($id, "New article has an id");
72
73
74  $article = RT::Article->new($RT::SystemUser);
75 $article->Load($id);
76 ok ($article->Id, "Found the article");
77 my $val;
78  ($val, $msg) = $article->Delete();
79 ok ($val, "Article Deleted: $msg");
80
81  $a2 = RT::Article->new($RT::SystemUser);
82 $a2->Load($id);
83 ok (!$a2->Id, "Did not find the article");
84
85 # NOT OK
86 #$RT::Handle->SimpleQuery("DELETE FROM Links");
87
88 my $article_a = RT::Article->new($RT::SystemUser);
89 ($id, $msg) = $article_a->Create( Class => $CLASS, Summary => "ArticleTestlink1".$$);
90 ok($id,$msg);
91
92 my $article_b = RT::Article->new($RT::SystemUser);
93 ($id, $msg) = $article_b->Create( Class => $CLASS, Summary => "ArticleTestlink2".$$);
94 ok($id,$msg);
95
96 # Create a link between two articles
97 ($id, $msg) = $article_a->AddLink( Type => 'RefersTo', Target => $article_b->URI);
98 ok($id,$msg);
99
100 # Make sure that Article Bs "ReferredToBy" links object refers to to this article
101 my $refers_to_b = $article_b->ReferredToBy;
102 is($refers_to_b->Count, 1, "Found one thing referring to b");
103 my $first = $refers_to_b->First;
104 ok ($first->isa('RT::Link'), "IT's an RT link - ref ".ref($first) );
105 is($first->TargetObj->Id, $article_b->Id, "Its target is B");
106
107 ok($refers_to_b->First->BaseObj->isa('RT::Article'), "Yep. its an article");
108
109
110 # Make sure that Article A's "RefersTo" links object refers to this article"
111 my $referred_To_by_a = $article_a->RefersTo;
112 is($referred_To_by_a->Count, 1, "Found one thing referring to b ".$referred_To_by_a->Count. "-".$referred_To_by_a->First->id . " - ".$referred_To_by_a->Last->id);
113  $first = $referred_To_by_a->First;
114 ok ($first->isa('RT::Link'), "IT's an RT link - ref ".ref($first) );
115 is ($first->TargetObj->Id, $article_b->Id, "Its target is B - " . $first->TargetObj->Id);
116 is ($first->BaseObj->Id, $article_a->Id, "Its base is A");
117
118 ok($referred_To_by_a->First->BaseObj->isa('RT::Article'), "Yep. its an article");
119
120 # Delete the link
121 ($id, $msg) = $article_a->DeleteLink(Type => 'RefersTo', Target => $article_b->URI);
122 ok($id,$msg);
123
124
125 # Create an Article A RefersTo Ticket 1 from the Articles side
126 use RT::Ticket;
127
128
129 my $tick = RT::Ticket->new($RT::SystemUser);
130 $tick->Create(Subject => "Article link test ", Queue => 'General');
131 $tick->Load($tick->Id);
132 ok ($tick->Id, "Found ticket ".$tick->id);
133 ($id, $msg) = $article_a->AddLink(Type => 'RefersTo', Target => $tick->URI);
134 ok($id,$msg);
135
136 # Find all tickets whhich refer to Article A
137
138 use RT::Tickets;
139 use RT::Links;
140
141 my $tix = RT::Tickets->new($RT::SystemUser);
142 ok ($tix, "Got an RT::Tickets object");
143 ok ($tix->LimitReferredToBy($article_a->URI)); 
144 is ($tix->Count, 1, "Found one ticket linked to that article");
145 is ($tix->First->Id, $tick->id, "It's even the right one");
146
147
148
149 # Find all articles which refer to Ticket 1
150 use RT::Articles;
151
152 my $articles = RT::Articles->new($RT::SystemUser);
153 ok($articles->isa('RT::Articles'), "Created an article collection");
154 ok($articles->isa('RT::SearchBuilder'), "Created an article collection");
155 ok($articles->isa('DBIx::SearchBuilder'), "Created an article collection");
156 ok($tick->URI, "The ticket does still have a URI");
157 $articles->LimitRefersTo($tick->URI);
158
159 is($articles->Count(), 1);
160 is ($articles->First->Id, $article_a->Id);
161 is ($articles->First->URI, $article_a->URI);
162
163
164
165 # Find all things which refer to ticket 1 using the RT API.
166
167 my $tix2 = RT::Links->new($RT::SystemUser);
168 ok ($tix2->isa('RT::Links'));
169 ok($tix2->LimitRefersTo($tick->URI));
170 is ($tix2->Count, 1);
171 is ($tix2->First->BaseObj->URI ,$article_a->URI);
172
173
174
175 # Delete the link from the RT side.
176 my $t2 = RT::Ticket->new($RT::SystemUser);
177 $t2->Load($tick->Id);
178 ($id, $msg)= $t2->DeleteLink( Base => $article_a->URI, Type => 'RefersTo');
179 ok ($id, $msg . " - $id - $msg");
180
181 # it is actually deleted
182 my $tix3 = RT::Links->new($RT::SystemUser);
183 $tix3->LimitReferredToBy($tick->URI);
184 is ($tix3->Count, 0);
185
186 # Recreate the link from teh RT site
187 ($id, $msg) = $t2->AddLink( Base => $article_a->URI, Type => 'RefersTo');
188 ok ($id, $msg);
189
190 # Find all tickets whhich refer to Article A
191
192 # Find all articles which refer to Ticket 1
193
194
195
196
197 my $art = RT::Article->new($RT::SystemUser);
198 ($id, $msg) = $art->Create (Class => $CLASS);
199 ok ($id,$msg);
200
201 ok($art->URI);
202 ok($art->__Value('URI') eq $art->URI, "The uri in the db is set correctly");
203
204
205
206
207  $art = RT::Article->new($RT::SystemUser);
208 ($id, $msg) = $art->Create (Class => $CLASS);
209 ok ($id,$msg);
210
211 ok($art->URIObj);
212 ok($art->__Value('URI') eq $art->URIObj->URI, "The uri in the db is set correctly");
213
214
215 my $art_id = $art->id;
216 $art = RT::Article->new($RT::SystemUser);
217 $art->Load($art_id);
218 is ($art->Id, $art_id, "Loaded article 1");
219 my $s =$art->Summary;
220 ($val, $msg) = $art->SetSummary("testFoo");
221 ok ($val, $msg);
222 ok ($art->Summary eq 'testFoo', "The Summary was set to foo");
223 my $t = $art->Transactions();
224 my $trans = $t->Last;
225 ok ($trans->Type eq 'Set', "It's a Set transaction");
226 ok ($trans->Field eq 'Summary', "it is about setting the Summary");
227 is  ($trans->NewValue , 'testFoo', "The new content is 'foo'");
228 is ($trans->OldValue,$s, "the old value was preserved");
229