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 t
::lib
::TestBuilder
;
31 use Test
::More tests
=> 33;
36 my $schema = Koha
::Database
->new()->schema();
37 $schema->storage->txn_begin();
39 # add two branches and a borrower
40 my $builder = t
::lib
::TestBuilder
->new;
43 push @branches, $builder->build({ source
=> 'Branch' });
45 my $borrower = $builder->build({ source
=> 'Borrower' });
47 my $biblio = MARC
::Record
->new();
48 my $title = 'Silence in the library';
49 $biblio->append_fields(
50 MARC
::Field
->new( '100', ' ', ' ', a
=> 'Moffat, Steven' ),
51 MARC
::Field
->new( '245', ' ', ' ', a
=> $title ),
53 my ( $biblionumber, $biblioitemnumber ) = AddBiblio
( $biblio, '' );
55 my $item = Koha
::Item
->new(
57 biblionumber
=> $biblionumber,
58 biblioitemnumber
=> $biblioitemnumber,
59 holdingbranch
=> $branches[0]->{branchcode
},
60 homebranch
=> $branches[0]->{branchcode
},
65 my $hold = Koha
::Hold
->new(
67 biblionumber
=> $biblionumber,
68 itemnumber
=> $item->id(),
69 waitingdate
=> '2000-01-01',
70 borrowernumber
=> $borrower->{borrowernumber
},
71 branchcode
=> $branches[1]->{branchcode
},
77 is
( $hold->suspend, 0, "Hold is not suspended" );
78 $hold->suspend_hold();
79 is
( $hold->suspend, 1, "Hold is suspended" );
81 is
( $hold->suspend, 0, "Hold is not suspended" );
82 my $dt = dt_from_string
();
83 $hold->suspend_hold( $dt );
84 $dt->truncate( to
=> 'day' );
85 is
( $hold->suspend, 1, "Hold is suspended" );
86 is
( $hold->suspend_until, "$dt", "Hold is suspended with a date, truncation takes place automatically" );
88 is
( $hold->suspend, 0, "Hold is not suspended" );
89 is
( $hold->suspend_until, undef, "Hold no longer has suspend_until date" );
91 warning_like
{ $hold->suspend_hold }
92 qr/Unable to suspend waiting hold!/, 'Catch warn about failed suspend';
93 is
( $hold->suspend, 0, "Waiting hold cannot be suspended" );
95 $item = $hold->item();
97 my $hold_borrower = $hold->borrower();
98 ok
( $hold_borrower, 'Got hold borrower' );
99 is
( $hold_borrower->borrowernumber(), $borrower->{borrowernumber
}, 'Hold borrower matches correct borrower' );
101 t
::lib
::Mocks
::mock_preference
( 'ReservesMaxPickUpDelay', '' );
102 $dt = $hold->waiting_expires_on();
103 is
( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if ReservesMaxPickUpDelay is not set" );
105 is
( $hold->is_waiting, 1, 'The hold is waiting' );
106 is
( $hold->is_found, 1, 'The hold is found');
107 ok
( !$hold->is_in_transit, 'The hold is not in transit' );
109 t
::lib
::Mocks
::mock_preference
( 'ReservesMaxPickUpDelay', '5' );
110 $dt = $hold->waiting_expires_on();
111 is
( $dt->ymd, "2000-01-06",
112 "Koha::Hold->waiting_expires_on returns DateTime of waitingdate + ReservesMaxPickUpDelay if set" );
115 $dt = $hold->waiting_expires_on();
116 is
( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to 'T' )" );
117 isnt
( $hold->is_waiting, 1, 'The hold is not waiting (T)' );
118 is
( $hold->is_found, 1, 'The hold is found');
119 is
( $hold->is_in_transit, 1, 'The hold is in transit' );
122 $dt = $hold->waiting_expires_on();
123 is
( $dt, undef, "Koha::Hold->waiting_expires_on returns undef if found is not 'W' ( Set to empty string )" );
124 isnt
( $hold->is_waiting, 1, 'The hold is not waiting (W)' );
125 is
( $hold->is_found, 0, 'The hold is not found' );
126 ok
( !$hold->is_in_transit, 'The hold is not in transit' );
128 # Test method is_cancelable
130 ok
( $hold->is_cancelable(), "Unfound hold is cancelable" );
132 ok
( $hold->is_cancelable, "Waiting hold is cancelable" );
134 ok
( !$hold->is_cancelable, "In transit hold is not cancelable" );
136 # Test method is_at_destination
138 ok
( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
140 ok
( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
142 ok
( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the same as the item's holdingbranch is not at destination" );
143 $item->holdingbranch( $branches[1]->{branchcode
} );
144 ok
( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
146 $schema->storage->txn_rollback();
148 subtest
"delete() tests" => sub {
152 $schema->storage->txn_begin();
155 t
::lib
::Mocks
::mock_preference
( 'HoldsLog', 0 );
157 my $hold = $builder->build({ source
=> 'Reserve' });
159 my $hold_object = Koha
::Holds
->find( $hold->{ reserve_id
} );
160 my $deleted = $hold_object->delete;
161 is
( $deleted, 1, 'Koha::Hold->delete should return 1 if the hold has been correctly deleted' );
162 is
( Koha
::Holds
->search({ reserve_id
=> $hold->{ reserve_id
} })->count, 0,
163 "Koha::Hold->delete should have deleted the hold" );
165 my $number_of_logs = $schema->resultset('ActionLog')->search(
166 { module
=> 'HOLDS', action
=> 'DELETE', object
=> $hold->{ reserve_id
} } )->count;
167 is
( $number_of_logs, 0, 'With HoldsLogs, Koha::Hold->delete shouldn\'t have been logged' );
170 t
::lib
::Mocks
::mock_preference
( 'HoldsLog', 1 );
172 $hold = $builder->build({ source
=> 'Reserve' });
174 $hold_object = Koha
::Holds
->find( $hold->{ reserve_id
} );
175 $deleted = $hold_object->delete;
176 is
( $deleted, 1, 'Koha::Hold->delete should return 1 if the hold has been correctly deleted' );
177 is
( Koha
::Holds
->search({ reserve_id
=> $hold->{ reserve_id
} })->count, 0,
178 "Koha::Hold->delete should have deleted the hold" );
180 $number_of_logs = $schema->resultset('ActionLog')->search(
181 { module
=> 'HOLDS', action
=> 'DELETE', object
=> $hold->{ reserve_id
} } )->count;
182 is
( $number_of_logs, 1, 'With HoldsLogs, Koha::Hold->delete should have been logged' );
184 $schema->storage->txn_rollback();