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>.
21 use C4::Biblio qw( AddBiblio );
28 use Test::More tests => 31;
32 my $schema = Koha::Database->new()->schema();
33 $schema->storage->txn_begin();
35 my $dbh = C4::Context->dbh;
36 $dbh->{RaiseError} = 1;
38 my @branches = Koha::Libraries->search();
39 my $borrower = Koha::Borrowers->search()->next();
41 my $biblio = MARC::Record->new();
42 my $title = 'Silence in the library';
43 $biblio->append_fields(
44 MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
45 MARC::Field->new( '245', ' ', ' ', a => $title ),
47 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
49 my $item = Koha::Item->new(
51 biblionumber => $biblionumber,
52 biblioitemnumber => $biblioitemnumber,
53 holdingbranch => $branches[0]->branchcode(),
54 homebranch => $branches[0]->branchcode(),
59 my $hold = Koha::Hold->new(
61 biblionumber => $biblionumber,
62 itemnumber => $item->id(),
63 waitingdate => '2000-01-01',
64 borrowernumber => $borrower->borrowernumber(),
65 branchcode => $branches[1]->branchcode(),
71 is( $hold->suspend, 0, "Hold is not suspended" );
72 $hold->suspend_hold();
73 is( $hold->suspend, 1, "Hold is suspended" );
75 is( $hold->suspend, 0, "Hold is not suspended" );
76 my $dt = dt_from_string();
77 $hold->suspend_hold( $dt );
78 is( $hold->suspend, 1, "Hold is suspended" );
79 is( $hold->suspend_until, "$dt", "Hold is suspended with a date" );
81 is( $hold->suspend, 0, "Hold is not suspended" );
82 is( $hold->suspend_until, undef, "Hold no longer has suspend_until date" );
84 $hold->suspend_hold();
85 is( $hold->suspend, 0, "Waiting hold cannot be suspended" );
87 $item = $hold->item();
89 my $hold_borrower = $hold->borrower();
90 ok( $hold_borrower, 'Got hold borrower' );
91 is( $hold_borrower->borrowernumber(), $borrower->borrowernumber(), 'Hold borrower matches correct borrower' );
93 C4::Context->set_preference( 'ReservesMaxPickUpDelay', '' );
94 $dt = $hold->waiting_expires_on();
95 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if ReservesMaxPickUpDelay is not set" );
97 is( $hold->is_waiting, 1, 'The hold is waiting' );
98 is( $hold->is_found, 1, 'The hold is found');
99 ok( !$hold->is_in_transit, 'The hold is not in transit' );
101 C4::Context->set_preference( 'ReservesMaxPickUpDelay', '5' );
102 $dt = $hold->waiting_expires_on();
103 is( $dt->ymd, "2000-01-06",
104 "Koha::Hold->waiting_expires_on returns DateTime of waitingdate + ReservesMaxPickUpDelay if set" );
107 $dt = $hold->waiting_expires_on();
108 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to 'T' )" );
109 isnt( $hold->is_waiting, 1, 'The hold is not waiting (T)' );
110 is( $hold->is_found, 1, 'The hold is found');
111 is( $hold->is_in_transit, 1, 'The hold is in transit' );
114 $dt = $hold->waiting_expires_on();
115 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to empty string )" );
116 isnt( $hold->is_waiting, 1, 'The hold is not waiting (W)' );
117 is( $hold->is_found, 0, 'The hold is not found' );
118 ok( !$hold->is_in_transit, 'The hold is not in transit' );
120 # Test method is_cancelable
122 ok( $hold->is_cancelable(), "Unfound hold is cancelable" );
124 ok( $hold->is_cancelable, "Waiting hold is cancelable" );
126 ok( !$hold->is_cancelable, "In transit hold is not cancelable" );
128 # Test method is_at_destination
130 ok( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
132 ok( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
134 ok( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the same as the item's holdingbranch is not at destination" );
135 $item->holdingbranch( $branches[1]->branchcode() );
136 ok( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
138 $schema->storage->txn_rollback();