Bug 19532: Make recalls.status an ENUM
[koha.git] / t / db_dependent / Koha / Recall.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 Test::More tests => 27;
21 use t::lib::TestBuilder;
22 use t::lib::Mocks;
23
24 use Koha::DateUtils qw( dt_from_string );
25
26 BEGIN {
27     require_ok('Koha::Recall');
28     require_ok('Koha::Recalls');
29 }
30
31 # Start transaction
32
33 my $database = Koha::Database->new();
34 my $schema = $database->schema();
35 $schema->storage->txn_begin();
36 my $dbh = C4::Context->dbh;
37
38 my $builder = t::lib::TestBuilder->new;
39
40 # Setup test variables
41
42 my $item1 = $builder->build_sample_item();
43 my $biblio1 = $item1->biblio;
44 my $branch1 = $item1->holdingbranch;
45 my $itemtype1 = $item1->effective_itemtype;
46
47 my $item2 = $builder->build_sample_item();
48 my $biblio2 = $item2->biblio;
49 my $branch2 = $item2->holdingbranch;
50 my $itemtype2 = $item2->effective_itemtype;
51
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;
57
58 Koha::CirculationRules->set_rule({
59     branchcode => undef,
60     categorycode => undef,
61     itemtype => undef,
62     rule_name => 'recalls_allowed',
63     rule_value => '10',
64 });
65
66 my $overdue_date = dt_from_string->subtract( days => 4 );
67 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode, $overdue_date );
68
69 my $recall1 = Koha::Recall->new({
70     borrowernumber => $patron1->borrowernumber,
71     recalldate => dt_from_string,
72     biblionumber => $biblio1->biblionumber,
73     branchcode => $branch1,
74     status => 'requested',
75     itemnumber => $item1->itemnumber,
76     expirationdate => undef,
77     item_level_recall => 1
78 })->store;
79
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 ok( $recall1->requested, "Recall has been requested" );
86
87 is( $recall1->should_be_overdue, 1, "Correctly calculated that recall should be marked overdue" );
88 $recall1->set_overdue({ interface => 'COMMANDLINE' });
89 ok( $recall1->overdue, "Recall is overdue" );
90
91 $recall1->set_cancelled;
92 ok( $recall1->cancelled, "Recall is cancelled" );
93
94 my $recall2 = Koha::Recall->new({
95     borrowernumber => $patron1->borrowernumber,
96     recalldate => dt_from_string,
97     biblionumber => $biblio1->biblionumber,
98     branchcode => $branch1,
99     itemnumber => $item1->itemnumber,
100     expirationdate => undef,
101     item_level_recall => 1
102 })->store;
103
104 Koha::CirculationRules->set_rule({
105     branchcode => undef,
106     categorycode => undef,
107     itemtype => undef,
108     rule_name => 'recall_shelf_time',
109     rule_value => undef,
110 });
111
112 t::lib::Mocks::mock_preference( 'RecallsMaxPickUpDelay', 7 );
113 my $expected_expirationdate = dt_from_string->add({ days => 7 });
114 my $expirationdate = $recall2->calc_expirationdate;
115 is( $expirationdate, $expected_expirationdate, "Expiration date calculated based on system preference as no circulation rules are set" );
116
117 Koha::CirculationRules->set_rule({
118     branchcode => undef,
119     categorycode => undef,
120     itemtype => undef,
121     rule_name => 'recall_shelf_time',
122     rule_value => '3',
123 });
124 $expected_expirationdate = dt_from_string->add({ days => 3 });
125 $expirationdate = $recall2->calc_expirationdate;
126 is( $expirationdate, $expected_expirationdate, "Expiration date calculated based on circulation rules" );
127
128 $recall2->set_waiting({ expirationdate => $expirationdate });
129 is( $recall2->waiting, 1, "Recall is waiting" );
130
131 my $notice = C4::Message->find_last_message( $patron1->unblessed, 'PICKUP_RECALLED_ITEM', 'email' );
132 ok( defined $notice, "Patron was notified to pick up waiting recall" );
133
134 $recall2->set_expired({ interface => 'COMMANDLINE' });
135 is( $recall2->expired, 1, "Recall has expired" );
136
137 my $old_recalls_count_now = Koha::Recalls->search({ old => 1 })->count;
138 is( $old_recalls_count_now, $old_recalls_count + 2, "Recalls have been flagged as old when cancelled or expired" );
139
140 my $recall3 = Koha::Recall->new({
141     borrowernumber => $patron1->borrowernumber,
142     recalldate => dt_from_string,
143     biblionumber => $biblio1->biblionumber,
144     branchcode => $branch1,
145     itemnumber => $item1->itemnumber,
146     expirationdate => undef,
147     item_level_recall => 1
148 })->store;
149
150 # test that recall gets T status
151 $recall3->start_transfer;
152 ok( $recall3->in_transit, "Recall is in transit" );
153
154 $recall3->revert_transfer;
155 ok( $recall3->requested, "Recall transfer has been cancelled and the status reverted" );
156 is( $recall3->itemnumber, $item1->itemnumber, "Item persists for item-level recall" );
157
158 # for testing purposes, pretend the item gets checked out
159 $recall3->set_fulfilled;
160 ok( $recall3->fulfilled, "Recall has been fulfilled" );
161
162 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
163 my $recall4 = Koha::Recall->new({
164     borrowernumber => $patron1->borrowernumber,
165     recalldate => dt_from_string,
166     biblionumber => $biblio1->biblionumber,
167     branchcode => $branch1,
168     itemnumber => undef,
169     expirationdate => undef,
170     item_level_recall => 0,
171 })->store;
172
173 ok( !defined $recall4->item, "No relevant item returned for a biblio-level recall" );
174 is( $recall4->checkout->itemnumber, $item1->itemnumber, "Return most relevant checkout for a biblio-level recall");
175
176 $recall4->set_waiting({ item => $item1, expirationdate => $expirationdate });
177 is( $recall4->itemnumber, $item1->itemnumber, "Item has been allocated to biblio-level recall" );
178
179 $recall4->revert_waiting;
180 ok( !defined $recall4->itemnumber, "Itemnumber has been removed from biblio-level recall when reverting waiting status" );
181
182 $recall4->start_transfer({ item => $item1 });
183 is( $recall4->itemnumber, $item1->itemnumber, "Itemnumber saved to recall when item is transferred" );
184 $recall4->revert_transfer;
185 ok( !defined $recall4->itemnumber, "Itemnumber has been removed from biblio-level recall when reverting transfer status" );
186
187 $schema->storage->txn_rollback();