Bug 29822: Update unit 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 my $original_dateformat                = C4::Context->preference('dateformat');
21 my $original_DefaultPatronSearchFields = C4::Context->preference('DefaultPatronSearchFields');
22 my $original_DefaultPatronSearchMethod = C4::Context->preference('DefaultPatronSearchMethod');
23 my $original_PatronsPerPage            = C4::Context->preference('PatronsPerPage');
24 our @cleanup;
25
26 END {
27     unless ( @cleanup ) { say "WARNING: Cleanup failed!" }
28     C4::Context->set_preference( 'dateformat',                $original_dateformat );
29     C4::Context->set_preference( 'DefaultPatronSearchFields', $original_DefaultPatronSearchFields );
30     C4::Context->set_preference( 'DefaultPatronSearchMethod', $original_DefaultPatronSearchMethod );
31     C4::Context->set_preference( 'PatronsPerPage',            $original_PatronsPerPage );
32     $_->delete for @cleanup;
33 };
34
35 use C4::Context;
36
37 use utf8;
38 use Test::More;
39 use Test::MockModule;
40
41 use C4::Context;
42 use Koha::AuthUtils;
43 use Koha::Patrons;
44 use t::lib::Mocks;
45 use t::lib::Selenium;
46 use t::lib::TestBuilder;
47
48 eval { require Selenium::Remote::Driver; };
49 if ( $@ ) {
50     plan skip_all => "Selenium::Remote::Driver is needed for selenium tests.";
51 } else {
52     plan tests => 1;
53 }
54
55 my $s             = t::lib::Selenium->new;
56 my $driver        = $s->driver;
57 my $opac_base_url = $s->opac_base_url;
58 my $base_url      = $s->base_url;
59 my $builder       = t::lib::TestBuilder->new;
60 my $schema        = Koha::Database->schema;
61
62 subtest 'Search patrons' => sub {
63     plan tests => 26;
64
65     if ( Koha::Patrons->search({surname => {-like => "test_patron_%"}})->count ) {
66         BAIL_OUT("Cannot run this test, data we need to create already exist in the DB");
67     }
68     my @patrons;
69     my $borrowernotes           = q|<strong>just 'a" note</strong> \123 ❤|;
70     my $borrowernotes_displayed = q|just 'a" note \123 ❤|;
71     my $branchname = q|<strong>just 'another" library</strong> \123 ❤|;
72     my $firstname  = q|<strong>fir's"tname</strong> \123 ❤|;
73     my $address    = q|<strong>add'res"s</strong> \123 ❤|;
74     my $email      = q|a<strong>bad_email</strong>@example\123 ❤.com|;
75     my $patron_category = $builder->build_object(
76         {
77             class => 'Koha::Patron::Categories',
78             value => { category_type => 'A' }
79         }
80     );
81     my $library = $builder->build_object(
82         { class => 'Koha::Libraries', value => { branchname => $branchname } }
83     );
84     for my $i ( 1 .. 25 ) {
85         push @patrons,
86           $builder->build_object(
87             {
88                 class => 'Koha::Patrons',
89                 value => {
90                     surname       => "test_patron_" . $i++,
91                     firstname     => $firstname,
92                     categorycode  => $patron_category->categorycode,
93                     branchcode    => $library->branchcode,
94                     borrowernotes => $borrowernotes,
95                     address       => $address,
96                     email         => $email,
97                 }
98             }
99           );
100     }
101
102     push @patrons, $builder->build_object(
103         {
104             class => 'Koha::Patrons',
105             value => {
106                 surname       => "test",
107                 firstname     => "not_p_a_t_r_o_n",                # won't match 'patron'
108                 categorycode  => $patron_category->categorycode,
109                 branchcode    => $library->branchcode,
110                 borrowernotes => $borrowernotes,
111                 address       => $address,
112                 email         => $email,
113             }
114         }
115     );
116
117     my $library_2 = $builder->build_object(
118         { class => 'Koha::Libraries', value => { branchname => 'X' . $branchname } }
119     );
120     my $patron_27 =
121       $builder->build_object(
122         {
123             class => 'Koha::Patrons',
124             value => {
125                 surname       => "test_patron_27",
126                 firstname     => $firstname,
127                 categorycode  => $patron_category->categorycode,
128                 branchcode    => $library_2->branchcode,
129                 borrowernotes => $borrowernotes,
130                 address       => $address,
131                 email         => $email,
132                 dateofbirth   => '1980-06-17',
133             }
134         }
135       );
136     push @patrons, $patron_27;
137
138     my $attribute_type = Koha::Patron::Attribute::Type->new(
139         {
140             code        => 'my code1',
141             description => 'my description1',
142         }
143     )->store;
144     my $attribute_type_searchable = Koha::Patron::Attribute::Type->new(
145         {
146             code             => 'my code2',
147             description      => 'my description2',
148             opac_display     => 1,
149             staff_searchable => 1
150         }
151     )->store;
152     $patrons[0]->extended_attributes([
153         { code => $attribute_type->code, attribute => 'test_attr_1' },
154         { code => $attribute_type_searchable->code, attribute => 'test_attr_2'},
155     ]);
156     $patrons[1]->extended_attributes([
157         { code => $attribute_type->code, attribute => 'test_attr_1' },
158         { code => $attribute_type_searchable->code, attribute => 'test_attr_2'},
159     ]);
160
161     my $total_number_of_patrons = Koha::Patrons->search->count;
162     my $table_id = "memberresultst";
163
164     $s->auth;
165     C4::Context->set_preference('DefaultPatronSearchFields',"");
166     C4::Context->set_preference('DefaultPatronSearchMethod',"contains");
167     my $PatronsPerPage = 15;
168     my $nb_standard_fields = 13;
169     C4::Context->set_preference('PatronsPerPage', $PatronsPerPage);
170     $driver->get( $base_url . "/members/members-home.pl" );
171     my @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option');
172     is( scalar @adv_options, $nb_standard_fields + 1, 'All standard fields are searchable if DefaultPatronSearchFields not set. middle_name is there.');
173     is( $adv_options[0]->get_value(), 'firstname|middle_name|surname|othernames|cardnumber|userid', 'Standard search uses hard coded list when DefaultPatronSearchFields not set');
174     my @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option');
175     is( scalar @filter_options, $nb_standard_fields + 1, 'All standard fields + middle_name are searchable by filter if DefaultPatronSearchFields not set');
176     is( $filter_options[0]->get_value(), 'firstname|middle_name|surname|othernames|cardnumber|userid', 'Standard filter uses hard coded list when DefaultPatronSearchFields not set');
177     C4::Context->set_preference('DefaultPatronSearchFields',"firstname|initials");
178     $driver->get( $base_url . "/members/members-home.pl" );
179     @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option');
180     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');
181     is( $adv_options[0]->get_value(), 'firstname|initials', 'Standard search uses DefaultPatronSearchFields when populated');
182     @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option');
183     is( scalar @filter_options, $nb_standard_fields, 'New filter option added when DefaultPatronSearchFields is populated with a field');
184     is( $filter_options[0]->get_value(), 'firstname|initials', 'Standard filter uses DefaultPatronSearchFields when populated');
185     C4::Context->set_preference('DefaultPatronSearchFields',"firstname|initials|horses");
186     $driver->get( $base_url . "/members/members-home.pl" );
187     @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option');
188     @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option');
189     is( scalar @adv_options, $nb_standard_fields, 'Invalid option not added when DefaultPatronSearchFields is populated with an invalid field');
190     is( scalar @filter_options, $nb_standard_fields, 'Invalid filter option not added when DefaultPatronSearchFields is populated with an invalid field');
191
192     # NOTE: We should probably ensure the bad field is removed from 'standard' search here, else searches are broken
193     C4::Context->set_preference('DefaultPatronSearchFields',"");
194     $driver->get( $base_url . "/members/members-home.pl" );
195     $s->fill_form( { search_patron_filter => 'test_patron' } );
196     $s->submit_form;
197     my $first_patron = $patrons[0];
198
199     $s->wait_for_ajax;
200     my @td = $driver->find_elements('//table[@id="'.$table_id.'"]/tbody/tr/td');
201     like ($td[2]->get_text, qr[\Q$firstname\E],
202         'Column "Name" should be the 3rd and contain the firstname correctly filtered'
203     );
204     like ($td[2]->get_text, qr[\Q$address\E],
205         'Column "Name" should be the 3rd and contain the address correctly filtered'
206     );
207     like ($td[2]->get_text, qr[\Q$email\E],
208         'Column "Name" should be the 3rd and contain the email address correctly filtered'
209     );
210     is( $td[4]->get_text, $branchname,
211         'Column "Library" should be the 6th and contain the html tags - they have been html filtered'
212     );
213     is( $td[9]->get_text, $borrowernotes_displayed,
214         'Column "Circ note" should be the 10th and not contain the html tags - they have not been html filtered'
215     );
216
217     $driver->find_element(
218             '//a[@href="/cgi-bin/koha/members/memberentry.pl?op=modify&destination=circ&borrowernumber='
219           . $first_patron->borrowernumber
220           . '"]' )->click;
221     is(
222         $driver->get_title,
223         sprintf(
224             "Modify patron %s %s %s (%s) %s (%s) (%s) › Patrons › Koha",
225             $first_patron->title, $first_patron->firstname, $first_patron->middle_name, $first_patron->othernames, $first_patron->surname, $first_patron->cardnumber,
226             $first_patron->category->description,
227         ),
228         'Page title is correct after following modification link'
229     );
230
231     $driver->get( $base_url . "/members/members-home.pl" );
232     $s->fill_form( { search_patron_filter => 'test_patron' } );
233     $s->submit_form;
234     $s->wait_for_ajax;
235
236     $s->driver->find_element('//*[@id="'.$table_id.'_filter"]//input')->send_keys('test_patron');
237     $s->wait_for_ajax;
238     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' );
239
240     $s->driver->find_element('//table[@id="'.$table_id.'"]//th[@data-filter="libraries"]/select/option[@value="'.$library->branchcode.'"]')->click;
241     $s->wait_for_ajax;
242     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' );
243
244     # Reset the filters
245     $driver->find_element('//form[@id="patron_search_form"]//*[@id="clear_search"]')->click();
246     $s->submit_form;
247     $s->wait_for_ajax;
248
249     # And make sure all the patrons are present
250     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' );
251
252     # Pattern terms must be split
253     $s->fill_form( { search_patron_filter => 'test patron' } );
254     $s->submit_form;
255
256     $s->wait_for_ajax;
257     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) );
258     $driver->find_element('//form[@id="patron_search_form"]//*[@id="clear_search"]')->click();
259     $s->submit_form;
260     $s->wait_for_ajax;
261
262     # Search on non-searchable attribute, we expect no result!
263     $s->fill_form( { search_patron_filter => 'test_attr_1' } );
264     $s->submit_form;
265     $s->wait_for_ajax;
266
267     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' );
268
269     # clear form
270     $driver->find_element('//form[@id="patron_search_form"]//*[@id="clear_search"]')->click();
271
272     # Search on searchable attribute, we expect 2 patrons
273     $s->fill_form( { search_patron_filter => 'test_attr_2' } );
274     $s->submit_form;
275     $s->wait_for_ajax;
276
277     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' );
278
279     # Refine search and search for test_patron in all the data using the DT global search
280     # No change in result expected, still 2 patrons
281     $s->driver->find_element('//*[@id="'.$table_id.'_filter"]//input')->send_keys('test_patron');
282     $s->wait_for_ajax;
283     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' );
284
285     # Adding the surname of the first patron in the "Name" column
286     # We expect only 1 result
287     $s->driver->find_element('//table[@id="'.$table_id.'"]//input[@placeholder="Name search"]')->send_keys($patrons[0]->surname);
288     $s->wait_for_ajax;
289     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' );
290
291     subtest 'remember_search' => sub {
292
293         plan tests => 7;
294
295         C4::Context->set_preference( 'PatronsPerPage', 5 );
296         $driver->get( $base_url . "/members/members-home.pl" );
297         $s->fill_form( { search_patron_filter => 'test_patron' } );
298         $s->submit_form;
299         $s->wait_for_ajax;
300         my $patron_selected_text = $driver->find_element('//div[@id="patron_search_selected"]/span')->get_text;
301         is( $patron_selected_text, "", "Patrons selected is not displayed" );
302
303         my @checkboxes = $driver->find_elements(
304             '//input[@type="checkbox"][@name="borrowernumber"]');
305         $checkboxes[2]->click;
306         $patron_selected_text = $driver->find_element('//div[@id="patron_search_selected"]/span')->get_text;
307         is( $patron_selected_text, "Patrons selected: 1", "One patron selected" );
308
309         $checkboxes[4]->click;
310         $patron_selected_text = $driver->find_element('//div[@id="patron_search_selected"]/span')->get_text;
311         is( $patron_selected_text, "Patrons selected: 2", "Two patrons are selected" );
312
313         $driver->find_element('//*[@id="memberresultst_next"]')->click;
314         $s->wait_for_ajax;
315         @checkboxes = $driver->find_elements(
316             '//input[@type="checkbox"][@name="borrowernumber"]');
317         $checkboxes[0]->click;
318         $patron_selected_text = $driver->find_element('//div[@id="patron_search_selected"]/span')->get_text;
319         is( $patron_selected_text, "Patrons selected: 3", "Three patrons are selected" );
320
321         # Perform another search
322         $driver->get( $base_url . "/members/members-home.pl" );
323         $s->fill_form( { search_patron_filter => 'test_patron' } );
324         $s->submit_form;
325         $s->wait_for_ajax;
326         $patron_selected_text = $driver->find_element('//div[@id="patron_search_selected"]/span')->get_text;
327         is( $patron_selected_text, "Patrons selected: 3", "Three patrons still selected" );
328
329         $driver->find_element('//*[@id="patronlist-menu"]')->click;
330         $driver->find_element('//a[@class="patron-list-add"]')->click;
331         my $patron_list_name = "my new list";
332         $driver->find_element('//input[@id="new_patron_list"]')->send_keys($patron_list_name);
333         $driver->find_element('//button[@id="add_to_patron_list_submit"]')->click;
334         $s->wait_for_ajax;
335         is( $driver->find_element('//*[@id="patron_list_dialog"]')->get_text, "Added 3 patrons to $patron_list_name." );
336         my $patron_list = $schema->resultset('PatronList')->search({ name => $patron_list_name })->next;
337         is( $schema->resultset('PatronListPatron')->search({ patron_list_id => $patron_list->patron_list_id })->count, 3 );
338
339         $patron_list->delete;
340     };
341
342     subtest 'filter by date of birth' => sub {
343         plan tests => 7;
344
345         C4::Context->set_preference( 'dateformat', 'metric' );
346
347         # We have a patron with date of birth=1980-06-17 => formatted as 17/06/1980
348
349         $driver->get( $base_url . "/members/members-home.pl" );
350         $s->fill_form( { search_patron_filter => 'test_patron' } );
351         $s->submit_form;
352         $s->wait_for_ajax;
353
354         $s->show_all_entries( '//div[@id="' . $table_id . '_wrapper"]' );
355         my $dob_search_filter =
356             $s->driver->find_element( '//table[@id="' . $table_id . '"]//input[@placeholder="Date of birth search"]' );
357
358         $dob_search_filter->send_keys('1980');
359         $s->wait_for_ajax;
360         is( is_patron_shown($patron_27), 1, 'search by correct year shows the patron' );
361         $dob_search_filter->clear;
362
363         $dob_search_filter->send_keys('1986');
364         $s->wait_for_ajax;
365         is( is_patron_shown($patron_27), 0, 'search by incorrect year does not show the patron' );
366         $dob_search_filter->clear;
367
368         $dob_search_filter->send_keys('1980-06');
369         $s->wait_for_ajax;
370         is( is_patron_shown($patron_27), 1, 'search by correct year-month shows the patron' );
371         $dob_search_filter->clear;
372
373         $dob_search_filter->send_keys('1980-06-17');
374         $s->wait_for_ajax;
375         is( is_patron_shown($patron_27), 1, 'search by correct full iso date shows the patron' );
376         $dob_search_filter->clear;
377
378         $dob_search_filter->send_keys('1986-06-17');
379         $s->wait_for_ajax;
380         is( is_patron_shown($patron_27), 0, 'search by incorrect full iso date does not show the patron' );
381         $dob_search_filter->clear;
382
383         $dob_search_filter->send_keys('17/06/1980');
384         $s->wait_for_ajax;
385         is( is_patron_shown($patron_27), 1, 'search by correct full formatted date shows the patron' );
386         $dob_search_filter->clear;
387
388         $dob_search_filter->send_keys('17/06/1986');
389         $s->wait_for_ajax;
390         is( is_patron_shown($patron_27), 0, 'search by incorrect full formatted date does not show the patron' );
391         $dob_search_filter->clear;
392     };
393
394     push @cleanup, $_ for @patrons;
395     push @cleanup, $library;
396     push @cleanup, $library_2;
397     push @cleanup, $patron_category;
398     push @cleanup, $attribute_type, $attribute_type_searchable;
399
400     $driver->quit();
401 };
402
403 sub is_patron_shown {
404     my ($patron) = @_;
405
406     my @checkboxes = $driver->find_elements('//input[@type="checkbox"][@name="borrowernumber"]');
407     return scalar( grep { $_->get_value == $patron->borrowernumber } @checkboxes );
408 }