7ea7e0bbcbbf14065c468b42221537239df66c25
[koha.git] / t / db_dependent / selenium / regressions.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 Test::More tests => 4;
23 use Test::MockModule;
24
25 use C4::Context;
26 use C4::Biblio qw( AddBiblio );
27 use C4::Circulation;
28 use Koha::AuthUtils;
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
37 my $driver = $s->driver;
38 my $opac_base_url = $s->opac_base_url;
39 my $base_url = $s->base_url;
40 my $builder = t::lib::TestBuilder->new;
41
42 # It seems that we do not have enough records indexed with ES
43 my $SearchEngine_value = C4::Context->preference('SearchEngine');
44 C4::Context->set_preference('SearchEngine', 'Zebra');
45
46 my $AudioAlerts_value = C4::Context->preference('AudioAlerts');
47 C4::Context->set_preference('AudioAlerts', '1');
48
49 my @data_to_cleanup;
50
51 subtest 'OPAC - Remove from cart' => sub {
52     plan tests => 4;
53
54     $driver->get( $opac_base_url . "opac-search.pl?q=d" );
55
56     # A better way to do that would be to modify the way we display the basket count
57     # We should show/hide the count instead or recreate the node
58     my @basket_count_elts = $driver->find_elements('//span[@id="basketcount"]/span');
59     is( scalar(@basket_count_elts), 0, 'Basket should be empty');
60
61     # This will fail if nothing is indexed, but at this point we should have everything setup correctly
62     my @checkboxes = $driver->find_elements('//input[@type="checkbox"][@name="biblionumber"]');
63     my $biblionumber1 = $checkboxes[0]->get_value();
64     my $biblionumber3 = $checkboxes[2]->get_value();
65     my $biblionumber5 = $checkboxes[4]->get_value();
66
67     $driver->find_element('//a[@class="addtocart cart'.$biblionumber1.'"]')->click;
68     my $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
69     is( $basket_count_elt->get_text(),
70         1, 'One element should have been added to the cart' );
71
72     $driver->find_element('//a[@class="addtocart cart'.$biblionumber3.'"]')->click;
73     $driver->find_element('//a[@class="addtocart cart'.$biblionumber5.'"]')->click;
74     $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
75     is( $basket_count_elt->get_text(),
76         3, '3 elements should have been added to the cart' );
77
78     $driver->find_element('//a[@class="cartRemove cartR'.$biblionumber3.'"]')->click;
79     $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
80     is( $basket_count_elt->get_text(),
81         2, '1 element should have been removed from the cart' );
82 };
83
84 subtest 'Play sound on the circulation page' => sub {
85     plan tests => 1;
86
87     my $builder  = t::lib::TestBuilder->new;
88     my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
89
90     my $mainpage = $s->base_url . q|mainpage.pl|;
91     $driver->get($mainpage . q|?logout.x=1|);
92     like( $driver->get_title(), qr(Log in to Koha), );
93     $s->auth;
94
95     $driver->get( $base_url . "/circ/circulation.pl?borrowernumber=" . $patron->borrowernumber );
96
97     my $audio_node = $driver->find_element('//span[@id="audio-alert"]/audio[@src="/intranet-tmpl/prog/sound/beep.ogg"]');
98
99     push @data_to_cleanup, $patron, $patron->category, $patron->library;
100 };
101
102 subtest 'Display circulation table correctly' => sub {
103     plan tests => 1;
104
105     my $builder = t::lib::TestBuilder->new;
106     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
107     my $patron  = $builder->build_object(
108         {
109             class => 'Koha::Patrons',
110             value => { branchcode => $library->branchcode, flags => 0 }
111         }
112     );
113
114     my ( $biblionumber, $biblioitemnumber ) = add_biblio();
115     my $item = $builder->build_object(
116         {
117             class => 'Koha::Items',
118             value => {
119                 biblionumber  => $biblionumber,
120                 homebranch    => $library->branchcode,
121                 holdingbranch => $library->branchcode,
122                 notforloan    => 0,
123                 itemlost      => 0,
124                 withdrawn     => 0,
125             }
126         }
127     );
128     my $context = Test::MockModule->new('C4::Context');
129     $context->mock(
130         'userenv',
131         sub {
132             return { branch => $library->branchcode };
133         }
134     );
135
136     C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
137
138     my $mainpage = $s->base_url . q|mainpage.pl|;
139     $driver->get($mainpage . q|?logout.x=1|);
140     $s->auth;
141
142     $driver->get( $base_url
143           . "/circ/circulation.pl?borrowernumber="
144           . $patron->borrowernumber );
145
146     # Display the table clicking on the "Show checkouts" button
147     $driver->find_element('//a[@id="issues-table-load-now-button"]')->click;
148
149     my @thead_th = $driver->find_elements('//table[@id="issues-table"]/thead/tr/th');
150     my $thead_length = 0;
151     $thead_length += $_->get_attribute('colspan') || 0 for @thead_th;
152
153     my @tfoot_td = $driver->find_elements('//table[@id="issues-table"]/tfoot/tr/td');
154     my $tfoot_length = 0;
155     $tfoot_length += $_->get_attribute('colspan') || 0 for @tfoot_td;
156
157     my @tbody_td = $driver->find_elements('//table[@id="issues-table"]/tbody/tr/td');
158     my $tbody_length = 0;
159     $tbody_length += $_->get_attribute('colspan') || 0 for @tbody_td;
160
161     is( $thead_length == $tfoot_length && $tfoot_length == $tbody_length,
162         1, "Checkouts table must be correctly aligned" )
163       or diag(
164         "thead: $thead_length ; tfoot: $tfoot_length ; tbody: $tbody_length");
165
166     push @data_to_cleanup, $patron->checkouts, $item->biblio, $item, $patron,
167       $patron->category, $library;
168 };
169
170 subtest 'XSS vulnerabilities in pagination' => sub {
171     plan tests => 3;
172
173     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
174     for ( 1 .. 30 ) { # We want the pagination to be displayed
175         push @data_to_cleanup, $builder->build_object(
176             {
177                 class => 'Koha::Virtualshelves',
178                 value => {
179                     category                 => 1,
180                     allow_change_from_owner  => 1,
181                     allow_change_from_others => 0,
182                     owner                    => $patron->borrowernumber
183                 }
184             }
185         );
186     }
187
188     my $password = Koha::AuthUtils::generate_password();
189     t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
190     $patron->set_password({ password => $password });
191     $s->opac_auth( $patron->userid, $password );
192
193     my $public_lists = $s->opac_base_url . q|opac-shelves.pl?op=list&category=1|;
194     $driver->get($public_lists);
195
196     $s->remove_error_handler;
197     my $alert_text = eval { $driver->get_alert_text() };
198     $s->add_error_handler;
199     is( $alert_text, undef, 'No alert box displayed' );
200
201     my $booh_alert = 'booh!';
202     $public_lists = $s->opac_base_url . qq|opac-shelves.pl?op=list&category=1"><script>alert('$booh_alert')</script>|;
203     $driver->get($public_lists);
204
205     $s->remove_error_handler;
206     $alert_text = eval { $driver->get_alert_text() };
207     $s->add_error_handler;
208     is( $alert_text, undef, 'No alert box displayed, even if evil intent' );
209
210     my $second_page = $driver->find_element('//div[@class="pages"]/span[@class="currentPage"]/following-sibling::a');
211     like( $second_page->get_attribute('href'), qr{category=1%22%3E%3Cscript%3Ealert%28%27booh%21%27%29%3C%2Fscript%3E}, 'The second patch should displayed the variables and attributes correctly URI escaped' );
212
213     push @data_to_cleanup, $patron, $patron->category, $patron->library;
214 };
215
216 END {
217     C4::Context->preference('SearchEngine', $SearchEngine_value);
218     C4::Context->preference('AudioAlerts', $AudioAlerts_value);
219     $_->delete for @data_to_cleanup;
220 };
221
222 sub add_biblio {
223     my ($title, $author) = @_;
224
225     my $marcflavour = C4::Context->preference('marcflavour');
226
227     my $biblio = MARC::Record->new();
228     my ( $tag, $code );
229     $tag = $marcflavour eq 'UNIMARC' ? '200' : '245';
230     $biblio->append_fields(
231         MARC::Field->new($tag, ' ', ' ', a => $title || 'a title'),
232     );
233
234     ($tag, $code) = $marcflavour eq 'UNIMARC' ? (200, 'f') : (100, 'a');
235     $biblio->append_fields(
236         MARC::Field->new($tag, ' ', ' ', $code => $author || 'an author'),
237     );
238
239     return C4::Biblio::AddBiblio($biblio, '');
240 }