Bug 29971: Add selenium tests
[koha.git] / t / db_dependent / selenium / patrons_search.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 our @cleanup;
21 END {
22     unless ( @cleanup ) { say "WARNING: Cleanup failed!" }
23     $_->delete for @cleanup;
24 };
25
26 use C4::Context;
27
28 use utf8;
29 use Test::More;
30 use Test::MockModule;
31
32 use C4::Context;
33 use Koha::AuthUtils;
34 use Koha::Patrons;
35 use t::lib::Mocks;
36 use t::lib::Selenium;
37 use t::lib::TestBuilder;
38
39 eval { require Selenium::Remote::Driver; };
40 if ( $@ ) {
41     plan skip_all => "Selenium::Remote::Driver is needed for selenium tests.";
42 } else {
43     plan tests => 2;
44 }
45
46
47 my $s             = t::lib::Selenium->new;
48 my $driver        = $s->driver;
49 my $base_url      = $s->base_url;
50 my $mainpage      = $s->base_url . q|mainpage.pl|;
51 my $builder       = t::lib::TestBuilder->new;
52 my $schema        = Koha::Database->schema;
53
54 subtest 'Search patrons' => sub {
55     plan tests => 25;
56
57     if ( Koha::Patrons->search({surname => {-like => "test_patron_%"}})->count ) {
58         BAIL_OUT("Cannot run this test, data we need to create already exist in the DB");
59     }
60     my @patrons;
61     my $borrowernotes           = q|<strong>just 'a" note</strong> \123 ❤|;
62     my $borrowernotes_displayed = q|just 'a" note \123 ❤|;
63     my $branchname = q|<strong>just 'another" library</strong> \123 ❤|;
64     my $firstname  = q|<strong>fir's"tname</strong> \123 ❤|;
65     my $address    = q|<strong>add'res"s</strong> \123 ❤|;
66     my $email      = q|a<strong>bad_email</strong>@example\123 ❤.com|;
67     my $patron_category = $builder->build_object(
68         {
69             class => 'Koha::Patron::Categories',
70             value => { category_type => 'A' }
71         }
72     );
73     my $library = $builder->build_object(
74         { class => 'Koha::Libraries', value => { branchname => $branchname } }
75     );
76     my $default_patron_search_fields = C4::Context->preference('DefaultPatronSearchFields');
77     my $default_patron_per_page = C4::Context->preference('PatronsPerPage');
78     for my $i ( 1 .. 25 ) {
79         push @patrons,
80           $builder->build_object(
81             {
82                 class => 'Koha::Patrons',
83                 value => {
84                     surname       => "test_patron_" . $i++,
85                     firstname     => $firstname,
86                     categorycode  => $patron_category->categorycode,
87                     branchcode    => $library->branchcode,
88                     borrowernotes => $borrowernotes,
89                     address       => $address,
90                     email         => $email,
91                 }
92             }
93           );
94     }
95
96     push @patrons, $builder->build_object(
97         {
98             class => 'Koha::Patrons',
99             value => {
100                 surname   => "test",
101                 firstname => "not_p_a_t_r_o_n",    # won't match 'patron'
102                 categorycode  => $patron_category->categorycode,
103                 branchcode    => $library->branchcode,
104                 borrowernotes => $borrowernotes,
105                 address       => $address,
106                 email         => $email,
107             }
108         }
109     );
110
111     my $library_2 = $builder->build_object(
112         { class => 'Koha::Libraries', value => { branchname => 'X' . $branchname } }
113     );
114     push @patrons,
115       $builder->build_object(
116         {
117             class => 'Koha::Patrons',
118             value => {
119                 surname       => "test_patron_26",
120                 firstname     => $firstname,
121                 categorycode  => $patron_category->categorycode,
122                 branchcode    => $library_2->branchcode,
123                 borrowernotes => $borrowernotes,
124                 address       => $address,
125                 email         => $email,
126             }
127         }
128       );
129
130     my $attribute_type = Koha::Patron::Attribute::Type->new(
131         {
132             code        => 'my code1',
133             description => 'my description1',
134         }
135     )->store;
136     my $attribute_type_searchable = Koha::Patron::Attribute::Type->new(
137         {
138             code             => 'my code2',
139             description      => 'my description2',
140             opac_display     => 1,
141             staff_searchable => 1
142         }
143     )->store;
144     $patrons[0]->extended_attributes([
145         { code => $attribute_type->code, attribute => 'test_attr_1' },
146         { code => $attribute_type_searchable->code, attribute => 'test_attr_2'},
147     ]);
148     $patrons[1]->extended_attributes([
149         { code => $attribute_type->code, attribute => 'test_attr_1' },
150         { code => $attribute_type_searchable->code, attribute => 'test_attr_2'},
151     ]);
152
153     my $total_number_of_patrons = Koha::Patrons->search->count;
154     my $table_id = "memberresultst";
155
156     $driver->get($mainpage . q|?logout.x=1|);
157     $s->auth;
158     C4::Context->set_preference('DefaultPatronSearchFields',"");
159     my $PatronsPerPage = 15;
160     my $nb_standard_fields = 13;
161     C4::Context->set_preference('PatronsPerPage', $PatronsPerPage);
162     $driver->get( $base_url . "/members/members-home.pl" );
163     my @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option');
164     is( scalar @adv_options, $nb_standard_fields + 1, 'All standard fields are searchable if DefaultPatronSearchFields not set. middle_name is there.');
165     is( $adv_options[0]->get_value(), 'firstname,middle_name,surname,othernames,cardnumber,userid', 'Standard search uses hard coded list when DefaultPatronSearchFields not set');
166     my @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option');
167     is( scalar @filter_options, $nb_standard_fields + 1, 'All standard fields + middle_name are searchable by filter if DefaultPatronSearchFields not set');
168     is( $filter_options[0]->get_value(), 'firstname,middle_name,surname,othernames,cardnumber,userid', 'Standard filter uses hard coded list when DefaultPatronSearchFields not set');
169     C4::Context->set_preference('DefaultPatronSearchFields',"firstname,initials");
170     $driver->get( $base_url . "/members/members-home.pl" );
171     @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option');
172     is( scalar @adv_options, $nb_standard_fields, 'New option added when DefaultPatronSearchFields is populated with a field. Note that middle_name disappears, we do not want it if not part of DefaultPatronSearchFields');
173     is( $adv_options[0]->get_value(), 'firstname,initials', 'Standard search uses DefaultPatronSearchFields when populated');
174     @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option');
175     is( scalar @filter_options, $nb_standard_fields, 'New filter option added when DefaultPatronSearchFields is populated with a field');
176     is( $filter_options[0]->get_value(), 'firstname,initials', 'Standard filter uses DefaultPatronSearchFields when populated');
177     C4::Context->set_preference('DefaultPatronSearchFields',"firstname,initials,horses");
178     $driver->get( $base_url . "/members/members-home.pl" );
179     @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option');
180     @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option');
181     is( scalar @adv_options, $nb_standard_fields, 'Invalid option not added when DefaultPatronSearchFields is populated with an invalid field');
182     is( scalar @filter_options, $nb_standard_fields, 'Invalid filter option not added when DefaultPatronSearchFields is populated with an invalid field');
183     # NOTE: We should probably ensure the bad field is removed from 'standard' search here, else searches are broken
184     C4::Context->set_preference('DefaultPatronSearchFields',"");
185     $driver->get( $base_url . "/members/members-home.pl" );
186     $s->fill_form( { search_patron_filter => 'test_patron' } );
187     $s->submit_form;
188     my $first_patron = $patrons[0];
189
190     $s->wait_for_ajax;
191     my @td = $driver->find_elements('//table[@id="'.$table_id.'"]/tbody/tr/td');
192     like ($td[2]->get_text, qr[\Q$firstname\E],
193         'Column "Name" should be the 3rd and contain the firstname correctly filtered'
194     );
195     like ($td[2]->get_text, qr[\Q$address\E],
196         'Column "Name" should be the 3rd and contain the address correctly filtered'
197     );
198     like ($td[2]->get_text, qr[\Q$email\E],
199         'Column "Name" should be the 3rd and contain the email address correctly filtered'
200     );
201     is( $td[4]->get_text, $branchname,
202         'Column "Library" should be the 6th and contain the html tags - they have been html filtered'
203     );
204     is( $td[9]->get_text, $borrowernotes_displayed,
205         'Column "Circ note" should be the 10th and not contain the html tags - they have not been html filtered'
206     );
207
208     $driver->find_element(
209             '//a[@href="/cgi-bin/koha/members/memberentry.pl?op=modify&destination=circ&borrowernumber='
210           . $first_patron->borrowernumber
211           . '"]' )->click;
212     is(
213         $driver->get_title,
214         sprintf(
215             "Modify patron %s %s %s (%s) %s (%s) (%s) › Patrons › Koha",
216             $first_patron->title, $first_patron->firstname, $first_patron->middle_name, $first_patron->othernames, $first_patron->surname, $first_patron->cardnumber,
217             $first_patron->category->description,
218         ),
219         'Page title is correct after following modification link'
220     );
221
222     $driver->get( $base_url . "/members/members-home.pl" );
223     $s->fill_form( { search_patron_filter => 'test_patron' } );
224     $s->submit_form;
225     $s->wait_for_ajax;
226
227     $s->driver->find_element('//*[@id="'.$table_id.'_filter"]//input')->send_keys('test_patron');
228     $s->wait_for_ajax;
229     is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries (filtered from %s total entries)', $PatronsPerPage, 26, $total_number_of_patrons), 'Searching in standard brings back correct results' );
230
231     $s->driver->find_element('//table[@id="'.$table_id.'"]//th[@data-filter="libraries"]/select/option[@value="'.$library->branchcode.'"]')->click;
232     $s->wait_for_ajax;
233     is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries (filtered from %s total entries)', $PatronsPerPage, 25, $total_number_of_patrons), 'Filtering on library works in combination with main search' );
234
235     # Reset the filters
236     $driver->find_element('//form[@id="patron_search_form"]//*[@id="clear_search"]')->click();
237     $s->submit_form;
238     $s->wait_for_ajax;
239
240     # And make sure all the patrons are present
241     is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries', $PatronsPerPage, $total_number_of_patrons), 'Resetting filters works as expected' );
242
243     # Pattern terms must be split
244     $s->fill_form( { search_patron_filter => 'test patron' } );
245     $s->submit_form;
246
247     $s->wait_for_ajax;
248     is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries (filtered from %s total entries)', $PatronsPerPage, 26, $total_number_of_patrons) );
249     $driver->find_element('//form[@id="patron_search_form"]//*[@id="clear_search"]')->click();
250     $s->submit_form;
251     $s->wait_for_ajax;
252
253     # Search on non-searchable attribute, we expect no result!
254     $s->fill_form( { search_patron_filter => 'test_attr_1' } );
255     $s->submit_form;
256     $s->wait_for_ajax;
257
258     is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('No entries to show (filtered from %s total entries)', $total_number_of_patrons), 'Searching on a non-searchable attribute returns no results' );
259
260     # clear form
261     $driver->find_element('//form[@id="patron_search_form"]//*[@id="clear_search"]')->click();
262     # Search on searchable attribute, we expect 2 patrons
263     $s->fill_form( { search_patron_filter => 'test_attr_2' } );
264     $s->submit_form;
265     $s->wait_for_ajax;
266
267     is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries (filtered from %s total entries)', 2, 2, $total_number_of_patrons), 'Searching on a searchable attribute returns correct results' );
268
269     # Refine search and search for test_patron in all the data using the DT global search
270     # No change in result expected, still 2 patrons
271     $s->driver->find_element('//*[@id="'.$table_id.'_filter"]//input')->send_keys('test_patron');
272     $s->wait_for_ajax;
273     is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries (filtered from %s total entries)', 2, 2, $total_number_of_patrons), 'Refining with DataTables search works to further filter the original query' );
274
275     # Adding the surname of the first patron in the "Name" column
276     # We expect only 1 result
277     $s->driver->find_element('//table[@id="'.$table_id.'"]//input[@placeholder="Name search"]')->send_keys($patrons[0]->surname);
278     $s->wait_for_ajax;
279     is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries (filtered from %s total entries)', 1, 1, $total_number_of_patrons), 'Refining with header filters works to further filter the original query' );
280
281     subtest 'remember_search' => sub {
282
283         plan tests => 7;
284
285         C4::Context->set_preference( 'PatronsPerPage', 5 );
286         $driver->get( $base_url . "/members/members-home.pl" );
287         $s->fill_form( { search_patron_filter => 'test_patron' } );
288         $s->submit_form;
289         $s->wait_for_ajax;
290         my $patron_selected_text = $driver->find_element('//div[@id="patron_search_selected"]/span')->get_text;
291         is( $patron_selected_text, "", "Patrons selected is not displayed" );
292
293         my @checkboxes = $driver->find_elements(
294             '//input[@type="checkbox"][@name="borrowernumber"]');
295         $checkboxes[2]->click;
296         $patron_selected_text = $driver->find_element('//div[@id="patron_search_selected"]/span')->get_text;
297         is( $patron_selected_text, "Patrons selected: 1", "One patron selected" );
298
299         $checkboxes[4]->click;
300         $patron_selected_text = $driver->find_element('//div[@id="patron_search_selected"]/span')->get_text;
301         is( $patron_selected_text, "Patrons selected: 2", "Two patrons are selected" );
302
303         $driver->find_element('//*[@id="memberresultst_next"]')->click;
304         @checkboxes = $driver->find_elements(
305             '//input[@type="checkbox"][@name="borrowernumber"]');
306         $checkboxes[0]->click;
307         $patron_selected_text = $driver->find_element('//div[@id="patron_search_selected"]/span')->get_text;
308         is( $patron_selected_text, "Patrons selected: 3", "Tree patrons are selected" );
309
310
311         # Perform another search
312         $driver->get( $base_url . "/members/members-home.pl" );
313         $s->fill_form( { search_patron_filter => 'test_patron' } );
314         $s->submit_form;
315         $s->wait_for_ajax;
316         $patron_selected_text = $driver->find_element('//div[@id="patron_search_selected"]/span')->get_text;
317         is( $patron_selected_text, "Patrons selected: 3", "Tree patrons still selected" );
318
319         $driver->find_element('//*[@id="patronlist-menu"]')->click;
320         $driver->find_element('//a[@class="patron-list-add"]')->click;
321         my $patron_list_name = "my new list";
322         $driver->find_element('//input[@id="new_patron_list"]')->send_keys($patron_list_name);
323         $driver->find_element('//button[@id="add_to_patron_list_submit"]')->click;
324         $s->wait_for_ajax;
325         is( $driver->find_element('//*[@id="patron_list_dialog"]')->get_text, "Added 3 patrons to $patron_list_name." );
326         my $patron_list = $schema->resultset('PatronList')->search({ name => $patron_list_name })->next;
327         is( $schema->resultset('PatronListPatron')->search({ patron_list_id => $patron_list->patron_list_id })->count, 3 );
328
329         $patron_list->delete;
330     };
331
332     push @cleanup, $_ for @patrons;
333     push @cleanup, $library;
334     push @cleanup, $library_2;
335     push @cleanup, $patron_category;
336     push @cleanup, $attribute_type, $attribute_type_searchable;
337     C4::Context->set_preference('DefaultPatronSearchFields',$default_patron_search_fields);
338     C4::Context->set_preference('PatronsPerPage',$default_patron_per_page);
339 };
340
341 subtest 'Error too long' => sub {
342     plan tests => 1;
343     $driver->get($mainpage . q|?logout.x=1|);
344     $s->auth;
345
346     my $very_long_string = q{some long test to search in patron fields some long test to search in patron fields some long test to search in patron fields};
347     $driver->get( $base_url . "/members/members-home.pl" );
348     $s->fill_form( { search_patron_filter => $very_long_string } );
349     $s->submit_form;
350     $s->wait_for_ajax;
351     ok( 1, "No error when querying a very long search" );
352 };
353
354 $driver->quit();