Bug 19532: (follow-up) aria-hidden attr on OPAC, and more
[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;
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 => 'R',
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 is( $recall1->requested, 1, "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 is( $recall1->overdue, 1, "Recall is overdue" );
90
91 $recall1->set_cancelled;
92 is( $recall1->cancelled, 1, "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     status => 'R',
100     itemnumber => $item1->itemnumber,
101     expirationdate => undef,
102     item_level_recall => 1
103 })->store;
104
105 Koha::CirculationRules->set_rule({
106     branchcode => undef,
107     categorycode => undef,
108     itemtype => undef,
109     rule_name => 'recall_shelf_time',
110     rule_value => undef,
111 });
112
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" );
117
118 Koha::CirculationRules->set_rule({
119     branchcode => undef,
120     categorycode => undef,
121     itemtype => undef,
122     rule_name => 'recall_shelf_time',
123     rule_value => '3',
124 });
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" );
128
129 $recall2->set_waiting({ expirationdate => $expirationdate });
130 is( $recall2->waiting, 1, "Recall is waiting" );
131
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" );
134
135 $recall2->set_expired({ interface => 'COMMANDLINE' });
136 is( $recall2->expired, 1, "Recall has expired" );
137
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" );
140
141 my $recall3 = Koha::Recall->new({
142     borrowernumber => $patron1->borrowernumber,
143     recalldate => dt_from_string,
144     biblionumber => $biblio1->biblionumber,
145     branchcode => $branch1,
146     status => 'R',
147     itemnumber => $item1->itemnumber,
148     expirationdate => undef,
149     item_level_recall => 1
150 })->store;
151
152 # test that recall gets T status
153 $recall3->start_transfer;
154 is( $recall3->in_transit, 1, "Recall is in transit" );
155
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" );
159
160 # for testing purposes, pretend the item gets checked out
161 $recall3->set_finished;
162 is( $recall3->finished, 1, "Recall has been fulfilled" );
163
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,
170     status => 'R',
171     itemnumber => undef,
172     expirationdate => undef,
173     item_level_recall => 0,
174 })->store;
175
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");
178
179 $recall4->set_waiting({ item => $item1, expirationdate => $expirationdate });
180 is( $recall4->itemnumber, $item1->itemnumber, "Item has been allocated to biblio-level recall" );
181
182 $recall4->revert_waiting;
183 ok( !defined $recall4->itemnumber, "Itemnumber has been removed from biblio-level recall when reverting waiting status" );
184
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" );
189
190 $schema->storage->txn_rollback();