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