Bug 21777: Add a regression test
[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 => 3;
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
41 # It seems that we do not have enough records indexed with ES
42 my $SearchEngine_value = C4::Context->preference('SearchEngine');
43 C4::Context->set_preference('SearchEngine', 'Zebra');
44
45 my $AudioAlerts_value = C4::Context->preference('AudioAlerts');
46 C4::Context->set_preference('AudioAlerts', '1');
47
48 my @data_to_cleanup;
49
50 subtest 'OPAC - Remove from cart' => sub {
51     plan tests => 4;
52
53     $driver->get( $opac_base_url . "opac-search.pl?q=d" );
54
55     # A better way to do that would be to modify the way we display the basket count
56     # We should show/hide the count instead or recreate the node
57     my @basket_count_elts = $driver->find_elements('//span[@id="basketcount"]/span');
58     is( scalar(@basket_count_elts), 0, 'Basket should be empty');
59
60     # This will fail if nothing is indexed, but at this point we should have everything setup correctly
61     my @checkboxes = $driver->find_elements('//input[@type="checkbox"][@name="biblionumber"]');
62     my $biblionumber1 = $checkboxes[0]->get_value();
63     my $biblionumber3 = $checkboxes[2]->get_value();
64     my $biblionumber5 = $checkboxes[4]->get_value();
65
66     $driver->find_element('//a[@class="addtocart cart'.$biblionumber1.'"]')->click;
67     my $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
68     is( $basket_count_elt->get_text(),
69         1, 'One element should have been added to the cart' );
70
71     $driver->find_element('//a[@class="addtocart cart'.$biblionumber3.'"]')->click;
72     $driver->find_element('//a[@class="addtocart cart'.$biblionumber5.'"]')->click;
73     $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
74     is( $basket_count_elt->get_text(),
75         3, '3 elements should have been added to the cart' );
76
77     $driver->find_element('//a[@class="cartRemove cartR'.$biblionumber3.'"]')->click;
78     $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
79     is( $basket_count_elt->get_text(),
80         2, '1 element should have been removed from the cart' );
81 };
82
83 subtest 'Play sound on the circulation page' => sub {
84     plan tests => 1;
85
86     my $builder  = t::lib::TestBuilder->new;
87     my $patron = $builder->build_object({ class => 'Koha::Patrons', value => { flags => 0 }});
88
89     my $mainpage = $s->base_url . q|mainpage.pl|;
90     $driver->get($mainpage . q|?logout.x=1|);
91     like( $driver->get_title(), qr(Log in to Koha), );
92     $s->auth;
93
94     $driver->get( $base_url . "/circ/circulation.pl?borrowernumber=" . $patron->borrowernumber );
95
96     my $audio_node = $driver->find_element('//span[@id="audio-alert"]/audio[@src="/intranet-tmpl/prog/sound/beep.ogg"]');
97
98     push @data_to_cleanup, $patron, $patron->category, $patron->library;
99 };
100
101 subtest 'Display circulation table correctly' => sub {
102     plan tests => 1;
103
104     my $builder = t::lib::TestBuilder->new;
105     my $library = $builder->build_object( { class => 'Koha::Libraries' } );
106     my $patron  = $builder->build_object(
107         {
108             class => 'Koha::Patrons',
109             value => { branchcode => $library->branchcode, flags => 0 }
110         }
111     );
112
113     my ( $biblionumber, $biblioitemnumber ) = add_biblio();
114     my $item = $builder->build_object(
115         {
116             class => 'Koha::Items',
117             value => {
118                 biblionumber  => $biblionumber,
119                 homebranch    => $library->branchcode,
120                 holdingbranch => $library->branchcode,
121                 notforloan    => 0,
122                 itemlost      => 0,
123                 withdrawn     => 0,
124             }
125         }
126     );
127     my $context = Test::MockModule->new('C4::Context');
128     $context->mock(
129         'userenv',
130         sub {
131             return { branch => $library->branchcode };
132         }
133     );
134
135     C4::Circulation::AddIssue( $patron->unblessed, $item->barcode );
136
137     my $mainpage = $s->base_url . q|mainpage.pl|;
138     $driver->get($mainpage . q|?logout.x=1|);
139     $s->auth;
140
141     $driver->get( $base_url
142           . "/circ/circulation.pl?borrowernumber="
143           . $patron->borrowernumber );
144
145     # Display the table clicking on the "Show checkouts" button
146     $driver->find_element('//a[@id="issues-table-load-now-button"]')->click;
147
148     my @thead_th = $driver->find_elements('//table[@id="issues-table"]/thead/tr/th');
149     my $thead_length = 0;
150     $thead_length += $_->get_attribute('colspan') || 0 for @thead_th;
151
152     my @tfoot_td = $driver->find_elements('//table[@id="issues-table"]/tfoot/tr/td');
153     my $tfoot_length = 0;
154     $tfoot_length += $_->get_attribute('colspan') || 0 for @tfoot_td;
155
156     my @tbody_td = $driver->find_elements('//table[@id="issues-table"]/tbody/tr/td');
157     my $tbody_length = 0;
158     $tbody_length += $_->get_attribute('colspan') || 0 for @tbody_td;
159
160     is( $thead_length == $tfoot_length && $tfoot_length == $tbody_length,
161         1, "Checkouts table must be correctly aligned" )
162       or diag(
163         "thead: $thead_length ; tfoot: $tfoot_length ; tbody: $tbody_length");
164
165     push @cleanup, $patron->checkouts, $item->biblio, $item, $patron,
166       $patron->category, $library;
167 };
168
169 END {
170     C4::Context->preference('SearchEngine', $SearchEngine_value);
171     C4::Context->preference('AudioAlerts', $AudioAlerts_value);
172     $_->delete for @data_to_cleanup;
173 };
174
175 sub add_biblio {
176     my ($title, $author) = @_;
177
178     my $marcflavour = C4::Context->preference('marcflavour');
179
180     my $biblio = MARC::Record->new();
181     my ( $tag, $code );
182     $tag = $marcflavour eq 'UNIMARC' ? '200' : '245';
183     $biblio->append_fields(
184         MARC::Field->new($tag, ' ', ' ', a => $title || 'a title'),
185     );
186
187     ($tag, $code) = $marcflavour eq 'UNIMARC' ? (200, 'f') : (100, 'a');
188     $biblio->append_fields(
189         MARC::Field->new($tag, ' ', ' ', $code => $author || 'an author'),
190     );
191
192     return C4::Biblio::AddBiblio($biblio, '');
193 }