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