Bug 29779: Make selenium tests return green even if lib is missing
[koha-ffzg.git] / t / db_dependent / selenium / update_child_to_adult.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;
23 use Test::MockModule;
24
25 use C4::Context;
26 use Koha::AuthUtils;
27 use t::lib::Mocks;
28 use t::lib::Selenium;
29 use t::lib::TestBuilder;
30
31 eval { require Selenium::Remote::Driver; };
32 if ( $@ ) {
33     plan skip_all => "Selenium::Remote::Driver is needed for selenium tests.";
34 } else {
35     plan tests => 1;
36 }
37
38 my $s             = t::lib::Selenium->new;
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 our @cleanup;
45 subtest 'Update child to patron' => sub {
46     plan tests => 3;
47     # We are going to test 3 scĂ©narios:
48     # 1. There are no adults in the DB => no "Update child" link appear
49     # 2. There are at least 2 adults in the DB => a window popup is displayed, letting the librarian choosing the adult category they want
50     # 3.An adult will not be able to click the "Update child" link
51
52     $s->auth;
53
54     # Creating the child
55     my $patron_category_C = $builder->build_object(
56         { class => 'Koha::Patron::Categories', value => { category_type => 'C' } } );
57
58     my $child = $builder->build_object(
59         {
60             class => 'Koha::Patrons',
61             value => {
62                 categorycode => $patron_category_C->categorycode,
63             }
64         }
65     );
66     my $child_borrowernumber = $child->borrowernumber;
67
68     subtest 'No adult categories' => sub {
69         plan tests => 1;
70
71         # That's pretty ugly, but we need 0 adult in the DB to really test the whole behaviorĂ 
72         Koha::Patron::Categories->search({ category_type => 'A' })->update({ category_type => 'Z' });
73
74         $driver->get( $base_url . "/members/moremember.pl?borrowernumber=" . $child_borrowernumber );
75         # Find the "More" button group, it's the last one
76         # Do not use "More" to select the button, to make it works even when translated
77         $driver->find_element('//div[@id="toolbar"]/div[@class="btn-group"][last()]')->click;
78
79         $s->remove_error_handler;
80         # Why ->id is needed to make it fail?
81         # We should expect ->find_element to return 0, but it returns a WebElement (??)
82         my $update_link_id = eval { $driver->find_element('//a[@id="updatechild"]')->id; };
83         $s->add_error_handler;
84         is ( $update_link_id, undef, 'No update link should be displayed' );
85
86         # Resetting the patrons to adult
87         Koha::Patron::Categories->search({ category_type => 'Z' })->update({ category_type => 'A' });
88     };
89
90     my $patron_category_A = $builder->build_object(
91         { class => 'Koha::Patron::Categories', value => { category_type => 'A' } } );
92     my $adult_1 = $builder->build_object(
93         {
94             class => 'Koha::Patrons',
95             value => {
96                 categorycode => $patron_category_A->categorycode,
97             }
98         }
99     );
100     my $adult_2 = $builder->build_object( # We want at least 2 adults to display the popup window
101         {
102             class => 'Koha::Patrons',
103             value => {
104                 categorycode => $patron_category_A->categorycode,
105             }
106         }
107     );
108     my $adult_borrowernumber = $adult_1->borrowernumber;
109
110     subtest 'Update child to adult' => sub {
111         plan tests => 3;
112         $driver->get( $base_url . "/members/moremember.pl?borrowernumber=" . $child_borrowernumber );
113         $driver->find_element('//div[@id="toolbar"]/div[@class="btn-group"][last()]')->click; # More button group
114         my $update_link = $driver->find_element('//a[@id="updatechild"]');
115
116         is($update_link->get_attribute('data-toggle'), undef, 'The update link should not have a data-toggle attribute => not a tooltip and can be clickable');
117         $update_link->click;
118         like( $driver->get_current_url, qr{/members/moremember\.pl\?borrowernumber=$child_borrowernumber\#$}, 'Current window has a "#" in the URL, event has been triggered');
119
120         # Switch to the popup window
121         # Note that if there is only 1 adult in the DB the popup does not appears, but an alert instead. Not tested so far.
122         my $handles = $driver->get_window_handles;
123         $driver->switch_to_window($handles->[1]);
124         $driver->find_element('//input[@id="catcode'.$patron_category_A->categorycode.'"]')->click;
125         $s->submit_form;
126
127         is( $child->get_from_storage->categorycode, $patron_category_A->categorycode, 'The child should now be an adult!' );
128
129         # Switching back to the main window
130         $driver->switch_to_window($handles->[0]);
131     };
132
133     subtest 'Cannot update an adult' => sub {
134         plan tests => 2;
135
136         # Go to the adult detail view
137         $driver->get( $base_url . "/members/moremember.pl?borrowernumber=$adult_borrowernumber" );
138         $driver->find_element('//div[@id="toolbar"]/div[@class="btn-group"][last()]')->click; # More button group
139
140         my $update_link = $driver->find_element('//a[@id="updatechild"]');
141         is($update_link->get_attribute('data-toggle', 1), 'tooltip', q|The update link should have a data-toggle attribute => it's a tooltip, not clickable|);
142         $update_link->click;
143         like( $driver->get_current_url, qr{/members/moremember\.pl\?borrowernumber=$adult_borrowernumber$}, 'After clicking the link, nothing happens, no # in the URL');
144     };
145
146     my @patrons = ( $adult_1, $adult_2, $child );
147     push @cleanup, $_, $_->library, for @patrons;
148     push @cleanup, $patron_category_A, $patron_category_C;
149
150
151     $driver->quit();
152 };
153
154 END {
155     # Resetting the patrons to adult, in case it has not been done earlier (if failures happened)
156     Koha::Patron::Categories->search({ category_type => 'Z' })->update({ category_type => 'A' });
157
158     $_->delete for @cleanup;
159 }