Bug 26384: Fix executable flags
[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 use C4::Context;
21
22 use utf8;
23 use Test::More tests => 1;
24 use Test::MockModule;
25
26 use C4::Context;
27 use Koha::AuthUtils;
28 use t::lib::Mocks;
29 use t::lib::Selenium;
30 use t::lib::TestBuilder;
31
32 eval { require Selenium::Remote::Driver; };
33 skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
34
35 my $s             = t::lib::Selenium->new;
36 my $driver        = $s->driver;
37 my $opac_base_url = $s->opac_base_url;
38 my $base_url      = $s->base_url;
39 my $builder       = t::lib::TestBuilder->new;
40
41 our @cleanup;
42 subtest 'Search patrons' => sub {
43     plan tests => 12;
44
45     my @patrons;
46     my $borrowernotes           = q|<strong>just 'a" note</strong> \123 ❤|;
47     my $borrowernotes_displayed = q|just 'a" note \123 ❤|;
48     my $branchname = q|<strong>just 'another" library</strong> \123 ❤|;
49     my $firstname  = q|<strong>fir's"tname</strong> \123 ❤|;
50     my $address    = q|<strong>add'res"s</strong> \123 ❤|;
51     my $email      = q|a<strong>bad_email</strong>@example\123 ❤.com|;
52     my $patron_category = $builder->build_object(
53         {
54             class => 'Koha::Patron::Categories',
55             value => { category_type => 'A' }
56         }
57     );
58     my $library = $builder->build_object(
59         { class => 'Koha::Libraries', value => { branchname => $branchname } }
60     );
61     my $default_patron_search_fields = C4::Context->preference('DefaultPatronSearchFields');
62     for my $i ( 1 .. 25 ) {
63         push @patrons,
64           $builder->build_object(
65             {
66                 class => 'Koha::Patrons',
67                 value => {
68                     surname       => "test_patron_" . $i++,
69                     firstname     => $firstname,
70                     categorycode  => $patron_category->categorycode,
71                     branchcode    => $library->branchcode,
72                     borrowernotes => $borrowernotes,
73                     address       => $address,
74                     email         => $email,
75                 }
76             }
77           );
78     }
79
80     $s->auth;
81     C4::Context->set_preference('DefaultPatronSearchFields',"");
82     $driver->get( $base_url . "/members/members-home.pl" );
83     my @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option');
84     my @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option');
85     is( scalar @adv_options, 11, 'All standard fields are searchable if DefaultPatronSearchFields not set');
86     is( scalar @filter_options, 11, 'All standard fields are searchable by filter if DefaultPatronSearchFields not set');
87     C4::Context->set_preference('DefaultPatronSearchFields',"initials");
88     $driver->get( $base_url . "/members/members-home.pl" );
89     @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option');
90     @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option');
91     is( scalar @adv_options, 12, 'New option added when DefaultPatronSearchFields is populated with a field');
92     is( scalar @filter_options, 12, 'New filter option added when DefaultPatronSearchFields is populated with a field');
93     C4::Context->set_preference('DefaultPatronSearchFields',"initials,horses");
94     $driver->get( $base_url . "/members/members-home.pl" );
95     @adv_options = $driver->find_elements('//select[@id="searchfieldstype"]/option');
96     @filter_options = $driver->find_elements('//select[@id="searchfieldstype_filter"]/option');
97     is( scalar @adv_options, 12, 'Invalid option not added when DefaultPatronSearchFields is populated with an invalid field');
98     is( scalar @filter_options, 12, 'Invalid filter option not added when DefaultPatronSearchFields is populated with an invalid field');
99     C4::Context->set_preference('DefaultPatronSearchFields',"");
100     $s->fill_form( { searchmember_filter => 'test_patron' } );
101     $s->submit_form;
102     my $first_patron = $patrons[0];
103
104     my @td = $driver->find_elements('//table[@id="memberresultst"]/tbody/tr/td');
105     like ($td[2]->get_text, qr[\Q$firstname\E],
106         'Column "Name" should be the 3rd and contain the firstname correctly filtered'
107     );
108     like ($td[2]->get_text, qr[\Q$address\E],
109         'Column "Name" should be the 3rd and contain the address correctly filtered'
110     );
111     like ($td[2]->get_text, qr[\Q$email\E],
112         'Column "Name" should be the 3rd and contain the email address correctly filtered'
113     );
114     is( $td[5]->get_text, $branchname,
115         'Column "Library" should be the 6th and contain the html tags - they have been html filtered'
116     );
117     is( $td[9]->get_text, $borrowernotes_displayed,
118         'Column "Circ note" should be the 10th and not contain the html tags - they have not been html filtered'
119     );
120
121     $driver->find_element(
122             '//a[@href="/cgi-bin/koha/members/memberentry.pl?op=modify&destination=circ&borrowernumber='
123           . $first_patron->borrowernumber
124           . '"]' )->click;
125     is(
126         $driver->get_title,
127         sprintf(
128             "Koha › Patrons › Modify patron %s %s (%s) %s (%s) (%s)",
129             $first_patron->title, $first_patron->firstname, $first_patron->othernames, $first_patron->surname, $first_patron->cardnumber,
130             $first_patron->category->description,
131         )
132     );
133     push @cleanup, $_ for @patrons;
134     push @cleanup, $library;
135     push @cleanup, $patron_category;
136     C4::Context->set_preference('DefaultPatronSearchFields',$default_patron_search_fields);
137 };
138
139 END {
140     $_->delete for @cleanup;
141 }