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>.
20 use Test::More tests => 20;
21 use t::lib::TestBuilder;
24 use Koha::DateUtils qw( dt_from_string );
27 require_ok('Koha::Recall');
28 require_ok('Koha::Recalls');
33 my $database = Koha::Database->new();
34 my $schema = $database->schema();
35 $schema->storage->txn_begin();
36 my $dbh = C4::Context->dbh;
38 my $builder = t::lib::TestBuilder->new;
40 # Setup test variables
42 my $item1 = $builder->build_sample_item();
43 my $biblio1 = $item1->biblio;
44 my $branch1 = $item1->holdingbranch;
45 my $itemtype1 = $item1->effective_itemtype;
46 my $item2 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber });
47 my $biblio2 = $item1->biblio;
48 my $branch2 = $item1->holdingbranch;
49 my $itemtype2 = $item1->effective_itemtype;
51 my $category1 = $builder->build({ source => 'Category' })->{ categorycode };
52 my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $category1, branchcode => $branch1 } });
53 my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $category1, branchcode => $branch2 } });
54 my $patron3 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $category1, branchcode => $branch1 } });
55 t::lib::Mocks::mock_userenv({ patron => $patron1 });
57 Koha::CirculationRules->set_rules({
59 categorycode => undef,
62 'recall_due_date_interval' => undef,
63 'recalls_allowed' => 10,
67 C4::Circulation::AddIssue( $patron3->unblessed, $item1->barcode );
68 C4::Circulation::AddIssue( $patron3->unblessed, $item2->barcode );
70 my ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
73 branchcode => $branch1,
75 expirationdate => undef,
76 interface => 'COMMANDLINE',
78 ok( !defined $recall, "Can't add a recall without specifying a patron" );
80 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
83 branchcode => $branch1,
85 expirationdate => undef,
86 interface => 'COMMANDLINE',
88 ok( !defined $recall, "Can't add a recall without specifying a biblio" );
90 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
93 branchcode => $branch1,
95 expirationdate => undef,
96 interface => 'COMMANDLINE',
98 ok( !defined $recall, "Can't add a recall without specifying a biblio" );
100 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
105 expirationdate => undef,
106 interface => 'COMMANDLINE',
108 is( $recall->branchcode, $branch2, "No pickup branch specified so patron branch used" );
109 is( $due_interval, 5, "Recall due date interval defaults to 5 if not specified" );
111 Koha::CirculationRules->set_rule({
113 categorycode => undef,
115 rule_name => 'recall_due_date_interval',
118 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
123 expirationdate => undef,
124 interface => 'COMMANDLINE',
126 is( $due_interval, 3, "Recall due date interval is based on circulation rules" );
128 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
131 branchcode => $branch1,
133 expirationdate => undef,
134 interface => 'COMMANDLINE',
136 is( $recall->item_level_recall, 0, "No item provided so recall not flagged as item-level" );
138 my $expected_due_date = dt_from_string->add( days => 3 );
139 is( dt_from_string( $recall->checkout->date_due ), $expected_due_date, "Checkout due date has correctly been extended by recall_due_date_interval days" );
140 is( $due_date, $expected_due_date, "Due date correctly returned" );
142 my $messages_count = Koha::Notice::Messages->search({ borrowernumber => $patron3->borrowernumber, letter_code => 'RETURN_RECALLED_ITEM' })->count;
143 is( $messages_count, 3, "RETURN_RECALLED_ITEM notice successfully sent to checkout borrower" );
145 my $message = Koha::Recalls->move_recall;
146 is( $message, 'no recall_id provided', "Can't move a recall without specifying which recall" );
148 $message = Koha::Recalls->move_recall({ recall_id => $recall->recall_id });
149 is( $message, 'no action provided', "No clear action to perform on recall" );
150 $message = Koha::Recalls->move_recall({ recall_id => $recall->recall_id, action => 'whatever' });
151 is( $message, 'no action provided', "Legal action not provided to perform on recall" );
153 $recall->set_waiting({ item => $item1 });
154 ok( $recall->waiting, "Recall is waiting" );
155 Koha::Recalls->move_recall({ recall_id => $recall->recall_id, action => 'revert' });
156 $recall = Koha::Recalls->find( $recall->recall_id );
157 ok( $recall->requested, "Recall reverted to requested with move_recall" );
159 Koha::Recalls->move_recall({ recall_id => $recall->recall_id, action => 'cancel' });
160 $recall = Koha::Recalls->find( $recall->recall_id );
161 ok( $recall->cancelled, "Recall cancelled with move_recall" );
163 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
166 branchcode => $branch1,
168 expirationdate => undef,
169 interface => 'COMMANDLINE',
171 $message = Koha::Recalls->move_recall({ recall_id => $recall->recall_id, item => $item2, borrowernumber => $patron1->borrowernumber });
172 $recall = Koha::Recalls->find( $recall->recall_id );
173 ok( $recall->fulfilled, "Recall fulfilled with move_recall" );
175 $schema->storage->txn_rollback();
177 subtest 'filter_by_current() and filter_by_finished() tests' => sub {
181 $schema->storage->txn_begin;
183 my $in_transit = $builder->build_object( { class => 'Koha::Recalls', value => { status => 'in_transit' } } );
184 my $overdue = $builder->build_object( { class => 'Koha::Recalls', value => { status => 'overdue' } } );
185 my $requested = $builder->build_object( { class => 'Koha::Recalls', value => { status => 'requested' } } );
186 my $waiting = $builder->build_object( { class => 'Koha::Recalls', value => { status => 'waiting' } } );
187 my $cancelled = $builder->build_object( { class => 'Koha::Recalls', value => { status => 'cancelled' } } );
188 my $expired = $builder->build_object( { class => 'Koha::Recalls', value => { status => 'expired' } } );
189 my $fulfilled = $builder->build_object( { class => 'Koha::Recalls', value => { status => 'fulfilled' } } );
191 my $recalls = Koha::Recalls->search(
203 { order_by => [ 'recall_id' ] }
206 is( $recalls->count, 7, 'Resultset count is correct' );
208 my $current_recalls = $recalls->filter_by_current;
209 is( $current_recalls->count, 4, 'Current recalls count correct' );
211 is( $current_recalls->next->status, 'in_transit', 'Resultset correctly includes in_transit recall');
212 is( $current_recalls->next->status, 'overdue', 'Resultset correctly includes overdue recall');
213 is( $current_recalls->next->status, 'requested', 'Resultset correctly includes requested recall');
214 is( $current_recalls->next->status, 'waiting', 'Resultset correctly includes waiting recall');
216 my $finished_recalls = $recalls->filter_by_finished;
217 is( $finished_recalls->count, 3, 'Finished recalls count correct' );
219 is( $finished_recalls->next->status, 'cancelled', 'Resultset correctly includes cancelled recall');
220 is( $finished_recalls->next->status, 'expired', 'Resultset correctly includes expired recall');
221 is( $finished_recalls->next->status, 'fulfilled', 'Resultset correctly includes fulfilled recall');
223 $schema->storage->txn_rollback;