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