Bug 15896 [QA Followup] - Supress warn when running unit tests
[koha.git] / t / db_dependent / Hold.t
blob098aced5765f8b4d201033a5681bf56cde6586c2
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use t::lib::Mocks;
21 use C4::Context;
22 use C4::Biblio qw( AddBiblio );
23 use Koha::Database;
24 use Koha::Libraries;
25 use Koha::Patrons;
26 use Koha::Holds;
27 use Koha::Item;
28 use Koha::DateUtils;
29 use t::lib::TestBuilder;
31 use Test::More tests => 33;
32 use Test::Warn;
34 use_ok('Koha::Hold');
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;
41 my @branches;
42 foreach( 1..2 ) {
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},
63 $item->store();
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},
72 suspend => 0,
75 $hold->store();
77 is( $hold->suspend, 0, "Hold is not suspended" );
78 $hold->suspend_hold();
79 is( $hold->suspend, 1, "Hold is suspended" );
80 $hold->resume();
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" );
87 $hold->resume();
88 is( $hold->suspend, 0, "Hold is not suspended" );
89 is( $hold->suspend_until, undef, "Hold no longer has suspend_until date" );
90 $hold->found('W');
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" );
114 $hold->found('T');
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' );
121 $hold->found(q{});
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
129 $hold->found(undef);
130 ok( $hold->is_cancelable(), "Unfound hold is cancelable" );
131 $hold->found('W');
132 ok( $hold->is_cancelable, "Waiting hold is cancelable" );
133 $hold->found('T');
134 ok( !$hold->is_cancelable, "In transit hold is not cancelable" );
136 # Test method is_at_destination
137 $hold->found(undef);
138 ok( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
139 $hold->found('T');
140 ok( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
141 $hold->found('W');
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 {
150 plan tests => 6;
152 $schema->storage->txn_begin();
154 # Disable logging
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' );
169 # Enable logging
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();