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 C4::Biblio qw( AddBiblio );
29 use Test::More tests => 31;
33 my $schema = Koha::Database->new()->schema();
34 $schema->storage->txn_begin();
36 my $dbh = C4::Context->dbh;
37 $dbh->{RaiseError} = 1;
39 my @branches = Koha::Libraries->search();
40 my $borrower = Koha::Patrons->search()->next();
42 my $biblio = MARC::Record->new();
43 my $title = 'Silence in the library';
44 $biblio->append_fields(
45 MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
46 MARC::Field->new( '245', ' ', ' ', a => $title ),
48 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
50 my $item = Koha::Item->new(
52 biblionumber => $biblionumber,
53 biblioitemnumber => $biblioitemnumber,
54 holdingbranch => $branches[0]->branchcode(),
55 homebranch => $branches[0]->branchcode(),
60 my $hold = Koha::Hold->new(
62 biblionumber => $biblionumber,
63 itemnumber => $item->id(),
64 waitingdate => '2000-01-01',
65 borrowernumber => $borrower->borrowernumber(),
66 branchcode => $branches[1]->branchcode(),
72 is( $hold->suspend, 0, "Hold is not suspended" );
73 $hold->suspend_hold();
74 is( $hold->suspend, 1, "Hold is suspended" );
76 is( $hold->suspend, 0, "Hold is not suspended" );
77 my $dt = dt_from_string();
78 $hold->suspend_hold( $dt );
79 $dt->truncate( to => 'day' );
80 is( $hold->suspend, 1, "Hold is suspended" );
81 is( $hold->suspend_until, "$dt", "Hold is suspended with a date, truncation takes place automatically" );
83 is( $hold->suspend, 0, "Hold is not suspended" );
84 is( $hold->suspend_until, undef, "Hold no longer has suspend_until date" );
86 $hold->suspend_hold();
87 is( $hold->suspend, 0, "Waiting hold cannot be suspended" );
89 $item = $hold->item();
91 my $hold_borrower = $hold->borrower();
92 ok( $hold_borrower, 'Got hold borrower' );
93 is( $hold_borrower->borrowernumber(), $borrower->borrowernumber(), 'Hold borrower matches correct borrower' );
95 t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', '' );
96 $dt = $hold->waiting_expires_on();
97 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if ReservesMaxPickUpDelay is not set" );
99 is( $hold->is_waiting, 1, 'The hold is waiting' );
100 is( $hold->is_found, 1, 'The hold is found');
101 ok( !$hold->is_in_transit, 'The hold is not in transit' );
103 t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', '5' );
104 $dt = $hold->waiting_expires_on();
105 is( $dt->ymd, "2000-01-06",
106 "Koha::Hold->waiting_expires_on returns DateTime of waitingdate + ReservesMaxPickUpDelay if set" );
109 $dt = $hold->waiting_expires_on();
110 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to 'T' )" );
111 isnt( $hold->is_waiting, 1, 'The hold is not waiting (T)' );
112 is( $hold->is_found, 1, 'The hold is found');
113 is( $hold->is_in_transit, 1, 'The hold is in transit' );
116 $dt = $hold->waiting_expires_on();
117 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to empty string )" );
118 isnt( $hold->is_waiting, 1, 'The hold is not waiting (W)' );
119 is( $hold->is_found, 0, 'The hold is not found' );
120 ok( !$hold->is_in_transit, 'The hold is not in transit' );
122 # Test method is_cancelable
124 ok( $hold->is_cancelable(), "Unfound hold is cancelable" );
126 ok( $hold->is_cancelable, "Waiting hold is cancelable" );
128 ok( !$hold->is_cancelable, "In transit hold is not cancelable" );
130 # Test method is_at_destination
132 ok( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
134 ok( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
136 ok( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the same as the item's holdingbranch is not at destination" );
137 $item->holdingbranch( $branches[1]->branchcode() );
138 ok( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
140 $schema->storage->txn_rollback();