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 );
28 use t::lib::TestBuilder;
30 use Test::More tests => 32;
35 my $schema = Koha::Database->new()->schema();
36 $schema->storage->txn_begin();
38 # add two branches and a borrower
39 my $builder = t::lib::TestBuilder->new;
42 push @branches, $builder->build({ source => 'Branch' });
44 my $borrower = $builder->build({ source => 'Borrower' });
46 my $biblio = MARC::Record->new();
47 my $title = 'Silence in the library';
48 $biblio->append_fields(
49 MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
50 MARC::Field->new( '245', ' ', ' ', a => $title ),
52 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
54 my $item = Koha::Item->new(
56 biblionumber => $biblionumber,
57 biblioitemnumber => $biblioitemnumber,
58 holdingbranch => $branches[0]->{branchcode},
59 homebranch => $branches[0]->{branchcode},
64 my $hold = Koha::Hold->new(
66 biblionumber => $biblionumber,
67 itemnumber => $item->id(),
68 waitingdate => '2000-01-01',
69 borrowernumber => $borrower->{borrowernumber},
70 branchcode => $branches[1]->{branchcode},
76 is( $hold->suspend, 0, "Hold is not suspended" );
77 $hold->suspend_hold();
78 is( $hold->suspend, 1, "Hold is suspended" );
80 is( $hold->suspend, 0, "Hold is not suspended" );
81 my $dt = dt_from_string();
82 $hold->suspend_hold( $dt );
83 $dt->truncate( to => 'day' );
84 is( $hold->suspend, 1, "Hold is suspended" );
85 is( $hold->suspend_until, "$dt", "Hold is suspended with a date, truncation takes place automatically" );
87 is( $hold->suspend, 0, "Hold is not suspended" );
88 is( $hold->suspend_until, undef, "Hold no longer has suspend_until date" );
90 warning_like { $hold->suspend_hold }
91 qr/Unable to suspend waiting hold!/, 'Catch warn about failed suspend';
92 is( $hold->suspend, 0, "Waiting hold cannot be suspended" );
94 $item = $hold->item();
96 my $hold_borrower = $hold->borrower();
97 ok( $hold_borrower, 'Got hold borrower' );
98 is( $hold_borrower->borrowernumber(), $borrower->{borrowernumber}, 'Hold borrower matches correct borrower' );
100 t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', '' );
101 $dt = $hold->waiting_expires_on();
102 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if ReservesMaxPickUpDelay is not set" );
104 is( $hold->is_waiting, 1, 'The hold is waiting' );
105 is( $hold->is_found, 1, 'The hold is found');
106 ok( !$hold->is_in_transit, 'The hold is not in transit' );
108 t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', '5' );
109 $dt = $hold->waiting_expires_on();
110 is( $dt->ymd, "2000-01-06",
111 "Koha::Hold->waiting_expires_on returns DateTime of waitingdate + ReservesMaxPickUpDelay if set" );
114 $dt = $hold->waiting_expires_on();
115 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to 'T' )" );
116 isnt( $hold->is_waiting, 1, 'The hold is not waiting (T)' );
117 is( $hold->is_found, 1, 'The hold is found');
118 is( $hold->is_in_transit, 1, 'The hold is in transit' );
121 $dt = $hold->waiting_expires_on();
122 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to empty string )" );
123 isnt( $hold->is_waiting, 1, 'The hold is not waiting (W)' );
124 is( $hold->is_found, 0, 'The hold is not found' );
125 ok( !$hold->is_in_transit, 'The hold is not in transit' );
127 # Test method is_cancelable
129 ok( $hold->is_cancelable(), "Unfound hold is cancelable" );
131 ok( $hold->is_cancelable, "Waiting hold is cancelable" );
133 ok( !$hold->is_cancelable, "In transit hold is not cancelable" );
135 # Test method is_at_destination
137 ok( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
139 ok( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
141 ok( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the same as the item's holdingbranch is not at destination" );
142 $item->holdingbranch( $branches[1]->{branchcode} );
143 ok( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
145 $schema->storage->txn_rollback();