Bug 29697: (QA follow-up) Remove useless warning
[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 use utf8;
20
21 use C4::Context;
22
23 use Test::More;
24 use Test::MockModule;
25
26 use C4::Context;
27 use C4::Biblio qw( AddBiblio );
28 use C4::Circulation qw( AddIssue );
29 use Koha::AuthUtils;
30 use t::lib::Mocks;
31 use t::lib::Selenium;
32 use t::lib::TestBuilder;
33 use t::lib::Mocks;
34
35 eval { require Selenium::Remote::Driver; };
36 if ( $@ ) {
37     plan skip_all => "Selenium::Remote::Driver is needed for selenium tests.";
38 } else {
39     plan tests => 8;
40 }
41
42 my $s = t::lib::Selenium->new;
43
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;
48
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');
52
53 my $AudioAlerts_value = C4::Context->preference('AudioAlerts');
54 C4::Context->set_preference('AudioAlerts', '1');
55
56 our @cleanup;
57 subtest 'OPAC - borrowernumber, branchcode and categorycode as html attributes' => sub {
58     plan tests => 3;
59
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.
71     );
72     is( $elt->get_attribute('data-borrowernumber', 1), $patron->borrowernumber,
73 "Since bug 20921 span.loggedinusername should contain data-borrowernumber"
74     );
75     is( $elt->get_attribute('data-categorycode', 1), $patron->categorycode,
76 "Since bug 26847 span.loggedinusername should contain data-categorycode"
77     );
78     push @cleanup, $patron, $patron->category, $patron->library;
79 };
80
81 subtest 'OPAC - Bibliographic record detail page must contain the data-biblionumber' => sub {
82     plan tests => 1;
83
84     my $builder = t::lib::TestBuilder->new;
85
86     my ( $biblionumber, $biblioitemnumber ) = add_biblio();
87     my $biblio = Koha::Biblios->find($biblionumber);
88
89     $driver->get( $opac_base_url . "opac-detail.pl?biblionumber=$biblionumber" );
90
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" );
94
95     push @cleanup, $biblio;
96 };
97
98 subtest 'Bibliographic record detail page must not explode even with invalid metadata' => sub {
99     plan tests => 2;
100
101     my $builder = t::lib::TestBuilder->new;
102     my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
103
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), );
107     $s->auth;
108
109     my ( $biblionumber, $biblioitemnumber ) = add_biblio();
110     my $biblio = Koha::Biblios->find($biblionumber);
111
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"?>
114     <record
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>
122     </record>};
123     $biblio->metadata->metadata($invalid_data)->store();
124
125     $driver->get( $base_url . "/catalogue/detail.pl?biblionumber=$biblionumber" );
126
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;
130 };
131
132 subtest 'Play sound on the circulation page' => sub {
133     plan tests => 1;
134
135     my $builder  = t::lib::TestBuilder->new;
136     my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
137
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), );
141     $s->auth;
142
143     $driver->get( $base_url . "/circ/circulation.pl?borrowernumber=" . $patron->borrowernumber );
144
145     my $audio_node = $driver->find_element('//span[@id="audio-alert"]/audio[@src="/intranet-tmpl/prog/sound/beep.ogg"]');
146
147     push @cleanup, $patron, $patron->category, $patron->library;
148 };
149
150 subtest 'Display circulation table correctly' => sub {
151     plan tests => 1;
152
153     my $builder = t::lib::TestBuilder->new;
154     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
155     my $patron  = $builder->build_object(
156         {
157             class => 'Koha::Patrons',
158             value => { branchcode => $library->branchcode, flags => 0 }
159         }
160     );
161
162     my ( $biblionumber, $biblioitemnumber ) = add_biblio();
163     my $item_type = $builder->build_object({ class => 'Koha::ItemTypes' });
164     my $item = $builder->build_sample_item(
165         {
166             biblionumber => $biblionumber,
167             library      => $library->branchcode,
168             itype        => $item_type->itemtype,
169         }
170     );
171     my $context = Test::MockModule->new('C4::Context');
172     $context->mock(
173         'userenv',
174         sub {
175             return { branch => $library->branchcode };
176         }
177     );
178
179     C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
180
181     my $mainpage = $s->base_url . q|mainpage.pl|;
182     $driver->get($mainpage . q|?logout.x=1|);
183     $s->auth;
184
185     $driver->get( $base_url
186           . "/circ/circulation.pl?borrowernumber="
187           . $patron->borrowernumber );
188
189     # Display the table clicking on the "Show checkouts" button
190     $driver->find_element('//a[@id="issues-table-load-now-button"]')->click;
191
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;
195
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;
199
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;
203
204     is( $thead_length == $tfoot_length && $tfoot_length == $tbody_length,
205         1, "Checkouts table must be correctly aligned" )
206       or diag(
207         "thead: $thead_length ; tfoot: $tfoot_length ; tbody: $tbody_length");
208
209     push @cleanup, $patron->checkouts, $item, $item->biblio, $patron,
210       $patron->category, $library;
211 };
212
213 subtest 'XSS vulnerabilities in pagination' => sub {
214     plan tests => 3;
215
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(
219             {
220                 class => 'Koha::Virtualshelves',
221                 value => {
222                     public                   => 1,
223                     allow_change_from_owner  => 1,
224                     allow_change_from_others => 0,
225                     owner                    => $patron->borrowernumber
226                 }
227             }
228         );
229     }
230
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 );
235
236     my $public_lists = $s->opac_base_url . q|opac-shelves.pl?op=list&public=1|;
237     $driver->get($public_lists);
238
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' );
243
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);
247
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' );
252
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' );
255
256     push @cleanup, $patron, $patron->category, $patron->library;
257
258 };
259
260 subtest 'Encoding in session variables' => sub {
261     plan tests => 18;
262
263     my $builder = t::lib::TestBuilder->new;
264     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
265     my $patron  = $builder->build_object(
266         {
267             class => 'Koha::Patrons',
268             value => { branchcode => $library->branchcode, flags => 0 }
269         }
270     );
271
272     my $biblio = $builder->build_sample_biblio;
273     my $item = $builder->build_sample_item(
274         {
275             biblionumber => $biblio->biblionumber,
276             library      => $library->branchcode,
277         }
278     );
279
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ä )) {
284             my $library =
285               Koha::Libraries->find($branchname) || $builder->build_object(
286                 {
287                     class => 'Koha::Libraries',
288                     value => {
289                         branchcode => $branchname,
290                         branchname => $branchname,
291                     }
292                 }
293               );
294             # Make sure we are logged in
295             $driver->get( $base_url . q|mainpage.pl?logout.x=1| );
296             $s->auth;
297             # Switch to the new library
298             $driver->get( $base_url . 'circ/set-library.pl' );
299             $s->fill_form( { branch => $branchname } );
300             $s->submit_form;
301             # Check an item out
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
306             is(
307                 $driver->find_element( '//span[@class="logged-in-branch-name"]')->get_text(),
308                 $branchname,
309                 sprintf( "logged-in-branch-name set - SessionStorage=%s, branchname=%s", $SessionStorage, $branchname
310                 )
311             );
312
313             $driver->find_element('//input[@id="barcode"]')->send_keys( $item->barcode );
314             $driver->find_element('//fieldset[@id="circ_circulation_issue"]/button[@type="submit"]')->click;
315
316             # Display the table clicking on the "Show checkouts" button
317             $driver->find_element('//a[@id="issues-table-load-now-button"]')
318               ->click;
319
320             my @tds = $driver->find_elements(
321                 '//table[@id="issues-table"]/tbody/tr[2]/td');
322
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];
325             is(
326                 $td_checked_out_from->get_text(),
327                 $branchname,
328                 sprintf( "'Checked out from' column should contain the branchname - SessionStorage=%s, branchname=%s", $SessionStorage, $branchname )
329             );
330
331             # Remove the check in
332             Koha::Checkouts->find({ itemnumber => $item->itemnumber })->delete;
333         }
334     }
335
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ä );
339
340 };
341
342 subtest 'OPAC - Suggest for purchase' => sub {
343     plan tests => 4;
344
345     my $builder = t::lib::TestBuilder->new;
346
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 );
352
353     my ( $biblionumber, $biblioitemnumber ) = add_biblio();
354     my $biblio = Koha::Biblios->find($biblionumber);
355     $driver->get( $opac_base_url . "opac-detail.pl?biblionumber=$biblionumber" );
356
357     $s->click({ href => '/opac-suggestions.pl?op=add&biblionumber=' . $biblionumber });
358     is( $driver->find_element('//input[@id="title"]')->get_value(),
359         $biblio->title,
360         "Suggestion's title correctly filled in with biblio's title" );
361
362     $driver->find_element('//textarea[@id="note"]')->send_keys('some notes');
363     $s->submit_form;
364
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} );
370
371     push @cleanup, $biblio, $suggestion;
372 };
373
374
375 $driver->quit();
376
377 END {
378     C4::Context->set_preference('SearchEngine', $SearchEngine_value);
379     C4::Context->set_preference('AudioAlerts', $AudioAlerts_value);
380     $_->delete for @cleanup;
381 };
382
383 sub add_biblio {
384     my ($title, $author) = @_;
385
386     my $marcflavour = C4::Context->preference('marcflavour');
387
388     my $biblio = MARC::Record->new();
389     my ( $tag, $code );
390     $tag = $marcflavour eq 'UNIMARC' ? '200' : '245';
391     $biblio->append_fields(
392         MARC::Field->new($tag, ' ', ' ', a => $title || 'a title'),
393     );
394
395     ($tag, $code) = $marcflavour eq 'UNIMARC' ? (200, 'f') : (100, 'a');
396     $biblio->append_fields(
397         MARC::Field->new($tag, ' ', ' ', $code => $author || 'an author'),
398     );
399
400     return C4::Biblio::AddBiblio($biblio, '');
401 }