07f8d9f1ad6b8eb9367b5403754d849586c471f6
[koha-ffzg.git] / t / db_dependent / Koha / Recalls.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 => 19;
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 my $item2 = $builder->build_sample_item();
47 my $biblio2 = $item1->biblio;
48 my $branch2 = $item1->holdingbranch;
49 my $itemtype2 = $item1->effective_itemtype;
50
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 });
56
57 Koha::CirculationRules->set_rules({
58     branchcode => undef,
59     categorycode => undef,
60     itemtype => undef,
61     rules => {
62         'recall_due_date_interval' => undef,
63         'recalls_allowed' => 10,
64     }
65 });
66
67 C4::Circulation::AddIssue( $patron3->unblessed, $item1->barcode );
68 C4::Circulation::AddIssue( $patron3->unblessed, $item2->barcode );
69
70 my ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
71     patron => undef,
72     biblio => $biblio1,
73     branchcode => $branch1,
74     item => $item1,
75     expirationdate => undef,
76     interface => 'COMMANDLINE',
77 });
78 ok( !defined $recall, "Can't add a recall without specifying a patron" );
79
80 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
81     patron => $patron1,
82     biblio => undef,
83     branchcode => $branch1,
84     item => $item1,
85     expirationdate => undef,
86     interface => 'COMMANDLINE',
87 });
88 ok( !defined $recall, "Can't add a recall without specifying a biblio" );
89
90 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
91     patron => $patron1,
92     biblio => undef,
93     branchcode => $branch1,
94     item => $item1,
95     expirationdate => undef,
96     interface => 'COMMANDLINE',
97 });
98 ok( !defined $recall, "Can't add a recall without specifying a biblio" );
99
100 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
101     patron => $patron2,
102     biblio => $biblio1,
103     branchcode => undef,
104     item => $item2,
105     expirationdate => undef,
106     interface => 'COMMANDLINE',
107 });
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" );
110
111 Koha::CirculationRules->set_rule({
112     branchcode => undef,
113     categorycode => undef,
114     itemtype => undef,
115     rule_name => 'recall_due_date_interval',
116     rule_value => 3,
117 });
118 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
119     patron => $patron1,
120     biblio => $biblio1,
121     branchcode => undef,
122     item => $item1,
123     expirationdate => undef,
124     interface => 'COMMANDLINE',
125 });
126 is( $due_interval, 3, "Recall due date interval is based on circulation rules" );
127
128 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
129     patron => $patron1,
130     biblio => $biblio1,
131     branchcode => $branch1,
132     item => undef,
133     expirationdate => undef,
134     interface => 'COMMANDLINE',
135 });
136 is( $recall->item_level_recall, 0, "No item provided so recall not flagged as item-level" );
137
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" );
141
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" );
144
145 my $message = Koha::Recalls->move_recall;
146 is( $message, 'no recall_id provided', "Can't move a recall without specifying which recall" );
147
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" );
152
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" );
158
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" );
162
163 ( $recall, $due_interval, $due_date ) = Koha::Recalls->add_recall({
164     patron => $patron1,
165     biblio => $biblio1,
166     branchcode => $branch1,
167     item => $item2,
168     expirationdate => undef,
169     interface => 'COMMANDLINE',
170 });
171 $message = Koha::Recalls->move_recall({ recall_id => $recall->recall_id, itemnumber => $item2->itemnumber, borrowernumber => $patron1->borrowernumber });
172 $recall = Koha::Recalls->find( $recall->recall_id );
173 ok( $recall->finished, "Recall fulfilled with move_recall" );
174
175 $schema->storage->txn_rollback();