fd21d8f3f8296db74267e5dedc17de80edc55ade
[koha-ffzg.git] / t / db_dependent / Circulation / Returns.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 => 3;
21 use Test::MockModule;
22 use t::lib::TestBuilder;
23
24 use t::lib::Mocks;
25 use C4::Biblio;
26 use C4::Circulation;
27 use C4::Items;
28 use C4::ItemType;
29 use C4::Members;
30 use Koha::Database;
31 use Koha::DateUtils;
32 use Koha::Items;
33
34 use MARC::Record;
35 use MARC::Field;
36
37 # Mock userenv, used by AddIssue
38 my $branch;
39 my $context = Test::MockModule->new('C4::Context');
40 $context->mock( 'userenv', sub {
41     return { branch => $branch }
42 });
43
44 my $schema = Koha::Database->schema;
45 $schema->storage->txn_begin;
46
47 my $builder = t::lib::TestBuilder->new();
48
49 subtest "InProcessingToShelvingCart tests" => sub {
50
51     plan tests => 2;
52
53     $branch = $builder->build({ source => 'Branch' })->{ branchcode };
54     my $permanent_location = 'TEST';
55     my $location           = 'PROC';
56
57     # Create a biblio record with biblio-level itemtype
58     my $record = MARC::Record->new();
59     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
60     my $built_item = $builder->build({
61         source => 'Item',
62         value  => {
63             biblionumber  => $biblionumber,
64             homebranch    => $branch,
65             holdingbranch => $branch,
66             location      => $location,
67             permanent_location => $permanent_location
68         }
69     });
70     my $barcode = $built_item->{ barcode };
71     my $itemnumber = $built_item->{ itemnumber };
72     my $item;
73
74     C4::Context->set_preference( "InProcessingToShelvingCart", 1 );
75     AddReturn( $barcode, $branch );
76     $item = GetItem( $itemnumber );
77     is( $item->{location}, 'CART',
78         "InProcessingToShelvingCart functions as intended" );
79
80     $item->{location} = $location;
81     ModItem( $item, undef, $itemnumber );
82
83     C4::Context->set_preference( "InProcessingToShelvingCart", 0 );
84     AddReturn( $barcode, $branch );
85     $item = GetItem( $itemnumber );
86     is( $item->{location}, $permanent_location,
87         "InProcessingToShelvingCart functions as intended" );
88 };
89
90
91 subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
92
93     plan tests => 2;
94
95     # Set item-level item types
96     C4::Context->set_preference( "item-level_itypes", 1 );
97
98     # Make sure logging is enabled
99     C4::Context->set_preference( "IssueLog", 1 );
100     C4::Context->set_preference( "ReturnLog", 1 );
101
102     # Create an itemtype for biblio-level item type
103     my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
104     # Create an itemtype for item-level item type
105     my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
106     # Create a branch
107     $branch = $builder->build({ source => 'Branch' })->{ branchcode };
108     # Create a borrower
109     my $borrowernumber = $builder->build({
110         source => 'Borrower',
111         value => { branchcode => $branch }
112     })->{ borrowernumber };
113     # Look for the defined MARC field for biblio-level itemtype
114     my $rs = $schema->resultset('MarcSubfieldStructure')->search({
115         frameworkcode => '',
116         kohafield     => 'biblioitems.itemtype'
117     });
118     my $tagfield    = $rs->first->tagfield;
119     my $tagsubfield = $rs->first->tagsubfield;
120
121     # Create a biblio record with biblio-level itemtype
122     my $record = MARC::Record->new();
123     $record->append_fields(
124         MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
125     );
126     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
127     my $item_with_itemtype = $builder->build(
128         {
129             source => 'Item',
130             value  => {
131                 biblionumber     => $biblionumber,
132                 biblioitemnumber => $biblioitemnumber,
133                 homebranch       => $branch,
134                 holdingbranch    => $branch,
135                 itype            => $ilevel_itemtype
136             }
137         }
138     );
139     my $item_without_itemtype = $builder->build(
140         {
141             source => 'Item',
142             value  => {
143                 biblionumber     => $biblionumber,
144                 biblioitemnumber => $biblioitemnumber,
145                 homebranch       => $branch,
146                 holdingbranch    => $branch,
147                 itype            => undef
148             }
149         }
150     );
151
152     my $borrower = GetMember( borrowernumber => $borrowernumber );
153     AddIssue( $borrower, $item_with_itemtype->{ barcode } );
154     AddReturn( $item_with_itemtype->{ barcode }, $branch );
155     # Test item-level itemtype was recorded on the 'statistics' table
156     my $stat = $schema->resultset('Statistic')->search({
157         branch     => $branch,
158         type       => 'return',
159         itemnumber => $item_with_itemtype->{ itemnumber }
160     }, { order_by => { -asc => 'datetime' } })->next();
161
162     is( $stat->itemtype, $ilevel_itemtype,
163         "item-level itype recorded on statistics for return");
164
165     AddIssue( $borrower, $item_without_itemtype->{ barcode } );
166     AddReturn( $item_without_itemtype->{ barcode }, $branch );
167     # Test biblio-level itemtype was recorded on the 'statistics' table
168     $stat = $schema->resultset('Statistic')->search({
169         branch     => $branch,
170         type       => 'return',
171         itemnumber => $item_without_itemtype->{ itemnumber }
172     }, { order_by => { -asc => 'datetime' } })->next();
173
174     is( $stat->itemtype, $blevel_itemtype,
175         "biblio-level itype recorded on statistics for return as a fallback for null item-level itype");
176
177 };
178
179 subtest "AddReturn logging on statistics table (item-level_itypes=0)" => sub {
180
181     plan tests => 2;
182
183     # Make sure logging is enabled
184     C4::Context->set_preference( "IssueLog", 1 );
185     C4::Context->set_preference( "ReturnLog", 1 );
186
187     # Set item-level item types
188     C4::Context->set_preference( "item-level_itypes", 0 );
189
190     # Create an itemtype for biblio-level item type
191     my $blevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
192     # Create an itemtype for item-level item type
193     my $ilevel_itemtype = $builder->build({ source => 'Itemtype' })->{ itemtype };
194     # Create a branch
195     $branch = $builder->build({ source => 'Branch' })->{ branchcode };
196     # Create a borrower
197     my $borrowernumber = $builder->build({
198         source => 'Borrower',
199         value => { branchcode => $branch }
200     })->{ borrowernumber };
201     # Look for the defined MARC field for biblio-level itemtype
202     my $rs = $schema->resultset('MarcSubfieldStructure')->search({
203         frameworkcode => '',
204         kohafield     => 'biblioitems.itemtype'
205     });
206     my $tagfield    = $rs->first->tagfield;
207     my $tagsubfield = $rs->first->tagsubfield;
208
209     # Create a biblio record with biblio-level itemtype
210     my $record = MARC::Record->new();
211     $record->append_fields(
212         MARC::Field->new($tagfield,'','', $tagsubfield => $blevel_itemtype )
213     );
214     my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
215     my $item_with_itemtype = $builder->build({
216         source => 'Item',
217         value  => {
218             biblionumber  => $biblionumber,
219             homebranch    => $branch,
220             holdingbranch => $branch,
221             itype         => $ilevel_itemtype
222         }
223     });
224     my $item_without_itemtype = $builder->build({
225         source => 'Item',
226         value  => {
227             biblionumber  => $biblionumber,
228             homebranch    => $branch,
229             holdingbranch => $branch,
230             itype         => undef
231         }
232     });
233
234     my $borrower = GetMember( borrowernumber => $borrowernumber );
235
236     AddIssue( $borrower, $item_with_itemtype->{ barcode } );
237     AddReturn( $item_with_itemtype->{ barcode }, $branch );
238     # Test item-level itemtype was recorded on the 'statistics' table
239     my $stat = $schema->resultset('Statistic')->search({
240         branch     => $branch,
241         type       => 'return',
242         itemnumber => $item_with_itemtype->{ itemnumber }
243     }, { order_by => { -asc => 'datetime' } })->next();
244
245     is( $stat->itemtype, $blevel_itemtype,
246         "biblio-level itype recorded on statistics for return");
247
248     AddIssue( $borrower, $item_without_itemtype->{ barcode } );
249     AddReturn( $item_without_itemtype->{ barcode }, $branch );
250     # Test biblio-level itemtype was recorded on the 'statistics' table
251     $stat = $schema->resultset('Statistic')->search({
252         branch     => $branch,
253         type       => 'return',
254         itemnumber => $item_without_itemtype->{ itemnumber }
255     }, { order_by => { -asc => 'datetime' } })->next();
256
257     is( $stat->itemtype, $blevel_itemtype,
258         "biblio-level itype recorded on statistics for return");
259 };
260
261 1;