Bug 21775: Logout before trying to 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 => 3;
23
24 use C4::Context;
25 use Koha::AuthUtils;
26 use t::lib::Selenium;
27 use t::lib::TestBuilder;
28
29 eval { require Selenium::Remote::Driver; };
30 skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
31
32 my $s = t::lib::Selenium->new;
33
34 my $driver = $s->driver;
35 my $opac_base_url = $s->opac_base_url;
36 my $base_url = $s->base_url;
37 my $builder = t::lib::TestBuilder->new;
38
39 # It seems that we do not have enough records indexed with ES
40 my $SearchEngine_value = C4::Context->preference('SearchEngine');
41 C4::Context->set_preference('SearchEngine', 'Zebra');
42
43 my $AudioAlerts_value = C4::Context->preference('AudioAlerts');
44 C4::Context->set_preference('AudioAlerts', '1');
45
46 our @cleanup;
47 subtest 'OPAC - borrowernumber and branchcode as html attributes' => sub {
48     plan tests => 2;
49
50     my $patron = $builder->build_object(
51         { class => 'Koha::Patrons', value => { flags => 1 } } );
52     my $password = Koha::AuthUtils::generate_password();
53     $patron->update_password( $patron->userid, $password );
54     $s->opac_auth( $patron->userid, $password );
55     my $elt = $driver->find_element('//span[@class="loggedinusername"]');
56     is( $elt->get_attribute('data-branchcode'), $patron->library->branchcode,
57         "Since bug 20921 span.loggedinusername should contain data-branchcode"
58     );
59     is( $elt->get_attribute('data-borrowernumber'), $patron->borrowernumber,
60 "Since bug 20921 span.loggedinusername should contain data-borrowernumber"
61     );
62     push @cleanup, $patron, $patron->category, $patron->library;
63 };
64
65 subtest 'OPAC - Remove from cart' => sub {
66     plan tests => 4;
67
68     $driver->get( $opac_base_url . "opac-search.pl?q=d" );
69
70     # A better way to do that would be to modify the way we display the basket count
71     # We should show/hide the count instead or recreate the node
72     my @basket_count_elts = $driver->find_elements('//span[@id="basketcount"]/span');
73     is( scalar(@basket_count_elts), 0, 'Basket should be empty');
74
75     # This will fail if nothing is indexed, but at this point we should have everything setup correctly
76     my @checkboxes = $driver->find_elements('//input[@type="checkbox"][@name="biblionumber"]');
77     my $biblionumber1 = $checkboxes[0]->get_value();
78     my $biblionumber3 = $checkboxes[2]->get_value();
79     my $biblionumber5 = $checkboxes[4]->get_value();
80
81     $driver->find_element('//a[@class="addtocart cart'.$biblionumber1.'"]')->click;
82     my $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
83     is( $basket_count_elt->get_text(),
84         1, 'One element should have been added to the cart' );
85
86     $driver->find_element('//a[@class="addtocart cart'.$biblionumber3.'"]')->click;
87     $driver->find_element('//a[@class="addtocart cart'.$biblionumber5.'"]')->click;
88     $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
89     is( $basket_count_elt->get_text(),
90         3, '3 elements should have been added to the cart' );
91
92     $driver->find_element('//a[@class="cartRemove cartR'.$biblionumber3.'"]')->click;
93     $basket_count_elt = $driver->find_element('//span[@id="basketcount"]/span');
94     is( $basket_count_elt->get_text(),
95         2, '1 element should have been removed from the cart' );
96 };
97
98 subtest 'Play sound on the circulation page' => sub {
99     plan tests => 1;
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     $driver->get( $base_url . "/circ/circulation.pl?borrowernumber=" . $patron->borrowernumber );
110
111     my $audio_node = $driver->find_element('//span[@id="audio-alert"]/audio[@src="/intranet-tmpl/prog/sound/beep.ogg"]');
112
113     push @cleanup, $patron, $patron->category, $patron->library;
114 };
115
116 END {
117     C4::Context->preference('SearchEngine', $SearchEngine_value);
118     C4::Context->preference('AudioAlerts', $AudioAlerts_value);
119     $_->delete for @cleanup;
120 };