Bug 31104: Add a selenium test
[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
53 subtest 'Search patrons' => sub {
54     plan tests => 24;
55
56     if ( Koha::Patrons->search({surname => {-like => "test_patron_%"}})->count ) {
57         BAIL_OUT("Cannot run this test, data we need to create already exist in the DB");
58     }
59     my @patrons;
60     my $borrowernotes           = q|<strong>just 'a" note</strong> \123 ❤|;
61     my $borrowernotes_displayed = q|just 'a" note \123 ❤|;
62     my $branchname = q|<strong>just 'another" library</strong> \123 ❤|;
63     my $firstname  = q|<strong>fir's"tname</strong> \123 ❤|;
64     my $address    = q|<strong>add'res"s</strong> \123 ❤|;
65     my $email      = q|a<strong>bad_email</strong>@example\123 ❤.com|;
66     my $patron_category = $builder->build_object(
67         {
68             class => 'Koha::Patron::Categories',
69             value => { category_type => 'A' }
70         }
71     );
72     my $library = $builder->build_object(
73         { class => 'Koha::Libraries', value => { branchname => $branchname } }
74     );
75     my $default_patron_search_fields = C4::Context->preference('DefaultPatronSearchFields');
76     my $default_patron_per_page = C4::Context->preference('PatronsPerPage');
77     for my $i ( 1 .. 25 ) {
78         push @patrons,
79           $builder->build_object(
80             {
81                 class => 'Koha::Patrons',
82                 value => {
83                     surname       => "test_patron_" . $i++,
84                     firstname     => $firstname,
85                     categorycode  => $patron_category->categorycode,
86                     branchcode    => $library->branchcode,
87                     borrowernotes => $borrowernotes,
88                     address       => $address,
89                     email         => $email,
90                 }
91             }
92           );
93     }
94
95     push @patrons, $builder->build_object(
96         {
97             class => 'Koha::Patrons',
98             value => {
99                 surname   => "test",
100                 firstname => "not_p_a_t_r_o_n",    # won't match 'patron'
101                 categorycode  => $patron_category->categorycode,
102                 branchcode    => $library->branchcode,
103                 borrowernotes => $borrowernotes,
104                 address       => $address,
105                 email         => $email,
106             }
107         }
108     );
109
110     my $library_2 = $builder->build_object(
111         { class => 'Koha::Libraries', value => { branchname => 'X' . $branchname } }
112     );
113     push @patrons,
114       $builder->build_object(
115         {
116             class => 'Koha::Patrons',
117             value => {
118                 surname       => "test_patron_26",
119                 firstname     => $firstname,
120                 categorycode  => $patron_category->categorycode,
121                 branchcode    => $library_2->branchcode,
122                 borrowernotes => $borrowernotes,
123                 address       => $address,
124                 email         => $email,
125             }
126         }
127       );
128
129     my $attribute_type = Koha::Patron::Attribute::Type->new(
130         {
131             code        => 'my code1',
132             description => 'my description1',
133         }
134     )->store;
135     my $attribute_type_searchable = Koha::Patron::Attribute::Type->new(
136         {
137             code             => 'my code2',
138             description      => 'my description2',
139             opac_display     => 1,
140             staff_searchable => 1
141         }
142     )->store;
143     $patrons[0]->extended_attributes([
144         { code => $attribute_type->code, attribute => 'test_attr_1' },
145         { code => $attribute_type_searchable->code, attribute => 'test_attr_2'},
146     ]);
147     $patrons[1]->extended_attributes([
148         { code => $attribute_type->code, attribute => 'test_attr_1' },
149         { code => $attribute_type_searchable->code, attribute => 'test_attr_2'},
150     ]);
151
152     my $total_number_of_patrons = Koha::Patrons->search->count;
153     my $table_id = "memberresultst";
154
155     $driver->get($mainpage . q|?logout.x=1|);
156     $s->auth;
157     C4::Context->set_preference('DefaultPatronSearchFields',"");
158     my $PatronsPerPage = 15;
159     my $nb_standard_fields = 13;
160     C4::Context->set_preference('PatronsPerPage', $PatronsPerPage);
161     $driver->get( $base_url . "/members/members-home.pl" );
162     my @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option');
163     is( scalar @adv_options, $nb_standard_fields + 1, 'All standard fields are searchable if DefaultPatronSearchFields not set. middle_name is there.');
164     is( $adv_options[0]->get_value(), 'firstname,middle_name,surname,othernames,cardnumber,userid', 'Standard search uses hard coded list when DefaultPatronSearchFields not set');
165     my @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option');
166     is( scalar @filter_options, $nb_standard_fields + 1, 'All standard fields + middle_name are searchable by filter if DefaultPatronSearchFields not set');
167     is( $filter_options[0]->get_value(), 'firstname,middle_name,surname,othernames,cardnumber,userid', 'Standard filter uses hard coded list when DefaultPatronSearchFields not set');
168     C4::Context->set_preference('DefaultPatronSearchFields',"firstname,initials");
169     $driver->get( $base_url . "/members/members-home.pl" );
170     @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option');
171     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');
172     is( $adv_options[0]->get_value(), 'firstname,initials', 'Standard search uses DefaultPatronSearchFields when populated');
173     @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option');
174     is( scalar @filter_options, $nb_standard_fields, 'New filter option added when DefaultPatronSearchFields is populated with a field');
175     is( $filter_options[0]->get_value(), 'firstname,initials', 'Standard filter uses DefaultPatronSearchFields when populated');
176     C4::Context->set_preference('DefaultPatronSearchFields',"firstname,initials,horses");
177     $driver->get( $base_url . "/members/members-home.pl" );
178     @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option');
179     @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option');
180     is( scalar @adv_options, $nb_standard_fields, 'Invalid option not added when DefaultPatronSearchFields is populated with an invalid field');
181     is( scalar @filter_options, $nb_standard_fields, 'Invalid filter option not added when DefaultPatronSearchFields is populated with an invalid field');
182     # NOTE: We should probably ensure the bad field is removed from 'standard' search here, else searches are broken
183     C4::Context->set_preference('DefaultPatronSearchFields',"");
184     $driver->get( $base_url . "/members/members-home.pl" );
185     $s->fill_form( { search_patron_filter => 'test_patron' } );
186     $s->submit_form;
187     my $first_patron = $patrons[0];
188
189     $s->wait_for_ajax;
190     my @td = $driver->find_elements('//table[@id="'.$table_id.'"]/tbody/tr/td');
191     like ($td[2]->get_text, qr[\Q$firstname\E],
192         'Column "Name" should be the 3rd and contain the firstname correctly filtered'
193     );
194     like ($td[2]->get_text, qr[\Q$address\E],
195         'Column "Name" should be the 3rd and contain the address correctly filtered'
196     );
197     like ($td[2]->get_text, qr[\Q$email\E],
198         'Column "Name" should be the 3rd and contain the email address correctly filtered'
199     );
200     is( $td[4]->get_text, $branchname,
201         'Column "Library" should be the 6th and contain the html tags - they have been html filtered'
202     );
203     is( $td[9]->get_text, $borrowernotes_displayed,
204         'Column "Circ note" should be the 10th and not contain the html tags - they have not been html filtered'
205     );
206
207     $driver->find_element(
208             '//a[@href="/cgi-bin/koha/members/memberentry.pl?op=modify&destination=circ&borrowernumber='
209           . $first_patron->borrowernumber
210           . '"]' )->click;
211     is(
212         $driver->get_title,
213         sprintf(
214             "Modify patron %s %s %s (%s) %s (%s) (%s) › Patrons › Koha",
215             $first_patron->title, $first_patron->firstname, $first_patron->middle_name, $first_patron->othernames, $first_patron->surname, $first_patron->cardnumber,
216             $first_patron->category->description,
217         ),
218         'Page title is correct after following modification link'
219     );
220
221     $driver->get( $base_url . "/members/members-home.pl" );
222     $s->fill_form( { search_patron_filter => 'test_patron' } );
223     $s->submit_form;
224     $s->wait_for_ajax;
225
226     $s->driver->find_element('//*[@id="'.$table_id.'_filter"]//input')->send_keys('test_patron');
227     $s->wait_for_ajax;
228     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' );
229
230     $s->driver->find_element('//table[@id="'.$table_id.'"]//th[@data-filter="libraries"]/select/option[@value="'.$library->branchcode.'"]')->click;
231     $s->wait_for_ajax;
232     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' );
233
234     # Reset the filters
235     $driver->find_element('//form[@id="patron_search_form"]//*[@id="clear_search"]')->click();
236     $s->submit_form;
237     $s->wait_for_ajax;
238
239     # And make sure all the patrons are present
240     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' );
241
242     # Pattern terms must be split
243     $s->fill_form( { search_patron_filter => 'test patron' } );
244     $s->submit_form;
245
246     $s->wait_for_ajax;
247     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) );
248     $driver->find_element('//form[@id="patron_search_form"]//*[@id="clear_search"]')->click();
249     $s->submit_form;
250     $s->wait_for_ajax;
251
252     # Search on non-searchable attribute, we expect no result!
253     $s->fill_form( { search_patron_filter => 'test_attr_1' } );
254     $s->submit_form;
255     $s->wait_for_ajax;
256
257     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' );
258
259     # clear form
260     $driver->find_element('//form[@id="patron_search_form"]//*[@id="clear_search"]')->click();
261     # Search on searchable attribute, we expect 2 patrons
262     $s->fill_form( { search_patron_filter => 'test_attr_2' } );
263     $s->submit_form;
264     $s->wait_for_ajax;
265
266     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' );
267
268     # Refine search and search for test_patron in all the data using the DT global search
269     # No change in result expected, still 2 patrons
270     $s->driver->find_element('//*[@id="'.$table_id.'_filter"]//input')->send_keys('test_patron');
271     $s->wait_for_ajax;
272     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' );
273
274     # Adding the surname of the first patron in the "Name" column
275     # We expect only 1 result
276     $s->driver->find_element('//table[@id="'.$table_id.'"]//input[@placeholder="Name search"]')->send_keys($patrons[0]->surname);
277     $s->wait_for_ajax;
278     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' );
279
280     push @cleanup, $_ for @patrons;
281     push @cleanup, $library;
282     push @cleanup, $library_2;
283     push @cleanup, $patron_category;
284     push @cleanup, $attribute_type, $attribute_type_searchable;
285     C4::Context->set_preference('DefaultPatronSearchFields',$default_patron_search_fields);
286     C4::Context->set_preference('PatronsPerPage',$default_patron_per_page);
287 };
288
289 subtest 'Error too long' => sub {
290     plan tests => 1;
291     $driver->get($mainpage . q|?logout.x=1|);
292     $s->auth;
293
294     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};
295     $driver->get( $base_url . "/members/members-home.pl" );
296     $s->fill_form( { search_patron_filter => $very_long_string } );
297     $s->submit_form;
298     $s->wait_for_ajax;
299     ok( 1, "No error when querying a very long search" );
300 };
301
302 $driver->quit();