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