first pass RT4 merge, RT#13852
[freeside.git] / rt / t / articles / interface.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use RT::Test tests => 53;
7
8 use RT::CustomField;
9 use RT::EmailParser;
10 use RT::Queue;
11 use RT::Ticket;
12 use_ok 'RT::Class';
13 use_ok 'RT::Topic';
14 use_ok 'RT::Article';
15
16 my ($url, $m) = RT::Test->started_ok;
17
18 # Variables to test return values
19 my ($ret, $msg);
20
21 # Create a test class
22 my $class = RT::Class->new($RT::SystemUser);
23 ($ret, $msg) = $class->Create('Name' => 'tlaTestClass-'.$$,
24                               'Description' => 'A general-purpose test class');
25 ok($ret, "Test class created");
26 my $class2 = RT::Class->new($RT::SystemUser);
27 ($ret, $msg) = $class2->Create('Name' => 'tlaTestClass2-'.$$,
28                               'Description' => 'Another general-purpose test class');
29 ok($ret, "Test class 2 created");
30
31
32 # Create a hierarchy of test topics
33 my $topic1 = RT::Topic->new($RT::SystemUser);
34 my $topic11 = RT::Topic->new($RT::SystemUser);
35 my $topic12 = RT::Topic->new($RT::SystemUser);
36 my $topic2 = RT::Topic->new($RT::SystemUser);
37 my $topic_class2= RT::Topic->new($RT::SystemUser);
38 my $gtopic = RT::Topic->new($RT::SystemUser);
39 ($ret, $msg) = $topic1->Create('Parent' => 0,
40                               'Name' => 'tlaTestTopic1-'.$$,
41                               'ObjectType' => 'RT::Class',
42                               'ObjectId' => $class->Id);
43 ok($ret, "Topic 1 created");
44 ($ret, $msg) = $topic11->Create('Parent' => $topic1->Id,
45                                'Name' => 'tlaTestTopic1.1-'.$$,
46                                'ObjectType' => 'RT::Class',
47                                'ObjectId' => $class->Id);
48 ok($ret, "Topic 1.1 created");
49 ($ret, $msg) = $topic12->Create('Parent' => $topic1->Id,
50                                'Name' => 'tlaTestTopic1.2-'.$$,
51                                'ObjectType' => 'RT::Class',
52                                'ObjectId' => $class->Id);
53 ok($ret, "Topic 1.2 created");
54 ($ret, $msg) = $topic2->Create('Parent' => 0,
55                               'Name' => 'tlaTestTopic2-'.$$,
56                               'ObjectType' => 'RT::Class',
57                               'ObjectId' => $class->Id);
58 ok($ret, "Topic 2 created");
59 ($ret, $msg) = $topic_class2->Create('Parent' => 0,
60                               'Name' => 'tlaTestTopicClass2-'.$$,
61                               'ObjectType' => 'RT::Class',
62                               'ObjectId' => $class2->Id);
63 ok($ret, "Topic Class2 created");
64 ($ret, $msg) = $gtopic->Create('Parent' => 0,
65                               'Name' => 'tlaTestTopicGlobal-'.$$,
66                               'ObjectType' => 'RT::System',
67                               'ObjectId' => $RT::System->Id );
68 ok($ret, "Global Topic created");
69
70 # Create some article custom fields
71
72 my $questionCF = RT::CustomField->new($RT::SystemUser);
73 my $answerCF = RT::CustomField->new($RT::SystemUser);
74 ($ret, $msg) = $questionCF->Create('Name' => 'Question-'.$$,
75                            'Type' => 'Text',
76                            'MaxValues' => 1,
77                            'LookupType' => 'RT::Class-RT::Article',
78                            'Description' => 'The question to be answered',
79                            'Disabled' => 0);
80 ok($ret, "Question CF created: $msg");
81 ($ret, $msg) = $answerCF->Create('Name' => 'Answer-'.$$,
82                          'Type' => 'Text',
83                          'MaxValues' => 1,
84                          'LookupType' => 'RT::Class-RT::Article',
85                          'Description' => 'The answer to the question',
86                          'Disabled' => 0);
87 ok($ret, "Answer CF created: $msg");
88
89 # Attach the custom fields to our class
90 ($ret, $msg) = $questionCF->AddToObject($class);
91 ok($ret, "Question CF added to class: $msg");
92 ($ret, $msg) = $answerCF->AddToObject($class);
93 ok($ret, "Answer CF added to class: $msg");
94 my ($qid, $aid) = ($questionCF->Id, $answerCF->Id);
95
96 my %cvals = ('article1q' => 'Some question about swallows',
97                 'article1a' => 'Some answer about Europe and Africa',
98                 'article2q' => 'Another question about Monty Python',
99                 'article2a' => 'Romani ite domum',
100                 'article3q' => 'Why should I eat my supper?',
101                 'article3a' => 'There are starving children in Africa',
102                 'article4q' => 'What did Brian originally write?',
103                 'article4a' => 'Romanes eunt domus');
104
105 # Create an article or two with our custom field values.
106
107 my $article1 = RT::Article->new($RT::SystemUser);
108 my $article2 = RT::Article->new($RT::SystemUser);
109 my $article3 = RT::Article->new($RT::SystemUser);
110 my $article4 = RT::Article->new($RT::SystemUser);
111 ($ret, $msg) = $article1->Create(Name => 'First article '.$$,
112                                  Summary => 'blah blah 1',
113                                  Class => $class->Id,
114                                  Topics => [$topic1->Id],
115                                  "CustomField-$qid" => $cvals{'article1q'},
116                                  "CustomField-$aid" => $cvals{'article1a'},
117                                  );
118 ok($ret, "article 1 created");
119 ($ret, $msg) = $article2->Create(Name => 'Second article '.$$,
120                                  Summary => 'foo bar 2',
121                                  Class => $class->Id,
122                                  Topics => [$topic11->Id],
123                                  "CustomField-$qid" => $cvals{'article2q'},
124                                  "CustomField-$aid" => $cvals{'article2a'},
125                                  );
126 ok($ret, "article 2 created");
127 ($ret, $msg) = $article3->Create(Name => 'Third article '.$$,
128                                  Summary => 'ping pong 3',
129                                  Class => $class->Id,
130                                  Topics => [$topic12->Id],
131                                  "CustomField-$qid" => $cvals{'article3q'},
132                                  "CustomField-$aid" => $cvals{'article3a'},
133                                  );
134 ok($ret, "article 3 created");
135 ($ret, $msg) = $article4->Create(Name => 'Fourth article '.$$,
136                                  Summary => 'hoi polloi 4',
137                                  Class => $class->Id,
138                                  Topics => [$topic2->Id],
139                                  "CustomField-$qid" => $cvals{'article4q'},
140                                  "CustomField-$aid" => $cvals{'article4a'},
141                                  );
142 ok($ret, "article 4 created");
143
144 # Create a ticket.
145 my $parser = RT::EmailParser->new();
146 $parser->ParseMIMEEntityFromScalar('From: root@localhost
147 To: rt@example.com
148 Subject: test ticket for articles
149
150 This is some form of new request.
151 May as well say something about Africa.');
152
153 my $ticket = RT::Ticket->new($RT::SystemUser);
154 my $obj;
155 ($ret, $obj, $msg) = $ticket->Create(Queue => 'General',
156                                Subject => 'test ticket for articles '.$$,
157                                MIMEObj => $parser->Entity);
158 ok($ret, "Test ticket for articles created: $msg");
159
160
161 #### Right.  That's our data.  Now begin the real testing.
162
163 isa_ok($m, 'Test::WWW::Mechanize');
164 ok($m->login, 'logged in');
165 $m->follow_link_ok( { text => 'Articles', url_regex => qr!^/Articles/! },
166     'UI -> Articles' );
167
168 $m->content_contains($article3->Name);
169 $m->follow_link_ok( {text => $article3->Name}, 'Articles -> '. $article3->Name );
170 $m->title_is("Article #" . $article3->Id . ": " . $article3->Name);
171 $m->follow_link_ok( { text => 'Modify'}, 'Article -> Modify' );
172
173 {
174 $m->content_like(qr/Refers to/, "found links edit box");
175 my $ticket_id = $ticket->Id;
176 my $turi = "t:$ticket_id";
177 my $a1uri = 'a:'.$article1->Id;
178 $m->submit_form(form_name => 'EditArticle',
179                 fields => { $article3->Id.'-RefersTo' => $turi,
180                             'RefersTo-'.$article3->Id => $a1uri }
181                 );
182
183 $m->content_like(qr/Ticket.*$ticket_id/, "Ticket linkto was created");
184 $m->content_like(qr/URI.*$a1uri/, "Article linkfrom was created");
185 }
186
187 # Now try to extract an article from a link.
188 $m->get_ok($url."/Ticket/Display.html?id=".$ticket->Id, 
189            "Loaded ticket display");
190 $m->content_like(qr/Extract Article/, "Article extraction link shows up");
191 $m->follow_link_ok( { text => 'Extract Article' }, '-> Extract Article' );
192 $m->content_contains($class->Name);
193 $m->follow_link_ok( { text => $class->Name }, 'Extract Article -> '. $class->Name );
194 $m->content_like(qr/Select topics for this article/i, 'selecting topic');
195 $m->form_number(3);
196 $m->set_visible([option => $topic1->Name]);
197 $m->submit;
198 $m->form_number(3);
199 $m->set_visible([option => $answerCF->Name]);
200 $m->click();
201 $m->title_like(qr/Create a new article/, "got edit page from extraction");
202 $m->submit_form(form_name => 'EditArticle');
203 $m->title_like(qr/Modify article/);
204 $m->follow_link_ok( { text => 'Display' }, '-> Display' );
205 $m->content_like(qr/Africa/, "Article content exist");
206 $m->content_contains($ticket->Subject,
207                      "Article references originating ticket");
208
209 diag("Test creating a ticket in Class2 and make sure we don't see Class1 Topics") if $ENV{TEST_VERBOSE};
210 {
211 $m->follow_link_ok( { text => 'Articles', url_regex => qr!^/Articles/! },
212     'UI -> Articles' );
213 $m->follow_link_ok( {text => 'New Article' }, 'Articles -> New Article' );
214 $m->follow_link_ok( {text => 'in class '.$class2->Name }, 'New Article -> in class '.$class2->Name );
215 $m->content_lacks( $topic1->Name, "Topic1 from Class1 isn't shown" );
216 $m->content_lacks( $topic11->Name, "Topic11 from Class1 isn't shown" );
217 $m->content_lacks( $topic12->Name, "Topic12 from Class1 isn't shown" );
218 $m->content_lacks( $topic2->Name, "Topic2 from Class1 isn't shown" );
219 $m->content_contains( $gtopic->Name, "Global Topic is shown" );
220 $m->content_contains( $topic_class2->Name, "Class2 topic is shown" );
221 }