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