6 use RT::Test tests => 67;
12 my $CLASS = 'ArticleTest-'.$$;
14 my $user = RT::CurrentUser->new('root');
16 my $class = RT::Class->new($user);
19 my ($id, $msg) = $class->Create(Name =>$CLASS);
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!");
31 ($id, $msg) = $article->Create( Class => $CLASS, Summary => $CLASS);
34 is ($article->Summary, $CLASS, "The summary is set correct");
35 my $at = RT::Article->new($RT::SystemUser);
38 is ($at->Summary, $article->Summary);
43 my $a1 = RT::Article->new($RT::SystemUser);
44 ($id, $msg) = $a1->Create(Class => $class->id, Name => 'ValidateNameTest'.$$);
49 my $a2 = RT::Article->new($RT::SystemUser);
50 ($id, $msg) = $a2->Create(Class => $class->id, Name => 'ValidateNameTest'.$$);
53 my $a3 = RT::Article->new($RT::SystemUser);
54 ($id, $msg) = $a3->Create(Class => $class->id, Name => 'ValidateNameTest2'.$$);
56 ($id, $msg) =$a3->SetName('ValidateNameTest'.$$);
60 ($id, $msg) =$a3->SetName('ValidateNametest2'.$$);
68 my $newart = RT::Article->new($RT::SystemUser);
69 $newart->Create(Name => 'DeleteTest'.$$, Class => '1');
72 ok($id, "New article has an id");
75 $article = RT::Article->new($RT::SystemUser);
77 ok ($article->Id, "Found the article");
79 ($val, $msg) = $article->Delete();
80 ok ($val, "Article Deleted: $msg");
82 $a2 = RT::Article->new($RT::SystemUser);
84 ok (!$a2->Id, "Did not find the article");
87 #$RT::Handle->SimpleQuery("DELETE FROM Links");
89 my $article_a = RT::Article->new($RT::SystemUser);
90 ($id, $msg) = $article_a->Create( Class => $CLASS, Summary => "ArticleTestlink1".$$);
93 my $article_b = RT::Article->new($RT::SystemUser);
94 ($id, $msg) = $article_b->Create( Class => $CLASS, Summary => "ArticleTestlink2".$$);
97 # Create a link between two articles
98 ($id, $msg) = $article_a->AddLink( Type => 'RefersTo', Target => $article_b->URI);
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");
108 ok($refers_to_b->First->BaseObj->isa('RT::Article'), "Yep. its an article");
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");
119 ok($referred_To_by_a->First->BaseObj->isa('RT::Article'), "Yep. its an article");
122 ($id, $msg) = $article_a->DeleteLink(Type => 'RefersTo', Target => $article_b->URI);
126 # Create an Article A RefersTo Ticket 1 from the Articles side
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);
137 # Find all tickets whhich refer to Article A
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");
150 # Find all articles which refer to Ticket 1
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);
160 is($articles->Count(), 1);
161 is ($articles->First->Id, $article_a->Id);
162 is ($articles->First->URI, $article_a->URI);
166 # Find all things which refer to ticket 1 using the RT API.
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);
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");
182 # it is actually deleted
183 my $tix3 = RT::Links->new($RT::SystemUser);
184 $tix3->LimitReferredToBy($tick->URI);
185 is ($tix3->Count, 0);
187 # Recreate the link from teh RT site
188 ($id, $msg) = $t2->AddLink( Base => $article_a->URI, Type => 'RefersTo');
191 # Find all tickets whhich refer to Article A
193 # Find all articles which refer to Ticket 1
198 my $art = RT::Article->new($RT::SystemUser);
199 ($id, $msg) = $art->Create (Class => $CLASS);
203 ok($art->__Value('URI') eq $art->URI, "The uri in the db is set correctly");
208 $art = RT::Article->new($RT::SystemUser);
209 ($id, $msg) = $art->Create (Class => $CLASS);
213 ok($art->__Value('URI') eq $art->URIObj->URI, "The uri in the db is set correctly");
216 my $art_id = $art->id;
217 $art = RT::Article->new($RT::SystemUser);
219 is ($art->Id, $art_id, "Loaded article 1");
220 my $s =$art->Summary;
221 ($val, $msg) = $art->SetSummary("testFoo");
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");