3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 3;
22 use t::lib::TestBuilder;
37 # Mock userenv, used by AddIssue
39 my $context = Test::MockModule->new('C4::Context');
40 $context->mock( 'userenv', sub {
41 return { branch => $branch }
44 my $schema = Koha::Database->schema;
45 $schema->storage->txn_begin;
47 my $builder = t::lib::TestBuilder->new();
49 subtest "InProcessingToShelvingCart tests" => sub {
53 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
54 my $permanent_location = 'TEST';
55 my $location = 'PROC';
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({
63 biblionumber => $biblionumber,
64 homebranch => $branch,
65 holdingbranch => $branch,
66 location => $location,
67 permanent_location => $permanent_location
70 my $barcode = $built_item->{ barcode };
71 my $itemnumber = $built_item->{ itemnumber };
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" );
80 $item->{location} = $location;
81 ModItem( $item, undef, $itemnumber );
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" );
91 subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
95 # Set item-level item types
96 C4::Context->set_preference( "item-level_itypes", 1 );
98 # Make sure logging is enabled
99 C4::Context->set_preference( "IssueLog", 1 );
100 C4::Context->set_preference( "ReturnLog", 1 );
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 };
107 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
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({
116 kohafield => 'biblioitems.itemtype'
118 my $tagfield = $rs->first->tagfield;
119 my $tagsubfield = $rs->first->tagsubfield;
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 )
126 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
127 my $item_with_itemtype = $builder->build(
131 biblionumber => $biblionumber,
132 biblioitemnumber => $biblioitemnumber,
133 homebranch => $branch,
134 holdingbranch => $branch,
135 itype => $ilevel_itemtype
139 my $item_without_itemtype = $builder->build(
143 biblionumber => $biblionumber,
144 biblioitemnumber => $biblioitemnumber,
145 homebranch => $branch,
146 holdingbranch => $branch,
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({
159 itemnumber => $item_with_itemtype->{ itemnumber }
160 }, { order_by => { -asc => 'datetime' } })->next();
162 is( $stat->itemtype, $ilevel_itemtype,
163 "item-level itype recorded on statistics for return");
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({
171 itemnumber => $item_without_itemtype->{ itemnumber }
172 }, { order_by => { -asc => 'datetime' } })->next();
174 is( $stat->itemtype, $blevel_itemtype,
175 "biblio-level itype recorded on statistics for return as a fallback for null item-level itype");
179 subtest "AddReturn logging on statistics table (item-level_itypes=0)" => sub {
183 # Make sure logging is enabled
184 C4::Context->set_preference( "IssueLog", 1 );
185 C4::Context->set_preference( "ReturnLog", 1 );
187 # Set item-level item types
188 C4::Context->set_preference( "item-level_itypes", 0 );
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 };
195 $branch = $builder->build({ source => 'Branch' })->{ branchcode };
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({
204 kohafield => 'biblioitems.itemtype'
206 my $tagfield = $rs->first->tagfield;
207 my $tagsubfield = $rs->first->tagsubfield;
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 )
214 my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
215 my $item_with_itemtype = $builder->build({
218 biblionumber => $biblionumber,
219 homebranch => $branch,
220 holdingbranch => $branch,
221 itype => $ilevel_itemtype
224 my $item_without_itemtype = $builder->build({
227 biblionumber => $biblionumber,
228 homebranch => $branch,
229 holdingbranch => $branch,
234 my $borrower = GetMember( borrowernumber => $borrowernumber );
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({
242 itemnumber => $item_with_itemtype->{ itemnumber }
243 }, { order_by => { -asc => 'datetime' } })->next();
245 is( $stat->itemtype, $blevel_itemtype,
246 "biblio-level itype recorded on statistics for return");
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({
254 itemnumber => $item_without_itemtype->{ itemnumber }
255 }, { order_by => { -asc => 'datetime' } })->next();
257 is( $stat->itemtype, $blevel_itemtype,
258 "biblio-level itype recorded on statistics for return");