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>.
27 use C4::Biblio qw( AddBiblio );
28 use C4::Circulation qw( AddIssue );
32 use t::lib::TestBuilder;
35 eval { require Selenium::Remote::Driver; };
37 plan skip_all => "Selenium::Remote::Driver is needed for selenium tests.";
42 my $s = t::lib::Selenium->new;
44 my $driver = $s->driver;
45 my $opac_base_url = $s->opac_base_url;
46 my $base_url = $s->base_url;
47 my $builder = t::lib::TestBuilder->new;
49 # It seems that we do not have enough records indexed with ES
50 my $SearchEngine_value = C4::Context->preference('SearchEngine');
51 C4::Context->set_preference('SearchEngine', 'Zebra');
53 my $AudioAlerts_value = C4::Context->preference('AudioAlerts');
54 C4::Context->set_preference('AudioAlerts', '1');
57 subtest 'OPAC - borrowernumber, branchcode and categorycode as html attributes' => sub {
60 my $patron = $builder->build_object(
61 { class => 'Koha::Patrons', value => { flags => 1 } } );
62 my $password = Koha::AuthUtils::generate_password($patron->category);
63 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
64 $patron->set_password({ password => $password });
65 $s->opac_auth( $patron->userid, $password );
66 my $elt = $driver->find_element('//span[@class="loggedinusername"]');
67 is( $elt->get_attribute('data-branchcode', 1), $patron->library->branchcode,
68 "Since bug 20921 span.loggedinusername should contain data-branchcode"
69 # No idea why we need the second param of get_attribute(). As
70 # data-branchcode is still there after page finished loading.
72 is( $elt->get_attribute('data-borrowernumber', 1), $patron->borrowernumber,
73 "Since bug 20921 span.loggedinusername should contain data-borrowernumber"
75 is( $elt->get_attribute('data-categorycode', 1), $patron->categorycode,
76 "Since bug 26847 span.loggedinusername should contain data-categorycode"
78 push @cleanup, $patron, $patron->category, $patron->library;
81 subtest 'OPAC - Bibliographic record detail page must contain the data-biblionumber' => sub {
84 my $builder = t::lib::TestBuilder->new;
86 my ( $biblionumber, $biblioitemnumber ) = add_biblio();
87 my $biblio = Koha::Biblios->find($biblionumber);
89 $driver->get( $opac_base_url . "opac-detail.pl?biblionumber=$biblionumber" );
91 my $elt = $driver->find_element('//div[@id="catalogue_detail_biblio"]');
92 is( $elt->get_attribute( 'data-biblionumber', 1 ),
93 $biblionumber, "#catalogue_detail_biblio contains data-biblionumber" );
95 push @cleanup, $biblio;
98 subtest 'Bibliographic record detail page must not explode even with invalid metadata' => sub {
101 my $builder = t::lib::TestBuilder->new;
102 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
104 my $mainpage = $s->base_url . q|mainpage.pl|;
105 $driver->get($mainpage . q|?logout.x=1|);
106 like( $driver->get_title(), qr(Log in to Koha), );
109 my ( $biblionumber, $biblioitemnumber ) = add_biblio();
110 my $biblio = Koha::Biblios->find($biblionumber);
112 # Note that there are several "non xml chars" in the control fields
113 my $invalid_data = qq{<?xml version="1.0" encoding="UTF-8"?>
115 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
116 xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
117 xmlns="http://www.loc.gov/MARC21/slim">
118 <leader>00772nam0a2200277 4500</leader>
119 <controlfield tag="001">00
\1faD000015937</controlfield>
120 <controlfield tag="004">00
\1fsa
\1ftm
\1frn
\1fu0</controlfield>
121 <controlfield tag="008">00
\1far19881981
\1fbdk
\1fldan</controlfield>
123 $biblio->metadata->metadata($invalid_data)->store();
125 $driver->get( $base_url . "/catalogue/detail.pl?biblionumber=$biblionumber" );
127 my $biberror = $driver->find_element('//span[@class="biberror"]')->get_text();
128 is( $biberror, "There is an error with this bibliographic record, the view may be degraded.");
129 push @cleanup, $biblio;
132 subtest 'Play sound on the circulation page' => sub {
135 my $builder = t::lib::TestBuilder->new;
136 my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
138 my $mainpage = $s->base_url . q|mainpage.pl|;
139 $driver->get($mainpage . q|?logout.x=1|);
140 like( $driver->get_title(), qr(Log in to Koha), );
143 $driver->get( $base_url . "/circ/circulation.pl?borrowernumber=" . $patron->borrowernumber );
145 my $audio_node = $driver->find_element('//span[@id="audio-alert"]/audio[@src="/intranet-tmpl/prog/sound/beep.ogg"]');
147 push @cleanup, $patron, $patron->category, $patron->library;
150 subtest 'Display circulation table correctly' => sub {
153 my $builder = t::lib::TestBuilder->new;
154 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
155 my $patron = $builder->build_object(
157 class => 'Koha::Patrons',
158 value => { branchcode => $library->branchcode, flags => 0 }
162 my ( $biblionumber, $biblioitemnumber ) = add_biblio();
163 my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' });
164 my $item = $builder->build_sample_item(
166 biblionumber => $biblionumber,
167 library => $library->branchcode,
168 itype => $item_type->itemtype,
171 my $context = Test::MockModule->new('C4::Context');
175 return { branch => $library->branchcode };
179 C4::Circulation::AddIssue( $patron, $item->barcode );
181 my $mainpage = $s->base_url . q|mainpage.pl|;
182 $driver->get($mainpage . q|?logout.x=1|);
185 $driver->get( $base_url
186 . "/circ/circulation.pl?borrowernumber="
187 . $patron->borrowernumber );
189 # Display the table clicking on the "Show checkouts" button
190 $driver->find_element('//a[@id="issues-table-load-now-button"]')->click;
192 my @thead_th = $driver->find_elements('//table[@id="issues-table"]/thead/tr/th');
193 my $thead_length = 0;
194 $thead_length += $_->get_attribute('colspan', 1) || 0 for @thead_th;
196 my @tfoot_td = $driver->find_elements('//table[@id="issues-table"]/tfoot/tr/td');
197 my $tfoot_length = 0;
198 $tfoot_length += $_->get_attribute('colspan', 1) || 0 for @tfoot_td;
200 my @tbody_td = $driver->find_elements('//table[@id="issues-table"]/tbody/tr[2]/td');
201 my $tbody_length = 0;
202 $tbody_length += 1 for @tbody_td;
204 is( $thead_length == $tfoot_length && $tfoot_length == $tbody_length,
205 1, "Checkouts table must be correctly aligned" )
207 "thead: $thead_length ; tfoot: $tfoot_length ; tbody: $tbody_length");
209 push @cleanup, $patron->checkouts, $item, $item->biblio, $patron,
210 $patron->category, $library;
213 subtest 'XSS vulnerabilities in pagination' => sub {
216 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
217 for ( 1 .. 30 ) { # We want the pagination to be displayed
218 push @cleanup, $builder->build_object(
220 class => 'Koha::Virtualshelves',
223 allow_change_from_owner => 1,
224 allow_change_from_others => 0,
225 owner => $patron->borrowernumber
231 my $password = Koha::AuthUtils::generate_password($patron->category);
232 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
233 $patron->set_password({ password => $password });
234 $s->opac_auth( $patron->userid, $password );
236 my $public_lists = $s->opac_base_url . q|opac-shelves.pl?op=list&public=1|;
237 $driver->get($public_lists);
239 $s->remove_error_handler;
240 my $alert_text = eval { $driver->get_alert_text() };
241 $s->add_error_handler;
242 is( $alert_text, undef, 'No alert box displayed' );
244 my $booh_alert = 'booh!';
245 $public_lists = $s->opac_base_url . qq|opac-shelves.pl?op=list&public=1"><script>alert('$booh_alert')</script>|;
246 $driver->get($public_lists);
248 $s->remove_error_handler;
249 $alert_text = eval { $driver->get_alert_text() };
250 $s->add_error_handler;
251 is( $alert_text, undef, 'No alert box displayed, even if evil intent' );
253 my $second_page = $driver->find_element('//div[@class="pages"]/span[@class="currentPage"]/following-sibling::a');
254 like( $second_page->get_attribute('href'), qr{(?|&)public=1(&|$)}, 'The second page should display category without the invalid value' );
256 push @cleanup, $patron, $patron->category, $patron->library;
260 subtest 'Encoding in session variables' => sub {
263 my $builder = t::lib::TestBuilder->new;
264 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
265 my $patron = $builder->build_object(
267 class => 'Koha::Patrons',
268 value => { branchcode => $library->branchcode, flags => 0 }
272 my $biblio = $builder->build_sample_biblio;
273 my $item = $builder->build_sample_item(
275 biblionumber => $biblio->biblionumber,
276 library => $library->branchcode,
280 my $original_SessionStorage = C4::Context->preference('SessionStorage');
281 for my $SessionStorage ( qw( memcached mysql tmp ) ) {
282 C4::Context->set_preference( 'SessionStorage', $SessionStorage );
283 for my $branchname (qw( Test1 Test2❤️ Test3ä )) {
285 Koha::Libraries->find($branchname) || $builder->build_object(
287 class => 'Koha::Libraries',
289 branchcode => $branchname,
290 branchname => $branchname,
294 # Make sure we are logged in
295 $driver->get( $base_url . q|mainpage.pl?logout.x=1| );
297 # Switch to the new library
298 $driver->get( $base_url . 'circ/set-library.pl' );
299 $s->fill_form( { branch => $branchname } );
302 $driver->get( $base_url
303 . 'circ/circulation.pl?borrowernumber='
304 . $patron->borrowernumber );
305 # We must have the logged-in-branch-name displayed, or we got a 500
307 $driver->find_element( '//span[@class="logged-in-branch-name"]')->get_text(),
309 sprintf( "logged-in-branch-name set - SessionStorage=%s, branchname=%s", $SessionStorage, $branchname
313 $driver->find_element('//input[@id="barcode"]')->send_keys( $item->barcode );
314 $driver->find_element('//fieldset[@id="circ_circulation_issue"]/button[@type="submit"]')->click;
316 # Display the table clicking on the "Show checkouts" button
317 $driver->find_element('//a[@id="issues-table-load-now-button"]')
320 my @tds = $driver->find_elements(
321 '//table[@id="issues-table"]/tbody/tr[2]/td');
323 # Select the td for "Checked out from" (FIXME this is not robust and could be improved
324 my $td_checked_out_from = $tds[8];
326 $td_checked_out_from->get_text(),
328 sprintf( "'Checked out from' column should contain the branchname - SessionStorage=%s, branchname=%s", $SessionStorage, $branchname )
331 # Remove the check in
332 Koha::Checkouts->find({ itemnumber => $item->itemnumber })->delete;
336 C4::Context->set_preference('SessionStorage', $original_SessionStorage);
337 push @cleanup, $item, $biblio, $patron, $patron->category, $patron->library;
338 push @cleanup, Koha::Libraries->find($_) for qw( Test1 Test2❤️ Test3ä );
342 subtest 'OPAC - Suggest for purchase' => sub {
345 my $builder = t::lib::TestBuilder->new;
347 my $patron = $builder->build_object( { class => 'Koha::Patrons', value => { flags => 1 } } );
348 my $password = Koha::AuthUtils::generate_password( $patron->category );
349 t::lib::Mocks::mock_preference( 'RequireStrongPassword', 0 );
350 $patron->set_password( { password => $password } );
351 $s->opac_auth( $patron->userid, $password );
353 my ( $biblionumber, $biblioitemnumber ) = add_biblio();
354 my $biblio = Koha::Biblios->find($biblionumber);
355 $driver->get( $opac_base_url . "opac-detail.pl?biblionumber=$biblionumber" );
357 $s->click({ href => '/opac-suggestions.pl?op=add&biblionumber=' . $biblionumber });
358 is( $driver->find_element('//a[@id="title"]')->get_text(),
360 "Suggestion's title correctly filled in with biblio's title" );
362 $driver->find_element('//textarea[@id="note"]')->send_keys('some notes');
365 my $suggestions = Koha::Suggestions->search( { biblionumber => $biblio->biblionumber } );
366 is( $suggestions->count, 1, 'Suggestion created' );
367 my $suggestion = $suggestions->next;
368 is( $suggestion->title, $biblio->title, q{suggestion's title has biblio's title} );
369 is( $suggestion->note, 'some notes', q{suggestion's note correctly saved} );
371 push @cleanup, $biblio, $suggestion;
378 C4::Context->set_preference('SearchEngine', $SearchEngine_value);
379 C4::Context->set_preference('AudioAlerts', $AudioAlerts_value);
380 $_->delete for @cleanup;
384 my ($title, $author) = @_;
386 my $marcflavour = C4::Context->preference('marcflavour');
388 my $biblio = MARC::Record->new();
390 $tag = $marcflavour eq 'UNIMARC' ? '200' : '245';
391 $biblio->append_fields(
392 MARC::Field->new($tag, ' ', ' ', a => $title || 'a title'),
395 ($tag, $code) = $marcflavour eq 'UNIMARC' ? (200, 'f') : (100, 'a');
396 $biblio->append_fields(
397 MARC::Field->new($tag, ' ', ' ', $code => $author || 'an author'),
400 return C4::Biblio::AddBiblio($biblio, '');