3 # This file is part of Koha.
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.
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.
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>.
22 use Test::More tests => 1;
29 use t::lib::TestBuilder;
31 eval { require Selenium::Remote::Driver; };
32 skip "Selenium::Remote::Driver is needed for selenium tests.", 1 if $@;
34 my $s = t::lib::Selenium->new;
35 my $driver = $s->driver;
36 my $opac_base_url = $s->opac_base_url;
37 my $base_url = $s->base_url;
38 my $builder = t::lib::TestBuilder->new;
41 subtest 'Update child to patron' => sub {
43 # We are going to test 3 scénarios:
44 # 1. There are no adults in the DB => no "Update child" link appear
45 # 2. There are at least 2 adults in the DB => a window popup is displayed, letting the librarian choosing the adult category they want
46 # 3.An adult will not be able to click the "Update child" link
51 my $patron_category_C = $builder->build_object(
52 { class => 'Koha::Patron::Categories', value => { category_type => 'C' } } );
54 my $child = $builder->build_object(
56 class => 'Koha::Patrons',
58 categorycode => $patron_category_C->categorycode,
62 my $child_borrowernumber = $child->borrowernumber;
64 subtest 'No adult categories' => sub {
67 # That's pretty ugly, but we need 0 adult in the DB to really test the whole behaviorĂ
68 Koha::Patron::Categories->search({ category_type => 'A' })->update({ category_type => 'Z' });
70 $driver->get( $base_url . "/members/moremember.pl?borrowernumber=" . $child_borrowernumber );
71 # Find the "More" button group, it's the last one
72 # Do not use "More" to select the button, to make it works even when translated
73 $driver->find_element('//div[@id="toolbar"]/div[@class="btn-group"][last()]')->click;
75 $s->remove_error_handler;
76 # Why ->id is needed to make it fail?
77 # We should expect ->find_element to return 0, but it returns a WebElement (??)
78 my $update_link_id = eval { $driver->find_element('//a[@id="updatechild"]')->id; };
79 $s->add_error_handler;
80 is ( $update_link_id, undef, 'No update link should be displayed' );
82 # Resetting the patrons to adult
83 Koha::Patron::Categories->search({ category_type => 'Z' })->update({ category_type => 'A' });
86 my $patron_category_A = $builder->build_object(
87 { class => 'Koha::Patron::Categories', value => { category_type => 'A' } } );
88 my $adult_1 = $builder->build_object(
90 class => 'Koha::Patrons',
92 categorycode => $patron_category_A->categorycode,
96 my $adult_2 = $builder->build_object( # We want at least 2 adults to display the popup window
98 class => 'Koha::Patrons',
100 categorycode => $patron_category_A->categorycode,
104 my $adult_borrowernumber = $adult_1->borrowernumber;
106 subtest 'Update child to adult' => sub {
108 $driver->get( $base_url . "/members/moremember.pl?borrowernumber=" . $child_borrowernumber );
109 $driver->find_element('//div[@id="toolbar"]/div[@class="btn-group"][last()]')->click; # More button group
110 my $update_link = $driver->find_element('//a[@id="updatechild"]');
112 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');
114 like( $driver->get_current_url, qr{/members/moremember\.pl\?borrowernumber=$child_borrowernumber\#$}, 'Current window has a "#" in the URL, event has been triggered');
116 # Switch to the popup window
117 # Note that if there is only 1 adult in the DB the popup does not appears, but an alert instead. Not tested so far.
118 my $handles = $driver->get_window_handles;
119 $driver->switch_to_window($handles->[1]);
120 $driver->find_element('//input[@id="catcode'.$patron_category_A->categorycode.'"]')->click;
123 is( $child->get_from_storage->categorycode, $patron_category_A->categorycode, 'The child should now be an adult!' );
125 # Switching back to the main window
126 $driver->switch_to_window($handles->[0]);
129 subtest 'Cannot update an adult' => sub {
132 # Go to the adult detail view
133 $driver->get( $base_url . "/members/moremember.pl?borrowernumber=$adult_borrowernumber" );
134 $driver->find_element('//div[@id="toolbar"]/div[@class="btn-group"][last()]')->click; # More button group
136 my $update_link = $driver->find_element('//a[@id="updatechild"]');
137 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|);
139 like( $driver->get_current_url, qr{/members/moremember\.pl\?borrowernumber=$adult_borrowernumber$}, 'After clicking the link, nothing happens, no # in the URL');
142 my @patrons = ( $adult_1, $adult_2, $child );
143 push @cleanup, $_, $_->library, for @patrons;
144 push @cleanup, $patron_category_A, $patron_category_C;
151 # Resetting the patrons to adult, in case it has not been done earlier (if failures happened)
152 Koha::Patron::Categories->search({ category_type => 'Z' })->update({ category_type => 'A' });
154 $_->delete for @cleanup;