RT 4.2.11, ticket#13852
[freeside.git] / rt / t / articles / search-interface.t
1
2 use strict;
3 use warnings;
4
5 use RT::Test tests => undef;
6
7 use RT::CustomField;
8 use RT::Queue;
9 use RT::Ticket;
10 use_ok 'RT::Class';
11 use_ok 'RT::Topic';
12 use_ok 'RT::Article';
13
14 my ($url, $m) = RT::Test->started_ok;
15
16 # Variables to test return values
17 my ($ret, $msg);
18
19 # Create two classes
20 my $class = RT::Class->new($RT::SystemUser);
21 ($ret, $msg) = $class->Create('Name' => 'First-class',
22                               'Description' => 'A general-purpose test class');
23 ok($ret, "Test class created");
24
25 ($ret, $msg) = $class->Create('Name' => 'Second-class',
26                               'Description' => 'Another class');
27 ok($ret, "Test class created");
28
29 my $questionCF = RT::CustomField->new($RT::SystemUser);
30 my $answerCF = RT::CustomField->new($RT::SystemUser);
31 my $ticketCF = RT::CustomField->new($RT::SystemUser);
32 ($ret, $msg) = $questionCF->Create('Name' => 'Question-'.$$,
33                            'Type' => 'Text',
34                            'MaxValues' => 1,
35                            'LookupType' => 'RT::Class-RT::Article',
36                            'Description' => 'The question to be answered',
37                            'Disabled' => 0);
38 ok($ret, "Question CF created: $msg");
39 ($ret, $msg) = $answerCF->Create('Name' => 'Answer-'.$$,
40                          'Type' => 'Text',
41                          'MaxValues' => 1,
42                          'LookupType' => 'RT::Class-RT::Article',
43                          'Description' => 'The answer to the question',
44                          'Disabled' => 0);
45 ok($ret, "Answer CF created: $msg");
46
47 ($ret, $msg) = $ticketCF->Create('Name' => 'Class',
48                          'Type' => 'Text',
49                          'MaxValues' => 1,
50                          'LookupType' => 'RT::Queue-RT::Ticket',
51                          'Disabled' => 0);
52 ok($ret, "Ticket CF 'Class' created: $msg");
53
54 # Attach the custom fields to our class
55 ($ret, $msg) = $questionCF->AddToObject($class);
56 ok($ret, "Question CF added to class: $msg");
57 ($ret, $msg) = $answerCF->AddToObject($class);
58 ok($ret, "Answer CF added to class: $msg");
59 my ($qid, $aid) = ($questionCF->Id, $answerCF->Id);
60
61 my $global_queue = RT::Queue->new($RT::SystemUser);
62 ($ret, $msg) = $ticketCF->AddToObject($global_queue);
63 ok($ret, "Ticket CF added globally: $msg");
64
65 my %cvals = ('article1q' => 'Some question about swallows',
66                 'article1a' => 'Some answer about Europe and Africa',
67                 'article2q' => 'Another question about Monty Python',
68                 'article2a' => 'Romani ite domum',
69                 'article3q' => 'Why should I eat my supper?',
70                 'article3a' => 'There are starving children in Africa',
71                 'article4q' => 'What did Brian originally write?',
72                 'article4a' => 'This is an answer that is longer than 255 '
73              . 'characters so these tests will be sure to use the LargeContent '
74              . 'SQL as well as the normal SQL that would be generated if this '
75              . 'was an answer that was shorter than 255 characters. This second '
76              . 'sentence has a few extra characters to get this string to go '
77              . 'over the 255 character boundary. Lorem ipsum.');
78
79 # Create an article or two with our custom field values.
80
81 my $article1 = RT::Article->new($RT::SystemUser);
82 my $article2 = RT::Article->new($RT::SystemUser);
83 my $article3 = RT::Article->new($RT::SystemUser);
84 my $article4 = RT::Article->new($RT::SystemUser);
85 ($ret, $msg) = $article1->Create(Name => 'First article '.$$,
86                                  Summary => 'blah blah 1',
87                                  Class => $class->Id,
88                                  "CustomField-$qid" => $cvals{'article1q'},
89                                  "CustomField-$aid" => $cvals{'article1a'},
90                                  );
91 ok($ret, "article 1 created");
92 ($ret, $msg) = $article2->Create(Name => 'Second article '.$$,
93                                  Summary => 'foo bar 2',
94                                  Class => $class->Id,
95                                  "CustomField-$qid" => $cvals{'article2q'},
96                                  "CustomField-$aid" => $cvals{'article2a'},
97                                  );
98 ok($ret, "article 2 created");
99 ($ret, $msg) = $article3->Create(Name => 'Third article '.$$,
100                                  Summary => 'ping pong 3',
101                                  Class => $class->Id,
102                                  "CustomField-$qid" => $cvals{'article3q'},
103                                  "CustomField-$aid" => $cvals{'article3a'},
104                                  );
105 ok($ret, "article 3 created");
106 ($ret, $msg) = $article4->Create(Name => 'Fourth article '.$$,
107                                  Summary => 'hoi polloi 4',
108                                  Class => $class->Id,
109                                  "CustomField-$qid" => $cvals{'article4q'},
110                                  "CustomField-$aid" => $cvals{'article4a'},
111                                  );
112 ok($ret, "article 4 created");
113
114 isa_ok($m, 'Test::WWW::Mechanize');
115 ok($m->login, 'logged in');
116 $m->follow_link_ok( { text => 'Articles', url_regex => qr!^/Articles/! },
117     'UI -> Articles' );
118
119 # In all of the search results below, the results page should
120 # have the summary text of the article it occurs in.
121
122 # Case sensitive search on small field.
123 DoArticleSearch($m, $class->Name, 'Africa');
124 $m->text_contains('Search results'); # Did we do a search?
125 $m->text_contains('blah blah 1');
126
127 # Case insensitive search on small field.
128 DoArticleSearch($m, $class->Name, 'africa');
129 $m->text_contains('Search results'); # Did we do a search?
130 $m->text_contains('blah blah 1');
131
132 # Case sensitive search on large field.
133 DoArticleSearch($m, $class->Name, 'ipsum');
134 $m->text_contains('Search results'); # Did we do a search?
135 $m->text_contains('hoi polloi 4');
136
137 # Case insensitive search on large field.
138 DoArticleSearch($m, $class->Name, 'lorem');
139 $m->text_contains('Search results'); # Did we do a search?
140 TODO:{
141     local $TODO = 'Case insensitive search on LONGBLOB not available in MySQL'
142       if RT->Config->Get('DatabaseType') eq 'mysql';
143     $m->text_contains('hoi polloi 4');
144 }
145
146 undef $m;
147 done_testing;
148
149 # When you send $m to this sub, it must be on a page with
150 # a Search link.
151 sub DoArticleSearch{
152   my $m = shift;
153   my $class_name = shift;
154   my $search_text = shift;
155
156   $m->follow_link_ok( {text => 'Articles'}, 'Articles Search');
157   $m->follow_link_ok( {text => 'in class '. $class_name}, 'Articles in class '. $class_name);
158   $m->text_contains('First article');
159
160   $m->submit_form_ok( {
161             form_number => 2,
162             fields      => {
163                 'Article~' => $search_text
164             },
165         }, "Search for $search_text"
166     );
167   return;
168 }
169