Bug 31183: Unit tests
[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::Dates;
22 use t::lib::TestBuilder;
23 use t::lib::Mocks;
24
25 use Koha::DateUtils qw( dt_from_string );
26
27 BEGIN {
28     require_ok('Koha::Recall');
29     require_ok('Koha::Recalls');
30 }
31
32 # Start transaction
33
34 my $database = Koha::Database->new();
35 my $schema = $database->schema();
36 $schema->storage->txn_begin();
37 my $dbh = C4::Context->dbh;
38
39 my $builder = t::lib::TestBuilder->new;
40
41 # Setup test variables
42
43 my $item1 = $builder->build_sample_item();
44 my $biblio1 = $item1->biblio;
45 my $branch1 = $item1->holdingbranch;
46 my $itemtype1 = $item1->effective_itemtype;
47
48 my $item2 = $builder->build_sample_item();
49 my $biblio2 = $item2->biblio;
50 my $branch2 = $item2->holdingbranch;
51 my $itemtype2 = $item2->effective_itemtype;
52
53 my $category1 = $builder->build({ source => 'Category' })->{ categorycode };
54 my $patron1 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $category1, branchcode => $branch1 } });
55 my $patron2 = $builder->build_object({ class => 'Koha::Patrons', value => { categorycode => $category1, branchcode => $branch1 } });
56 t::lib::Mocks::mock_userenv({ patron => $patron1 });
57 my $old_recalls_count = Koha::Recalls->search({ completed => 1 })->count;
58
59 Koha::CirculationRules->set_rule({
60     branchcode => undef,
61     categorycode => undef,
62     itemtype => undef,
63     rule_name => 'recalls_allowed',
64     rule_value => '10',
65 });
66
67 my $overdue_date = dt_from_string->subtract( days => 4 );
68 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode, $overdue_date );
69
70 my $recall1 = Koha::Recall->new({
71     patron_id => $patron1->borrowernumber,
72     created_date => dt_from_string,
73     biblio_id => $biblio1->biblionumber,
74     pickup_library_id => $branch1,
75     status => 'requested',
76     item_id => $item1->itemnumber,
77     expiration_date => undef,
78     item_level => 1
79 })->store;
80
81 is( $recall1->biblio->title, $biblio1->title, "Recall biblio relationship correctly linked" );
82 is( $recall1->item->homebranch, $item1->homebranch, "Recall item relationship correctly linked" );
83 is( $recall1->patron->categorycode, $category1, "Recall patron relationship correctly linked" );
84 is( $recall1->library->branchname, Koha::Libraries->find( $branch1 )->branchname, "Recall library relationship correctly linked" );
85 is( $recall1->checkout->itemnumber, $item1->itemnumber, "Recall checkout relationship correctly linked" );
86 ok( $recall1->requested, "Recall has been requested" );
87
88 is( $recall1->should_be_overdue, 1, "Correctly calculated that recall should be marked overdue" );
89 $recall1->set_overdue({ interface => 'COMMANDLINE' });
90 ok( $recall1->overdue, "Recall is overdue" );
91
92 $recall1->set_cancelled;
93 ok( $recall1->cancelled, "Recall is cancelled" );
94
95 my $recall2 = Koha::Recall->new({
96     patron_id => $patron1->borrowernumber,
97     created_date => dt_from_string,
98     biblio_id => $biblio1->biblionumber,
99     pickup_library_id => $branch1,
100     item_id => $item1->itemnumber,
101     expiration_date => undef,
102     item_level => 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( t::lib::Dates::compare( $expirationdate, $expected_expirationdate ), 0, "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( t::lib::Dates::compare( $expirationdate, $expected_expirationdate ), 0, "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({ completed => 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     patron_id => $patron1->borrowernumber,
143     created_date => dt_from_string,
144     biblio_id => $biblio1->biblionumber,
145     pickup_library_id => $branch1,
146     item_id => $item1->itemnumber,
147     expiration_date => undef,
148     item_level => 1
149 })->store;
150
151 # test that recall gets T status
152 $recall3->start_transfer;
153 ok( $recall3->in_transit, "Recall is in transit" );
154
155 $recall3->revert_transfer;
156 ok( $recall3->requested, "Recall transfer has been cancelled and the status reverted" );
157 is( $recall3->item_id, $item1->itemnumber, "Item persists for item-level recall" );
158
159 # for testing purposes, pretend the item gets checked out
160 $recall3->set_fulfilled;
161 ok( $recall3->fulfilled, "Recall has been fulfilled" );
162
163 C4::Circulation::AddIssue( $patron2->unblessed, $item1->barcode );
164 my $recall4 = Koha::Recall->new({
165     patron_id => $patron1->borrowernumber,
166     created_date => dt_from_string,
167     biblio_id => $biblio1->biblionumber,
168     pickup_library_id => $branch1,
169     item_id => undef,
170     expiration_date => undef,
171     item_level => 0
172 })->store;
173
174 ok( !defined $recall4->item, "No relevant item returned for a biblio-level recall" );
175 is( $recall4->checkout->itemnumber, $item1->itemnumber, "Return most relevant checkout for a biblio-level recall");
176
177 $recall4->set_waiting({ item => $item1, expirationdate => $expirationdate });
178 is( $recall4->item_id, $item1->itemnumber, "Item has been allocated to biblio-level recall" );
179
180 $recall4->revert_waiting;
181 ok( !defined $recall4->item_id, "Itemnumber has been removed from biblio-level recall when reverting waiting status" );
182
183 $recall4->start_transfer({ item => $item1 });
184 is( $recall4->item_id, $item1->itemnumber, "Itemnumber saved to recall when item is transferred" );
185 $recall4->revert_transfer;
186 ok( !defined $recall4->item_id, "Itemnumber has been removed from biblio-level recall when reverting transfer status" );
187
188 $schema->storage->txn_rollback();