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 => 27;
21 use t::lib::TestBuilder;
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;
47 my $item2 = $builder->build_sample_item();
48 my $biblio2 = $item2->biblio;
49 my $branch2 = $item2->holdingbranch;
50 my $itemtype2 = $item2->effective_itemtype;
52 my $category1 = $builder->build({ source => 'Category' })->{ categorycode };
53 my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $category1, branchcode => $branch1 } });
54 my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $category1, branchcode => $branch1 } });
55 t::lib::Mocks::mock_userenv({ patron => $patron1 });
56 my $old_recalls_count = Koha::Recalls->search({ old => 1 })->count;
58 Koha::CirculationRules->set_rule({
60 categorycode => undef,
62 rule_name => 'recalls_allowed',
66 my $overdue_date = dt_from_string->subtract( days => 4 );
67 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode, $overdue_date );
69 my $recall1 = Koha::Recall->new({
70 borrowernumber => $patron1->borrowernumber,
71 recalldate => dt_from_string,
72 biblionumber => $biblio1->biblionumber,
73 branchcode => $branch1,
75 itemnumber => $item1->itemnumber,
76 expirationdate => undef,
77 item_level_recall => 1
80 is( $recall1->biblio->title, $biblio1->title, "Recall biblio relationship correctly linked" );
81 is( $recall1->item->homebranch, $item1->homebranch, "Recall item relationship correctly linked" );
82 is( $recall1->patron->categorycode, $category1, "Recall patron relationship correctly linked" );
83 is( $recall1->library->branchname, Koha::Libraries->find( $branch1 )->branchname, "Recall library relationship correctly linked" );
84 is( $recall1->checkout->itemnumber, $item1->itemnumber, "Recall checkout relationship correctly linked" );
85 is( $recall1->requested, 1, "Recall has been requested" );
87 is( $recall1->should_be_overdue, 1, "Correctly calculated that recall should be marked overdue" );
88 $recall1->set_overdue({ interface => 'COMMANDLINE' });
89 is( $recall1->overdue, 1, "Recall is overdue" );
91 $recall1->set_cancelled;
92 is( $recall1->cancelled, 1, "Recall is cancelled" );
94 my $recall2 = Koha::Recall->new({
95 borrowernumber => $patron1->borrowernumber,
96 recalldate => dt_from_string,
97 biblionumber => $biblio1->biblionumber,
98 branchcode => $branch1,
100 itemnumber => $item1->itemnumber,
101 expirationdate => undef,
102 item_level_recall => 1
105 Koha::CirculationRules->set_rule({
107 categorycode => undef,
109 rule_name => 'recall_shelf_time',
113 t::lib::Mocks::mock_preference( 'RecallsMaxPickUpDelay', 7 );
114 my $expected_expirationdate = dt_from_string->add({ days => 7 });
115 my $expirationdate = $recall2->calc_expirationdate;
116 is( $expirationdate, $expected_expirationdate, "Expiration date calculated based on system preference as no circulation rules are set" );
118 Koha::CirculationRules->set_rule({
120 categorycode => undef,
122 rule_name => 'recall_shelf_time',
125 $expected_expirationdate = dt_from_string->add({ days => 3 });
126 $expirationdate = $recall2->calc_expirationdate;
127 is( $expirationdate, $expected_expirationdate, "Expiration date calculated based on circulation rules" );
129 $recall2->set_waiting({ expirationdate => $expirationdate });
130 is( $recall2->waiting, 1, "Recall is waiting" );
132 my $notice = C4::Message->find_last_message( $patron1->unblessed, 'PICKUP_RECALLED_ITEM', 'email' );
133 ok( defined $notice, "Patron was notified to pick up waiting recall" );
135 $recall2->set_expired({ interface => 'COMMANDLINE' });
136 is( $recall2->expired, 1, "Recall has expired" );
138 my $old_recalls_count_now = Koha::Recalls->search({ old => 1 })->count;
139 is( $old_recalls_count_now, $old_recalls_count + 2, "Recalls have been flagged as old when cancelled or expired" );
141 my $recall3 = Koha::Recall->new({
142 borrowernumber => $patron1->borrowernumber,
143 recalldate => dt_from_string,
144 biblionumber => $biblio1->biblionumber,
145 branchcode => $branch1,
147 itemnumber => $item1->itemnumber,
148 expirationdate => undef,
149 item_level_recall => 1
152 # test that recall gets T status
153 $recall3->start_transfer;
154 is( $recall3->in_transit, 1, "Recall is in transit" );
156 $recall3->revert_transfer;
157 is( $recall3->requested, 1, "Recall transfer has been cancelled and the status reverted" );
158 is( $recall3->itemnumber, $item1->itemnumber, "Item persists for item-level recall" );
160 # for testing purposes, pretend the item gets checked out
161 $recall3->set_finished;
162 is( $recall3->finished, 1, "Recall has been fulfilled" );
164 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
165 my $recall4 = Koha::Recall->new({
166 borrowernumber => $patron1->borrowernumber,
167 recalldate => dt_from_string,
168 biblionumber => $biblio1->biblionumber,
169 branchcode => $branch1,
172 expirationdate => undef,
173 item_level_recall => 0,
176 ok( !defined $recall4->item, "No relevant item returned for a biblio-level recall" );
177 is( $recall4->checkout->itemnumber, $item1->itemnumber, "Return most relevant checkout for a biblio-level recall");
179 $recall4->set_waiting({ item => $item1, expirationdate => $expirationdate });
180 is( $recall4->itemnumber, $item1->itemnumber, "Item has been allocated to biblio-level recall" );
182 $recall4->revert_waiting;
183 ok( !defined $recall4->itemnumber, "Itemnumber has been removed from biblio-level recall when reverting waiting status" );
185 $recall4->start_transfer({ item => $item1 });
186 is( $recall4->itemnumber, $item1->itemnumber, "Itemnumber saved to recall when item is transferred" );
187 $recall4->revert_transfer;
188 ok( !defined $recall4->itemnumber, "Itemnumber has been removed from biblio-level recall when reverting transfer status" );
190 $schema->storage->txn_rollback();