Bug 16330: Move patches to OpenAPI
[koha.git] / t / db_dependent / Hold.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
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 C4::Calendar;
26 use Koha::Patrons;
27 use Koha::Holds;
28 use Koha::Item;
29 use Koha::DateUtils;
30 use t::lib::TestBuilder;
31
32 use Test::More tests => 31;
33 use Test::Warn;
34
35 use_ok('Koha::Hold');
36
37 my $schema = Koha::Database->new()->schema();
38 $schema->storage->txn_begin();
39
40 # add two branches and a borrower
41 my $builder = t::lib::TestBuilder->new;
42 my @branches;
43 foreach( 1..2 ) {
44     push @branches, $builder->build({ source => 'Branch' });
45 }
46 my $borrower = $builder->build({ source => 'Borrower' });
47
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 ),
53 );
54 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
55
56 my $item = Koha::Item->new(
57     {
58         biblionumber     => $biblionumber,
59         biblioitemnumber => $biblioitemnumber,
60         holdingbranch    => $branches[0]->{branchcode},
61         homebranch       => $branches[0]->{branchcode},
62     }
63 );
64 $item->store();
65
66 my $hold = Koha::Hold->new(
67     {
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},
74         suspend        => 0,
75     }
76 );
77 $hold->store();
78
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");
84
85 is( $hold->suspend, 0, "Hold is not suspended" );
86 $hold->suspend_hold();
87 is( $hold->suspend, 1, "Hold is suspended" );
88 $hold->resume();
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" );
95 $hold->resume();
96 is( $hold->suspend, 0, "Hold is not suspended" );
97 is( $hold->suspend_until, undef, "Hold no longer has suspend_until date" );
98 $hold->found('W');
99 warning_like { $hold->suspend_hold }
100     qr/Unable to suspend waiting hold!/, 'Catch warn about failed suspend';
101 is( $hold->suspend, 0, "Waiting hold cannot be suspended" );
102
103 $item = $hold->item();
104
105 my $hold_borrower = $hold->borrower();
106 ok( $hold_borrower, 'Got hold borrower' );
107 is( $hold_borrower->borrowernumber(), $borrower->{borrowernumber}, 'Hold borrower matches correct borrower' );
108
109 is( $hold->is_waiting, 1, 'The hold is waiting' );
110 is( $hold->is_found, 1, 'The hold is found');
111 ok( !$hold->is_in_transit, 'The hold is not in transit' );
112
113 t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', '5' );
114 $hold->found('T');
115 isnt( $hold->is_waiting, 1, 'The hold is not waiting (T)' );
116 is( $hold->is_found, 1, 'The hold is found');
117 is( $hold->is_in_transit, 1, 'The hold is in transit' );
118
119 $hold->found(q{});
120 isnt( $hold->is_waiting, 1, 'The hold is not waiting (W)' );
121 is( $hold->is_found, 0, 'The hold is not found' );
122 ok( !$hold->is_in_transit, 'The hold is not in transit' );
123
124 # Test method is_cancelable
125 $hold->found(undef);
126 ok( $hold->is_cancelable(), "Unfound hold is cancelable" );
127 $hold->found('W');
128 ok( $hold->is_cancelable, "Waiting hold is cancelable" );
129 $hold->found('T');
130 ok( !$hold->is_cancelable, "In transit hold is not cancelable" );
131
132 # Test method is_at_destination
133 $hold->found(undef);
134 ok( !$hold->is_at_destination(), "Unfound hold cannot be at destination" );
135 $hold->found('T');
136 ok( !$hold->is_at_destination(), "In transit hold cannot be at destination" );
137 $hold->found('W');
138 ok( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the same as the item's holdingbranch is not at destination" );
139 $item->holdingbranch( $branches[1]->{branchcode} );
140 ok( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" );
141
142 $schema->storage->txn_rollback();
143
144 subtest "delete() tests" => sub {
145
146     plan tests => 6;
147
148     $schema->storage->txn_begin();
149
150     # Disable logging
151     t::lib::Mocks::mock_preference( 'HoldsLog', 0 );
152
153     my $hold = $builder->build({ source => 'Reserve' });
154
155     my $hold_object = Koha::Holds->find( $hold->{ reserve_id } );
156     my $deleted = $hold_object->delete;
157     is( $deleted, 1, 'Koha::Hold->delete should return 1 if the hold has been correctly deleted' );
158     is( Koha::Holds->search({ reserve_id => $hold->{ reserve_id } })->count, 0,
159         "Koha::Hold->delete should have deleted the hold" );
160
161     my $number_of_logs = $schema->resultset('ActionLog')->search(
162             { module => 'HOLDS', action => 'DELETE', object => $hold->{ reserve_id } } )->count;
163     is( $number_of_logs, 0, 'With HoldsLogs, Koha::Hold->delete shouldn\'t have been logged' );
164
165     # Enable logging
166     t::lib::Mocks::mock_preference( 'HoldsLog', 1 );
167
168     $hold = $builder->build({ source => 'Reserve' });
169
170     $hold_object = Koha::Holds->find( $hold->{ reserve_id } );
171     $deleted = $hold_object->delete;
172     is( $deleted, 1, 'Koha::Hold->delete should return 1 if the hold has been correctly deleted' );
173     is( Koha::Holds->search({ reserve_id => $hold->{ reserve_id } })->count, 0,
174         "Koha::Hold->delete should have deleted the hold" );
175
176     $number_of_logs = $schema->resultset('ActionLog')->search(
177             { module => 'HOLDS', action => 'DELETE', object => $hold->{ reserve_id } } )->count;
178     is( $number_of_logs, 1, 'With HoldsLogs, Koha::Hold->delete should have been logged' );
179
180     $schema->storage->txn_rollback();
181  };
182