3 # This file is part of Koha.
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.
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.
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>.
22 use Test::More tests => 6;
26 use C4::Biblio qw( AddBiblio );
31 use t::lib::TestBuilder;
34 eval { require Selenium::Remote::Driver; };
35 skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
37 my $s = t::lib::Selenium->new;
39 my $driver = $s->driver;
40 my $opac_base_url = $s->opac_base_url;
41 my $base_url = $s->base_url;
42 my $builder = t::lib::TestBuilder->new;
44 # It seems that we do not have enough records indexed with ES
45 my $SearchEngine_value = C4::Context->preference('SearchEngine');
46 C4::Context->set_preference('SearchEngine', 'Zebra');
48 my $AudioAlerts_value = C4::Context->preference('AudioAlerts');
49 C4::Context->set_preference('AudioAlerts', '1');
52 subtest 'OPAC - borrowernumber, branchcode and categorycode as html attributes' => sub {
55 my $patron = $builder->build_object(
56 { class => 'Koha::Patrons', value => { flags => 1 } } );
57 my $password = Koha::AuthUtils::generate_password($patron->category);
58 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
59 $patron->set_password({ password => $password });
60 $s->opac_auth( $patron->userid, $password );
61 my $elt = $driver->find_element('//span[@class="loggedinusername"]');
62 is( $elt->get_attribute('data-branchcode', 1), $patron->library->branchcode,
63 "Since bug 20921 span.loggedinusername should contain data-branchcode"
64 # No idea why we need the second param of get_attribute(). As
65 # data-branchcode is still there after page finished loading.
67 is( $elt->get_attribute('data-borrowernumber', 1), $patron->borrowernumber,
68 "Since bug 20921 span.loggedinusername should contain data-borrowernumber"
70 is( $elt->get_attribute('data-categorycode', 1), $patron->categorycode,
71 "Since bug 26847 span.loggedinusername should contain data-categorycode"
73 push @cleanup, $patron, $patron->category, $patron->library;
76 subtest 'OPAC - Bibliographic record detail page must contain the data-biblionumber' => sub {
79 my $builder = t::lib::TestBuilder->new;
81 my ( $biblionumber, $biblioitemnumber ) = add_biblio();
82 my $biblio = Koha::Biblios->find($biblionumber);
84 $driver->get( $opac_base_url . "opac-detail.pl?biblionumber=$biblionumber" );
86 my $elt = $driver->find_element('//div[@id="catalogue_detail_biblio"]');
87 is( $elt->get_attribute( 'data-biblionumber', 1 ),
88 $biblionumber, "#catalogue_detail_biblio contains data-biblionumber" );
90 push @cleanup, $biblio;
93 subtest 'OPAC - Remove from cart' => sub {
96 # We need to prevent scrolling to prevent the floating toolbar from overlapping buttons we are testing
97 my $window_size = $driver->get_window_size();
98 $driver->set_window_size(1920,10800);
100 $driver->get( $opac_base_url . "opac-search.pl?q=d" );
102 # A better way to do that would be to modify the way we display the basket count
103 # We should show/hide the count instead or recreate the node
104 my @basket_count_elts = $driver->find_elements('//span[@id="basketcount"]/span');
105 is( scalar(@basket_count_elts), 0, 'Basket should be empty');
107 # This will fail if nothing is indexed, but at this point we should have everything setup correctly
108 my @checkboxes = $driver->find_elements('//input[@type="checkbox"][@name="biblionumber"]');
109 my $biblionumber1 = $checkboxes[0]->get_value();
110 my $biblionumber3 = $checkboxes[2]->get_value();
111 my $biblionumber5 = $checkboxes[4]->get_value();
113 $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber1.'"]')->click;
114 my $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
115 is( $basket_count_elt->get_text(),
116 1, 'One element should have been added to the cart' );
118 $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber3.'"]')->click;
119 $driver->find_element('//a[@class="btn btn-link btn-sm addtocart cart cart'.$biblionumber5.'"]')->click;
120 $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
121 is( $basket_count_elt->get_text(),
122 3, '3 elements should have been added to the cart' );
124 $driver->find_element('//a[@class="btn btn-link btn-sm remove cartRemove cartR'.$biblionumber3.'"]')->click;
125 $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
126 is( $basket_count_elt->get_text(),
127 2, '1 element should have been removed from the cart' );
130 $driver->set_window_size($window_size->{'height'}, $window_size->{'width'});
133 subtest 'Play sound on the circulation page' => sub {
136 my $builder = t::lib::TestBuilder->new;
137 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
139 my $mainpage = $s->base_url . q|mainpage.pl|;
140 $driver->get($mainpage . q|?logout.x=1|);
141 like( $driver->get_title(), qr(Log in to Koha), );
144 $driver->get( $base_url . "/circ/circulation.pl?borrowernumber=" . $patron->borrowernumber );
146 my $audio_node = $driver->find_element('//span[@id="audio-alert"]/audio[@src="/intranet-tmpl/prog/sound/beep.ogg"]');
148 push @cleanup, $patron, $patron->category, $patron->library;
151 subtest 'Display circulation table correctly' => sub {
154 my $builder = t::lib::TestBuilder->new;
155 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
156 my $patron = $builder->build_object(
158 class => 'Koha::Patrons',
159 value => { branchcode => $library->branchcode, flags => 0 }
163 my ( $biblionumber, $biblioitemnumber ) = add_biblio();
164 my $item = $builder->build_sample_item(
166 biblionumber => $biblionumber,
167 library => $library->branchcode,
170 my $context = Test::MockModule->new('C4::Context');
174 return { branch => $library->branchcode };
178 C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
180 my $mainpage = $s->base_url . q|mainpage.pl|;
181 $driver->get($mainpage . q|?logout.x=1|);
184 $driver->get( $base_url
185 . "/circ/circulation.pl?borrowernumber="
186 . $patron->borrowernumber );
188 # Display the table clicking on the "Show checkouts" button
189 $driver->find_element('//a[@id="issues-table-load-now-button"]')->click;
191 my @thead_th = $driver->find_elements('//table[@id="issues-table"]/thead/tr/th');
192 my $thead_length = 0;
193 $thead_length += $_->get_attribute('colspan', 1) || 0 for @thead_th;
195 my @tfoot_td = $driver->find_elements('//table[@id="issues-table"]/tfoot/tr/td');
196 my $tfoot_length = 0;
197 $tfoot_length += $_->get_attribute('colspan', 1) || 0 for @tfoot_td;
199 my @tbody_td = $driver->find_elements('//table[@id="issues-table"]/tbody/tr[2]/td');
200 my $tbody_length = 0;
201 $tbody_length += 1 for @tbody_td;
203 is( $thead_length == $tfoot_length && $tfoot_length == $tbody_length,
204 1, "Checkouts table must be correctly aligned" )
206 "thead: $thead_length ; tfoot: $tfoot_length ; tbody: $tbody_length");
208 push @cleanup, $patron->checkouts, $item, $item->biblio, $patron,
209 $patron->category, $library;
212 subtest 'XSS vulnerabilities in pagination' => sub {
215 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
216 for ( 1 .. 30 ) { # We want the pagination to be displayed
217 push @cleanup, $builder->build_object(
219 class => 'Koha::Virtualshelves',
222 allow_change_from_owner => 1,
223 allow_change_from_others => 0,
224 owner => $patron->borrowernumber
230 my $password = Koha::AuthUtils::generate_password($patron->category);
231 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
232 $patron->set_password({ password => $password });
233 $s->opac_auth( $patron->userid, $password );
235 my $public_lists = $s->opac_base_url . q|opac-shelves.pl?op=list&category=2|;
236 $driver->get($public_lists);
238 $s->remove_error_handler;
239 my $alert_text = eval { $driver->get_alert_text() };
240 $s->add_error_handler;
241 is( $alert_text, undef, 'No alert box displayed' );
243 my $booh_alert = 'booh!';
244 $public_lists = $s->opac_base_url . qq|opac-shelves.pl?op=list&category=2"><script>alert('$booh_alert')</script>|;
245 $driver->get($public_lists);
247 $s->remove_error_handler;
248 $alert_text = eval { $driver->get_alert_text() };
249 $s->add_error_handler;
250 is( $alert_text, undef, 'No alert box displayed, even if evil intent' );
252 my $second_page = $driver->find_element('//div[@class="pages"]/span[@class="currentPage"]/following-sibling::a');
253 like( $second_page->get_attribute('href'), qr{category=2%22%3E%3Cscript%3Ealert%28%27booh%21%27%29%3C%2Fscript%3E}, 'The second page should display the variables and attributes correctly URI escaped' );
255 push @cleanup, $patron, $patron->category, $patron->library;
261 C4::Context->set_preference('SearchEngine', $SearchEngine_value);
262 C4::Context->set_preference('AudioAlerts', $AudioAlerts_value);
263 $_->delete for @cleanup;
267 my ($title, $author) = @_;
269 my $marcflavour = C4::Context->preference('marcflavour');
271 my $biblio = MARC::Record->new();
273 $tag = $marcflavour eq 'UNIMARC' ? '200' : '245';
274 $biblio->append_fields(
275 MARC::Field->new($tag, ' ', ' ', a => $title || 'a title'),
278 ($tag, $code) = $marcflavour eq 'UNIMARC' ? (200, 'f') : (100, 'a');
279 $biblio->append_fields(
280 MARC::Field->new($tag, ' ', ' ', $code => $author || 'an author'),
283 return C4::Biblio::AddBiblio($biblio, '');