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 );
27 use Test::More tests => 23;
31 my $schema = Koha::Database->new()->schema();
32 $schema->storage->txn_begin();
34 my $dbh = C4::Context->dbh;
35 $dbh->{RaiseError} = 1;
37 my @branches = Koha::Branches->search();
38 my $borrower = Koha::Borrowers->search()->next();
40 my $biblio = MARC::Record->new();
41 my $title = 'Silence in the library';
42 $biblio->append_fields(
43 MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ),
44 MARC::Field->new( '245', ' ', ' ', a => $title ),
46 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
48 my $item = Koha::Item->new(
50 biblionumber => $biblionumber,
51 biblioitemnumber => $biblioitemnumber,
52 holdingbranch => $branches[0]->branchcode(),
53 homebranch => $branches[0]->branchcode(),
58 my $hold = Koha::Hold->new(
60 biblionumber => $biblionumber,
61 itemnumber => $item->id(),
63 waitingdate => '2000-01-01',
64 borrowernumber => $borrower->borrowernumber(),
65 branchcode => $branches[1]->branchcode(),
70 $item = $hold->item();
72 my $hold_borrower = $hold->borrower();
73 ok( $hold_borrower, 'Got hold borrower' );
74 is( $hold_borrower->borrowernumber(), $borrower->borrowernumber(), 'Hold borrower matches correct borrower' );
76 C4::Context->set_preference( 'ReservesMaxPickUpDelay', '' );
77 my $dt = $hold->waiting_expires_on();
78 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if ReservesMaxPickUpDelay is not set" );
80 is( $hold->is_waiting, 1, 'The hold is waiting' );
81 is( $hold->is_found, 1, 'The hold is found');
82 ok( !$hold->is_in_transit, 'The hold is not in transit' );
84 C4::Context->set_preference( 'ReservesMaxPickUpDelay', '5' );
85 $dt = $hold->waiting_expires_on();
86 is( $dt->ymd, "2000-01-06",
87 "Koha::Hold->waiting_expires_on returns DateTime of waitingdate + ReservesMaxPickUpDelay if set" );
90 $dt = $hold->waiting_expires_on();
91 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to 'T' )" );
92 isnt( $hold->is_waiting, 1, 'The hold is not waiting (T)' );
93 is( $hold->is_found, 1, 'The hold is found');
94 is( $hold->is_in_transit, 1, 'The hold is in transit' );
97 $dt = $hold->waiting_expires_on();
98 is( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to empty string )" );
99 isnt( $hold->is_waiting, 1, 'The hold is not waiting (W)' );
100 is( $hold->is_found, 0, 'The hold is not found' );
101 ok( !$hold->is_in_transit, 'The hold is not in transit' );
103 # Test method is_cancelable
105 ok( $hold->is_cancelable(), "Unfound hold is cancelable" );
107 ok( $hold->is_cancelable, "Waiting hold is cancelable" );
109 ok( !$hold->is_cancelable, "In transit hold is not cancelable" );
111 # Test method is_at_destination
113 ok( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
115 ok( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
117 ok( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the same as the item's holdingbranch is not at destination" );
118 $item->holdingbranch( $branches[1]->branchcode() );
119 ok( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
121 $schema->storage->txn_rollback();