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