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