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>.
20 # wget https://selenium-release.storage.googleapis.com/2.53/selenium-server-standalone-2.53.1.jar # Does not work with 3.4, did not test the ones between
21 # sudo apt-get install xvfb firefox-esr
22 # SELENIUM_PATH=/home/vagrant/selenium-server-standalone-2.53.1.jar
23 # Xvfb :1 -screen 0 1024x768x24 2>&1 >/dev/null &
24 # DISPLAY=:1 java -jar $SELENIUM_PATH
26 # Then you can execute the test file.
29 # Wide character in print at /usr/local/share/perl/5.20.2/Test2/Formatter/TAP.pm line 105.
30 # # 'Koha › Patrons › Add patron test_patron_surname (Adult)'
31 # # doesn't match '(?^u:Patron details for test_patron_surname)'
33 # Ignore and retry (FIXME LATER...)
37 use Time
::HiRes
qw(gettimeofday);
39 use C4
::Biblio
qw( AddBiblio ); # We shouldn't use it
41 use Test
::More tests
=> 20;
45 my $dbh = C4
::Context
->dbh;
46 my $login = $ENV{KOHA_USER
} || 'koha';
47 my $password = $ENV{KOHA_PASS
} || 'koha';
49 = ( $ENV{KOHA_INTRANET_URL
} || 'http://' . C4
::Context
->preference("staffClientBaseURL") )
51 my $selenium_addr = $ENV{SELENIUM_ADDR
} || 'localhost';
52 my $selenium_port = $ENV{SELENIUM_PORT
} || 4444;
56 my $number_of_biblios_to_insert = 3;
59 categorycode
=> 'TEST_CAT',
60 description
=> 'test cat description',
61 enrolmentperiod
=> '12',
65 surname
=> 'test_patron_surname',
66 cardnumber
=> '4242424242',
67 userid
=> 'test_username',
68 password
=> 'password',
69 password2
=> 'password'
72 itemtype
=> 'IT4TEST',
73 description
=> 'Just an itemtype for tests',
78 categorycode
=> 'test_cat',
79 itemtype
=> 'IT4test',
87 our ( $borrowernumber, $start, $prev_time, $cleanup_needed );
90 eval { require Selenium
::Remote
::Driver
; };
91 skip
"Selenium::Remote::Driver is needed for selenium tests.", 20 if $@
;
95 open my $fh, '>>', '/tmp/output.txt';
97 my $driver = Selenium
::Remote
::Driver
->new(
98 port
=> $selenium_port,
99 remote_server_addr
=> $selenium_addr
102 $start = gettimeofday
;
104 $driver->get($base_url."mainpage.pl");
105 like
( $driver->get_title(), qr
(Log
in to Koha
), );
106 auth
( $driver, $login, $password );
109 $driver->get($base_url.'admin/categories.pl');
110 like
( $driver->get_title(), qr
(Patron categories
), );
111 $driver->find_element('//a[@id="newcategory"]')->click;
112 like
( $driver->get_title(), qr
(New category
), );
113 fill_form
( $driver, $sample_data->{category
} );
114 $driver->find_element('//fieldset[@class="action"]/input[@type="submit"]')->click;
116 time_diff
("add patron category");
117 $driver->get($base_url.'/members/memberentry.pl?op=add&categorycode='.$sample_data->{category
}{categorycode
});
118 like
( $driver->get_title(), qr
(Add
.*$sample_data->{category
}{description
}), );
119 fill_form
( $driver, $sample_data->{patron
} );
120 $driver->find_element('//button[@id="saverecord"]')->click;
121 like
( $driver->get_title(), qr
(Patron details
for $sample_data->{patron
}{surname
}), );
123 ####$driver->get($base_url.'/members/members-home.pl');
124 ####fill_form( $driver, { searchmember => $sample_data->{patron}{cardnumber} } );
125 ####$driver->find_element('//div[@id="header_search"]/div/form/input[@type="submit"]')->click;
126 ####like( $driver->get_title(), qr(Patron details for), );
128 time_diff
("add patron");
130 $borrowernumber = $dbh->selectcol_arrayref(q
|SELECT borrowernumber FROM borrowers WHERE userid
=?
|, {}, $sample_data->{patron
}{userid
} )->[0];
133 for my $i ( 1 .. $number_of_biblios_to_insert ) {
134 my $biblio = MARC
::Record
->new();
135 my $title = 'test biblio '.$i;
136 if ( C4
::Context
->preference('marcflavour') eq 'UNIMARC' ) {
137 $biblio->append_fields(
138 MARC
::Field
->new('200', ' ', ' ', a
=> 'test biblio '.$i),
139 MARC
::Field
->new('200', ' ', ' ', f
=> 'test author '.$i),
142 $biblio->append_fields(
143 MARC
::Field
->new('245', ' ', ' ', a
=> 'test biblio '.$i),
144 MARC
::Field
->new('100', ' ', ' ', a
=> 'test author '.$i),
147 my ($biblionumber, $biblioitemnumber) = AddBiblio
($biblio, '');
148 push @biblionumbers, $biblionumber;
151 time_diff
("add biblio");
153 my $itemtype = $sample_data->{itemtype
};
154 $dbh->do(q
|INSERT INTO itemtypes
(itemtype
, description
, rentalcharge
, notforloan
) VALUES
(?
, ?
, ?
, ?
)|, undef, $itemtype->{itemtype
}, $itemtype->{description
}, $itemtype->{rentalcharge
}, $itemtype->{notforloan
});
156 my $issuing_rules = $sample_data->{issuingrule
};
157 $dbh->do(q
|INSERT INTO issuingrules
(categorycode
, itemtype
, branchcode
, maxissueqty
, issuelength
, lengthunit
, renewalperiod
) VALUES
(?
, ?
, ?
, ?
, ?
, ?
, ?
)|, undef, $issuing_rules->{categorycode
}, $issuing_rules->{itemtype
}, $issuing_rules->{branchcode
}, $issuing_rules->{maxissueqty
}, $issuing_rules->{issuelength
}, $issuing_rules->{lengthunit
}, $issuing_rules->{renewalperiod
});
159 for my $biblionumber ( @biblionumbers ) {
160 $driver->get($base_url."/cataloguing/additem.pl?biblionumber=$biblionumber");
161 like
( $driver->get_title(), qr
(test biblio \d
+ by test author
), );
162 my $form = $driver->find_element('//form[@name="f"]');
163 my $inputs = $driver->find_child_elements($form, '//input[@type="text"]');
164 for my $input ( @
$inputs ) {
165 next if $input->is_hidden();
167 my $id = $input->get_attribute('id');
168 next unless $id =~ m
|^tag_952_subfield
|;
170 $input->send_keys('t_value_bib'.$biblionumber);
173 $driver->find_element('//input[@name="add_submit"]')->click;
174 like
( $driver->get_title(), qr
($biblionumber.*Items
) );
176 $dbh->do(q
|UPDATE items SET notforloan
=0 WHERE biblionumber
=?
|, {}, $biblionumber );
177 $dbh->do(q
|UPDATE biblioitems SET itemtype
=? WHERE biblionumber
=?
|, {}, $itemtype->{itemtype
}, $biblionumber);
178 $dbh->do(q
|UPDATE items SET itype
=? WHERE biblionumber
=?
|, {}, $itemtype->{itemtype
}, $biblionumber);
181 time_diff
("add items");
183 my $nb_of_checkouts = 0;
184 for my $biblionumber ( @biblionumbers ) {
185 $driver->get($base_url."/circ/circulation.pl?borrowernumber=".$borrowernumber);
186 $driver->find_element('//input[@id="barcode"]')->send_keys('t_value_bib'.$biblionumber);
187 $driver->find_element('//fieldset[@id="circ_circulation_issue"]/button[@type="submit"]')->click;
189 like
( $driver->get_title(), qr
(Checking out to
$sample_data->{patron
}{surname
}) );
190 is
( $driver->find_element('//a[@href="#checkouts"]')->get_attribute('text'), $nb_of_checkouts.' Checkout(s)', );
193 time_diff
("checkout");
195 for my $biblionumber ( @biblionumbers ) {
196 $driver->get($base_url."/circ/returns.pl");
197 $driver->find_element('//input[@id="barcode"]')->send_keys('t_value_bib'.$biblionumber);
198 $driver->find_element('//form[@id="checkin-form"]/div/fieldset/input[@type="submit"]')->click;
199 like
( $driver->get_title(), qr
(Check
in test biblio \d
+) );
202 time_diff
("checkin");
209 cleanup
() if $cleanup_needed;
213 my ( $driver, $login, $password) = @_;
214 fill_form
( $driver, { userid
=> $login, password
=> $password } );
215 my $login_button = $driver->find_element('//input[@id="submit"]');
216 $login_button->submit();
220 my ( $driver, $values ) = @_;
221 while ( my ( $id, $value ) = each %$values ) {
222 my $element = $driver->find_element('//*[@id="'.$id.'"]');
223 my $tag = $element->get_tag_name();
224 if ( $tag eq 'input' ) {
225 $driver->find_element('//input[@id="'.$id.'"]')->send_keys($value);
226 } elsif ( $tag eq 'select' ) {
227 $driver->find_element('//select[@id="'.$id.'"]/option[@value="'.$value.'"]')->click;
233 my $dbh = C4
::Context
->dbh;
234 $dbh->do(q
|DELETE FROM issues where borrowernumber
=?
|, {}, $borrowernumber);
235 $dbh->do(q
|DELETE FROM old_issues where borrowernumber
=?
|, {}, $borrowernumber);
236 for my $i ( 1 .. $number_of_biblios_to_insert ) {
237 $dbh->do(qq|DELETE items
, biblio FROM biblio INNER JOIN items ON biblio
.biblionumber
= items
.biblionumber WHERE biblio
.title
= "test biblio$i"|);
239 $dbh->do(q
|DELETE FROM borrowers WHERE userid
= ?
|, {}, $sample_data->{patron
}{userid
});
240 $dbh->do(q
|DELETE FROM categories WHERE categorycode
= ?
|, {}, $sample_data->{category
}{categorycode
});
241 for my $i ( 1 .. $number_of_biblios_to_insert ) {
242 $dbh->do(qq|DELETE FROM biblio WHERE title
= "test biblio $i"|);
244 $dbh->do(q
|DELETE FROM itemtypes WHERE itemtype
=?
|, undef, $sample_data->{itemtype
}{itemtype
});
245 $dbh->do(q
|DELETE FROM issuingrules WHERE categorycode
=? AND itemtype
=? AND branchcode
=?
|, undef, $sample_data->{issuingrule
}{categorycode
}, $sample_data->{issuingrule
}{itemtype
}, $sample_data->{issuingrule
}{branchcode
});
250 my $now = gettimeofday
;
251 warn "CP $lib = " . sprintf("%.2f", $now - $prev_time ) . "\n";