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 );
30 use t
::lib
::TestBuilder
;
32 use Test
::More tests
=> 33;
37 my $schema = Koha
::Database
->new()->schema();
38 $schema->storage->txn_begin();
40 # add two branches and a borrower
41 my $builder = t
::lib
::TestBuilder
->new;
44 push @branches, $builder->build({ source
=> 'Branch' });
46 my $borrower = $builder->build({ source
=> 'Borrower' });
48 my $biblio = MARC
::Record
->new();
49 my $title = 'Silence in the library';
50 $biblio->append_fields(
51 MARC
::Field
->new( '100', ' ', ' ', a
=> 'Moffat, Steven' ),
52 MARC
::Field
->new( '245', ' ', ' ', a
=> $title ),
54 my ( $biblionumber, $biblioitemnumber ) = AddBiblio
( $biblio, '' );
56 my $item = Koha
::Item
->new(
58 biblionumber
=> $biblionumber,
59 biblioitemnumber
=> $biblioitemnumber,
60 holdingbranch
=> $branches[0]->{branchcode
},
61 homebranch
=> $branches[0]->{branchcode
},
66 my $hold = Koha
::Hold
->new(
68 biblionumber
=> $biblionumber,
69 itemnumber
=> $item->id(),
70 reservedate
=> '2017-01-01',
71 waitingdate
=> '2000-01-01',
72 borrowernumber
=> $borrower->{borrowernumber
},
73 branchcode
=> $branches[1]->{branchcode
},
79 my $b1_cal = C4
::Calendar
->new( branchcode
=> $branches[1]->{branchcode
} );
80 $b1_cal->insert_single_holiday( day
=> 02, month
=> 01, year
=> 2017, title
=> "Morty Day", description
=> "Rick" ); #Add a holiday
81 my $today = dt_from_string
;
82 is
( $hold->age(), $today->delta_days( dt_from_string
( '2017-01-01' ) )->in_units( 'days') , "Age of hold is days from reservedate to now if calendar ignored");
83 is
( $hold->age(1), $today->delta_days( dt_from_string
( '2017-01-01' ) )->in_units( 'days' ) - 1 , "Age of hold is days from reservedate to now minus 1 if calendar used");
85 is
( $hold->suspend, 0, "Hold is not suspended" );
86 $hold->suspend_hold();
87 is
( $hold->suspend, 1, "Hold is suspended" );
89 is
( $hold->suspend, 0, "Hold is not suspended" );
90 my $dt = dt_from_string
();
91 $hold->suspend_hold( $dt );
92 $dt->truncate( to
=> 'day' );
93 is
( $hold->suspend, 1, "Hold is suspended" );
94 is
( $hold->suspend_until, "$dt", "Hold is suspended with a date, truncation takes place automatically" );
96 is
( $hold->suspend, 1, "Hold is suspended" );
97 is
( $hold->suspend_until, undef, "Hold is suspended without a date" );
99 is
( $hold->suspend, 0, "Hold is not suspended" );
100 is
( $hold->suspend_until, undef, "Hold no longer has suspend_until date" );
102 warning_like
{ $hold->suspend_hold }
103 qr/Unable to suspend waiting hold!/, 'Catch warn about failed suspend';
104 is
( $hold->suspend, 0, "Waiting hold cannot be suspended" );
106 $item = $hold->item();
108 my $hold_borrower = $hold->borrower();
109 ok
( $hold_borrower, 'Got hold borrower' );
110 is
( $hold_borrower->borrowernumber(), $borrower->{borrowernumber
}, 'Hold borrower matches correct borrower' );
112 is
( $hold->is_waiting, 1, 'The hold is waiting' );
113 is
( $hold->is_found, 1, 'The hold is found');
114 ok
( !$hold->is_in_transit, 'The hold is not in transit' );
116 t
::lib
::Mocks
::mock_preference
( 'ReservesMaxPickUpDelay', '5' );
118 isnt
( $hold->is_waiting, 1, 'The hold is not waiting (T)' );
119 is
( $hold->is_found, 1, 'The hold is found');
120 is
( $hold->is_in_transit, 1, 'The hold is in transit' );
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_from_opac
129 is
( $hold->is_cancelable_from_opac, 1, "Unfound hold is cancelable" );
131 is
( $hold->is_cancelable_from_opac, 0, "Waiting hold is not cancelable" );
133 is
( $hold->is_cancelable_from_opac, 0, "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();
147 subtest
"delete() tests" => sub {
151 $schema->storage->txn_begin();
154 t
::lib
::Mocks
::mock_preference
( 'HoldsLog', 0 );
156 my $hold = $builder->build({ source
=> 'Reserve' });
158 my $hold_object = Koha
::Holds
->find( $hold->{ reserve_id
} );
159 my $deleted = $hold_object->delete;
160 is
( $deleted, 1, 'Koha::Hold->delete should return 1 if the hold has been correctly deleted' );
161 is
( Koha
::Holds
->search({ reserve_id
=> $hold->{ reserve_id
} })->count, 0,
162 "Koha::Hold->delete should have deleted the hold" );
164 my $number_of_logs = $schema->resultset('ActionLog')->search(
165 { module
=> 'HOLDS', action
=> 'DELETE', object
=> $hold->{ reserve_id
} } )->count;
166 is
( $number_of_logs, 0, 'With HoldsLogs, Koha::Hold->delete shouldn\'t have been logged' );
169 t
::lib
::Mocks
::mock_preference
( 'HoldsLog', 1 );
171 $hold = $builder->build({ source
=> 'Reserve' });
173 $hold_object = Koha
::Holds
->find( $hold->{ reserve_id
} );
174 $deleted = $hold_object->delete;
175 is
( $deleted, 1, 'Koha::Hold->delete should return 1 if the hold has been correctly deleted' );
176 is
( Koha
::Holds
->search({ reserve_id
=> $hold->{ reserve_id
} })->count, 0,
177 "Koha::Hold->delete should have deleted the hold" );
179 $number_of_logs = $schema->resultset('ActionLog')->search(
180 { module
=> 'HOLDS', action
=> 'DELETE', object
=> $hold->{ reserve_id
} } )->count;
181 is
( $number_of_logs, 1, 'With HoldsLogs, Koha::Hold->delete should have been logged' );
183 $schema->storage->txn_rollback();